returning reference to local struct should be denied
Maintainers usually reply within 3 days
Nobody has claimed this yet.
Assessment
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Newbie friendliness
- 58/100
Research direction
Start with the a.rs reproducer in the issue and compare crab1's result with rustc's E0515 diagnostic. Trace the relevant lifetime or borrow-checking entry point, then verify that returning a reference to the local S is rejected without regressing the accepted argument-reference case.
Written by the indexing model from the issue text.
Description
struct S;
fn foo<'a>(x: &'a S) -> &'a S {
let x = S;
&x
}
fn main() {}
Here I'm creating S inside the function and then returning an reference to it.
Rustc rejects this:
error[E0515]: cannot return reference to local variable `x`
--> a.rs:5:5
|
5 | &x
| ^^ returns a reference to data owned by the current function
warning: unused variable: `x`
crab1 does not error here:
a.rs:1:1: warning: struct is never constructed: ‘S’
1 | struct S;
| ^~~~~~
a.rs:3:4: warning: function is never used: ‘foo’
3 | fn foo<'a>(x: &'a S) -> &'a S {
| ^~~
a.rs:3:12: warning: unused name ‘x’ [-Wunused-variable]
3 | fn foo<'a>(x: &'a S) -> &'a S {
| ^
maybe it does not check that the new object actually overwrites the function argument (which would be fine to pass out of the function again via reference)?
crab1 @ 0c3711b3e1a7771032351e99db617e27c719dea
- Dominant language
- C++
- Stars
- 3k
- Forks
- 238
- Avg merge
- 4d 8h
- Merged PRs (30d)
- 31
Getting set up
- Ships a Dockerfile or Docker Compose file
- Has a pull request template
- Read the contributing 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.
More from Rust-GCC/gccrs
-
Difficulty 2/5 1-3 hours Newbie friendliness 65/100
Maintainers usually reply within 3 days
-
Allow pattern matching of floating point literalsPossibly taken @Vivekkumar0995 claimed this 7 days ago. Opencleanup good-first-pr typechecking
Difficulty 2/5 1-3 hours Newbie friendliness 62/100
Rust-GCC/gccrs#5047 · 3 comments · 1 assignee ·
Maintainers usually reply within 3 days
-
ICE in variance analysis on core, projection field keeps the impl's type paramPossibly taken @u7k4rs6 claimed this 1 day ago. Opentypechecking
Rust-GCC/gccrs#5074 · 1 assignee ·
Maintainers usually reply within 3 days
-
subset-error typechecking
Difficulty 4/5 3-5 days Newbie friendliness 25/100
Maintainers usually reply within 3 days
-
Difficulty 3/5 1-2 days Newbie friendliness 46/100
Rust-GCC/gccrs#5060 · 2 comments ·
Maintainers usually reply within 3 days
Similar issues
-
help wanted new command
Difficulty 2/5 1-3 hours Newbie friendliness 76/100
tldr-pages/tldr#25111 ·
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 88/100
Maintainers usually reply within 1 day
-
bug test
Difficulty 2/5 1-3 hours Newbie friendliness 82/100
OpenAssetIO/OpenAssetIO#1527 ·
-
iOS Fabric pointer-handler reset leaves the next lone touch non-primaryPossibly taken @alleneubank claimed this today. Open
Difficulty 2/5 1-3 hours Newbie friendliness 68/100
react/react-native#59919 ·
Maintainers usually reply within 2 days
-
Difficulty 2/5 1-3 hours Newbie friendliness 66/100
ROCm/rocm-systems#15475 ·
Maintainers usually reply within 2 days