From 6e85f467c59e7ae943404a2aee9cc83ee475cf23 Mon Sep 17 00:00:00 2001 From: Nadrieril Date: Thu, 21 Nov 2019 18:45:28 +0000 Subject: Initial implementation of or-pattern usefulness checking --- src/test/compile-fail/or-patterns.rs | 45 ++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 src/test/compile-fail/or-patterns.rs (limited to 'src/test') diff --git a/src/test/compile-fail/or-patterns.rs b/src/test/compile-fail/or-patterns.rs new file mode 100644 index 00000000000..e1b5dce852d --- /dev/null +++ b/src/test/compile-fail/or-patterns.rs @@ -0,0 +1,45 @@ +// should-ice +#![allow(incomplete_features)] +#![feature(or_patterns)] +#![deny(unreachable_patterns)] + +// The ice will get removed once or-patterns are correctly implemented +fn main() { + // We wrap patterns in a tuple because top-level or-patterns are special-cased for now. + match (0u8,) { + (1 | 2,) => {} + //~^ ERROR simplifyable pattern found + // This above is the ICE error message + _ => {} + } + + match (0u8,) { + (1 | 2,) => {} + (1,) => {} //~ ERROR unreachable pattern + _ => {} + } + match (0u8,) { + (1 | 2,) => {} + (2,) => {} //~ ERROR unreachable pattern + _ => {} + } + match (0u8,) { + (1,) => {} + (2,) => {} + (1 | 2,) => {} //~ ERROR unreachable pattern + _ => {} + } + match (0u8,) { + (1 | 1,) => {} // redundancy not detected for now + _ => {} + } + match (0u8, 0u8) { + (1 | 2, 3 | 4) => {} + (1, 2) => {} + (1, 3) => {} //~ ERROR unreachable pattern + (1, 4) => {} //~ ERROR unreachable pattern + (2, 4) => {} //~ ERROR unreachable pattern + (2 | 1, 4) => {} //~ ERROR unreachable pattern + _ => {} + } +} -- cgit 1.4.1-3-g733a5 From 0030a777f286bbbd938b9cc4f0b559843fffd37a Mon Sep 17 00:00:00 2001 From: Nadrieril Date: Thu, 21 Nov 2019 19:06:49 +0000 Subject: Add some more tests --- src/test/compile-fail/or-patterns.rs | 11 +++++++++++ 1 file changed, 11 insertions(+) (limited to 'src/test') diff --git a/src/test/compile-fail/or-patterns.rs b/src/test/compile-fail/or-patterns.rs index e1b5dce852d..fb243f24328 100644 --- a/src/test/compile-fail/or-patterns.rs +++ b/src/test/compile-fail/or-patterns.rs @@ -42,4 +42,15 @@ fn main() { (2 | 1, 4) => {} //~ ERROR unreachable pattern _ => {} } + match (Some(0u8),) { + (None | Some(1 | 2),) => {} + (Some(1),) => {} //~ ERROR unreachable pattern + (None,) => {} //~ ERROR unreachable pattern + (Some(_),) => {} + } + match ((0u8,),) { + ((1 | 2,) | (3 | 4,),) => {}, + ((1..=4,),) => {}, //~ ERROR unreachable pattern + ((_,),) => {}, + } } -- cgit 1.4.1-3-g733a5 From b5ec4d11835d16167ee48bb6e3cf7e8347f367e1 Mon Sep 17 00:00:00 2001 From: Nadrieril Date: Mon, 25 Nov 2019 18:23:09 +0000 Subject: Address reviews --- src/librustc_mir/hair/pattern/_match.rs | 6 +++--- src/test/compile-fail/or-patterns.rs | 7 ++++++- 2 files changed, 9 insertions(+), 4 deletions(-) (limited to 'src/test') diff --git a/src/librustc_mir/hair/pattern/_match.rs b/src/librustc_mir/hair/pattern/_match.rs index 7d3bbbc1ee4..2e339b1eb88 100644 --- a/src/librustc_mir/hair/pattern/_match.rs +++ b/src/librustc_mir/hair/pattern/_match.rs @@ -400,7 +400,7 @@ impl<'p, 'tcx> PatStack<'p, 'tcx> { } // If the first pattern is an or-pattern, expand this pattern. Otherwise, return `None`. - fn expand_or_pat(&self) -> Option>> { + fn expand_or_pat(&self) -> Option> { if self.is_empty() { None } else if let PatKind::Or { pats } = &*self.head().kind { @@ -1838,7 +1838,7 @@ fn pat_constructor<'tcx>( if slice.is_some() { VarLen(prefix, suffix) } else { FixedLen(prefix + suffix) }; Some(Slice(Slice { array_len, kind })) } - PatKind::Or { .. } => bug!(), // Should have been expanded earlier on. + PatKind::Or { .. } => bug!("Or-pattern hould have been expanded earlier on."), } } @@ -2444,7 +2444,7 @@ fn specialize_one_pattern<'p, 'a: 'p, 'q: 'p, 'tcx>( _ => span_bug!(pat.span, "unexpected ctor {:?} for slice pat", constructor), }, - PatKind::Or { .. } => bug!(), // Should have been expanded earlier on. + PatKind::Or { .. } => bug!("Or-pattern hould have been expanded earlier on."), }; debug!("specialize({:#?}, {:#?}) = {:#?}", pat, ctor_wild_subpatterns, result); diff --git a/src/test/compile-fail/or-patterns.rs b/src/test/compile-fail/or-patterns.rs index fb243f24328..ba8427045e3 100644 --- a/src/test/compile-fail/or-patterns.rs +++ b/src/test/compile-fail/or-patterns.rs @@ -1,6 +1,7 @@ // should-ice -#![allow(incomplete_features)] #![feature(or_patterns)] +#![feature(slice_patterns)] +#![allow(incomplete_features)] #![deny(unreachable_patterns)] // The ice will get removed once or-patterns are correctly implemented @@ -53,4 +54,8 @@ fn main() { ((1..=4,),) => {}, //~ ERROR unreachable pattern ((_,),) => {}, } + match (&[0u8][..],) { + ([] | [0 | 1..=255] | [_, ..],) => {}, + (_,) => {}, //~ ERROR unreachable pattern + } } -- cgit 1.4.1-3-g733a5 From cdc844e81f186c62e9368a24e3b8edba1ef6143b Mon Sep 17 00:00:00 2001 From: Nadrieril Date: Tue, 26 Nov 2019 14:32:47 +0000 Subject: Make the ice a fatal error --- src/librustc_mir/build/matches/test.rs | 8 +++++++- src/test/compile-fail/or-patterns.rs | 11 +++++++---- 2 files changed, 14 insertions(+), 5 deletions(-) (limited to 'src/test') diff --git a/src/librustc_mir/build/matches/test.rs b/src/librustc_mir/build/matches/test.rs index 5c2f72c0a06..f534c2a347c 100644 --- a/src/librustc_mir/build/matches/test.rs +++ b/src/librustc_mir/build/matches/test.rs @@ -84,10 +84,16 @@ impl<'a, 'tcx> Builder<'a, 'tcx> { } } + PatKind::Or { .. } => { + self.hir.tcx().sess.span_fatal( + match_pair.pattern.span, + "or-patterns are not fully implemented yet" + ) + } + PatKind::AscribeUserType { .. } | PatKind::Array { .. } | PatKind::Wild | - PatKind::Or { .. } | PatKind::Binding { .. } | PatKind::Leaf { .. } | PatKind::Deref { .. } => { diff --git a/src/test/compile-fail/or-patterns.rs b/src/test/compile-fail/or-patterns.rs index ba8427045e3..118090c9eb2 100644 --- a/src/test/compile-fail/or-patterns.rs +++ b/src/test/compile-fail/or-patterns.rs @@ -1,16 +1,19 @@ -// should-ice #![feature(or_patterns)] #![feature(slice_patterns)] #![allow(incomplete_features)] #![deny(unreachable_patterns)] -// The ice will get removed once or-patterns are correctly implemented fn main() { // We wrap patterns in a tuple because top-level or-patterns are special-cased for now. + + // Get the fatal error out of the way + match (0u8,) { + (0 | _,) => {} + //~^ ERROR or-patterns are not fully implemented yet + } + match (0u8,) { (1 | 2,) => {} - //~^ ERROR simplifyable pattern found - // This above is the ICE error message _ => {} } -- cgit 1.4.1-3-g733a5 From 0881750173d714770713f989332464eb5498574e Mon Sep 17 00:00:00 2001 From: Nadrieril Date: Tue, 26 Nov 2019 14:53:54 +0000 Subject: Move tests to ui, split them and add some --- src/test/compile-fail/or-patterns.rs | 64 ----------------- .../or-patterns/exhaustiveness-non-exhaustive.rs | 26 +++++++ .../exhaustiveness-non-exhaustive.stderr | 33 +++++++++ src/test/ui/or-patterns/exhaustiveness-pass.rs | 40 +++++++++++ src/test/ui/or-patterns/exhaustiveness-pass.stderr | 8 +++ .../exhaustiveness-unreachable-pattern.rs | 51 ++++++++++++++ .../exhaustiveness-unreachable-pattern.stderr | 80 ++++++++++++++++++++++ 7 files changed, 238 insertions(+), 64 deletions(-) delete mode 100644 src/test/compile-fail/or-patterns.rs create mode 100644 src/test/ui/or-patterns/exhaustiveness-non-exhaustive.rs create mode 100644 src/test/ui/or-patterns/exhaustiveness-non-exhaustive.stderr create mode 100644 src/test/ui/or-patterns/exhaustiveness-pass.rs create mode 100644 src/test/ui/or-patterns/exhaustiveness-pass.stderr create mode 100644 src/test/ui/or-patterns/exhaustiveness-unreachable-pattern.rs create mode 100644 src/test/ui/or-patterns/exhaustiveness-unreachable-pattern.stderr (limited to 'src/test') diff --git a/src/test/compile-fail/or-patterns.rs b/src/test/compile-fail/or-patterns.rs deleted file mode 100644 index 118090c9eb2..00000000000 --- a/src/test/compile-fail/or-patterns.rs +++ /dev/null @@ -1,64 +0,0 @@ -#![feature(or_patterns)] -#![feature(slice_patterns)] -#![allow(incomplete_features)] -#![deny(unreachable_patterns)] - -fn main() { - // We wrap patterns in a tuple because top-level or-patterns are special-cased for now. - - // Get the fatal error out of the way - match (0u8,) { - (0 | _,) => {} - //~^ ERROR or-patterns are not fully implemented yet - } - - match (0u8,) { - (1 | 2,) => {} - _ => {} - } - - match (0u8,) { - (1 | 2,) => {} - (1,) => {} //~ ERROR unreachable pattern - _ => {} - } - match (0u8,) { - (1 | 2,) => {} - (2,) => {} //~ ERROR unreachable pattern - _ => {} - } - match (0u8,) { - (1,) => {} - (2,) => {} - (1 | 2,) => {} //~ ERROR unreachable pattern - _ => {} - } - match (0u8,) { - (1 | 1,) => {} // redundancy not detected for now - _ => {} - } - match (0u8, 0u8) { - (1 | 2, 3 | 4) => {} - (1, 2) => {} - (1, 3) => {} //~ ERROR unreachable pattern - (1, 4) => {} //~ ERROR unreachable pattern - (2, 4) => {} //~ ERROR unreachable pattern - (2 | 1, 4) => {} //~ ERROR unreachable pattern - _ => {} - } - match (Some(0u8),) { - (None | Some(1 | 2),) => {} - (Some(1),) => {} //~ ERROR unreachable pattern - (None,) => {} //~ ERROR unreachable pattern - (Some(_),) => {} - } - match ((0u8,),) { - ((1 | 2,) | (3 | 4,),) => {}, - ((1..=4,),) => {}, //~ ERROR unreachable pattern - ((_,),) => {}, - } - match (&[0u8][..],) { - ([] | [0 | 1..=255] | [_, ..],) => {}, - (_,) => {}, //~ ERROR unreachable pattern - } -} diff --git a/src/test/ui/or-patterns/exhaustiveness-non-exhaustive.rs b/src/test/ui/or-patterns/exhaustiveness-non-exhaustive.rs new file mode 100644 index 00000000000..d7c191bb5a2 --- /dev/null +++ b/src/test/ui/or-patterns/exhaustiveness-non-exhaustive.rs @@ -0,0 +1,26 @@ +#![feature(or_patterns)] +#![feature(slice_patterns)] +#![allow(incomplete_features)] +#![deny(unreachable_patterns)] + +// We wrap patterns in a tuple because top-level or-patterns are special-cased for now. +fn main() { + // Get the fatal error out of the way + match (0u8,) { + (0 | _,) => {} + //~^ ERROR or-patterns are not fully implemented yet + } + + match (0u8, 0u8) { + //~^ ERROR non-exhaustive patterns: `(2u8..=std::u8::MAX, _)` + (0 | 1, 2 | 3) => {} + } + match ((0u8,),) { + //~^ ERROR non-exhaustive patterns: `((4u8..=std::u8::MAX))` + ((0 | 1,) | (2 | 3,),) => {}, + } + match (Some(0u8),) { + //~^ ERROR non-exhaustive patterns: `(Some(2u8..=std::u8::MAX))` + (None | Some(0 | 1),) => {} + } +} diff --git a/src/test/ui/or-patterns/exhaustiveness-non-exhaustive.stderr b/src/test/ui/or-patterns/exhaustiveness-non-exhaustive.stderr new file mode 100644 index 00000000000..e6aa157d278 --- /dev/null +++ b/src/test/ui/or-patterns/exhaustiveness-non-exhaustive.stderr @@ -0,0 +1,33 @@ +error[E0004]: non-exhaustive patterns: `(2u8..=std::u8::MAX, _)` not covered + --> $DIR/exhaustiveness-non-exhaustive.rs:14:11 + | +LL | match (0u8, 0u8) { + | ^^^^^^^^^^ pattern `(2u8..=std::u8::MAX, _)` not covered + | + = help: ensure that all possible cases are being handled, possibly by adding wildcards or more match arms + +error[E0004]: non-exhaustive patterns: `((4u8..=std::u8::MAX))` not covered + --> $DIR/exhaustiveness-non-exhaustive.rs:18:11 + | +LL | match ((0u8,),) { + | ^^^^^^^^^ pattern `((4u8..=std::u8::MAX))` not covered + | + = help: ensure that all possible cases are being handled, possibly by adding wildcards or more match arms + +error[E0004]: non-exhaustive patterns: `(Some(2u8..=std::u8::MAX))` not covered + --> $DIR/exhaustiveness-non-exhaustive.rs:22:11 + | +LL | match (Some(0u8),) { + | ^^^^^^^^^^^^ pattern `(Some(2u8..=std::u8::MAX))` not covered + | + = help: ensure that all possible cases are being handled, possibly by adding wildcards or more match arms + +error: or-patterns are not fully implemented yet + --> $DIR/exhaustiveness-non-exhaustive.rs:10:10 + | +LL | (0 | _,) => {} + | ^^^^^ + +error: aborting due to 4 previous errors + +For more information about this error, try `rustc --explain E0004`. diff --git a/src/test/ui/or-patterns/exhaustiveness-pass.rs b/src/test/ui/or-patterns/exhaustiveness-pass.rs new file mode 100644 index 00000000000..a29cc7253ad --- /dev/null +++ b/src/test/ui/or-patterns/exhaustiveness-pass.rs @@ -0,0 +1,40 @@ +#![feature(or_patterns)] +#![feature(slice_patterns)] +#![allow(incomplete_features)] +#![deny(unreachable_patterns)] + +// We wrap patterns in a tuple because top-level or-patterns are special-cased for now. +fn main() { + // Get the fatal error out of the way + match (0u8,) { + (0 | _,) => {} + //~^ ERROR or-patterns are not fully implemented yet + } + + match (0u8,) { + (1 | 2,) => {} + _ => {} + } + + match (0u8,) { + (1 | 1,) => {} // redundancy not detected for now + _ => {} + } + match (0u8, 0u8) { + (1 | 2, 3 | 4) => {} + (1, 2) => {} + (2, 1) => {} + _ => {} + } + match (Some(0u8),) { + (None | Some(0 | 1),) => {} + (Some(2..=255),) => {} + } + match ((0u8,),) { + ((0 | 1,) | (2 | 3,),) => {}, + ((_,),) => {}, + } + match (&[0u8][..],) { + ([] | [0 | 1..=255] | [_, ..],) => {}, + } +} diff --git a/src/test/ui/or-patterns/exhaustiveness-pass.stderr b/src/test/ui/or-patterns/exhaustiveness-pass.stderr new file mode 100644 index 00000000000..1f4278c4b80 --- /dev/null +++ b/src/test/ui/or-patterns/exhaustiveness-pass.stderr @@ -0,0 +1,8 @@ +error: or-patterns are not fully implemented yet + --> $DIR/exhaustiveness-pass.rs:10:10 + | +LL | (0 | _,) => {} + | ^^^^^ + +error: aborting due to previous error + diff --git a/src/test/ui/or-patterns/exhaustiveness-unreachable-pattern.rs b/src/test/ui/or-patterns/exhaustiveness-unreachable-pattern.rs new file mode 100644 index 00000000000..2cd8ca2dbac --- /dev/null +++ b/src/test/ui/or-patterns/exhaustiveness-unreachable-pattern.rs @@ -0,0 +1,51 @@ +#![feature(or_patterns)] +#![feature(slice_patterns)] +#![allow(incomplete_features)] +#![deny(unreachable_patterns)] + +// We wrap patterns in a tuple because top-level or-patterns are special-cased for now. +fn main() { + // Get the fatal error out of the way + match (0u8,) { + (0 | _,) => {} + //~^ ERROR or-patterns are not fully implemented yet + } + + match (0u8,) { + (1 | 2,) => {} + (1,) => {} //~ ERROR unreachable pattern + _ => {} + } + match (0u8,) { + (1 | 2,) => {} + (2,) => {} //~ ERROR unreachable pattern + _ => {} + } + match (0u8,) { + (1,) => {} + (2,) => {} + (1 | 2,) => {} //~ ERROR unreachable pattern + _ => {} + } + match (0u8, 0u8) { + (1 | 2, 3 | 4) => {} + (1, 3) => {} //~ ERROR unreachable pattern + (1, 4) => {} //~ ERROR unreachable pattern + (2, 4) => {} //~ ERROR unreachable pattern + (2 | 1, 4) => {} //~ ERROR unreachable pattern + (1, 5 | 6) => {} + (1, 4 | 5) => {} //~ ERROR unreachable pattern + _ => {} + } + match (Some(0u8),) { + (None | Some(1 | 2),) => {} + (Some(1),) => {} //~ ERROR unreachable pattern + (None,) => {} //~ ERROR unreachable pattern + _ => {} + } + match ((0u8,),) { + ((1 | 2,) | (3 | 4,),) => {}, + ((1..=4,),) => {}, //~ ERROR unreachable pattern + _ => {}, + } +} diff --git a/src/test/ui/or-patterns/exhaustiveness-unreachable-pattern.stderr b/src/test/ui/or-patterns/exhaustiveness-unreachable-pattern.stderr new file mode 100644 index 00000000000..a4d55d805c3 --- /dev/null +++ b/src/test/ui/or-patterns/exhaustiveness-unreachable-pattern.stderr @@ -0,0 +1,80 @@ +error: unreachable pattern + --> $DIR/exhaustiveness-unreachable-pattern.rs:16:9 + | +LL | (1,) => {} + | ^^^^ + | +note: lint level defined here + --> $DIR/exhaustiveness-unreachable-pattern.rs:4:9 + | +LL | #![deny(unreachable_patterns)] + | ^^^^^^^^^^^^^^^^^^^^ + +error: unreachable pattern + --> $DIR/exhaustiveness-unreachable-pattern.rs:21:9 + | +LL | (2,) => {} + | ^^^^ + +error: unreachable pattern + --> $DIR/exhaustiveness-unreachable-pattern.rs:27:9 + | +LL | (1 | 2,) => {} + | ^^^^^^^^ + +error: unreachable pattern + --> $DIR/exhaustiveness-unreachable-pattern.rs:32:9 + | +LL | (1, 3) => {} + | ^^^^^^ + +error: unreachable pattern + --> $DIR/exhaustiveness-unreachable-pattern.rs:33:9 + | +LL | (1, 4) => {} + | ^^^^^^ + +error: unreachable pattern + --> $DIR/exhaustiveness-unreachable-pattern.rs:34:9 + | +LL | (2, 4) => {} + | ^^^^^^ + +error: unreachable pattern + --> $DIR/exhaustiveness-unreachable-pattern.rs:35:9 + | +LL | (2 | 1, 4) => {} + | ^^^^^^^^^^ + +error: unreachable pattern + --> $DIR/exhaustiveness-unreachable-pattern.rs:37:9 + | +LL | (1, 4 | 5) => {} + | ^^^^^^^^^^ + +error: unreachable pattern + --> $DIR/exhaustiveness-unreachable-pattern.rs:42:9 + | +LL | (Some(1),) => {} + | ^^^^^^^^^^ + +error: unreachable pattern + --> $DIR/exhaustiveness-unreachable-pattern.rs:43:9 + | +LL | (None,) => {} + | ^^^^^^^ + +error: unreachable pattern + --> $DIR/exhaustiveness-unreachable-pattern.rs:48:9 + | +LL | ((1..=4,),) => {}, + | ^^^^^^^^^^^ + +error: or-patterns are not fully implemented yet + --> $DIR/exhaustiveness-unreachable-pattern.rs:10:10 + | +LL | (0 | _,) => {} + | ^^^^^ + +error: aborting due to 12 previous errors + -- cgit 1.4.1-3-g733a5 From 45c4e11e43e177056d87b15ea4bddbf03a469036 Mon Sep 17 00:00:00 2001 From: Tomasz Miąsko Date: Fri, 29 Nov 2019 00:00:00 +0000 Subject: SimplifyArmIdentity only for locals with the same type Co-Authored-By: Mazdak Farrokhzad --- src/librustc_mir/transform/simplify_try.rs | 8 +++- src/test/mir-opt/simplify-arm-identity.rs | 75 ++++++++++++++++++++++++++++++ src/test/ui/issues/issue-66851.rs | 20 ++++++++ 3 files changed, 102 insertions(+), 1 deletion(-) create mode 100644 src/test/mir-opt/simplify-arm-identity.rs create mode 100644 src/test/ui/issues/issue-66851.rs (limited to 'src/test') diff --git a/src/librustc_mir/transform/simplify_try.rs b/src/librustc_mir/transform/simplify_try.rs index de5c2ebb571..9dc5daa9b07 100644 --- a/src/librustc_mir/transform/simplify_try.rs +++ b/src/librustc_mir/transform/simplify_try.rs @@ -34,7 +34,8 @@ pub struct SimplifyArmIdentity; impl<'tcx> MirPass<'tcx> for SimplifyArmIdentity { fn run_pass(&self, _: TyCtxt<'tcx>, _: MirSource<'tcx>, body: &mut Body<'tcx>) { - for bb in body.basic_blocks_mut() { + let (basic_blocks, local_decls) = body.basic_blocks_and_local_decls_mut(); + for bb in basic_blocks { // Need 3 statements: let (s0, s1, s2) = match &mut *bb.statements { [s0, s1, s2] => (s0, s1, s2), @@ -51,7 +52,12 @@ impl<'tcx> MirPass<'tcx> for SimplifyArmIdentity { Some(x) => x, }; if local_tmp_s0 != local_tmp_s1 + // The field-and-variant information match up. || vf_s0 != vf_s1 + // Source and target locals have the same type. + // FIXME(Centril | oli-obk): possibly relax to same layout? + || local_decls[local_0].ty != local_decls[local_1].ty + // We're setting the discriminant of `local_0` to this variant. || Some((local_0, vf_s0.var_idx)) != match_set_discr(s2) { continue; diff --git a/src/test/mir-opt/simplify-arm-identity.rs b/src/test/mir-opt/simplify-arm-identity.rs new file mode 100644 index 00000000000..a8fa64255fb --- /dev/null +++ b/src/test/mir-opt/simplify-arm-identity.rs @@ -0,0 +1,75 @@ +// Checks that `SimplifyArmIdentity` is not applied if enums have incompatible layouts. +// Regression test for issue #66856. +// +// compile-flags: -Zmir-opt-level=2 + +enum Src { + Foo(u8), + Bar, +} + +enum Dst { + Foo(u8), +} + +fn main() { + let e: Src = Src::Foo(0); + let _: Dst = match e { + Src::Foo(x) => Dst::Foo(x), + Src::Bar => Dst::Foo(0), + }; +} + +// END RUST SOURCE +// START rustc.main.SimplifyArmIdentity.before.mir +// fn main() -> () { +// ... +// bb0: { +// StorageLive(_1); +// ((_1 as Foo).0: u8) = const 0u8; +// discriminant(_1) = 0; +// StorageLive(_2); +// _3 = discriminant(_1); +// switchInt(move _3) -> [0isize: bb3, 1isize: bb1, otherwise: bb2]; +// } +// bb1: { +// ((_2 as Foo).0: u8) = const 0u8; +// discriminant(_2) = 0; +// goto -> bb4; +// } +// ... +// bb3: { +// _4 = ((_1 as Foo).0: u8); +// ((_2 as Foo).0: u8) = move _4; +// discriminant(_2) = 0; +// goto -> bb4; +// } +// ... +// } +// END rustc.main.SimplifyArmIdentity.before.mir +// START rustc.main.SimplifyArmIdentity.after.mir +// fn main() -> () { +// ... +// bb0: { +// StorageLive(_1); +// ((_1 as Foo).0: u8) = const 0u8; +// discriminant(_1) = 0; +// StorageLive(_2); +// _3 = discriminant(_1); +// switchInt(move _3) -> [0isize: bb3, 1isize: bb1, otherwise: bb2]; +// } +// bb1: { +// ((_2 as Foo).0: u8) = const 0u8; +// discriminant(_2) = 0; +// goto -> bb4; +// } +// ... +// bb3: { +// _4 = ((_1 as Foo).0: u8); +// ((_2 as Foo).0: u8) = move _4; +// discriminant(_2) = 0; +// goto -> bb4; +// } +// ... +// } +// END rustc.main.SimplifyArmIdentity.after.mir diff --git a/src/test/ui/issues/issue-66851.rs b/src/test/ui/issues/issue-66851.rs new file mode 100644 index 00000000000..72d62a30a33 --- /dev/null +++ b/src/test/ui/issues/issue-66851.rs @@ -0,0 +1,20 @@ +// This used to mis-compile because the mir-opt `SimplifyArmIdentity` +// did not check that the types matched up in the `Ok(r)` branch. +// +// run-pass +// compile-flags: -Zmir-opt-level=2 + +#[derive(Debug, PartialEq, Eq)] +enum SpecialsRes { Res(u64) } + +fn e103() -> SpecialsRes { + if let Ok(r) = "1".parse() { + SpecialsRes::Res(r) + } else { + SpecialsRes::Res(42) + } +} + +fn main() { + assert_eq!(e103(), SpecialsRes::Res(1)); +} -- cgit 1.4.1-3-g733a5 From 0f4c5fb20cf5d499bc3d6426b1909863f1c86a5b Mon Sep 17 00:00:00 2001 From: Nadrieril Feneanar Date: Sat, 30 Nov 2019 13:35:46 +0000 Subject: Apply suggestions from code review Co-Authored-By: varkor --- src/librustc_mir/hair/pattern/_match.rs | 4 ++-- src/test/ui/or-patterns/exhaustiveness-pass.rs | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) (limited to 'src/test') diff --git a/src/librustc_mir/hair/pattern/_match.rs b/src/librustc_mir/hair/pattern/_match.rs index 2e339b1eb88..d54f6b03cdd 100644 --- a/src/librustc_mir/hair/pattern/_match.rs +++ b/src/librustc_mir/hair/pattern/_match.rs @@ -1838,7 +1838,7 @@ fn pat_constructor<'tcx>( if slice.is_some() { VarLen(prefix, suffix) } else { FixedLen(prefix + suffix) }; Some(Slice(Slice { array_len, kind })) } - PatKind::Or { .. } => bug!("Or-pattern hould have been expanded earlier on."), + PatKind::Or { .. } => bug!("Or-pattern should have been expanded earlier on."), } } @@ -2444,7 +2444,7 @@ fn specialize_one_pattern<'p, 'a: 'p, 'q: 'p, 'tcx>( _ => span_bug!(pat.span, "unexpected ctor {:?} for slice pat", constructor), }, - PatKind::Or { .. } => bug!("Or-pattern hould have been expanded earlier on."), + PatKind::Or { .. } => bug!("Or-pattern should have been expanded earlier on."), }; debug!("specialize({:#?}, {:#?}) = {:#?}", pat, ctor_wild_subpatterns, result); diff --git a/src/test/ui/or-patterns/exhaustiveness-pass.rs b/src/test/ui/or-patterns/exhaustiveness-pass.rs index a29cc7253ad..62a851719f9 100644 --- a/src/test/ui/or-patterns/exhaustiveness-pass.rs +++ b/src/test/ui/or-patterns/exhaustiveness-pass.rs @@ -17,7 +17,7 @@ fn main() { } match (0u8,) { - (1 | 1,) => {} // redundancy not detected for now + (1 | 1,) => {} // FIXME(or_patterns): redundancy not detected for now. _ => {} } match (0u8, 0u8) { -- cgit 1.4.1-3-g733a5