Skip to content

copy bindings in pattern guards are broken #3291

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
nikomatsakis opened this issue Aug 27, 2012 · 2 comments
Closed

copy bindings in pattern guards are broken #3291

nikomatsakis opened this issue Aug 27, 2012 · 2 comments
Assignees
Labels
A-codegen Area: Code generation I-crash Issue: The compiler crashes (SIGSEGV, SIGABRT, etc). Use I-ICE instead when the compiler panics.
Milestone

Comments

@nikomatsakis
Copy link
Contributor

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?

@bblum
Copy link
Contributor

bblum commented Aug 27, 2012

#3271. I expect move is broken here too.

@bblum
Copy link
Contributor

bblum commented Sep 4, 2012

part of #3271

RalfJung pushed a commit to RalfJung/rust that referenced this issue Feb 17, 2024
jaisnan pushed a commit to jaisnan/rust-dev that referenced this issue Jul 29, 2024
Update Rust toolchain from nightly-2024-06-23 to nightly-2024-06-24
without any other source changes.
This is an automatically generated pull request. If any of the CI checks
fail, manual intervention is required. In such a case, review the
changes at https://github.com/rust-lang/rust from
rust-lang@3cb521a
up to
rust-lang@bcf94de.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-codegen Area: Code generation I-crash Issue: The compiler crashes (SIGSEGV, SIGABRT, etc). Use I-ICE instead when the compiler panics.
Projects
None yet
Development

No branches or pull requests

2 participants