Skip to content

copy bindings in pattern guards are broken #3291

Closed
@nikomatsakis

Description

@nikomatsakis

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?

Metadata

Metadata

Assignees

Labels

A-codegenArea: Code generationI-crashIssue: The compiler crashes (SIGSEGV, SIGABRT, etc). Use I-ICE instead when the compiler panics.

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions