Skip to content

match should fail when there's no matching branch #14325

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
ghost opened this issue May 21, 2014 · 1 comment
Closed

match should fail when there's no matching branch #14325

ghost opened this issue May 21, 2014 · 1 comment

Comments

@ghost
Copy link

ghost commented May 21, 2014

This program incorrectly prints "second branch" even though the match expression should fail.

extern crate core;
use core::mem;

enum Value {
    Val1,
    Val2
}

fn main() {
    let mut value = Val2;
    unsafe { *mem::transmute::<&mut Value, &mut int>(&mut value) += 10; }

    match value {
        Val1 => println!("first branch"),
        Val2 => println!("second branch")
    }
}
@huonw
Copy link
Member

huonw commented May 21, 2014

Storing value in an enum type other than one of the predefined variants is undefined behaviour, that is, it's fundamentally incorrect and invalid to do it, even (especially!) in unsafe code. Hence this isn't a bug.

See the manual for the full list of UB, for this specific case:

  • Invalid values in primitive types, even in private fields/locals:
    • A discriminant in an enum not included in the type definition

(Thanks for making the effort to file it!)

@huonw huonw closed this as completed May 21, 2014
flip1995 pushed a commit to flip1995/rust that referenced this issue Feb 28, 2025
node_args doesn't work with struct literals and expr_ty must be used
instead

r? @y21

changelog: none

(No changelog, as this ICE didn't make it to the Rust repo, as it was
caught during the sync)

Fixes rust-lang#14325
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant