Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Compiler lets escape borrowed pointer to local variable. #8229

Closed
dim-an opened this issue Aug 2, 2013 · 11 comments
Closed

Compiler lets escape borrowed pointer to local variable. #8229

dim-an opened this issue Aug 2, 2013 · 11 comments
Labels
E-hard Call for participation: Hard difficulty. Experience needed to fix: A lot. E-needs-test Call for participation: An issue has been fixed and does not reproduce, but no test has been added.
Milestone

Comments

@dim-an
Copy link
Contributor

dim-an commented Aug 2, 2013

Following code can be successfully compiled using rust-0.7 and from master.

fn foo<'a>(a: &'a int) -> &'a Option<int> {
    let v: &'a Option<int> = &None;
    v
}
@thestinger
Copy link
Contributor

cc @nikomatsakis

@dim-an
Copy link
Contributor Author

dim-an commented Aug 4, 2013

As far as I understand type checker considers None to be static variable. But looks like in generated code None is on the stack.

~/workspace/rust (master) » cat ~/qux.rs 
fn foo<'a>(_: &'a int) -> &Option<int> {
    let v: &Option<int> = &None;
    v
}

fn main() {
    let a = 4;
    let b = foo(&a);
    let c = [1, ..1000]; // fill some stack space
    printfln!(b);
}

~/workspace/rust (master) » rust run ~/qux.rs 
/home/ermolovd/qux.rs:9:8: 9:9 warning: unused variable: `c` [-W unused-variable (default)]
/home/ermolovd/qux.rs:9     let c = [1, ..1000]; // fill some stack space
                                ^
rust: task failed at 'enum value matched no variant', /home/ermolovd/workspace/rust/src/libstd/repr.rs:521
rust: domain main @0x232a110 root task failed

@nikomatsakis
Copy link
Contributor

So, the borrow checker considers None to be a static constant, but I guess that trans disagrees and creates a local temporary. I am not sure who is correct, to be honest, we should bring it up at a meeting I guess. I feel like the borrow checker is correct, but only for unary variant values.

@nikomatsakis
Copy link
Contributor

Thinking more about it, there is also this special treatment of static constants that was needed for move checking (they can be moved, which seems surprising). But this treatment makes more sense if None is just a special kind of rvalue. So perhaps trans is correct and we should modify the memory categorizer / borrow checker to categorize nullary variants as a distinguished kind of rvalue (datasort refinements would be the more principled sol'n here).

@nikomatsakis
Copy link
Contributor

(cc #1679, which is somewhat related)

@emberian
Copy link
Member

Still a bug. This is pretty nasty, and I'm pretty sure it's a soundness hole. Nominating.

@pnkfelix
Copy link
Member

pnkfelix commented Jan 9, 2014

accepted for first major release, P-backcompat-lang

@dmski
Copy link
Contributor

dmski commented Mar 12, 2014

No longer reproduces with master for me
cc #11681

@alexcrichton
Copy link
Member

Flagging as needstest.

@dmski
Copy link
Contributor

dmski commented Mar 12, 2014

There is https://github.com/mozilla/rust/blob/master/src/test/compile-fail/regions-lifetime-of-struct-or-enum-variant.rs which looks ok. Should a test with the code from issue description be added?

@alexcrichton
Copy link
Member

Those look sufficient to me! thanks

flip1995 pushed a commit to flip1995/rust that referenced this issue Jan 13, 2022
cover trait for `trait_duplication_in_bounds`

closes rust-lang#8229

changelog: [`trait_duplication_in_bounds`] covers trait functions with `Self` bounds
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
E-hard Call for participation: Hard difficulty. Experience needed to fix: A lot. E-needs-test Call for participation: An issue has been fixed and does not reproduce, but no test has been added.
Projects
None yet
Development

No branches or pull requests

7 participants