Skip to content

element drop order inconsistent between in-progress versus completed arrays #23222

Closed
@pnkfelix

Description

@pnkfelix

This code:

use std::thread;

struct D(u32);

impl Drop for D {
    fn drop(&mut self) {
        println!("Dropping {}", self.0);
    }
}

fn main() {
    fn die() -> D { panic!("Oh no"); }
    let g = thread::spawn(|| {
        let _a: [D; 3]      = [D(101), D(102), D(103)];
        let _d: [D; 4]      = [D(401), D(402), die(), D(404)];
    });
    assert!(g.join().is_err());
}

prints

Dropping 402
Dropping 401
Dropping 101
Dropping 102
Dropping 103

i.e. when the panic occurs, we do the dropping in reverse order for the array being built, but then we do the drop in "declaration order" for the array that was fully constructed.

See also rust-lang/rfcs#744 and #16493

Metadata

Metadata

Assignees

No one assigned

    Labels

    T-langRelevant to the language 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