Skip to content

[NLL] Dropck is too permissive for generators #49918

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

Closed
matthewjasper opened this issue Apr 12, 2018 · 0 comments
Closed

[NLL] Dropck is too permissive for generators #49918

matthewjasper opened this issue Apr 12, 2018 · 0 comments
Assignees
Labels
A-coroutines Area: Coroutines A-NLL Area: Non-lexical lifetimes (NLL) NLL-sound Working towards the "invalid code does not compile" goal

Comments

@matthewjasper
Copy link
Contributor

Code shows a case where y doesn't stay borrowed for long enough allowing it to be mutated while a reference to it exists. I think treating generators as if they implement Drop would fix this, but I'm not sure if this will end up causing incorrect errors in other places.

#![feature(nll)]
#![feature(generators, generator_trait)]

use std::ops::{GeneratorState, Generator};

struct SetToNone<'a: 'b, 'b>(&'b mut Option<&'a i32>);

impl<'a, 'b> Drop for SetToNone<'a, 'b> {
    fn drop(&mut self) {
        *self.0 = None;
    }
}

fn drop_using_generator() -> i32 {
    let mut y = Some(&0);
    let z = &mut y;
    let r;
    {
        let mut g = move || { let _s = SetToNone(z); yield; };
        unsafe { g.resume() }; // documented as unsafe only because of unmovable closures.
        r = y.as_ref().unwrap();
    } // y is set to 'null' here
    **r // Segmentation fault
}

fn main() {
    println!("{}", drop_using_generator());
}
@matthewjasper matthewjasper added A-NLL Area: Non-lexical lifetimes (NLL) A-coroutines Area: Coroutines WG-compiler-nll labels Apr 12, 2018
@pnkfelix pnkfelix self-assigned this Apr 13, 2018
@pnkfelix pnkfelix added the NLL-sound Working towards the "invalid code does not compile" goal label Apr 17, 2018
pnkfelix added a commit to pnkfelix/rust that referenced this issue May 1, 2018
…cals' dtors.

This is meant to address rust-lang#49918.

Review feedback: put back comment justifying skipping interior traversal.

Review feedback: dropck generators like trait objects: all their upvars must
outlive the generator itself, so just create a DtorckConstraint saying so.
bors added a commit that referenced this issue May 2, 2018
Treat generators as if they have an arbitrary destructor

Conservatively assume dropping a generator touches its upvars, via locals' destructors.

Fix #49918
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-coroutines Area: Coroutines A-NLL Area: Non-lexical lifetimes (NLL) NLL-sound Working towards the "invalid code does not compile" goal
Projects
None yet
Development

No branches or pull requests

2 participants