"Reference to data owned by current function" for a function owning no data.
Nobody has claimed this yet.
- Dominant language
- Rust
- Stars
- 119k
- Forks
- 16.1k
- PR merge metrics
- PR metrics pending
Description
Code
fn f(a: &mut Vec<u64>) -> &[u64] {
let v = a;
v.push(1);
&v
}
Current output
error[E0515]: cannot return reference to local variable `v`
--> src/main.rs:4:5
|
4 | &v
| ^^ returns a reference to data owned by the current function
Desired output
Type error: expected `&[u64]`, found `& &mut Vec<u64>`.
Suggestion: change `&v` to `v`.
Rationale and extra context
It seems like the autoderef is making the borrowck confused here, or something? I had trouble interpreting the message because the function owns no data (only copy-able refs).
@Kyuuhachi I don't know the exact details, but derefing a
&'local &'self mut [u64]gives a&'local [u64], not a&'self [u64][1]
@Kyuuhachi Kind of a weird edge case here I think, because if instead chose to first relax the mutref into a
&'local &'self [u64]it would be able to get a&'self [u64]from that [2]
Not sure how to help more?
Other cases
Changing &v to v makes it compile without error.
Rust Version
$ rustc --version --verbose
rustc 1.83.0-nightly (f79a912d9 2024-09-18)
binary: rustc
commit-hash: f79a912d9edc3ad4db910c0e93672ed5c65133fa
commit-date: 2024-09-18
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
Reproduce the diagnostic from the issue's src/main.rs example with the noted rustc nightly version. Start by tracing how rustc reports the return of &v after autoderef, then compare it with the requested type error and suggestion. Done means the example reports the requested explanation without regressing the compiling v variant.
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