Closed
Description
This test segfaults:
fn foo(x: option<~int>, b: bool) -> int {
match x {
none => { 1 }
some(copy x) if b => { *x }
some(_) => { 0 }
}
}
fn main() {
foo(some(~22), true);
foo(some(~22), false);
foo(none, true);
foo(none, false);
}
the reason is that copy bindings (currently, anyhow) create a temporary that is only used in the guard, copy into it, and then free it on exit from the match. But this temporary is never initialized. If you wind up in an arm that doesn't use the temporary, then, you try to free uninitialized data. Bad.
It would be better for copy bindings to perform the copy, test the guard, then free the data if the guard is false---but reuse the data if the guard is true. I'm going to see how much surgery that would be.
Or maybe copy bindings and guards should just be incompatible?