Skip to content
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

Fix Broken MIR on match branch simplification w/ u8 #75507 #75508

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 7 additions & 9 deletions src/librustc_mir/transform/match_branches.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,15 +48,13 @@ impl<'tcx> MirPass<'tcx> for MatchBranchSimplification {
(
StatementKind::Assign(box (lhs_f, Rvalue::Use(Operand::Constant(f_c)))),
StatementKind::Assign(box (lhs_s, Rvalue::Use(Operand::Constant(s_c)))),
) if lhs_f == lhs_s => {
if let Some(f_c) = f_c.literal.try_eval_bool(tcx, param_env) {
// This should also be a bool because it's writing to the same place
let s_c = s_c.literal.try_eval_bool(tcx, param_env).unwrap();
if f_c != s_c {
// have to check this here because f_c & s_c might have
// different spans.
continue;
}
) if lhs_f == lhs_s && f_c.literal.ty.is_bool() && s_c.literal.ty.is_bool() => {
let f_c = f_c.literal.try_eval_bool(tcx, param_env).unwrap();
let s_c = s_c.literal.try_eval_bool(tcx, param_env).unwrap();
if f_c != s_c {
// have to check this here because f_c & s_c might have
// different spans.
continue;
}
continue 'outer;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
- // MIR for `exhaustive_match` before MatchBranchSimplification
+ // MIR for `exhaustive_match` after MatchBranchSimplification

fn exhaustive_match(_1: E) -> u8 {
debug e => _1; // in scope 0 at $DIR/matches_u8.rs:11:25: 11:26
let mut _0: u8; // return place in scope 0 at $DIR/matches_u8.rs:11:34: 11:36
let mut _2: isize; // in scope 0 at $DIR/matches_u8.rs:13:9: 13:13

bb0: {
_2 = discriminant(_1); // scope 0 at $DIR/matches_u8.rs:13:9: 13:13
switchInt(move _2) -> [0_isize: bb2, otherwise: bb1]; // scope 0 at $DIR/matches_u8.rs:13:9: 13:13
}

bb1: {
_0 = const 1_u8; // scope 0 at $DIR/matches_u8.rs:14:17: 14:18
// ty::Const
// + ty: u8
// + val: Value(Scalar(0x01))
// mir::Constant
// + span: $DIR/matches_u8.rs:14:17: 14:18
// + literal: Const { ty: u8, val: Value(Scalar(0x01)) }
goto -> bb3; // scope 0 at $DIR/matches_u8.rs:12:5: 15:6
}

bb2: {
_0 = const 0_u8; // scope 0 at $DIR/matches_u8.rs:13:17: 13:18
// ty::Const
// + ty: u8
// + val: Value(Scalar(0x00))
// mir::Constant
// + span: $DIR/matches_u8.rs:13:17: 13:18
// + literal: Const { ty: u8, val: Value(Scalar(0x00)) }
goto -> bb3; // scope 0 at $DIR/matches_u8.rs:12:5: 15:6
}

bb3: {
return; // scope 0 at $DIR/matches_u8.rs:16:2: 16:2
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
- // MIR for `exhaustive_match` before MatchBranchSimplification
+ // MIR for `exhaustive_match` after MatchBranchSimplification

fn exhaustive_match(_1: E) -> u8 {
debug e => _1; // in scope 0 at $DIR/matches_u8.rs:11:25: 11:26
let mut _0: u8; // return place in scope 0 at $DIR/matches_u8.rs:11:34: 11:36
let mut _2: isize; // in scope 0 at $DIR/matches_u8.rs:13:9: 13:13

bb0: {
_2 = discriminant(_1); // scope 0 at $DIR/matches_u8.rs:13:9: 13:13
switchInt(move _2) -> [0_isize: bb2, otherwise: bb1]; // scope 0 at $DIR/matches_u8.rs:13:9: 13:13
}

bb1: {
_0 = const 1_u8; // scope 0 at $DIR/matches_u8.rs:14:17: 14:18
// ty::Const
// + ty: u8
// + val: Value(Scalar(0x01))
// mir::Constant
// + span: $DIR/matches_u8.rs:14:17: 14:18
// + literal: Const { ty: u8, val: Value(Scalar(0x01)) }
goto -> bb3; // scope 0 at $DIR/matches_u8.rs:12:5: 15:6
}

bb2: {
_0 = const 0_u8; // scope 0 at $DIR/matches_u8.rs:13:17: 13:18
// ty::Const
// + ty: u8
// + val: Value(Scalar(0x00))
// mir::Constant
// + span: $DIR/matches_u8.rs:13:17: 13:18
// + literal: Const { ty: u8, val: Value(Scalar(0x00)) }
goto -> bb3; // scope 0 at $DIR/matches_u8.rs:12:5: 15:6
}

bb3: {
return; // scope 0 at $DIR/matches_u8.rs:16:2: 16:2
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
- // MIR for `exhaustive_match_i8` before MatchBranchSimplification
+ // MIR for `exhaustive_match_i8` after MatchBranchSimplification

fn exhaustive_match_i8(_1: E) -> i8 {
debug e => _1; // in scope 0 at $DIR/matches_u8.rs:19:28: 19:29
let mut _0: i8; // return place in scope 0 at $DIR/matches_u8.rs:19:37: 19:39
let mut _2: isize; // in scope 0 at $DIR/matches_u8.rs:21:9: 21:13

bb0: {
_2 = discriminant(_1); // scope 0 at $DIR/matches_u8.rs:21:9: 21:13
switchInt(move _2) -> [0_isize: bb2, otherwise: bb1]; // scope 0 at $DIR/matches_u8.rs:21:9: 21:13
}

bb1: {
_0 = const 1_i8; // scope 0 at $DIR/matches_u8.rs:22:17: 22:18
// ty::Const
// + ty: i8
// + val: Value(Scalar(0x01))
// mir::Constant
// + span: $DIR/matches_u8.rs:22:17: 22:18
// + literal: Const { ty: i8, val: Value(Scalar(0x01)) }
goto -> bb3; // scope 0 at $DIR/matches_u8.rs:20:5: 23:6
}

bb2: {
_0 = const 0_i8; // scope 0 at $DIR/matches_u8.rs:21:17: 21:18
// ty::Const
// + ty: i8
// + val: Value(Scalar(0x00))
// mir::Constant
// + span: $DIR/matches_u8.rs:21:17: 21:18
// + literal: Const { ty: i8, val: Value(Scalar(0x00)) }
goto -> bb3; // scope 0 at $DIR/matches_u8.rs:20:5: 23:6
}

bb3: {
return; // scope 0 at $DIR/matches_u8.rs:24:2: 24:2
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
- // MIR for `exhaustive_match_i8` before MatchBranchSimplification
+ // MIR for `exhaustive_match_i8` after MatchBranchSimplification

fn exhaustive_match_i8(_1: E) -> i8 {
debug e => _1; // in scope 0 at $DIR/matches_u8.rs:19:28: 19:29
let mut _0: i8; // return place in scope 0 at $DIR/matches_u8.rs:19:37: 19:39
let mut _2: isize; // in scope 0 at $DIR/matches_u8.rs:21:9: 21:13

bb0: {
_2 = discriminant(_1); // scope 0 at $DIR/matches_u8.rs:21:9: 21:13
switchInt(move _2) -> [0_isize: bb2, otherwise: bb1]; // scope 0 at $DIR/matches_u8.rs:21:9: 21:13
}

bb1: {
_0 = const 1_i8; // scope 0 at $DIR/matches_u8.rs:22:17: 22:18
// ty::Const
// + ty: i8
// + val: Value(Scalar(0x01))
// mir::Constant
// + span: $DIR/matches_u8.rs:22:17: 22:18
// + literal: Const { ty: i8, val: Value(Scalar(0x01)) }
goto -> bb3; // scope 0 at $DIR/matches_u8.rs:20:5: 23:6
}

bb2: {
_0 = const 0_i8; // scope 0 at $DIR/matches_u8.rs:21:17: 21:18
// ty::Const
// + ty: i8
// + val: Value(Scalar(0x00))
// mir::Constant
// + span: $DIR/matches_u8.rs:21:17: 21:18
// + literal: Const { ty: i8, val: Value(Scalar(0x00)) }
goto -> bb3; // scope 0 at $DIR/matches_u8.rs:20:5: 23:6
}

bb3: {
return; // scope 0 at $DIR/matches_u8.rs:24:2: 24:2
}
}

32 changes: 32 additions & 0 deletions src/test/mir-opt/matches_u8.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// EMIT_MIR_FOR_EACH_BIT_WIDTH
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are our mir-opt tests running on mir-opt-level 3 by default?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Uh, I'm not sure, but when I ran it initially with this file it failed

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think they run with mir-opt-level=2 but I can't find it in the source off hand.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

MirOpt => {
rustc.args(&[
"-Zdump-mir=all",
"-Zmir-opt-level=3",
"-Zdump-mir-exclude-pass-number",
]);

// EMIT_MIR matches_u8.exhaustive_match.MatchBranchSimplification.diff
// EMIT_MIR matches_u8.exhaustive_match_i8.MatchBranchSimplification.diff

pub enum E {
A,
B,
}

#[no_mangle]
pub fn exhaustive_match(e: E) -> u8 {
match e {
E::A => 0,
E::B => 1,
}
}

#[no_mangle]
pub fn exhaustive_match_i8(e: E) -> i8 {
match e {
E::A => 0,
E::B => 1,
}
}

fn main() {
assert_eq!(exhaustive_match(E::A), 0);
assert_eq!(exhaustive_match(E::B), 1);

assert_eq!(exhaustive_match_i8(E::A), 0);
assert_eq!(exhaustive_match_i8(E::B), 1);
}