rust
rust copied to clipboard
Drop can run impure code in pure functions
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
Not a 100% sure this is actually bad.
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.
Follow #4301 for the more general issue!