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

Drop flags on the stack not reinitialized in loops #27401

Closed
alexcrichton opened this issue Jul 30, 2015 · 1 comment · Fixed by #27582
Closed

Drop flags on the stack not reinitialized in loops #27401

alexcrichton opened this issue Jul 30, 2015 · 1 comment · Fixed by #27582
Assignees
Labels
T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@alexcrichton
Copy link
Member

It looks like that when a local variable is initialized in a loop and dropped in the same loop, the stack local drop flag for that local isn't reinitialized to "not dropped" when the loop runs after the first time.

Concretely, this test passes on stable and fails on nightly right now:

struct A<'a>(&'a mut i32);

impl<'a> Drop for A<'a> {
    fn drop(&mut self) {
        *self.0 += 1;
    }
}

fn main() {
    let mut cnt = 0;
    for i in 0..2 {
        let a = A(&mut cnt);
        if i == 1 {
            break
        }
        drop(a);
    }
    assert_eq!(cnt, 2);
}

cc @pnkfelix

@alexcrichton alexcrichton added I-nominated T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Jul 30, 2015
@pnkfelix pnkfelix self-assigned this Jul 30, 2015
@pnkfelix
Copy link
Member

Interestingly this slight variation on the above code does drop twice as expected:

playpen

struct A<'a>(&'a mut i32);

impl<'a> Drop for A<'a> {
    fn drop(&mut self) {
        *self.0 += 1;
    }
}

fn main() {
    let mut cnt = 0;
    for i in 0..2 {
        let a; a = A(&mut cnt); // This was `let a = A(&mut cnt);` before
        if i == 1 {
            break
        }
        drop(a);
    }
    assert_eq!(cnt, 2);
}

pnkfelix added a commit to pnkfelix/rust that referenced this issue Jul 31, 2015
Such bindings can occur in loops, and thus the binding can be executed
after a previous move cleared the flag, thus necessitating the flag be
reset to `DTOR_NEEDED_HINT`.

Fix rust-lang#27401.
pnkfelix added a commit to pnkfelix/rust that referenced this issue Jul 31, 2015
bors added a commit that referenced this issue Jul 31, 2015
Reinitialize the dropflag hint in occurrences of variable bindings.

Such bindings can occur in loops, and thus the binding can be executed after a previous move cleared the flag, thus necessitating the flag be reset to `DTOR_NEEDED_HINT`.

Fix #27401.
pnkfelix added a commit to pnkfelix/rust that referenced this issue Aug 7, 2015
bors added a commit that referenced this issue Aug 7, 2015
…lt, r=nikomatsakis

Turn nonzeroing move hints back off by default.

Works around bugs injected by PR #26173.

 * (@pnkfelix is unavailable in the short-term (i.e. for the next week) to fix them.)

 * When the bugs are fixed, we will turn nonzeroing move hints back on by default.

Fix #27401
brson pushed a commit to brson/rust that referenced this issue Aug 10, 2015
sfackler pushed a commit to sfackler/rust that referenced this issue Aug 11, 2015
pnkfelix added a commit to pnkfelix/rust that referenced this issue Aug 19, 2015
Such bindings can occur in loops, and thus the binding can be executed
after a previous move cleared the flag, thus necessitating the flag be
reset to `DTOR_NEEDED_HINT`.

Fix rust-lang#27401.
pnkfelix added a commit to pnkfelix/rust that referenced this issue Aug 19, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants