Skip to content

Drop flags on the stack not reinitialized in loops #27401

Closed
@alexcrichton

Description

@alexcrichton

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

Metadata

Metadata

Assignees

Labels

T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions