rust icon indicating copy to clipboard operation
rust copied to clipboard

Drop can run impure code in pure functions

Open 14427 opened this issue 13 years ago • 3 comments

See the following code for an example:

struct Goodbye { msg: ~str }

impl Goodbye: Drop {
    fn finalize(&self) {
        io::println(self.msg);
    }
}

pure fn test() -> Goodbye {
    let x = Goodbye { msg: ~"Good" };
    let _y = Goodbye { msg: ~"Bad" };
    return x;
}

fn main() {
    io::println("Before");
    let _z = test();
    io::println("After");
}

Result:

Before
Bad
After
Good

14427 avatar Dec 12 '12 20:12 14427

Not a 100% sure this is actually bad.

14427 avatar Dec 12 '12 20:12 14427

Upon conferring with @nikomatsakis : This is not exactly a bug, but part of the general unsoundness to do with how we treat the Drop trait. It's not necessarily wrong that pure functions can implicitly call impure functions, but it is a problem that destructors are allowed to mutate non-owned memory.

@nikomatsakis said he would open a more general issue about Drop and unsoundness, so I'll close this.

catamorphism avatar Dec 28 '12 00:12 catamorphism

Follow #4301 for the more general issue!

catamorphism avatar Dec 28 '12 00:12 catamorphism