Improve impl Trait 2024 capture rules compiler output
Nobody has claimed this yet.
- Dominant language
- Rust
- Stars
- 119k
- Forks
- 16.1k
- PR merge metrics
- PR metrics pending
Description
Code
struct Foo;
impl Foo {
fn get(&self) -> impl Sized {
()
}
fn mutate(&mut self) {}
}
fn bar(_: impl Sized) {}
fn test() {
let mut foo = Foo;
let x = foo.get();
foo.mutate();
bar(x);
}
Current output
Compiling example v0.1.0 (/tmp/example)
error[E0502]: cannot borrow `foo` as mutable because it is also borrowed as immutable
--> src/main.rs:19:5
|
18 | let x = foo.get();
| --- immutable borrow occurs here
19 | foo.mutate();
| ^^^^^^^^^^^^ mutable borrow occurs here
20 | bar(x);
| - immutable borrow later used here
For more information about this error, try `rustc --explain E0502`.
error: could not compile `example` (bin "example") due to 1 previous error
Desired output
I can't produce nice looking example, but to put it simply I would like a note section that would point to the definition of Foo::get and pointed that impl Sized implicitly captured &self. And another note could point out that if this is unwanted, then I could add + use<> bound to disable capturing of this lifetime.
Rationale and extra context
Firstly, if this is already tracked then I'm sorry for posting a duplicate. I don't know how to grep for that kind of issues.
I was playing with new impl Trait capture rules and have written an invalid example, that fails to compile. In function Foo::get returned opaque type impl Sized captures lifetime of &self. However compiler error is not as helpful, as it could be. It points to the error on the call side, but does not show an additional context on the declaration site.
I have no idea how easy or how hard this reasoning would be to implement, but this surely would be helpful information. Especially that this code compiles in Edition 2021.
Other cases
No response
Rust Version
rustc 1.83.0-nightly (9c01301c5 2024-09-05)
binary: rustc
commit-hash: 9c01301c52df5d2d7b6fe337707a74e011d68d6f
commit-date: 2024-09-05
host: x86_64-unknown-linux-gnu
release: 1.83.0-nightly
LLVM version: 19.1.0
Anything else?
No response
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
Start by reproducing the supplied Rust example with the reported rustc 1.83.0-nightly version and compare its current borrow-checker output with the requested diagnostic. Trace how the Foo::get declaration and its implicitly captured &self lifetime relate to the call-site error; done should include notes pointing to that definition and explaining the use<> alternative.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- rust
- Domain
- compilers
- Issue type
- Feature
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100