self-referencial struct: mutable borrow not dropped?
Open
Nobody has claimed this yet.
C-discussion
- Dominant language
- Rust
- Stars
- 119k
- Forks
- 16.1k
- PR merge metrics
- PR metrics pending
Description
I tried this code:
struct Foo<'a> {
name: Option<&'a str>,
foo: String
}
impl<'a> Foo<'a> {
fn op1(&'a mut self) {
// self.name = Some(&self.foo);
}
fn op2(&self) {
}
}
fn x() {
let mut foo = Foo { name: None, foo: "foo".to_string() };
foo.op1();
foo.op2(); // Cannot borrow as immutable because borrowed as mutable
}
I expected to see this happen: should compile.
Instead, this happened: compiler seems to think foo.op2() hangs onto the mutable borrow in op1
Meta
rustc --version --verbose:
$ rustc --version --verbose
rustc 1.87.0-nightly (cbfdf0b01 2025-03-13)
binary: rustc
commit-hash: cbfdf0b014cb04982a9cbeec1578001001167f6e
commit-date: 2025-03-13
host: x86_64-unknown-linux-gnu
release: 1.87.0-nightly
LLVM version: 20.1.0
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Reproduce the inline example with the reported rustc 1.87.0-nightly version and compare its behavior with current compiler versions. Start by investigating the borrow-checker handling of the mutable self-reference and determine whether the reported rejection is intended. Done means reaching a confirmed compiler diagnosis and, if it is a bug, adding the appropriate regression coverage.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- rust
- Domain
- compilers
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100