Skip to content

Commit

Permalink
Auto merge of rust-lang#77549 - tmiasko:simplify-branch-same-fix, r=o…
Browse files Browse the repository at this point in the history
…li-obk

 Fix miscompile in SimplifyBranchSame

Cherry-picked from rust-lang#77486, but with a different test case that used to be compiled incorrectly on both master & beta branches.
  • Loading branch information
bors committed Oct 5, 2020
2 parents 62bfcfd + f271957 commit d890e64
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
3 changes: 2 additions & 1 deletion compiler/rustc_mir/src/transform/simplify_try.rs
Original file line number Diff line number Diff line change
Expand Up @@ -630,7 +630,8 @@ impl<'a, 'tcx> SimplifyBranchSameOptimizationFinder<'a, 'tcx> {
// All successor basic blocks must be equal or contain statements that are pairwise considered equal.
for ((target_and_value_l,bb_l), (target_and_value_r,bb_r)) in iter_bbs_reachable.tuple_windows() {
let trivial_checks = bb_l.is_cleanup == bb_r.is_cleanup
&& bb_l.terminator().kind == bb_r.terminator().kind;
&& bb_l.terminator().kind == bb_r.terminator().kind
&& bb_l.statements.len() == bb_r.statements.len();
let statement_check = || {
bb_l.statements.iter().zip(&bb_r.statements).try_fold(StatementEquality::TrivialEqual, |acc,(l,r)| {
let stmt_equality = self.statement_equality(*adt_matched_on, &l, target_and_value_l, &r, target_and_value_r);
Expand Down
21 changes: 21 additions & 0 deletions src/test/ui/mir/simplify-branch-same.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// Regression test for SimplifyBranchSame miscompilation.
// run-pass

macro_rules! m {
($a:expr, $b:expr, $c:block) => {
match $a {
Lto::Fat | Lto::Thin => { $b; (); $c }
Lto::No => { $b; () }
}
}
}

pub enum Lto { No, Thin, Fat }

fn f(mut cookie: u32, lto: Lto) -> u32 {
let mut _a = false;
m!(lto, _a = true, {cookie = 0});
cookie
}

fn main() { assert_eq!(f(42, Lto::Thin), 0) }

0 comments on commit d890e64

Please sign in to comment.