From 3b37d941a29d91afbfa06afcb63f702ee7b810d6 Mon Sep 17 00:00:00 2001 From: Nadrieril Date: Sat, 17 Oct 2020 19:01:10 +0100 Subject: Add some tests --- src/test/ui/consts/const_in_pattern/issue-44333.rs | 25 ++++ .../ui/consts/const_in_pattern/issue-44333.stderr | 25 ++++ src/test/ui/consts/const_in_pattern/issue-78057.rs | 17 +++ .../ui/consts/const_in_pattern/issue-78057.stderr | 20 +++ src/test/ui/issues/issue-44333.rs | 25 ---- src/test/ui/issues/issue-44333.stderr | 25 ---- src/test/ui/pattern/usefulness/consts-opaque.rs | 113 +++++++++++++++ .../ui/pattern/usefulness/consts-opaque.stderr | 154 +++++++++++++++++++++ 8 files changed, 354 insertions(+), 50 deletions(-) create mode 100644 src/test/ui/consts/const_in_pattern/issue-44333.rs create mode 100644 src/test/ui/consts/const_in_pattern/issue-44333.stderr create mode 100644 src/test/ui/consts/const_in_pattern/issue-78057.rs create mode 100644 src/test/ui/consts/const_in_pattern/issue-78057.stderr delete mode 100644 src/test/ui/issues/issue-44333.rs delete mode 100644 src/test/ui/issues/issue-44333.stderr create mode 100644 src/test/ui/pattern/usefulness/consts-opaque.rs create mode 100644 src/test/ui/pattern/usefulness/consts-opaque.stderr (limited to 'src/test') diff --git a/src/test/ui/consts/const_in_pattern/issue-44333.rs b/src/test/ui/consts/const_in_pattern/issue-44333.rs new file mode 100644 index 00000000000..96e8795e52d --- /dev/null +++ b/src/test/ui/consts/const_in_pattern/issue-44333.rs @@ -0,0 +1,25 @@ +// run-pass + +#![warn(pointer_structural_match)] + +type Func = fn(usize, usize) -> usize; + +fn foo(a: usize, b: usize) -> usize { a + b } +fn bar(a: usize, b: usize) -> usize { a * b } +fn test(x: usize) -> Func { + if x % 2 == 0 { foo } + else { bar } +} + +const FOO: Func = foo; +const BAR: Func = bar; + +fn main() { + match test(std::env::consts::ARCH.len()) { + FOO => println!("foo"), //~ WARN pointers in patterns behave unpredictably + //~^ WARN will become a hard error + BAR => println!("bar"), //~ WARN pointers in patterns behave unpredictably + //~^ WARN will become a hard error + _ => unreachable!(), + } +} diff --git a/src/test/ui/consts/const_in_pattern/issue-44333.stderr b/src/test/ui/consts/const_in_pattern/issue-44333.stderr new file mode 100644 index 00000000000..8302b09e533 --- /dev/null +++ b/src/test/ui/consts/const_in_pattern/issue-44333.stderr @@ -0,0 +1,25 @@ +warning: function pointers and unsized pointers in patterns behave unpredictably and should not be relied upon. See https://github.com/rust-lang/rust/issues/70861 for details. + --> $DIR/issue-44333.rs:19:9 + | +LL | FOO => println!("foo"), + | ^^^ + | +note: the lint level is defined here + --> $DIR/issue-44333.rs:3:9 + | +LL | #![warn(pointer_structural_match)] + | ^^^^^^^^^^^^^^^^^^^^^^^^ + = warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release! + = note: for more information, see issue #62411 + +warning: function pointers and unsized pointers in patterns behave unpredictably and should not be relied upon. See https://github.com/rust-lang/rust/issues/70861 for details. + --> $DIR/issue-44333.rs:21:9 + | +LL | BAR => println!("bar"), + | ^^^ + | + = warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release! + = note: for more information, see issue #62411 + +warning: 2 warnings emitted + diff --git a/src/test/ui/consts/const_in_pattern/issue-78057.rs b/src/test/ui/consts/const_in_pattern/issue-78057.rs new file mode 100644 index 00000000000..69cf8404da1 --- /dev/null +++ b/src/test/ui/consts/const_in_pattern/issue-78057.rs @@ -0,0 +1,17 @@ +#![deny(unreachable_patterns)] + +#[derive(PartialEq)] +struct Opaque(i32); + +impl Eq for Opaque {} + +const FOO: Opaque = Opaque(42); + +fn main() { + match FOO { + FOO => {}, + //~^ ERROR must be annotated with `#[derive(PartialEq, Eq)]` + _ => {} + //~^ ERROR unreachable pattern + } +} diff --git a/src/test/ui/consts/const_in_pattern/issue-78057.stderr b/src/test/ui/consts/const_in_pattern/issue-78057.stderr new file mode 100644 index 00000000000..0d49d0e96c8 --- /dev/null +++ b/src/test/ui/consts/const_in_pattern/issue-78057.stderr @@ -0,0 +1,20 @@ +error: to use a constant of type `Opaque` in a pattern, `Opaque` must be annotated with `#[derive(PartialEq, Eq)]` + --> $DIR/issue-78057.rs:12:9 + | +LL | FOO => {}, + | ^^^ + +error: unreachable pattern + --> $DIR/issue-78057.rs:14:9 + | +LL | _ => {} + | ^ + | +note: the lint level is defined here + --> $DIR/issue-78057.rs:1:9 + | +LL | #![deny(unreachable_patterns)] + | ^^^^^^^^^^^^^^^^^^^^ + +error: aborting due to 2 previous errors + diff --git a/src/test/ui/issues/issue-44333.rs b/src/test/ui/issues/issue-44333.rs deleted file mode 100644 index 96e8795e52d..00000000000 --- a/src/test/ui/issues/issue-44333.rs +++ /dev/null @@ -1,25 +0,0 @@ -// run-pass - -#![warn(pointer_structural_match)] - -type Func = fn(usize, usize) -> usize; - -fn foo(a: usize, b: usize) -> usize { a + b } -fn bar(a: usize, b: usize) -> usize { a * b } -fn test(x: usize) -> Func { - if x % 2 == 0 { foo } - else { bar } -} - -const FOO: Func = foo; -const BAR: Func = bar; - -fn main() { - match test(std::env::consts::ARCH.len()) { - FOO => println!("foo"), //~ WARN pointers in patterns behave unpredictably - //~^ WARN will become a hard error - BAR => println!("bar"), //~ WARN pointers in patterns behave unpredictably - //~^ WARN will become a hard error - _ => unreachable!(), - } -} diff --git a/src/test/ui/issues/issue-44333.stderr b/src/test/ui/issues/issue-44333.stderr deleted file mode 100644 index 8302b09e533..00000000000 --- a/src/test/ui/issues/issue-44333.stderr +++ /dev/null @@ -1,25 +0,0 @@ -warning: function pointers and unsized pointers in patterns behave unpredictably and should not be relied upon. See https://github.com/rust-lang/rust/issues/70861 for details. - --> $DIR/issue-44333.rs:19:9 - | -LL | FOO => println!("foo"), - | ^^^ - | -note: the lint level is defined here - --> $DIR/issue-44333.rs:3:9 - | -LL | #![warn(pointer_structural_match)] - | ^^^^^^^^^^^^^^^^^^^^^^^^ - = warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release! - = note: for more information, see issue #62411 - -warning: function pointers and unsized pointers in patterns behave unpredictably and should not be relied upon. See https://github.com/rust-lang/rust/issues/70861 for details. - --> $DIR/issue-44333.rs:21:9 - | -LL | BAR => println!("bar"), - | ^^^ - | - = warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release! - = note: for more information, see issue #62411 - -warning: 2 warnings emitted - diff --git a/src/test/ui/pattern/usefulness/consts-opaque.rs b/src/test/ui/pattern/usefulness/consts-opaque.rs new file mode 100644 index 00000000000..928756723a6 --- /dev/null +++ b/src/test/ui/pattern/usefulness/consts-opaque.rs @@ -0,0 +1,113 @@ +// This file tests the exhaustiveness algorithm on opaque constants. Most of the examples give +// unnecessary warnings because const_to_pat.rs converts a constant pattern to a wildcard when the +// constant is not allowed as a pattern. This is an edge case so we may not care to fix it. +// See also https://github.com/rust-lang/rust/issues/78057 + +#![deny(unreachable_patterns)] + +#[derive(PartialEq)] +struct Foo(i32); +impl Eq for Foo {} +const FOO: Foo = Foo(42); +const FOO_REF: &Foo = &Foo(42); +const FOO_REF_REF: &&Foo = &&Foo(42); + +#[derive(PartialEq)] +struct Bar; +impl Eq for Bar {} +const BAR: Bar = Bar; + +#[derive(PartialEq)] +enum Baz { + Baz1, + Baz2 +} +impl Eq for Baz {} +const BAZ: Baz = Baz::Baz1; + +type Quux = fn(usize, usize) -> usize; +fn quux(a: usize, b: usize) -> usize { a + b } +const QUUX: Quux = quux; + +fn main() { + match FOO { + FOO => {} + //~^ ERROR must be annotated with `#[derive(PartialEq, Eq)]` + _ => {} // should not be emitting unreachable warning + //~^ ERROR unreachable pattern + } + + match FOO_REF { + FOO_REF => {} + //~^ ERROR must be annotated with `#[derive(PartialEq, Eq)]` + Foo(_) => {} // should not be emitting unreachable warning + //~^ ERROR unreachable pattern + } + + // FIXME: this causes an ICE (https://github.com/rust-lang/rust/issues/78071) + //match FOO_REF_REF { + // FOO_REF_REF => {} + // Foo(_) => {} + //} + + match BAR { + Bar => {} + BAR => {} // should not be emitting unreachable warning + //~^ ERROR must be annotated with `#[derive(PartialEq, Eq)]` + //~| ERROR unreachable pattern + _ => {} + //~^ ERROR unreachable pattern + } + + match BAR { + BAR => {} + //~^ ERROR must be annotated with `#[derive(PartialEq, Eq)]` + Bar => {} // should not be emitting unreachable warning + //~^ ERROR unreachable pattern + _ => {} + //~^ ERROR unreachable pattern + } + + match BAR { + BAR => {} + //~^ ERROR must be annotated with `#[derive(PartialEq, Eq)]` + BAR => {} // should not be emitting unreachable warning + //~^ ERROR must be annotated with `#[derive(PartialEq, Eq)]` + //~| ERROR unreachable pattern + _ => {} // should not be emitting unreachable warning + //~^ ERROR unreachable pattern + } + + match BAZ { + BAZ => {} + //~^ ERROR must be annotated with `#[derive(PartialEq, Eq)]` + Baz::Baz1 => {} // should not be emitting unreachable warning + //~^ ERROR unreachable pattern + _ => {} + //~^ ERROR unreachable pattern + } + + match BAZ { + Baz::Baz1 => {} + BAZ => {} + //~^ ERROR must be annotated with `#[derive(PartialEq, Eq)]` + _ => {} + //~^ ERROR unreachable pattern + } + + match BAZ { + BAZ => {} + //~^ ERROR must be annotated with `#[derive(PartialEq, Eq)]` + Baz::Baz2 => {} // should not be emitting unreachable warning + //~^ ERROR unreachable pattern + _ => {} // should not be emitting unreachable warning + //~^ ERROR unreachable pattern + } + + match QUUX { + QUUX => {} + QUUX => {} // should not be emitting unreachable warning + //~^ ERROR unreachable pattern + _ => {} + } +} diff --git a/src/test/ui/pattern/usefulness/consts-opaque.stderr b/src/test/ui/pattern/usefulness/consts-opaque.stderr new file mode 100644 index 00000000000..07cdc1c95fa --- /dev/null +++ b/src/test/ui/pattern/usefulness/consts-opaque.stderr @@ -0,0 +1,154 @@ +error: to use a constant of type `Foo` in a pattern, `Foo` must be annotated with `#[derive(PartialEq, Eq)]` + --> $DIR/consts-opaque.rs:34:9 + | +LL | FOO => {} + | ^^^ + +error: unreachable pattern + --> $DIR/consts-opaque.rs:36:9 + | +LL | _ => {} // should not be emitting unreachable warning + | ^ + | +note: the lint level is defined here + --> $DIR/consts-opaque.rs:6:9 + | +LL | #![deny(unreachable_patterns)] + | ^^^^^^^^^^^^^^^^^^^^ + +error: to use a constant of type `Foo` in a pattern, `Foo` must be annotated with `#[derive(PartialEq, Eq)]` + --> $DIR/consts-opaque.rs:41:9 + | +LL | FOO_REF => {} + | ^^^^^^^ + +error: unreachable pattern + --> $DIR/consts-opaque.rs:43:9 + | +LL | Foo(_) => {} // should not be emitting unreachable warning + | ^^^^^^ + +error: to use a constant of type `Bar` in a pattern, `Bar` must be annotated with `#[derive(PartialEq, Eq)]` + --> $DIR/consts-opaque.rs:55:9 + | +LL | BAR => {} // should not be emitting unreachable warning + | ^^^ + +error: unreachable pattern + --> $DIR/consts-opaque.rs:55:9 + | +LL | Bar => {} + | --- matches any value +LL | BAR => {} // should not be emitting unreachable warning + | ^^^ unreachable pattern + +error: unreachable pattern + --> $DIR/consts-opaque.rs:58:9 + | +LL | Bar => {} + | --- matches any value +... +LL | _ => {} + | ^ unreachable pattern + +error: to use a constant of type `Bar` in a pattern, `Bar` must be annotated with `#[derive(PartialEq, Eq)]` + --> $DIR/consts-opaque.rs:63:9 + | +LL | BAR => {} + | ^^^ + +error: unreachable pattern + --> $DIR/consts-opaque.rs:65:9 + | +LL | Bar => {} // should not be emitting unreachable warning + | ^^^ + +error: unreachable pattern + --> $DIR/consts-opaque.rs:67:9 + | +LL | Bar => {} // should not be emitting unreachable warning + | --- matches any value +LL | +LL | _ => {} + | ^ unreachable pattern + +error: to use a constant of type `Bar` in a pattern, `Bar` must be annotated with `#[derive(PartialEq, Eq)]` + --> $DIR/consts-opaque.rs:72:9 + | +LL | BAR => {} + | ^^^ + +error: to use a constant of type `Bar` in a pattern, `Bar` must be annotated with `#[derive(PartialEq, Eq)]` + --> $DIR/consts-opaque.rs:74:9 + | +LL | BAR => {} // should not be emitting unreachable warning + | ^^^ + +error: unreachable pattern + --> $DIR/consts-opaque.rs:74:9 + | +LL | BAR => {} // should not be emitting unreachable warning + | ^^^ + +error: unreachable pattern + --> $DIR/consts-opaque.rs:77:9 + | +LL | _ => {} // should not be emitting unreachable warning + | ^ + +error: to use a constant of type `Baz` in a pattern, `Baz` must be annotated with `#[derive(PartialEq, Eq)]` + --> $DIR/consts-opaque.rs:82:9 + | +LL | BAZ => {} + | ^^^ + +error: unreachable pattern + --> $DIR/consts-opaque.rs:84:9 + | +LL | Baz::Baz1 => {} // should not be emitting unreachable warning + | ^^^^^^^^^ + +error: unreachable pattern + --> $DIR/consts-opaque.rs:86:9 + | +LL | _ => {} + | ^ + +error: to use a constant of type `Baz` in a pattern, `Baz` must be annotated with `#[derive(PartialEq, Eq)]` + --> $DIR/consts-opaque.rs:92:9 + | +LL | BAZ => {} + | ^^^ + +error: unreachable pattern + --> $DIR/consts-opaque.rs:94:9 + | +LL | _ => {} + | ^ + +error: to use a constant of type `Baz` in a pattern, `Baz` must be annotated with `#[derive(PartialEq, Eq)]` + --> $DIR/consts-opaque.rs:99:9 + | +LL | BAZ => {} + | ^^^ + +error: unreachable pattern + --> $DIR/consts-opaque.rs:101:9 + | +LL | Baz::Baz2 => {} // should not be emitting unreachable warning + | ^^^^^^^^^ + +error: unreachable pattern + --> $DIR/consts-opaque.rs:103:9 + | +LL | _ => {} // should not be emitting unreachable warning + | ^ + +error: unreachable pattern + --> $DIR/consts-opaque.rs:109:9 + | +LL | QUUX => {} // should not be emitting unreachable warning + | ^^^^ + +error: aborting due to 23 previous errors + -- cgit 1.4.1-3-g733a5 From bb8111069ed38455d915f7f8f0e050bb3c3720cf Mon Sep 17 00:00:00 2001 From: Oliver Scherer Date: Tue, 29 Sep 2020 18:28:39 +0200 Subject: Destructure byte array constants to array patterns instead of keeping them opaque --- .../rustc_mir_build/src/thir/pattern/_match.rs | 32 +++------------------- .../src/thir/pattern/const_to_pat.rs | 11 +++++--- .../usefulness/match-byte-array-patterns-2.stderr | 4 +-- 3 files changed, 13 insertions(+), 34 deletions(-) (limited to 'src/test') diff --git a/compiler/rustc_mir_build/src/thir/pattern/_match.rs b/compiler/rustc_mir_build/src/thir/pattern/_match.rs index 904524e13ae..b657f25f1ed 100644 --- a/compiler/rustc_mir_build/src/thir/pattern/_match.rs +++ b/compiler/rustc_mir_build/src/thir/pattern/_match.rs @@ -304,12 +304,11 @@ use std::iter::{FromIterator, IntoIterator}; use std::ops::RangeInclusive; crate fn expand_pattern<'a, 'tcx>(cx: &MatchCheckCtxt<'a, 'tcx>, pat: Pat<'tcx>) -> Pat<'tcx> { - LiteralExpander { tcx: cx.tcx, param_env: cx.param_env }.fold_pattern(&pat) + LiteralExpander { tcx: cx.tcx }.fold_pattern(&pat) } struct LiteralExpander<'tcx> { tcx: TyCtxt<'tcx>, - param_env: ty::ParamEnv<'tcx>, } impl<'tcx> LiteralExpander<'tcx> { @@ -328,40 +327,17 @@ impl<'tcx> LiteralExpander<'tcx> { ) -> ConstValue<'tcx> { debug!("fold_const_value_deref {:?} {:?} {:?}", val, rty, crty); match (val, &crty.kind(), &rty.kind()) { - // the easy case, deref a reference - (ConstValue::Scalar(p), x, y) if x == y => { - match p { - Scalar::Ptr(p) => { - let alloc = self.tcx.global_alloc(p.alloc_id).unwrap_memory(); - ConstValue::ByRef { alloc, offset: p.offset } - } - Scalar::Raw { .. } => { - let layout = self.tcx.layout_of(self.param_env.and(rty)).unwrap(); - if layout.is_zst() { - // Deref of a reference to a ZST is a nop. - ConstValue::Scalar(Scalar::zst()) - } else { - // FIXME(oli-obk): this is reachable for `const FOO: &&&u32 = &&&42;` - bug!("cannot deref {:#?}, {} -> {}", val, crty, rty); - } - } - } - } // unsize array to slice if pattern is array but match value or other patterns are slice (ConstValue::Scalar(Scalar::Ptr(p)), ty::Array(t, n), ty::Slice(u)) => { assert_eq!(t, u); + assert_eq!(p.offset, Size::ZERO); ConstValue::Slice { data: self.tcx.global_alloc(p.alloc_id).unwrap_memory(), - start: p.offset.bytes().try_into().unwrap(), + start: 0, end: n.eval_usize(self.tcx, ty::ParamEnv::empty()).try_into().unwrap(), } } - // fat pointers stay the same - (ConstValue::Slice { .. }, _, _) - | (_, ty::Slice(_), ty::Slice(_)) - | (_, ty::Str, ty::Str) => val, - // FIXME(oli-obk): this is reachable for `const FOO: &&&u32 = &&&42;` being used - _ => bug!("cannot deref {:#?}, {} -> {}", val, crty, rty), + _ => val, } } } diff --git a/compiler/rustc_mir_build/src/thir/pattern/const_to_pat.rs b/compiler/rustc_mir_build/src/thir/pattern/const_to_pat.rs index a203b3a1428..32b9b0a2d3b 100644 --- a/compiler/rustc_mir_build/src/thir/pattern/const_to_pat.rs +++ b/compiler/rustc_mir_build/src/thir/pattern/const_to_pat.rs @@ -390,11 +390,14 @@ impl<'a, 'tcx> ConstToPat<'a, 'tcx> { ty::Slice(elem_ty) if elem_ty == tcx.types.u8 => PatKind::Constant { value: cv }, // `b"foo"` produces a `&[u8; 3]`, but you can't use constants of array type when // matching against references, you can only use byte string literals. - // FIXME: clean this up, likely by permitting array patterns when matching on slices - ty::Array(elem_ty, _) if elem_ty == tcx.types.u8 => PatKind::Constant { value: cv }, + // The typechecker has a special case for byte string literals, by treating them + // as slices. This means we turn `&[T; N]` constants into slice patterns, which + // has no negative effects on pattern matching, even if we're actually matching on + // arrays. + ty::Array(..) | // Cannot merge this with the catch all branch below, because the `const_deref` - // changes the type from slice to array, and slice patterns behave differently from - // array patterns. + // changes the type from slice to array, we need to keep the original type in the + // pattern. ty::Slice(..) => { let old = self.behind_reference.replace(true); let array = tcx.deref_const(self.param_env.and(cv)); diff --git a/src/test/ui/pattern/usefulness/match-byte-array-patterns-2.stderr b/src/test/ui/pattern/usefulness/match-byte-array-patterns-2.stderr index ffc8433403f..7968f9713ff 100644 --- a/src/test/ui/pattern/usefulness/match-byte-array-patterns-2.stderr +++ b/src/test/ui/pattern/usefulness/match-byte-array-patterns-2.stderr @@ -7,11 +7,11 @@ LL | match buf { = help: ensure that all possible cases are being handled, possibly by adding wildcards or more match arms = note: the matched value is of type `&[u8; 4]` -error[E0004]: non-exhaustive patterns: `&[]`, `&[_]`, `&[_, _]` and 2 more not covered +error[E0004]: non-exhaustive patterns: `&[0_u8..=64_u8, _, _, _]` and `&[66_u8..=u8::MAX, _, _, _]` not covered --> $DIR/match-byte-array-patterns-2.rs:10:11 | LL | match buf { - | ^^^ patterns `&[]`, `&[_]`, `&[_, _]` and 2 more not covered + | ^^^ patterns `&[0_u8..=64_u8, _, _, _]` and `&[66_u8..=u8::MAX, _, _, _]` not covered | = help: ensure that all possible cases are being handled, possibly by adding wildcards or more match arms = note: the matched value is of type `&[u8]` -- cgit 1.4.1-3-g733a5 From da0ba2f64574e0ecca65d0d1045af00abe1515fd Mon Sep 17 00:00:00 2001 From: Nadrieril Date: Sun, 18 Oct 2020 13:48:54 +0100 Subject: The only remaining constant patterns are opaque --- .../rustc_mir_build/src/thir/pattern/_match.rs | 69 +++++++++++++--------- compiler/rustc_mir_build/src/thir/pattern/mod.rs | 7 +++ src/test/ui/pattern/usefulness/consts-opaque.rs | 3 +- .../ui/pattern/usefulness/consts-opaque.stderr | 8 +-- 4 files changed, 50 insertions(+), 37 deletions(-) (limited to 'src/test') diff --git a/compiler/rustc_mir_build/src/thir/pattern/_match.rs b/compiler/rustc_mir_build/src/thir/pattern/_match.rs index 31ec83a22ba..fb068f21d8a 100644 --- a/compiler/rustc_mir_build/src/thir/pattern/_match.rs +++ b/compiler/rustc_mir_build/src/thir/pattern/_match.rs @@ -394,9 +394,15 @@ impl<'p, 'tcx> PatStack<'p, 'tcx> { cx: &mut MatchCheckCtxt<'p, 'tcx>, constructor: &Constructor<'tcx>, ctor_wild_subpatterns: &Fields<'p, 'tcx>, + is_my_head_ctor: bool, ) -> Option> { - let new_fields = - specialize_one_pattern(cx, self.head(), constructor, ctor_wild_subpatterns)?; + let new_fields = specialize_one_pattern( + cx, + self.head(), + constructor, + ctor_wild_subpatterns, + is_my_head_ctor, + )?; Some(new_fields.push_on_patstack(&self.0[1..])) } } @@ -574,6 +580,7 @@ impl<'p, 'tcx> Matrix<'p, 'tcx> { cx, constructor, ctor_wild_subpatterns, + false, ) }) .collect() @@ -599,7 +606,9 @@ impl<'p, 'tcx> Matrix<'p, 'tcx> { SpecializationCache::Incompatible => self .patterns .iter() - .filter_map(|r| r.specialize_constructor(cx, constructor, ctor_wild_subpatterns)) + .filter_map(|r| { + r.specialize_constructor(cx, constructor, ctor_wild_subpatterns, false) + }) .collect(), } } @@ -821,8 +830,6 @@ enum Constructor<'tcx> { Single, /// Enum variants. Variant(DefId), - /// Literal values. - ConstantValue(&'tcx ty::Const<'tcx>), /// Ranges of integer literal values (`2`, `2..=5` or `2..5`). IntRange(IntRange<'tcx>), /// Ranges of floating-point literal values (`2.0..=5.2`). @@ -831,27 +838,22 @@ enum Constructor<'tcx> { Str(&'tcx ty::Const<'tcx>), /// Array and slice patterns. Slice(Slice), + /// Constants that must not be matched structurally. They are treated as black + /// boxes for the purposes of exhaustiveness: we must not inspect them, and they + /// don't count towards making a match exhaustive. + Opaque, /// Fake extra constructor for enums that aren't allowed to be matched exhaustively. NonExhaustive, } impl<'tcx> Constructor<'tcx> { - fn variant_index_for_adt<'a>( - &self, - cx: &MatchCheckCtxt<'a, 'tcx>, - adt: &'tcx ty::AdtDef, - ) -> VariantIdx { + fn variant_index_for_adt(&self, adt: &'tcx ty::AdtDef) -> VariantIdx { match *self { Variant(id) => adt.variant_index_with_id(id), Single => { assert!(!adt.is_enum()); VariantIdx::new(0) } - ConstantValue(c) => cx - .tcx - .destructure_const(cx.param_env.and(c)) - .variant - .expect("destructed const of adt without variant id"), _ => bug!("bad constructor {:?} for adt {:?}", self, adt), } } @@ -865,7 +867,7 @@ impl<'tcx> Constructor<'tcx> { match self { // Those constructors can only match themselves. - Single | Variant(_) | ConstantValue(..) | Str(..) | FloatRange(..) => { + Single | Variant(_) | Str(..) | FloatRange(..) => { if other_ctors.iter().any(|c| c == self) { vec![] } else { vec![self.clone()] } } &Slice(slice) => { @@ -936,6 +938,7 @@ impl<'tcx> Constructor<'tcx> { } // This constructor is never covered by anything else NonExhaustive => vec![NonExhaustive], + Opaque => bug!("unexpected opaque ctor {:?} found in all_ctors", self), } } @@ -975,7 +978,7 @@ impl<'tcx> Constructor<'tcx> { PatKind::Variant { adt_def: adt, substs, - variant_index: self.variant_index_for_adt(cx, adt), + variant_index: self.variant_index_for_adt(adt), subpatterns, } } else { @@ -1014,11 +1017,11 @@ impl<'tcx> Constructor<'tcx> { PatKind::Slice { prefix, slice: Some(wild), suffix } } }, - &ConstantValue(value) => PatKind::Constant { value }, &Str(value) => PatKind::Constant { value }, &FloatRange(lo, hi, end) => PatKind::Range(PatRange { lo, hi, end }), IntRange(range) => return range.to_pat(cx.tcx), NonExhaustive => PatKind::Wild, + Opaque => bug!("we should not try to apply an opaque constructor {:?}", self), }; Pat { ty, span: DUMMY_SP, kind: Box::new(pat) } @@ -1122,7 +1125,7 @@ impl<'p, 'tcx> Fields<'p, 'tcx> { // Use T as the sub pattern type of Box. Fields::from_single_pattern(wildcard_from_ty(substs.type_at(0))) } else { - let variant = &adt.variants[constructor.variant_index_for_adt(cx, adt)]; + let variant = &adt.variants[constructor.variant_index_for_adt(adt)]; // Whether we must not match the fields of this variant exhaustively. let is_non_exhaustive = variant.is_field_list_non_exhaustive() && !adt.did.is_local(); @@ -1170,9 +1173,7 @@ impl<'p, 'tcx> Fields<'p, 'tcx> { } _ => bug!("bad slice pattern {:?} {:?}", constructor, ty), }, - ConstantValue(..) | Str(..) | FloatRange(..) | IntRange(..) | NonExhaustive => { - Fields::empty() - } + Str(..) | FloatRange(..) | IntRange(..) | NonExhaustive | Opaque => Fields::empty(), }; debug!("Fields::wildcards({:?}, {:?}) = {:#?}", constructor, ty, ret); ret @@ -2085,7 +2086,7 @@ fn is_useful_specialized<'p, 'tcx>( // We cache the result of `Fields::wildcards` because it is used a lot. let ctor_wild_subpatterns = Fields::wildcards(cx, &ctor, ty); let matrix = matrix.specialize_constructor(cx, &ctor, &ctor_wild_subpatterns); - v.specialize_constructor(cx, &ctor, &ctor_wild_subpatterns) + v.specialize_constructor(cx, &ctor, &ctor_wild_subpatterns, true) .map(|v| is_useful(cx, &matrix, &v, witness_preference, hir_id, is_under_guard, false)) .map(|u| u.apply_constructor(cx, &ctor, ty, &ctor_wild_subpatterns)) .unwrap_or(NotUseful) @@ -2112,7 +2113,7 @@ fn pat_constructor<'tcx>( match value.ty.kind() { ty::Float(_) => Some(FloatRange(value, value, RangeEnd::Included)), ty::Ref(_, t, _) if t.is_str() => Some(Str(value)), - _ => Some(ConstantValue(value)), + _ => Some(Opaque), } } } @@ -2461,15 +2462,26 @@ fn specialize_one_pattern<'p, 'tcx>( pat: &'p Pat<'tcx>, constructor: &Constructor<'tcx>, ctor_wild_subpatterns: &Fields<'p, 'tcx>, + is_its_own_ctor: bool, // Whether `ctor` is known to be derived from `pat` ) -> Option> { if let NonExhaustive = constructor { - // Only a wildcard pattern can match the special extra constructor + // Only a wildcard pattern can match the special extra constructor. if !pat.is_wildcard() { return None; } return Some(Fields::empty()); } + if let Opaque = constructor { + // Only a wildcard pattern can match an opaque constant, unless we're specializing the + // value against its own constructor. + if is_its_own_ctor || pat.is_wildcard() { + return Some(Fields::empty()); + } else { + return None; + } + } + let result = match *pat.kind { PatKind::AscribeUserType { .. } => bug!(), // Handled by `expand_pattern` @@ -2491,7 +2503,6 @@ fn specialize_one_pattern<'p, 'tcx>( PatKind::Constant { .. } | PatKind::Range { .. } => { match constructor { - Single => {} IntRange(ctor) => { let pat = IntRange::from_pat(cx.tcx, cx.param_env, pat)?; ctor.intersection(cx.tcx, &pat)?; @@ -2514,7 +2525,7 @@ fn specialize_one_pattern<'p, 'tcx>( return None; } } - ConstantValue(ctor_value) | Str(ctor_value) => { + Str(ctor_value) => { let pat_value = match *pat.kind { PatKind::Constant { value } => value, _ => span_bug!( @@ -2532,7 +2543,9 @@ fn specialize_one_pattern<'p, 'tcx>( } } _ => { - span_bug!(pat.span, "unexpected pattern {:?} with ctor {:?}", pat, constructor) + // If we reach here, we must be trying to inspect an opaque constant. Thus we skip + // the row. + return None; } } Some(Fields::empty()) diff --git a/compiler/rustc_mir_build/src/thir/pattern/mod.rs b/compiler/rustc_mir_build/src/thir/pattern/mod.rs index 718ed78889f..485e4cab87f 100644 --- a/compiler/rustc_mir_build/src/thir/pattern/mod.rs +++ b/compiler/rustc_mir_build/src/thir/pattern/mod.rs @@ -158,6 +158,13 @@ crate enum PatKind<'tcx> { subpattern: Pat<'tcx>, }, + /// One of the following: + /// * `&str`, which will be handled as a string pattern and thus exhaustiveness + /// checking will detect if you use the same string twice in different patterns. + /// * integer, bool, char or float, which will be handled by exhaustivenes to cover exactly + /// its own value, similar to `&str`, but these values are much simpler. + /// * Opaque constants, that must not be matched structurally. So anything that does not derive + /// `PartialEq` and `Eq`. Constant { value: &'tcx ty::Const<'tcx>, }, diff --git a/src/test/ui/pattern/usefulness/consts-opaque.rs b/src/test/ui/pattern/usefulness/consts-opaque.rs index 928756723a6..761b293e998 100644 --- a/src/test/ui/pattern/usefulness/consts-opaque.rs +++ b/src/test/ui/pattern/usefulness/consts-opaque.rs @@ -106,8 +106,7 @@ fn main() { match QUUX { QUUX => {} - QUUX => {} // should not be emitting unreachable warning - //~^ ERROR unreachable pattern + QUUX => {} _ => {} } } diff --git a/src/test/ui/pattern/usefulness/consts-opaque.stderr b/src/test/ui/pattern/usefulness/consts-opaque.stderr index 07cdc1c95fa..6d7e36e01f7 100644 --- a/src/test/ui/pattern/usefulness/consts-opaque.stderr +++ b/src/test/ui/pattern/usefulness/consts-opaque.stderr @@ -144,11 +144,5 @@ error: unreachable pattern LL | _ => {} // should not be emitting unreachable warning | ^ -error: unreachable pattern - --> $DIR/consts-opaque.rs:109:9 - | -LL | QUUX => {} // should not be emitting unreachable warning - | ^^^^ - -error: aborting due to 23 previous errors +error: aborting due to 22 previous errors -- cgit 1.4.1-3-g733a5 From 9890217c0eedcbe4742e412ea59e851ecd500bf5 Mon Sep 17 00:00:00 2001 From: Mara Bos Date: Mon, 19 Oct 2020 22:36:45 +0200 Subject: Fix ui test for updated core::panic behaviour. It now throws a &str instead of a String. --- src/test/ui/intrinsics/panic-uninitialized-zeroed.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'src/test') diff --git a/src/test/ui/intrinsics/panic-uninitialized-zeroed.rs b/src/test/ui/intrinsics/panic-uninitialized-zeroed.rs index 24474cabf1e..4a91198ab9f 100644 --- a/src/test/ui/intrinsics/panic-uninitialized-zeroed.rs +++ b/src/test/ui/intrinsics/panic-uninitialized-zeroed.rs @@ -53,8 +53,8 @@ enum LR_NonZero { fn test_panic_msg(op: impl (FnOnce() -> T) + panic::UnwindSafe, msg: &str) { let err = panic::catch_unwind(op).err(); assert_eq!( - err.as_ref().and_then(|a| a.downcast_ref::()).map(|s| &**s), - Some(msg) + err.as_ref().and_then(|a| a.downcast_ref::<&str>()), + Some(&msg) ); } -- cgit 1.4.1-3-g733a5 From 5bfd3e7259caa4b94f9cfcf2af9ccc1d8420e286 Mon Sep 17 00:00:00 2001 From: Nadrieril Date: Tue, 20 Oct 2020 00:06:00 +0100 Subject: Accidentally fixed #78071 --- src/test/ui/pattern/usefulness/consts-opaque.rs | 12 +++--- .../ui/pattern/usefulness/consts-opaque.stderr | 48 +++++++++++++--------- 2 files changed, 36 insertions(+), 24 deletions(-) (limited to 'src/test') diff --git a/src/test/ui/pattern/usefulness/consts-opaque.rs b/src/test/ui/pattern/usefulness/consts-opaque.rs index 761b293e998..f87f96e34fc 100644 --- a/src/test/ui/pattern/usefulness/consts-opaque.rs +++ b/src/test/ui/pattern/usefulness/consts-opaque.rs @@ -44,11 +44,13 @@ fn main() { //~^ ERROR unreachable pattern } - // FIXME: this causes an ICE (https://github.com/rust-lang/rust/issues/78071) - //match FOO_REF_REF { - // FOO_REF_REF => {} - // Foo(_) => {} - //} + // This used to cause an ICE (https://github.com/rust-lang/rust/issues/78071) + match FOO_REF_REF { + FOO_REF_REF => {} + //~^ WARNING must be annotated with `#[derive(PartialEq, Eq)]` + //~| WARNING this was previously accepted by the compiler but is being phased out + Foo(_) => {} + } match BAR { Bar => {} diff --git a/src/test/ui/pattern/usefulness/consts-opaque.stderr b/src/test/ui/pattern/usefulness/consts-opaque.stderr index 6d7e36e01f7..f10166d5a35 100644 --- a/src/test/ui/pattern/usefulness/consts-opaque.stderr +++ b/src/test/ui/pattern/usefulness/consts-opaque.stderr @@ -28,14 +28,24 @@ error: unreachable pattern LL | Foo(_) => {} // should not be emitting unreachable warning | ^^^^^^ +warning: to use a constant of type `Foo` in a pattern, `Foo` must be annotated with `#[derive(PartialEq, Eq)]` + --> $DIR/consts-opaque.rs:49:9 + | +LL | FOO_REF_REF => {} + | ^^^^^^^^^^^ + | + = note: `#[warn(indirect_structural_match)]` on by default + = warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release! + = note: for more information, see issue #62411 + error: to use a constant of type `Bar` in a pattern, `Bar` must be annotated with `#[derive(PartialEq, Eq)]` - --> $DIR/consts-opaque.rs:55:9 + --> $DIR/consts-opaque.rs:57:9 | LL | BAR => {} // should not be emitting unreachable warning | ^^^ error: unreachable pattern - --> $DIR/consts-opaque.rs:55:9 + --> $DIR/consts-opaque.rs:57:9 | LL | Bar => {} | --- matches any value @@ -43,7 +53,7 @@ LL | BAR => {} // should not be emitting unreachable warning | ^^^ unreachable pattern error: unreachable pattern - --> $DIR/consts-opaque.rs:58:9 + --> $DIR/consts-opaque.rs:60:9 | LL | Bar => {} | --- matches any value @@ -52,19 +62,19 @@ LL | _ => {} | ^ unreachable pattern error: to use a constant of type `Bar` in a pattern, `Bar` must be annotated with `#[derive(PartialEq, Eq)]` - --> $DIR/consts-opaque.rs:63:9 + --> $DIR/consts-opaque.rs:65:9 | LL | BAR => {} | ^^^ error: unreachable pattern - --> $DIR/consts-opaque.rs:65:9 + --> $DIR/consts-opaque.rs:67:9 | LL | Bar => {} // should not be emitting unreachable warning | ^^^ error: unreachable pattern - --> $DIR/consts-opaque.rs:67:9 + --> $DIR/consts-opaque.rs:69:9 | LL | Bar => {} // should not be emitting unreachable warning | --- matches any value @@ -73,76 +83,76 @@ LL | _ => {} | ^ unreachable pattern error: to use a constant of type `Bar` in a pattern, `Bar` must be annotated with `#[derive(PartialEq, Eq)]` - --> $DIR/consts-opaque.rs:72:9 + --> $DIR/consts-opaque.rs:74:9 | LL | BAR => {} | ^^^ error: to use a constant of type `Bar` in a pattern, `Bar` must be annotated with `#[derive(PartialEq, Eq)]` - --> $DIR/consts-opaque.rs:74:9 + --> $DIR/consts-opaque.rs:76:9 | LL | BAR => {} // should not be emitting unreachable warning | ^^^ error: unreachable pattern - --> $DIR/consts-opaque.rs:74:9 + --> $DIR/consts-opaque.rs:76:9 | LL | BAR => {} // should not be emitting unreachable warning | ^^^ error: unreachable pattern - --> $DIR/consts-opaque.rs:77:9 + --> $DIR/consts-opaque.rs:79:9 | LL | _ => {} // should not be emitting unreachable warning | ^ error: to use a constant of type `Baz` in a pattern, `Baz` must be annotated with `#[derive(PartialEq, Eq)]` - --> $DIR/consts-opaque.rs:82:9 + --> $DIR/consts-opaque.rs:84:9 | LL | BAZ => {} | ^^^ error: unreachable pattern - --> $DIR/consts-opaque.rs:84:9 + --> $DIR/consts-opaque.rs:86:9 | LL | Baz::Baz1 => {} // should not be emitting unreachable warning | ^^^^^^^^^ error: unreachable pattern - --> $DIR/consts-opaque.rs:86:9 + --> $DIR/consts-opaque.rs:88:9 | LL | _ => {} | ^ error: to use a constant of type `Baz` in a pattern, `Baz` must be annotated with `#[derive(PartialEq, Eq)]` - --> $DIR/consts-opaque.rs:92:9 + --> $DIR/consts-opaque.rs:94:9 | LL | BAZ => {} | ^^^ error: unreachable pattern - --> $DIR/consts-opaque.rs:94:9 + --> $DIR/consts-opaque.rs:96:9 | LL | _ => {} | ^ error: to use a constant of type `Baz` in a pattern, `Baz` must be annotated with `#[derive(PartialEq, Eq)]` - --> $DIR/consts-opaque.rs:99:9 + --> $DIR/consts-opaque.rs:101:9 | LL | BAZ => {} | ^^^ error: unreachable pattern - --> $DIR/consts-opaque.rs:101:9 + --> $DIR/consts-opaque.rs:103:9 | LL | Baz::Baz2 => {} // should not be emitting unreachable warning | ^^^^^^^^^ error: unreachable pattern - --> $DIR/consts-opaque.rs:103:9 + --> $DIR/consts-opaque.rs:105:9 | LL | _ => {} // should not be emitting unreachable warning | ^ -error: aborting due to 22 previous errors +error: aborting due to 22 previous errors; 1 warning emitted -- cgit 1.4.1-3-g733a5 From a4dc92b483420e1975111c8e7a2c5dff49f13845 Mon Sep 17 00:00:00 2001 From: Tomasz Miąsko Date: Wed, 21 Oct 2020 00:00:00 +0000 Subject: Introduce a temporary for discriminant value in MatchBranchSimplification The optimization introduces additional uses of the discriminant operand, but does not ensure that it is still valid to evaluate it or that it still evaluates to the same value. Evaluate it once at original position, and store the result in a new temporary. --- compiler/rustc_mir/src/transform/match_branches.rs | 30 ++++-- ...anches.bar.MatchBranchSimplification.32bit.diff | 8 +- ...anches.bar.MatchBranchSimplification.64bit.diff | 8 +- ...anches.foo.MatchBranchSimplification.32bit.diff | 6 +- ...anches.foo.MatchBranchSimplification.64bit.diff | 6 +- ..._nested_if.MatchBranchSimplification.32bit.diff | 116 +++++++++++++++++++++ ..._nested_if.MatchBranchSimplification.64bit.diff | 116 +++++++++++++++++++++ src/test/mir-opt/matches_reduce_branches.rs | 10 +- .../mir-opt/not_equal_false.opt.InstCombine.diff | 57 +++++----- 9 files changed, 309 insertions(+), 48 deletions(-) create mode 100644 src/test/mir-opt/matches_reduce_branches.match_nested_if.MatchBranchSimplification.32bit.diff create mode 100644 src/test/mir-opt/matches_reduce_branches.match_nested_if.MatchBranchSimplification.64bit.diff (limited to 'src/test') diff --git a/compiler/rustc_mir/src/transform/match_branches.rs b/compiler/rustc_mir/src/transform/match_branches.rs index 0f9b2ff5ab8..06690dcbf6e 100644 --- a/compiler/rustc_mir/src/transform/match_branches.rs +++ b/compiler/rustc_mir/src/transform/match_branches.rs @@ -38,19 +38,16 @@ pub struct MatchBranchSimplification; impl<'tcx> MirPass<'tcx> for MatchBranchSimplification { fn run_pass(&self, tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) { - // FIXME: This optimization can result in unsoundness, because it introduces - // additional uses of a place holding the discriminant value without ensuring that - // it is valid to do so. - if !tcx.sess.opts.debugging_opts.unsound_mir_opts { + if tcx.sess.opts.debugging_opts.mir_opt_level <= 1 { return; } let param_env = tcx.param_env(body.source.def_id()); - let bbs = body.basic_blocks_mut(); + let (bbs, local_decls) = body.basic_blocks_and_local_decls_mut(); 'outer: for bb_idx in bbs.indices() { let (discr, val, switch_ty, first, second) = match bbs[bb_idx].terminator().kind { TerminatorKind::SwitchInt { - discr: Operand::Copy(ref place) | Operand::Move(ref place), + discr: ref discr @ (Operand::Copy(_) | Operand::Move(_)), switch_ty, ref targets, .. @@ -59,7 +56,7 @@ impl<'tcx> MirPass<'tcx> for MatchBranchSimplification { if target == targets.otherwise() { continue; } - (place, value, switch_ty, target, targets.otherwise()) + (discr, value, switch_ty, target, targets.otherwise()) } // Only optimize switch int statements _ => continue, @@ -99,6 +96,10 @@ impl<'tcx> MirPass<'tcx> for MatchBranchSimplification { // Take ownership of items now that we know we can optimize. let discr = discr.clone(); + // Introduce a temporary for the discriminant value. + let source_info = bbs[bb_idx].terminator().source_info; + let discr_local = local_decls.push(LocalDecl::new(switch_ty, source_info.span)); + // We already checked that first and second are different blocks, // and bb_idx has a different terminator from both of them. let (from, first, second) = bbs.pick3_mut(bb_idx, first, second); @@ -127,7 +128,11 @@ impl<'tcx> MirPass<'tcx> for MatchBranchSimplification { rustc_span::DUMMY_SP, ); let op = if f_b { BinOp::Eq } else { BinOp::Ne }; - let rhs = Rvalue::BinaryOp(op, Operand::Copy(discr.clone()), const_cmp); + let rhs = Rvalue::BinaryOp( + op, + Operand::Copy(Place::from(discr_local)), + const_cmp, + ); Statement { source_info: f.source_info, kind: StatementKind::Assign(box (*lhs, rhs)), @@ -138,7 +143,16 @@ impl<'tcx> MirPass<'tcx> for MatchBranchSimplification { _ => unreachable!(), } }); + + from.statements + .push(Statement { source_info, kind: StatementKind::StorageLive(discr_local) }); + from.statements.push(Statement { + source_info, + kind: StatementKind::Assign(box (Place::from(discr_local), Rvalue::Use(discr))), + }); from.statements.extend(new_stmts); + from.statements + .push(Statement { source_info, kind: StatementKind::StorageDead(discr_local) }); from.terminator_mut().kind = first.terminator().kind.clone(); } } diff --git a/src/test/mir-opt/matches_reduce_branches.bar.MatchBranchSimplification.32bit.diff b/src/test/mir-opt/matches_reduce_branches.bar.MatchBranchSimplification.32bit.diff index 648cf241cba..d3a29aa5d51 100644 --- a/src/test/mir-opt/matches_reduce_branches.bar.MatchBranchSimplification.32bit.diff +++ b/src/test/mir-opt/matches_reduce_branches.bar.MatchBranchSimplification.32bit.diff @@ -10,6 +10,7 @@ let mut _8: bool; // in scope 0 at $DIR/matches_reduce_branches.rs:35:9: 35:10 let mut _9: bool; // in scope 0 at $DIR/matches_reduce_branches.rs:35:12: 35:13 let mut _10: bool; // in scope 0 at $DIR/matches_reduce_branches.rs:35:15: 35:16 ++ let mut _11: i32; // in scope 0 at $DIR/matches_reduce_branches.rs:19:9: 19:10 scope 1 { debug a => _2; // in scope 1 at $DIR/matches_reduce_branches.rs:13:9: 13:10 let _3: bool; // in scope 1 at $DIR/matches_reduce_branches.rs:14:9: 14:10 @@ -33,10 +34,13 @@ StorageLive(_5); // scope 3 at $DIR/matches_reduce_branches.rs:16:9: 16:10 StorageLive(_6); // scope 4 at $DIR/matches_reduce_branches.rs:18:5: 33:6 - switchInt(_1) -> [7_i32: bb2, otherwise: bb1]; // scope 4 at $DIR/matches_reduce_branches.rs:19:9: 19:10 -+ _2 = Ne(_1, const 7_i32); // scope 4 at $DIR/matches_reduce_branches.rs:20:13: 20:22 -+ _3 = Eq(_1, const 7_i32); // scope 4 at $DIR/matches_reduce_branches.rs:21:13: 21:21 ++ StorageLive(_11); // scope 4 at $DIR/matches_reduce_branches.rs:19:9: 19:10 ++ _11 = _1; // scope 4 at $DIR/matches_reduce_branches.rs:19:9: 19:10 ++ _2 = Ne(_11, const 7_i32); // scope 4 at $DIR/matches_reduce_branches.rs:20:13: 20:22 ++ _3 = Eq(_11, const 7_i32); // scope 4 at $DIR/matches_reduce_branches.rs:21:13: 21:21 + _4 = const false; // scope 4 at $DIR/matches_reduce_branches.rs:22:13: 22:22 + _5 = const true; // scope 4 at $DIR/matches_reduce_branches.rs:23:13: 23:21 ++ StorageDead(_11); // scope 4 at $DIR/matches_reduce_branches.rs:19:9: 19:10 + goto -> bb3; // scope 4 at $DIR/matches_reduce_branches.rs:19:9: 19:10 } diff --git a/src/test/mir-opt/matches_reduce_branches.bar.MatchBranchSimplification.64bit.diff b/src/test/mir-opt/matches_reduce_branches.bar.MatchBranchSimplification.64bit.diff index 648cf241cba..d3a29aa5d51 100644 --- a/src/test/mir-opt/matches_reduce_branches.bar.MatchBranchSimplification.64bit.diff +++ b/src/test/mir-opt/matches_reduce_branches.bar.MatchBranchSimplification.64bit.diff @@ -10,6 +10,7 @@ let mut _8: bool; // in scope 0 at $DIR/matches_reduce_branches.rs:35:9: 35:10 let mut _9: bool; // in scope 0 at $DIR/matches_reduce_branches.rs:35:12: 35:13 let mut _10: bool; // in scope 0 at $DIR/matches_reduce_branches.rs:35:15: 35:16 ++ let mut _11: i32; // in scope 0 at $DIR/matches_reduce_branches.rs:19:9: 19:10 scope 1 { debug a => _2; // in scope 1 at $DIR/matches_reduce_branches.rs:13:9: 13:10 let _3: bool; // in scope 1 at $DIR/matches_reduce_branches.rs:14:9: 14:10 @@ -33,10 +34,13 @@ StorageLive(_5); // scope 3 at $DIR/matches_reduce_branches.rs:16:9: 16:10 StorageLive(_6); // scope 4 at $DIR/matches_reduce_branches.rs:18:5: 33:6 - switchInt(_1) -> [7_i32: bb2, otherwise: bb1]; // scope 4 at $DIR/matches_reduce_branches.rs:19:9: 19:10 -+ _2 = Ne(_1, const 7_i32); // scope 4 at $DIR/matches_reduce_branches.rs:20:13: 20:22 -+ _3 = Eq(_1, const 7_i32); // scope 4 at $DIR/matches_reduce_branches.rs:21:13: 21:21 ++ StorageLive(_11); // scope 4 at $DIR/matches_reduce_branches.rs:19:9: 19:10 ++ _11 = _1; // scope 4 at $DIR/matches_reduce_branches.rs:19:9: 19:10 ++ _2 = Ne(_11, const 7_i32); // scope 4 at $DIR/matches_reduce_branches.rs:20:13: 20:22 ++ _3 = Eq(_11, const 7_i32); // scope 4 at $DIR/matches_reduce_branches.rs:21:13: 21:21 + _4 = const false; // scope 4 at $DIR/matches_reduce_branches.rs:22:13: 22:22 + _5 = const true; // scope 4 at $DIR/matches_reduce_branches.rs:23:13: 23:21 ++ StorageDead(_11); // scope 4 at $DIR/matches_reduce_branches.rs:19:9: 19:10 + goto -> bb3; // scope 4 at $DIR/matches_reduce_branches.rs:19:9: 19:10 } diff --git a/src/test/mir-opt/matches_reduce_branches.foo.MatchBranchSimplification.32bit.diff b/src/test/mir-opt/matches_reduce_branches.foo.MatchBranchSimplification.32bit.diff index a52abfb1a72..ba963e3fe92 100644 --- a/src/test/mir-opt/matches_reduce_branches.foo.MatchBranchSimplification.32bit.diff +++ b/src/test/mir-opt/matches_reduce_branches.foo.MatchBranchSimplification.32bit.diff @@ -6,12 +6,16 @@ let mut _0: (); // return place in scope 0 at $DIR/matches_reduce_branches.rs:6:25: 6:25 let mut _2: bool; // in scope 0 at $SRC_DIR/core/src/macros/mod.rs:LL:COL let mut _3: isize; // in scope 0 at $DIR/matches_reduce_branches.rs:7:22: 7:26 ++ let mut _4: isize; // in scope 0 at $DIR/matches_reduce_branches.rs:7:22: 7:26 bb0: { StorageLive(_2); // scope 0 at $SRC_DIR/core/src/macros/mod.rs:LL:COL _3 = discriminant(_1); // scope 0 at $DIR/matches_reduce_branches.rs:7:22: 7:26 - switchInt(move _3) -> [0_isize: bb2, otherwise: bb1]; // scope 0 at $DIR/matches_reduce_branches.rs:7:22: 7:26 -+ _2 = Eq(_3, const 0_isize); // scope 0 at $SRC_DIR/core/src/macros/mod.rs:LL:COL ++ StorageLive(_4); // scope 0 at $DIR/matches_reduce_branches.rs:7:22: 7:26 ++ _4 = move _3; // scope 0 at $DIR/matches_reduce_branches.rs:7:22: 7:26 ++ _2 = Eq(_4, const 0_isize); // scope 0 at $SRC_DIR/core/src/macros/mod.rs:LL:COL ++ StorageDead(_4); // scope 0 at $DIR/matches_reduce_branches.rs:7:22: 7:26 + goto -> bb3; // scope 0 at $DIR/matches_reduce_branches.rs:7:22: 7:26 } diff --git a/src/test/mir-opt/matches_reduce_branches.foo.MatchBranchSimplification.64bit.diff b/src/test/mir-opt/matches_reduce_branches.foo.MatchBranchSimplification.64bit.diff index a52abfb1a72..ba963e3fe92 100644 --- a/src/test/mir-opt/matches_reduce_branches.foo.MatchBranchSimplification.64bit.diff +++ b/src/test/mir-opt/matches_reduce_branches.foo.MatchBranchSimplification.64bit.diff @@ -6,12 +6,16 @@ let mut _0: (); // return place in scope 0 at $DIR/matches_reduce_branches.rs:6:25: 6:25 let mut _2: bool; // in scope 0 at $SRC_DIR/core/src/macros/mod.rs:LL:COL let mut _3: isize; // in scope 0 at $DIR/matches_reduce_branches.rs:7:22: 7:26 ++ let mut _4: isize; // in scope 0 at $DIR/matches_reduce_branches.rs:7:22: 7:26 bb0: { StorageLive(_2); // scope 0 at $SRC_DIR/core/src/macros/mod.rs:LL:COL _3 = discriminant(_1); // scope 0 at $DIR/matches_reduce_branches.rs:7:22: 7:26 - switchInt(move _3) -> [0_isize: bb2, otherwise: bb1]; // scope 0 at $DIR/matches_reduce_branches.rs:7:22: 7:26 -+ _2 = Eq(_3, const 0_isize); // scope 0 at $SRC_DIR/core/src/macros/mod.rs:LL:COL ++ StorageLive(_4); // scope 0 at $DIR/matches_reduce_branches.rs:7:22: 7:26 ++ _4 = move _3; // scope 0 at $DIR/matches_reduce_branches.rs:7:22: 7:26 ++ _2 = Eq(_4, const 0_isize); // scope 0 at $SRC_DIR/core/src/macros/mod.rs:LL:COL ++ StorageDead(_4); // scope 0 at $DIR/matches_reduce_branches.rs:7:22: 7:26 + goto -> bb3; // scope 0 at $DIR/matches_reduce_branches.rs:7:22: 7:26 } diff --git a/src/test/mir-opt/matches_reduce_branches.match_nested_if.MatchBranchSimplification.32bit.diff b/src/test/mir-opt/matches_reduce_branches.match_nested_if.MatchBranchSimplification.32bit.diff new file mode 100644 index 00000000000..1f46d3777be --- /dev/null +++ b/src/test/mir-opt/matches_reduce_branches.match_nested_if.MatchBranchSimplification.32bit.diff @@ -0,0 +1,116 @@ +- // MIR for `match_nested_if` before MatchBranchSimplification ++ // MIR for `match_nested_if` after MatchBranchSimplification + + fn match_nested_if() -> bool { + let mut _0: bool; // return place in scope 0 at $DIR/matches_reduce_branches.rs:38:25: 38:29 + let _1: bool; // in scope 0 at $DIR/matches_reduce_branches.rs:39:9: 39:12 + let mut _2: (); // in scope 0 at $DIR/matches_reduce_branches.rs:39:21: 39:23 + let mut _3: bool; // in scope 0 at $DIR/matches_reduce_branches.rs:40:15: 40:88 + let mut _4: bool; // in scope 0 at $DIR/matches_reduce_branches.rs:40:18: 40:68 + let mut _5: bool; // in scope 0 at $DIR/matches_reduce_branches.rs:40:21: 40:48 + let mut _6: bool; // in scope 0 at $DIR/matches_reduce_branches.rs:40:24: 40:28 ++ let mut _7: bool; // in scope 0 at $DIR/matches_reduce_branches.rs:40:21: 40:48 ++ let mut _8: bool; // in scope 0 at $DIR/matches_reduce_branches.rs:40:18: 40:68 ++ let mut _9: bool; // in scope 0 at $DIR/matches_reduce_branches.rs:40:15: 40:88 ++ let mut _10: bool; // in scope 0 at $DIR/matches_reduce_branches.rs:40:15: 40:88 + scope 1 { + debug val => _1; // in scope 1 at $DIR/matches_reduce_branches.rs:39:9: 39:12 + } + + bb0: { + StorageLive(_1); // scope 0 at $DIR/matches_reduce_branches.rs:39:9: 39:12 + StorageLive(_2); // scope 0 at $DIR/matches_reduce_branches.rs:39:21: 39:23 + StorageLive(_3); // scope 0 at $DIR/matches_reduce_branches.rs:40:15: 40:88 + StorageLive(_4); // scope 0 at $DIR/matches_reduce_branches.rs:40:18: 40:68 + StorageLive(_5); // scope 0 at $DIR/matches_reduce_branches.rs:40:21: 40:48 + StorageLive(_6); // scope 0 at $DIR/matches_reduce_branches.rs:40:24: 40:28 + _6 = const true; // scope 0 at $DIR/matches_reduce_branches.rs:40:24: 40:28 +- switchInt(_6) -> [false: bb2, otherwise: bb1]; // scope 0 at $DIR/matches_reduce_branches.rs:40:21: 40:48 ++ StorageLive(_7); // scope 0 at $DIR/matches_reduce_branches.rs:40:21: 40:48 ++ _7 = _6; // scope 0 at $DIR/matches_reduce_branches.rs:40:21: 40:48 ++ _5 = Ne(_7, const false); // scope 0 at $DIR/matches_reduce_branches.rs:40:42: 40:47 ++ StorageDead(_7); // scope 0 at $DIR/matches_reduce_branches.rs:40:21: 40:48 ++ goto -> bb3; // scope 0 at $DIR/matches_reduce_branches.rs:40:21: 40:48 + } + + bb1: { + _5 = const true; // scope 0 at $DIR/matches_reduce_branches.rs:40:30: 40:34 + goto -> bb3; // scope 0 at $DIR/matches_reduce_branches.rs:40:21: 40:48 + } + + bb2: { + _5 = const false; // scope 0 at $DIR/matches_reduce_branches.rs:40:42: 40:47 + goto -> bb3; // scope 0 at $DIR/matches_reduce_branches.rs:40:21: 40:48 + } + + bb3: { + StorageDead(_6); // scope 0 at $DIR/matches_reduce_branches.rs:40:47: 40:48 +- switchInt(_5) -> [false: bb4, otherwise: bb5]; // scope 0 at $DIR/matches_reduce_branches.rs:40:18: 40:68 ++ StorageLive(_8); // scope 0 at $DIR/matches_reduce_branches.rs:40:18: 40:68 ++ _8 = _5; // scope 0 at $DIR/matches_reduce_branches.rs:40:18: 40:68 ++ _4 = Ne(_8, const false); // scope 0 at $DIR/matches_reduce_branches.rs:40:62: 40:67 ++ StorageDead(_8); // scope 0 at $DIR/matches_reduce_branches.rs:40:18: 40:68 ++ goto -> bb6; // scope 0 at $DIR/matches_reduce_branches.rs:40:18: 40:68 + } + + bb4: { + _4 = const false; // scope 0 at $DIR/matches_reduce_branches.rs:40:62: 40:67 + goto -> bb6; // scope 0 at $DIR/matches_reduce_branches.rs:40:18: 40:68 + } + + bb5: { + _4 = const true; // scope 0 at $DIR/matches_reduce_branches.rs:40:50: 40:54 + goto -> bb6; // scope 0 at $DIR/matches_reduce_branches.rs:40:18: 40:68 + } + + bb6: { + StorageDead(_5); // scope 0 at $DIR/matches_reduce_branches.rs:40:67: 40:68 +- switchInt(_4) -> [false: bb7, otherwise: bb8]; // scope 0 at $DIR/matches_reduce_branches.rs:40:15: 40:88 ++ StorageLive(_9); // scope 0 at $DIR/matches_reduce_branches.rs:40:15: 40:88 ++ _9 = _4; // scope 0 at $DIR/matches_reduce_branches.rs:40:15: 40:88 ++ _3 = Ne(_9, const false); // scope 0 at $DIR/matches_reduce_branches.rs:40:82: 40:87 ++ StorageDead(_9); // scope 0 at $DIR/matches_reduce_branches.rs:40:15: 40:88 ++ goto -> bb9; // scope 0 at $DIR/matches_reduce_branches.rs:40:15: 40:88 + } + + bb7: { + _3 = const false; // scope 0 at $DIR/matches_reduce_branches.rs:40:82: 40:87 + goto -> bb9; // scope 0 at $DIR/matches_reduce_branches.rs:40:15: 40:88 + } + + bb8: { + _3 = const true; // scope 0 at $DIR/matches_reduce_branches.rs:40:70: 40:74 + goto -> bb9; // scope 0 at $DIR/matches_reduce_branches.rs:40:15: 40:88 + } + + bb9: { + StorageDead(_4); // scope 0 at $DIR/matches_reduce_branches.rs:40:87: 40:88 +- switchInt(move _3) -> [false: bb11, otherwise: bb10]; // scope 0 at $DIR/matches_reduce_branches.rs:40:15: 40:88 ++ StorageLive(_10); // scope 0 at $DIR/matches_reduce_branches.rs:40:15: 40:88 ++ _10 = move _3; // scope 0 at $DIR/matches_reduce_branches.rs:40:15: 40:88 ++ StorageDead(_3); // scope 0 at $DIR/matches_reduce_branches.rs:40:95: 40:96 ++ _1 = Ne(_10, const false); // scope 0 at $DIR/matches_reduce_branches.rs:41:14: 41:19 ++ StorageDead(_10); // scope 0 at $DIR/matches_reduce_branches.rs:40:15: 40:88 ++ goto -> bb12; // scope 0 at $DIR/matches_reduce_branches.rs:40:15: 40:88 + } + + bb10: { + StorageDead(_3); // scope 0 at $DIR/matches_reduce_branches.rs:40:95: 40:96 + _1 = const true; // scope 0 at $DIR/matches_reduce_branches.rs:40:92: 40:96 + goto -> bb12; // scope 0 at $DIR/matches_reduce_branches.rs:39:15: 42:6 + } + + bb11: { + StorageDead(_3); // scope 0 at $DIR/matches_reduce_branches.rs:40:95: 40:96 + _1 = const false; // scope 0 at $DIR/matches_reduce_branches.rs:41:14: 41:19 + goto -> bb12; // scope 0 at $DIR/matches_reduce_branches.rs:39:15: 42:6 + } + + bb12: { + StorageDead(_2); // scope 0 at $DIR/matches_reduce_branches.rs:42:6: 42:7 + _0 = _1; // scope 1 at $DIR/matches_reduce_branches.rs:43:5: 43:8 + StorageDead(_1); // scope 0 at $DIR/matches_reduce_branches.rs:44:1: 44:2 + return; // scope 0 at $DIR/matches_reduce_branches.rs:44:2: 44:2 + } + } + diff --git a/src/test/mir-opt/matches_reduce_branches.match_nested_if.MatchBranchSimplification.64bit.diff b/src/test/mir-opt/matches_reduce_branches.match_nested_if.MatchBranchSimplification.64bit.diff new file mode 100644 index 00000000000..1f46d3777be --- /dev/null +++ b/src/test/mir-opt/matches_reduce_branches.match_nested_if.MatchBranchSimplification.64bit.diff @@ -0,0 +1,116 @@ +- // MIR for `match_nested_if` before MatchBranchSimplification ++ // MIR for `match_nested_if` after MatchBranchSimplification + + fn match_nested_if() -> bool { + let mut _0: bool; // return place in scope 0 at $DIR/matches_reduce_branches.rs:38:25: 38:29 + let _1: bool; // in scope 0 at $DIR/matches_reduce_branches.rs:39:9: 39:12 + let mut _2: (); // in scope 0 at $DIR/matches_reduce_branches.rs:39:21: 39:23 + let mut _3: bool; // in scope 0 at $DIR/matches_reduce_branches.rs:40:15: 40:88 + let mut _4: bool; // in scope 0 at $DIR/matches_reduce_branches.rs:40:18: 40:68 + let mut _5: bool; // in scope 0 at $DIR/matches_reduce_branches.rs:40:21: 40:48 + let mut _6: bool; // in scope 0 at $DIR/matches_reduce_branches.rs:40:24: 40:28 ++ let mut _7: bool; // in scope 0 at $DIR/matches_reduce_branches.rs:40:21: 40:48 ++ let mut _8: bool; // in scope 0 at $DIR/matches_reduce_branches.rs:40:18: 40:68 ++ let mut _9: bool; // in scope 0 at $DIR/matches_reduce_branches.rs:40:15: 40:88 ++ let mut _10: bool; // in scope 0 at $DIR/matches_reduce_branches.rs:40:15: 40:88 + scope 1 { + debug val => _1; // in scope 1 at $DIR/matches_reduce_branches.rs:39:9: 39:12 + } + + bb0: { + StorageLive(_1); // scope 0 at $DIR/matches_reduce_branches.rs:39:9: 39:12 + StorageLive(_2); // scope 0 at $DIR/matches_reduce_branches.rs:39:21: 39:23 + StorageLive(_3); // scope 0 at $DIR/matches_reduce_branches.rs:40:15: 40:88 + StorageLive(_4); // scope 0 at $DIR/matches_reduce_branches.rs:40:18: 40:68 + StorageLive(_5); // scope 0 at $DIR/matches_reduce_branches.rs:40:21: 40:48 + StorageLive(_6); // scope 0 at $DIR/matches_reduce_branches.rs:40:24: 40:28 + _6 = const true; // scope 0 at $DIR/matches_reduce_branches.rs:40:24: 40:28 +- switchInt(_6) -> [false: bb2, otherwise: bb1]; // scope 0 at $DIR/matches_reduce_branches.rs:40:21: 40:48 ++ StorageLive(_7); // scope 0 at $DIR/matches_reduce_branches.rs:40:21: 40:48 ++ _7 = _6; // scope 0 at $DIR/matches_reduce_branches.rs:40:21: 40:48 ++ _5 = Ne(_7, const false); // scope 0 at $DIR/matches_reduce_branches.rs:40:42: 40:47 ++ StorageDead(_7); // scope 0 at $DIR/matches_reduce_branches.rs:40:21: 40:48 ++ goto -> bb3; // scope 0 at $DIR/matches_reduce_branches.rs:40:21: 40:48 + } + + bb1: { + _5 = const true; // scope 0 at $DIR/matches_reduce_branches.rs:40:30: 40:34 + goto -> bb3; // scope 0 at $DIR/matches_reduce_branches.rs:40:21: 40:48 + } + + bb2: { + _5 = const false; // scope 0 at $DIR/matches_reduce_branches.rs:40:42: 40:47 + goto -> bb3; // scope 0 at $DIR/matches_reduce_branches.rs:40:21: 40:48 + } + + bb3: { + StorageDead(_6); // scope 0 at $DIR/matches_reduce_branches.rs:40:47: 40:48 +- switchInt(_5) -> [false: bb4, otherwise: bb5]; // scope 0 at $DIR/matches_reduce_branches.rs:40:18: 40:68 ++ StorageLive(_8); // scope 0 at $DIR/matches_reduce_branches.rs:40:18: 40:68 ++ _8 = _5; // scope 0 at $DIR/matches_reduce_branches.rs:40:18: 40:68 ++ _4 = Ne(_8, const false); // scope 0 at $DIR/matches_reduce_branches.rs:40:62: 40:67 ++ StorageDead(_8); // scope 0 at $DIR/matches_reduce_branches.rs:40:18: 40:68 ++ goto -> bb6; // scope 0 at $DIR/matches_reduce_branches.rs:40:18: 40:68 + } + + bb4: { + _4 = const false; // scope 0 at $DIR/matches_reduce_branches.rs:40:62: 40:67 + goto -> bb6; // scope 0 at $DIR/matches_reduce_branches.rs:40:18: 40:68 + } + + bb5: { + _4 = const true; // scope 0 at $DIR/matches_reduce_branches.rs:40:50: 40:54 + goto -> bb6; // scope 0 at $DIR/matches_reduce_branches.rs:40:18: 40:68 + } + + bb6: { + StorageDead(_5); // scope 0 at $DIR/matches_reduce_branches.rs:40:67: 40:68 +- switchInt(_4) -> [false: bb7, otherwise: bb8]; // scope 0 at $DIR/matches_reduce_branches.rs:40:15: 40:88 ++ StorageLive(_9); // scope 0 at $DIR/matches_reduce_branches.rs:40:15: 40:88 ++ _9 = _4; // scope 0 at $DIR/matches_reduce_branches.rs:40:15: 40:88 ++ _3 = Ne(_9, const false); // scope 0 at $DIR/matches_reduce_branches.rs:40:82: 40:87 ++ StorageDead(_9); // scope 0 at $DIR/matches_reduce_branches.rs:40:15: 40:88 ++ goto -> bb9; // scope 0 at $DIR/matches_reduce_branches.rs:40:15: 40:88 + } + + bb7: { + _3 = const false; // scope 0 at $DIR/matches_reduce_branches.rs:40:82: 40:87 + goto -> bb9; // scope 0 at $DIR/matches_reduce_branches.rs:40:15: 40:88 + } + + bb8: { + _3 = const true; // scope 0 at $DIR/matches_reduce_branches.rs:40:70: 40:74 + goto -> bb9; // scope 0 at $DIR/matches_reduce_branches.rs:40:15: 40:88 + } + + bb9: { + StorageDead(_4); // scope 0 at $DIR/matches_reduce_branches.rs:40:87: 40:88 +- switchInt(move _3) -> [false: bb11, otherwise: bb10]; // scope 0 at $DIR/matches_reduce_branches.rs:40:15: 40:88 ++ StorageLive(_10); // scope 0 at $DIR/matches_reduce_branches.rs:40:15: 40:88 ++ _10 = move _3; // scope 0 at $DIR/matches_reduce_branches.rs:40:15: 40:88 ++ StorageDead(_3); // scope 0 at $DIR/matches_reduce_branches.rs:40:95: 40:96 ++ _1 = Ne(_10, const false); // scope 0 at $DIR/matches_reduce_branches.rs:41:14: 41:19 ++ StorageDead(_10); // scope 0 at $DIR/matches_reduce_branches.rs:40:15: 40:88 ++ goto -> bb12; // scope 0 at $DIR/matches_reduce_branches.rs:40:15: 40:88 + } + + bb10: { + StorageDead(_3); // scope 0 at $DIR/matches_reduce_branches.rs:40:95: 40:96 + _1 = const true; // scope 0 at $DIR/matches_reduce_branches.rs:40:92: 40:96 + goto -> bb12; // scope 0 at $DIR/matches_reduce_branches.rs:39:15: 42:6 + } + + bb11: { + StorageDead(_3); // scope 0 at $DIR/matches_reduce_branches.rs:40:95: 40:96 + _1 = const false; // scope 0 at $DIR/matches_reduce_branches.rs:41:14: 41:19 + goto -> bb12; // scope 0 at $DIR/matches_reduce_branches.rs:39:15: 42:6 + } + + bb12: { + StorageDead(_2); // scope 0 at $DIR/matches_reduce_branches.rs:42:6: 42:7 + _0 = _1; // scope 1 at $DIR/matches_reduce_branches.rs:43:5: 43:8 + StorageDead(_1); // scope 0 at $DIR/matches_reduce_branches.rs:44:1: 44:2 + return; // scope 0 at $DIR/matches_reduce_branches.rs:44:2: 44:2 + } + } + diff --git a/src/test/mir-opt/matches_reduce_branches.rs b/src/test/mir-opt/matches_reduce_branches.rs index 54b79a84263..e95a62aeeb0 100644 --- a/src/test/mir-opt/matches_reduce_branches.rs +++ b/src/test/mir-opt/matches_reduce_branches.rs @@ -1,7 +1,7 @@ -// compile-flags: -Zunsound-mir-opts // EMIT_MIR_FOR_EACH_BIT_WIDTH // EMIT_MIR matches_reduce_branches.foo.MatchBranchSimplification.diff // EMIT_MIR matches_reduce_branches.bar.MatchBranchSimplification.diff +// EMIT_MIR matches_reduce_branches.match_nested_if.MatchBranchSimplification.diff fn foo(bar: Option<()>) { if matches!(bar, None) { @@ -35,9 +35,17 @@ fn bar(i: i32) -> (bool, bool, bool, bool) { (a, b, c, d) } +fn match_nested_if() -> bool { + let val = match () { + () if if if if true {true} else {false} {true} else {false} {true} else {false} => true, + _ => false, + }; + val +} fn main() { let _ = foo(None); let _ = foo(Some(())); let _ = bar(0); + let _ = match_nested_if(); } diff --git a/src/test/mir-opt/not_equal_false.opt.InstCombine.diff b/src/test/mir-opt/not_equal_false.opt.InstCombine.diff index 39830946aeb..dc3a6a36d9e 100644 --- a/src/test/mir-opt/not_equal_false.opt.InstCombine.diff +++ b/src/test/mir-opt/not_equal_false.opt.InstCombine.diff @@ -8,61 +8,52 @@ let mut _3: isize; // in scope 0 at $DIR/not_equal_false.rs:4:17: 4:21 let mut _4: bool; // in scope 0 at $SRC_DIR/core/src/macros/mod.rs:LL:COL let mut _5: isize; // in scope 0 at $DIR/not_equal_false.rs:4:38: 4:45 + let mut _6: isize; // in scope 0 at $DIR/not_equal_false.rs:4:17: 4:21 + let mut _7: isize; // in scope 0 at $DIR/not_equal_false.rs:4:38: 4:45 + let mut _8: bool; // in scope 0 at $DIR/not_equal_false.rs:4:5: 4:46 bb0: { StorageLive(_2); // scope 0 at $SRC_DIR/core/src/macros/mod.rs:LL:COL _3 = discriminant(_1); // scope 0 at $DIR/not_equal_false.rs:4:17: 4:21 - switchInt(move _3) -> [0_isize: bb6, otherwise: bb5]; // scope 0 at $DIR/not_equal_false.rs:4:17: 4:21 + StorageLive(_6); // scope 0 at $DIR/not_equal_false.rs:4:17: 4:21 + _6 = move _3; // scope 0 at $DIR/not_equal_false.rs:4:17: 4:21 + _2 = Eq(_6, const 0_isize); // scope 0 at $SRC_DIR/core/src/macros/mod.rs:LL:COL + StorageDead(_6); // scope 0 at $DIR/not_equal_false.rs:4:17: 4:21 + goto -> bb4; // scope 0 at $DIR/not_equal_false.rs:4:17: 4:21 } bb1: { _0 = const true; // scope 0 at $DIR/not_equal_false.rs:4:5: 4:46 - goto -> bb4; // scope 0 at $DIR/not_equal_false.rs:4:5: 4:46 + goto -> bb3; // scope 0 at $DIR/not_equal_false.rs:4:5: 4:46 } bb2: { - _0 = const false; // scope 0 at $DIR/not_equal_false.rs:4:5: 4:46 - goto -> bb4; // scope 0 at $DIR/not_equal_false.rs:4:5: 4:46 - } - - bb3: { StorageLive(_4); // scope 0 at $SRC_DIR/core/src/macros/mod.rs:LL:COL _5 = discriminant(_1); // scope 0 at $DIR/not_equal_false.rs:4:38: 4:45 - switchInt(move _5) -> [1_isize: bb9, otherwise: bb8]; // scope 0 at $DIR/not_equal_false.rs:4:38: 4:45 + StorageLive(_7); // scope 0 at $DIR/not_equal_false.rs:4:38: 4:45 + _7 = move _5; // scope 0 at $DIR/not_equal_false.rs:4:38: 4:45 + _4 = Eq(_7, const 1_isize); // scope 0 at $SRC_DIR/core/src/macros/mod.rs:LL:COL + StorageDead(_7); // scope 0 at $DIR/not_equal_false.rs:4:38: 4:45 + goto -> bb5; // scope 0 at $DIR/not_equal_false.rs:4:38: 4:45 } - bb4: { + bb3: { StorageDead(_4); // scope 0 at $DIR/not_equal_false.rs:4:45: 4:46 StorageDead(_2); // scope 0 at $DIR/not_equal_false.rs:4:45: 4:46 return; // scope 0 at $DIR/not_equal_false.rs:5:2: 5:2 } - bb5: { - _2 = const false; // scope 0 at $SRC_DIR/core/src/macros/mod.rs:LL:COL - goto -> bb7; // scope 0 at $SRC_DIR/core/src/macros/mod.rs:LL:COL - } - - bb6: { - _2 = const true; // scope 0 at $SRC_DIR/core/src/macros/mod.rs:LL:COL - goto -> bb7; // scope 0 at $SRC_DIR/core/src/macros/mod.rs:LL:COL - } - - bb7: { - switchInt(move _2) -> [false: bb3, otherwise: bb1]; // scope 0 at $DIR/not_equal_false.rs:4:5: 4:46 - } - - bb8: { - _4 = const false; // scope 0 at $SRC_DIR/core/src/macros/mod.rs:LL:COL - goto -> bb10; // scope 0 at $SRC_DIR/core/src/macros/mod.rs:LL:COL - } - - bb9: { - _4 = const true; // scope 0 at $SRC_DIR/core/src/macros/mod.rs:LL:COL - goto -> bb10; // scope 0 at $SRC_DIR/core/src/macros/mod.rs:LL:COL + bb4: { + switchInt(move _2) -> [false: bb2, otherwise: bb1]; // scope 0 at $DIR/not_equal_false.rs:4:5: 4:46 } - bb10: { - switchInt(move _4) -> [false: bb2, otherwise: bb1]; // scope 0 at $DIR/not_equal_false.rs:4:5: 4:46 + bb5: { + StorageLive(_8); // scope 0 at $DIR/not_equal_false.rs:4:5: 4:46 + _8 = move _4; // scope 0 at $DIR/not_equal_false.rs:4:5: 4:46 +- _0 = Ne(_8, const false); // scope 0 at $DIR/not_equal_false.rs:4:5: 4:46 ++ _0 = _8; // scope 0 at $DIR/not_equal_false.rs:4:5: 4:46 + StorageDead(_8); // scope 0 at $DIR/not_equal_false.rs:4:5: 4:46 + goto -> bb3; // scope 0 at $DIR/not_equal_false.rs:4:5: 4:46 } } -- cgit 1.4.1-3-g733a5 From 35194117006d987d59dabb44ee3e4f38281c9e1b Mon Sep 17 00:00:00 2001 From: Nadrieril Date: Wed, 21 Oct 2020 19:24:59 +0100 Subject: Add a test for #53708 This issue was accidentally fixed recently, probably by #70743 --- src/test/ui/consts/const_in_pattern/issue-53708.rs | 11 +++++++++++ 1 file changed, 11 insertions(+) create mode 100644 src/test/ui/consts/const_in_pattern/issue-53708.rs (limited to 'src/test') diff --git a/src/test/ui/consts/const_in_pattern/issue-53708.rs b/src/test/ui/consts/const_in_pattern/issue-53708.rs new file mode 100644 index 00000000000..355ba63790f --- /dev/null +++ b/src/test/ui/consts/const_in_pattern/issue-53708.rs @@ -0,0 +1,11 @@ +// check-pass +// https://github.com/rust-lang/rust/issues/53708 +#[derive(PartialEq, Eq)] +struct S; + +fn main() { + const C: &S = &S; + match C { + C => {} + } +} -- cgit 1.4.1-3-g733a5 From 713012780fd6d7d236f040e6cdc78aaad9d926e4 Mon Sep 17 00:00:00 2001 From: Mara Bos Date: Sun, 18 Oct 2020 15:02:42 +0200 Subject: Add test for const panic!(CONST). --- src/test/ui/consts/const-eval/const_panic.rs | 8 ++++ src/test/ui/consts/const-eval/const_panic.stderr | 54 ++++++++++++++++-------- 2 files changed, 45 insertions(+), 17 deletions(-) (limited to 'src/test') diff --git a/src/test/ui/consts/const-eval/const_panic.rs b/src/test/ui/consts/const-eval/const_panic.rs index ada9feec897..799c185fb8e 100644 --- a/src/test/ui/consts/const-eval/const_panic.rs +++ b/src/test/ui/consts/const-eval/const_panic.rs @@ -1,6 +1,8 @@ #![feature(const_panic)] #![crate_type = "lib"] +const MSG: &str = "hello"; + const Z: () = std::panic!("cheese"); //~^ ERROR any use of this value will cause an error @@ -12,6 +14,9 @@ const Y: () = std::unreachable!(); const X: () = std::unimplemented!(); //~^ ERROR any use of this value will cause an error +// +const W: () = std::panic!(MSG); +//~^ ERROR any use of this value will cause an error const Z_CORE: () = core::panic!("cheese"); //~^ ERROR any use of this value will cause an error @@ -24,3 +29,6 @@ const Y_CORE: () = core::unreachable!(); const X_CORE: () = core::unimplemented!(); //~^ ERROR any use of this value will cause an error + +const W_CORE: () = core::panic!(MSG); +//~^ ERROR any use of this value will cause an error diff --git a/src/test/ui/consts/const-eval/const_panic.stderr b/src/test/ui/consts/const-eval/const_panic.stderr index e4ca1f4fa26..c2711952d58 100644 --- a/src/test/ui/consts/const-eval/const_panic.stderr +++ b/src/test/ui/consts/const-eval/const_panic.stderr @@ -1,83 +1,103 @@ error: any use of this value will cause an error - --> $DIR/const_panic.rs:4:15 + --> $DIR/const_panic.rs:6:15 | LL | const Z: () = std::panic!("cheese"); | --------------^^^^^^^^^^^^^^^^^^^^^- | | - | the evaluated program panicked at 'cheese', $DIR/const_panic.rs:4:15 + | the evaluated program panicked at 'cheese', $DIR/const_panic.rs:6:15 | = note: `#[deny(const_err)]` on by default = note: this error originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info) error: any use of this value will cause an error - --> $DIR/const_panic.rs:7:16 + --> $DIR/const_panic.rs:9:16 | LL | const Z2: () = std::panic!(); | ---------------^^^^^^^^^^^^^- | | - | the evaluated program panicked at 'explicit panic', $DIR/const_panic.rs:7:16 + | the evaluated program panicked at 'explicit panic', $DIR/const_panic.rs:9:16 | = note: this error originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info) error: any use of this value will cause an error - --> $DIR/const_panic.rs:10:15 + --> $DIR/const_panic.rs:12:15 | LL | const Y: () = std::unreachable!(); | --------------^^^^^^^^^^^^^^^^^^^- | | - | the evaluated program panicked at 'internal error: entered unreachable code', $DIR/const_panic.rs:10:15 + | the evaluated program panicked at 'internal error: entered unreachable code', $DIR/const_panic.rs:12:15 | = note: this error originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info) error: any use of this value will cause an error - --> $DIR/const_panic.rs:13:15 + --> $DIR/const_panic.rs:15:15 | LL | const X: () = std::unimplemented!(); | --------------^^^^^^^^^^^^^^^^^^^^^- | | - | the evaluated program panicked at 'not implemented', $DIR/const_panic.rs:13:15 + | the evaluated program panicked at 'not implemented', $DIR/const_panic.rs:15:15 | = note: this error originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info) error: any use of this value will cause an error - --> $DIR/const_panic.rs:16:20 + --> $DIR/const_panic.rs:18:15 + | +LL | const W: () = std::panic!(MSG); + | --------------^^^^^^^^^^^^^^^^- + | | + | the evaluated program panicked at 'hello', $DIR/const_panic.rs:18:15 + | + = note: this error originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info) + +error: any use of this value will cause an error + --> $DIR/const_panic.rs:21:20 | LL | const Z_CORE: () = core::panic!("cheese"); | -------------------^^^^^^^^^^^^^^^^^^^^^^- | | - | the evaluated program panicked at 'cheese', $DIR/const_panic.rs:16:20 + | the evaluated program panicked at 'cheese', $DIR/const_panic.rs:21:20 | = note: this error originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info) error: any use of this value will cause an error - --> $DIR/const_panic.rs:19:21 + --> $DIR/const_panic.rs:24:21 | LL | const Z2_CORE: () = core::panic!(); | --------------------^^^^^^^^^^^^^^- | | - | the evaluated program panicked at 'explicit panic', $DIR/const_panic.rs:19:21 + | the evaluated program panicked at 'explicit panic', $DIR/const_panic.rs:24:21 | = note: this error originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info) error: any use of this value will cause an error - --> $DIR/const_panic.rs:22:20 + --> $DIR/const_panic.rs:27:20 | LL | const Y_CORE: () = core::unreachable!(); | -------------------^^^^^^^^^^^^^^^^^^^^- | | - | the evaluated program panicked at 'internal error: entered unreachable code', $DIR/const_panic.rs:22:20 + | the evaluated program panicked at 'internal error: entered unreachable code', $DIR/const_panic.rs:27:20 | = note: this error originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info) error: any use of this value will cause an error - --> $DIR/const_panic.rs:25:20 + --> $DIR/const_panic.rs:30:20 | LL | const X_CORE: () = core::unimplemented!(); | -------------------^^^^^^^^^^^^^^^^^^^^^^- | | - | the evaluated program panicked at 'not implemented', $DIR/const_panic.rs:25:20 + | the evaluated program panicked at 'not implemented', $DIR/const_panic.rs:30:20 + | + = note: this error originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info) + +error: any use of this value will cause an error + --> $DIR/const_panic.rs:33:20 + | +LL | const W_CORE: () = core::panic!(MSG); + | -------------------^^^^^^^^^^^^^^^^^- + | | + | the evaluated program panicked at 'hello', $DIR/const_panic.rs:33:20 | = note: this error originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info) -error: aborting due to 8 previous errors +error: aborting due to 10 previous errors -- cgit 1.4.1-3-g733a5 From 6ad140ca197a1147e476f34908c2b69a60cc6d2f Mon Sep 17 00:00:00 2001 From: Bastian Kauschke Date: Fri, 23 Oct 2020 12:13:44 +0200 Subject: const_eval_checked: deal with unused nodes + div --- .../src/traits/const_evaluatable.rs | 63 +++++++++++++++++----- .../const_evaluatable_checked/division.rs | 11 ++++ .../const_evaluatable_checked/unused_expr.rs | 25 +++++++++ .../const_evaluatable_checked/unused_expr.stderr | 26 +++++++++ 4 files changed, 113 insertions(+), 12 deletions(-) create mode 100644 src/test/ui/const-generics/const_evaluatable_checked/division.rs create mode 100644 src/test/ui/const-generics/const_evaluatable_checked/unused_expr.rs create mode 100644 src/test/ui/const-generics/const_evaluatable_checked/unused_expr.stderr (limited to 'src/test') diff --git a/compiler/rustc_trait_selection/src/traits/const_evaluatable.rs b/compiler/rustc_trait_selection/src/traits/const_evaluatable.rs index 1e1eb16faf4..cb0950e3627 100644 --- a/compiler/rustc_trait_selection/src/traits/const_evaluatable.rs +++ b/compiler/rustc_trait_selection/src/traits/const_evaluatable.rs @@ -227,7 +227,12 @@ struct AbstractConstBuilder<'a, 'tcx> { tcx: TyCtxt<'tcx>, body: &'a mir::Body<'tcx>, /// The current WIP node tree. - nodes: IndexVec>, + /// + /// We require all nodes to be used in the final abstract const, + /// so we store this here. Note that we also consider nodes as used + /// if they are mentioned in an assert, so some used nodes are never + /// actually reachable by walking the [`AbstractConst`]. + nodes: IndexVec, bool)>, locals: IndexVec, /// We only allow field accesses if they access /// the result of a checked operation. @@ -274,6 +279,27 @@ impl<'a, 'tcx> AbstractConstBuilder<'a, 'tcx> { Ok(Some(builder)) } + fn add_node(&mut self, n: Node<'tcx>) -> NodeId { + // Mark used nodes. + match n { + Node::Leaf(_) => (), + Node::Binop(_, lhs, rhs) => { + self.nodes[lhs].1 = true; + self.nodes[rhs].1 = true; + } + Node::UnaryOp(_, input) => { + self.nodes[input].1 = true; + } + Node::FunctionCall(func, nodes) => { + self.nodes[func].1 = true; + nodes.iter().for_each(|&n| self.nodes[n].1 = true); + } + } + + // Nodes start as unused. + self.nodes.push((n, false)) + } + fn place_to_local( &mut self, span: Span, @@ -311,7 +337,7 @@ impl<'a, 'tcx> AbstractConstBuilder<'a, 'tcx> { let local = self.place_to_local(span, p)?; Ok(self.locals[local]) } - mir::Operand::Constant(ct) => Ok(self.nodes.push(Node::Leaf(ct.literal))), + mir::Operand::Constant(ct) => Ok(self.add_node(Node::Leaf(ct.literal))), } } @@ -348,7 +374,7 @@ impl<'a, 'tcx> AbstractConstBuilder<'a, 'tcx> { Rvalue::BinaryOp(op, ref lhs, ref rhs) if Self::check_binop(op) => { let lhs = self.operand_to_node(stmt.source_info.span, lhs)?; let rhs = self.operand_to_node(stmt.source_info.span, rhs)?; - self.locals[local] = self.nodes.push(Node::Binop(op, lhs, rhs)); + self.locals[local] = self.add_node(Node::Binop(op, lhs, rhs)); if op.is_checkable() { bug!("unexpected unchecked checkable binary operation"); } else { @@ -358,13 +384,13 @@ impl<'a, 'tcx> AbstractConstBuilder<'a, 'tcx> { Rvalue::CheckedBinaryOp(op, ref lhs, ref rhs) if Self::check_binop(op) => { let lhs = self.operand_to_node(stmt.source_info.span, lhs)?; let rhs = self.operand_to_node(stmt.source_info.span, rhs)?; - self.locals[local] = self.nodes.push(Node::Binop(op, lhs, rhs)); + self.locals[local] = self.add_node(Node::Binop(op, lhs, rhs)); self.checked_op_locals.insert(local); Ok(()) } Rvalue::UnaryOp(op, ref operand) if Self::check_unop(op) => { let operand = self.operand_to_node(stmt.source_info.span, operand)?; - self.locals[local] = self.nodes.push(Node::UnaryOp(op, operand)); + self.locals[local] = self.add_node(Node::UnaryOp(op, operand)); Ok(()) } _ => self.error(Some(stmt.source_info.span), "unsupported rvalue")?, @@ -415,13 +441,9 @@ impl<'a, 'tcx> AbstractConstBuilder<'a, 'tcx> { .map(|arg| self.operand_to_node(terminator.source_info.span, arg)) .collect::, _>>()?, ); - self.locals[local] = self.nodes.push(Node::FunctionCall(func, args)); + self.locals[local] = self.add_node(Node::FunctionCall(func, args)); Ok(Some(target)) } - // We only allow asserts for checked operations. - // - // These asserts seem to all have the form `!_local.0` so - // we only allow exactly that. TerminatorKind::Assert { ref cond, expected: false, target, .. } => { let p = match cond { mir::Operand::Copy(p) | mir::Operand::Move(p) => p, @@ -430,7 +452,15 @@ impl<'a, 'tcx> AbstractConstBuilder<'a, 'tcx> { const ONE_FIELD: mir::Field = mir::Field::from_usize(1); debug!("proj: {:?}", p.projection); - if let &[mir::ProjectionElem::Field(ONE_FIELD, _)] = p.projection.as_ref() { + if let Some(p) = p.as_local() { + debug_assert!(!self.checked_op_locals.contains(p)); + // Mark locals directly used in asserts as used. + // + // This is needed because division does not use `CheckedBinop` but instead + // adds an explicit assert for `divisor != 0`. + self.nodes[self.locals[p]].1 = true; + return Ok(Some(target)); + } else if let &[mir::ProjectionElem::Field(ONE_FIELD, _)] = p.projection.as_ref() { // Only allow asserts checking the result of a checked operation. if self.checked_op_locals.contains(p.local) { return Ok(Some(target)); @@ -457,7 +487,16 @@ impl<'a, 'tcx> AbstractConstBuilder<'a, 'tcx> { if let Some(next) = self.build_terminator(block.terminator())? { block = &self.body.basic_blocks()[next]; } else { - return Ok(self.tcx.arena.alloc_from_iter(self.nodes)); + assert_eq!(self.locals[mir::Local::from_usize(0)], self.nodes.last().unwrap()); + self.nodes[self.locals[mir::Local::from_usize(0)]].1 = true; + if !self.nodes.iter().all(|n| n.1) { + self.error(None, "unused node")?; + } + + return Ok(self + .tcx + .arena + .alloc_from_iter(self.nodes.into_iter().map(|(n, _used)| n))); } } } diff --git a/src/test/ui/const-generics/const_evaluatable_checked/division.rs b/src/test/ui/const-generics/const_evaluatable_checked/division.rs new file mode 100644 index 00000000000..71a5f2d3472 --- /dev/null +++ b/src/test/ui/const-generics/const_evaluatable_checked/division.rs @@ -0,0 +1,11 @@ +// run-pass +#![feature(const_generics, const_evaluatable_checked)] +#![allow(incomplete_features)] + +fn with_bound() where [u8; N / 2]: Sized { + let _: [u8; N / 2] = [0; N / 2]; +} + +fn main() { + with_bound::<4>(); +} diff --git a/src/test/ui/const-generics/const_evaluatable_checked/unused_expr.rs b/src/test/ui/const-generics/const_evaluatable_checked/unused_expr.rs new file mode 100644 index 00000000000..9c603c57a48 --- /dev/null +++ b/src/test/ui/const-generics/const_evaluatable_checked/unused_expr.rs @@ -0,0 +1,25 @@ +#![feature(const_generics, const_evaluatable_checked)] +#![allow(incomplete_features)] + +fn add() -> [u8; { N + 1; 5 }] { + //~^ ERROR overly complex generic constant + todo!() +} + +fn div() -> [u8; { N / 1; 5 }] { + //~^ ERROR overly complex generic constant + todo!() +} + +const fn foo(n: usize) {} + +fn fn_call() -> [u8; { foo(N); 5 }] { + //~^ ERROR overly complex generic constant + todo!() +} + +fn main() { + add::<12>(); + div::<9>(); + fn_call::<14>(); +} diff --git a/src/test/ui/const-generics/const_evaluatable_checked/unused_expr.stderr b/src/test/ui/const-generics/const_evaluatable_checked/unused_expr.stderr new file mode 100644 index 00000000000..73c6d9393ce --- /dev/null +++ b/src/test/ui/const-generics/const_evaluatable_checked/unused_expr.stderr @@ -0,0 +1,26 @@ +error: overly complex generic constant + --> $DIR/unused_expr.rs:4:34 + | +LL | fn add() -> [u8; { N + 1; 5 }] { + | ^^^^^^^^^^^^ unused node + | + = help: consider moving this anonymous constant into a `const` function + +error: overly complex generic constant + --> $DIR/unused_expr.rs:9:34 + | +LL | fn div() -> [u8; { N / 1; 5 }] { + | ^^^^^^^^^^^^ unused node + | + = help: consider moving this anonymous constant into a `const` function + +error: overly complex generic constant + --> $DIR/unused_expr.rs:16:38 + | +LL | fn fn_call() -> [u8; { foo(N); 5 }] { + | ^^^^^^^^^^^^^ unused node + | + = help: consider moving this anonymous constant into a `const` function + +error: aborting due to 3 previous errors + -- cgit 1.4.1-3-g733a5 From 47cb871f14b48653df2f42082cf93b6c16e2b2f1 Mon Sep 17 00:00:00 2001 From: Bastian Kauschke Date: Fri, 23 Oct 2020 15:04:12 +0200 Subject: review --- .../src/traits/const_evaluatable.rs | 68 ++++++++++++---------- .../const_evaluatable_checked/unused_expr.stderr | 12 +++- 2 files changed, 45 insertions(+), 35 deletions(-) (limited to 'src/test') diff --git a/compiler/rustc_trait_selection/src/traits/const_evaluatable.rs b/compiler/rustc_trait_selection/src/traits/const_evaluatable.rs index cb0950e3627..c79b2624f8c 100644 --- a/compiler/rustc_trait_selection/src/traits/const_evaluatable.rs +++ b/compiler/rustc_trait_selection/src/traits/const_evaluatable.rs @@ -223,6 +223,13 @@ impl AbstractConst<'tcx> { } } +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +struct WorkNode<'tcx> { + node: Node<'tcx>, + span: Span, + used: bool, +} + struct AbstractConstBuilder<'a, 'tcx> { tcx: TyCtxt<'tcx>, body: &'a mir::Body<'tcx>, @@ -232,7 +239,7 @@ struct AbstractConstBuilder<'a, 'tcx> { /// so we store this here. Note that we also consider nodes as used /// if they are mentioned in an assert, so some used nodes are never /// actually reachable by walking the [`AbstractConst`]. - nodes: IndexVec, bool)>, + nodes: IndexVec>, locals: IndexVec, /// We only allow field accesses if they access /// the result of a checked operation. @@ -279,25 +286,25 @@ impl<'a, 'tcx> AbstractConstBuilder<'a, 'tcx> { Ok(Some(builder)) } - fn add_node(&mut self, n: Node<'tcx>) -> NodeId { + fn add_node(&mut self, node: Node<'tcx>, span: Span) -> NodeId { // Mark used nodes. - match n { + match node { Node::Leaf(_) => (), Node::Binop(_, lhs, rhs) => { - self.nodes[lhs].1 = true; - self.nodes[rhs].1 = true; + self.nodes[lhs].used = true; + self.nodes[rhs].used = true; } Node::UnaryOp(_, input) => { - self.nodes[input].1 = true; + self.nodes[input].used = true; } Node::FunctionCall(func, nodes) => { - self.nodes[func].1 = true; - nodes.iter().for_each(|&n| self.nodes[n].1 = true); + self.nodes[func].used = true; + nodes.iter().for_each(|&n| self.nodes[n].used = true); } } // Nodes start as unused. - self.nodes.push((n, false)) + self.nodes.push(WorkNode { node, span, used: false }) } fn place_to_local( @@ -337,7 +344,7 @@ impl<'a, 'tcx> AbstractConstBuilder<'a, 'tcx> { let local = self.place_to_local(span, p)?; Ok(self.locals[local]) } - mir::Operand::Constant(ct) => Ok(self.add_node(Node::Leaf(ct.literal))), + mir::Operand::Constant(ct) => Ok(self.add_node(Node::Leaf(ct.literal), span)), } } @@ -362,19 +369,19 @@ impl<'a, 'tcx> AbstractConstBuilder<'a, 'tcx> { fn build_statement(&mut self, stmt: &mir::Statement<'tcx>) -> Result<(), ErrorReported> { debug!("AbstractConstBuilder: stmt={:?}", stmt); + let span = stmt.source_info.span; match stmt.kind { StatementKind::Assign(box (ref place, ref rvalue)) => { - let local = self.place_to_local(stmt.source_info.span, place)?; + let local = self.place_to_local(span, place)?; match *rvalue { Rvalue::Use(ref operand) => { - self.locals[local] = - self.operand_to_node(stmt.source_info.span, operand)?; + self.locals[local] = self.operand_to_node(span, operand)?; Ok(()) } Rvalue::BinaryOp(op, ref lhs, ref rhs) if Self::check_binop(op) => { - let lhs = self.operand_to_node(stmt.source_info.span, lhs)?; - let rhs = self.operand_to_node(stmt.source_info.span, rhs)?; - self.locals[local] = self.add_node(Node::Binop(op, lhs, rhs)); + let lhs = self.operand_to_node(span, lhs)?; + let rhs = self.operand_to_node(span, rhs)?; + self.locals[local] = self.add_node(Node::Binop(op, lhs, rhs), span); if op.is_checkable() { bug!("unexpected unchecked checkable binary operation"); } else { @@ -382,18 +389,18 @@ impl<'a, 'tcx> AbstractConstBuilder<'a, 'tcx> { } } Rvalue::CheckedBinaryOp(op, ref lhs, ref rhs) if Self::check_binop(op) => { - let lhs = self.operand_to_node(stmt.source_info.span, lhs)?; - let rhs = self.operand_to_node(stmt.source_info.span, rhs)?; - self.locals[local] = self.add_node(Node::Binop(op, lhs, rhs)); + let lhs = self.operand_to_node(span, lhs)?; + let rhs = self.operand_to_node(span, rhs)?; + self.locals[local] = self.add_node(Node::Binop(op, lhs, rhs), span); self.checked_op_locals.insert(local); Ok(()) } Rvalue::UnaryOp(op, ref operand) if Self::check_unop(op) => { - let operand = self.operand_to_node(stmt.source_info.span, operand)?; - self.locals[local] = self.add_node(Node::UnaryOp(op, operand)); + let operand = self.operand_to_node(span, operand)?; + self.locals[local] = self.add_node(Node::UnaryOp(op, operand), span); Ok(()) } - _ => self.error(Some(stmt.source_info.span), "unsupported rvalue")?, + _ => self.error(Some(span), "unsupported rvalue")?, } } // These are not actually relevant for us here, so we can ignore them. @@ -441,7 +448,7 @@ impl<'a, 'tcx> AbstractConstBuilder<'a, 'tcx> { .map(|arg| self.operand_to_node(terminator.source_info.span, arg)) .collect::, _>>()?, ); - self.locals[local] = self.add_node(Node::FunctionCall(func, args)); + self.locals[local] = self.add_node(Node::FunctionCall(func, args), fn_span); Ok(Some(target)) } TerminatorKind::Assert { ref cond, expected: false, target, .. } => { @@ -458,7 +465,7 @@ impl<'a, 'tcx> AbstractConstBuilder<'a, 'tcx> { // // This is needed because division does not use `CheckedBinop` but instead // adds an explicit assert for `divisor != 0`. - self.nodes[self.locals[p]].1 = true; + self.nodes[self.locals[p]].used = true; return Ok(Some(target)); } else if let &[mir::ProjectionElem::Field(ONE_FIELD, _)] = p.projection.as_ref() { // Only allow asserts checking the result of a checked operation. @@ -487,16 +494,13 @@ impl<'a, 'tcx> AbstractConstBuilder<'a, 'tcx> { if let Some(next) = self.build_terminator(block.terminator())? { block = &self.body.basic_blocks()[next]; } else { - assert_eq!(self.locals[mir::Local::from_usize(0)], self.nodes.last().unwrap()); - self.nodes[self.locals[mir::Local::from_usize(0)]].1 = true; - if !self.nodes.iter().all(|n| n.1) { - self.error(None, "unused node")?; + assert_eq!(self.locals[mir::RETURN_PLACE], self.nodes.last().unwrap()); + self.nodes[self.locals[mir::RETURN_PLACE]].used = true; + if let Some(&unused) = self.nodes.iter().find(|n| !n.used) { + self.error(Some(unused.span), "dead code")?; } - return Ok(self - .tcx - .arena - .alloc_from_iter(self.nodes.into_iter().map(|(n, _used)| n))); + return Ok(self.tcx.arena.alloc_from_iter(self.nodes.into_iter().map(|n| n.node))); } } } diff --git a/src/test/ui/const-generics/const_evaluatable_checked/unused_expr.stderr b/src/test/ui/const-generics/const_evaluatable_checked/unused_expr.stderr index 73c6d9393ce..1687dbbcbe3 100644 --- a/src/test/ui/const-generics/const_evaluatable_checked/unused_expr.stderr +++ b/src/test/ui/const-generics/const_evaluatable_checked/unused_expr.stderr @@ -2,7 +2,9 @@ error: overly complex generic constant --> $DIR/unused_expr.rs:4:34 | LL | fn add() -> [u8; { N + 1; 5 }] { - | ^^^^^^^^^^^^ unused node + | ^^-----^^^^^ + | | + | dead code | = help: consider moving this anonymous constant into a `const` function @@ -10,7 +12,9 @@ error: overly complex generic constant --> $DIR/unused_expr.rs:9:34 | LL | fn div() -> [u8; { N / 1; 5 }] { - | ^^^^^^^^^^^^ unused node + | ^^-----^^^^^ + | | + | dead code | = help: consider moving this anonymous constant into a `const` function @@ -18,7 +22,9 @@ error: overly complex generic constant --> $DIR/unused_expr.rs:16:38 | LL | fn fn_call() -> [u8; { foo(N); 5 }] { - | ^^^^^^^^^^^^^ unused node + | ^^------^^^^^ + | | + | dead code | = help: consider moving this anonymous constant into a `const` function -- cgit 1.4.1-3-g733a5 From b334eef162ba9c10faa97c9d5c5d249f25afc7df Mon Sep 17 00:00:00 2001 From: Esteban Küber Date: Tue, 13 Oct 2020 21:42:59 -0700 Subject: Do not ICE with TraitPredicates containing [type error] Fix #77919. --- .../rustc_trait_selection/src/traits/select/mod.rs | 19 ++++++--- src/test/ui/issues/issue-77919.rs | 13 ++++++ src/test/ui/issues/issue-77919.stderr | 46 ++++++++++++++++++++++ 3 files changed, 72 insertions(+), 6 deletions(-) create mode 100644 src/test/ui/issues/issue-77919.rs create mode 100644 src/test/ui/issues/issue-77919.stderr (limited to 'src/test') diff --git a/compiler/rustc_trait_selection/src/traits/select/mod.rs b/compiler/rustc_trait_selection/src/traits/select/mod.rs index 4cc4bc0acda..d4124c82197 100644 --- a/compiler/rustc_trait_selection/src/traits/select/mod.rs +++ b/compiler/rustc_trait_selection/src/traits/select/mod.rs @@ -2031,12 +2031,19 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> { &predicate.subst(tcx, substs), &mut obligations, ); - obligations.push(Obligation { - cause: cause.clone(), - recursion_depth, - param_env, - predicate, - }); + if predicate.references_error() { + self.tcx().sess.delay_span_bug( + cause.span, + &format!("impl_or_trait_obligation with errors: {:?}", predicate), + ); + } else { + obligations.push(Obligation { + cause: cause.clone(), + recursion_depth, + param_env, + predicate, + }); + } } // We are performing deduplication here to avoid exponential blowups diff --git a/src/test/ui/issues/issue-77919.rs b/src/test/ui/issues/issue-77919.rs new file mode 100644 index 00000000000..9b04d5ee000 --- /dev/null +++ b/src/test/ui/issues/issue-77919.rs @@ -0,0 +1,13 @@ +fn main() { + [1; >::VAL]; //~ ERROR evaluation of constant value failed +} +trait TypeVal { + const VAL: T; //~ ERROR any use of this value will cause an error +} +struct Five; +struct Multiply { + _n: PhantomData, //~ ERROR cannot find type `PhantomData` in this scope +} +impl TypeVal for Multiply where N: TypeVal {} +//~^ ERROR cannot find type `VAL` in this scope +//~| ERROR not all trait items implemented, missing: `VAL` diff --git a/src/test/ui/issues/issue-77919.stderr b/src/test/ui/issues/issue-77919.stderr new file mode 100644 index 00000000000..129af00644f --- /dev/null +++ b/src/test/ui/issues/issue-77919.stderr @@ -0,0 +1,46 @@ +error[E0412]: cannot find type `PhantomData` in this scope + --> $DIR/issue-77919.rs:9:9 + | +LL | _n: PhantomData, + | ^^^^^^^^^^^ not found in this scope + | +help: consider importing this struct + | +LL | use std::marker::PhantomData; + | + +error[E0412]: cannot find type `VAL` in this scope + --> $DIR/issue-77919.rs:11:63 + | +LL | impl TypeVal for Multiply where N: TypeVal {} + | - ^^^ not found in this scope + | | + | help: you might be missing a type parameter: `, VAL` + +error[E0046]: not all trait items implemented, missing: `VAL` + --> $DIR/issue-77919.rs:11:1 + | +LL | const VAL: T; + | ------------- `VAL` from trait +... +LL | impl TypeVal for Multiply where N: TypeVal {} + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ missing `VAL` in implementation + +error: any use of this value will cause an error + --> $DIR/issue-77919.rs:5:5 + | +LL | const VAL: T; + | ^^^^^^^^^^^^^ no MIR body is available for DefId(0:7 ~ issue_77919[317d]::TypeVal::VAL) + | + = note: `#[deny(const_err)]` on by default + +error[E0080]: evaluation of constant value failed + --> $DIR/issue-77919.rs:2:9 + | +LL | [1; >::VAL]; + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^ referenced constant has errors + +error: aborting due to 5 previous errors + +Some errors have detailed explanations: E0046, E0080, E0412. +For more information about an error, try `rustc --explain E0046`. -- cgit 1.4.1-3-g733a5 From ef09ed200227207872b8e201d091681be3240c5b Mon Sep 17 00:00:00 2001 From: Vadim Petrochenkov Date: Sat, 24 Oct 2020 21:17:34 +0300 Subject: resolve: Relax macro resolution consistency check to account for any errors --- compiler/rustc_resolve/src/macros.rs | 8 ++++---- src/test/ui/macros/issue-78325-inconsistent-resolution.rs | 12 ++++++++++++ .../ui/macros/issue-78325-inconsistent-resolution.stderr | 13 +++++++++++++ 3 files changed, 29 insertions(+), 4 deletions(-) create mode 100644 src/test/ui/macros/issue-78325-inconsistent-resolution.rs create mode 100644 src/test/ui/macros/issue-78325-inconsistent-resolution.stderr (limited to 'src/test') diff --git a/compiler/rustc_resolve/src/macros.rs b/compiler/rustc_resolve/src/macros.rs index bea71389647..b5b281b93bc 100644 --- a/compiler/rustc_resolve/src/macros.rs +++ b/compiler/rustc_resolve/src/macros.rs @@ -19,7 +19,7 @@ use rustc_feature::is_builtin_attr_name; use rustc_hir::def::{self, DefKind, NonMacroAttrKind}; use rustc_hir::def_id; use rustc_middle::middle::stability; -use rustc_middle::{span_bug, ty}; +use rustc_middle::ty; use rustc_session::lint::builtin::UNUSED_MACROS; use rustc_session::Session; use rustc_span::edition::Edition; @@ -885,11 +885,11 @@ impl<'a> Resolver<'a> { initial_res: Option, res: Res| { if let Some(initial_res) = initial_res { - if res != initial_res && res != Res::Err && this.ambiguity_errors.is_empty() { + if res != initial_res { // Make sure compilation does not succeed if preferred macro resolution // has changed after the macro had been expanded. In theory all such - // situations should be reported as ambiguity errors, so this is a bug. - span_bug!(span, "inconsistent resolution for a macro"); + // situations should be reported as errors, so this is a bug. + this.session.delay_span_bug(span, "inconsistent resolution for a macro"); } } else { // It's possible that the macro was unresolved (indeterminate) and silently diff --git a/src/test/ui/macros/issue-78325-inconsistent-resolution.rs b/src/test/ui/macros/issue-78325-inconsistent-resolution.rs new file mode 100644 index 00000000000..919eca4f9bf --- /dev/null +++ b/src/test/ui/macros/issue-78325-inconsistent-resolution.rs @@ -0,0 +1,12 @@ +macro_rules! define_other_core { + ( ) => { + extern crate std as core; + //~^ ERROR macro-expanded `extern crate` items cannot shadow names passed with `--extern` + }; +} + +fn main() { + core::panic!(); +} + +define_other_core!(); diff --git a/src/test/ui/macros/issue-78325-inconsistent-resolution.stderr b/src/test/ui/macros/issue-78325-inconsistent-resolution.stderr new file mode 100644 index 00000000000..cf3af593141 --- /dev/null +++ b/src/test/ui/macros/issue-78325-inconsistent-resolution.stderr @@ -0,0 +1,13 @@ +error: macro-expanded `extern crate` items cannot shadow names passed with `--extern` + --> $DIR/issue-78325-inconsistent-resolution.rs:3:9 + | +LL | extern crate std as core; + | ^^^^^^^^^^^^^^^^^^^^^^^^^ +... +LL | define_other_core!(); + | --------------------- in this macro invocation + | + = note: this error originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info) + +error: aborting due to previous error + -- cgit 1.4.1-3-g733a5