From bc243a7f5597dbc6bd6947e9d799ed9fba428634 Mon Sep 17 00:00:00 2001 From: Yuki Okushi Date: Sat, 19 Jun 2021 11:46:14 +0900 Subject: [PATCH 1/2] Refactor `PatternError` structure --- .../rustc_mir_build/src/thir/pattern/mod.rs | 9 +++--- src/test/ui/issues/issue-27895.rs | 10 ------- src/test/ui/issues/issue-27895.stderr | 9 ------ src/test/ui/non-constant-in-const-path.rs | 7 ----- src/test/ui/non-constant-in-const-path.stderr | 9 ------ .../issue-68394-let-pat-runtime-value.rs | 5 ---- .../issue-68394-let-pat-runtime-value.stderr | 9 ------ .../ui/pattern/non-constant-in-const-path.rs | 18 ++++++++++++ .../pattern/non-constant-in-const-path.stderr | 28 +++++++++++++++++++ 9 files changed, 50 insertions(+), 54 deletions(-) delete mode 100644 src/test/ui/issues/issue-27895.rs delete mode 100644 src/test/ui/issues/issue-27895.stderr delete mode 100644 src/test/ui/non-constant-in-const-path.rs delete mode 100644 src/test/ui/non-constant-in-const-path.stderr delete mode 100644 src/test/ui/pattern/issue-68394-let-pat-runtime-value.rs delete mode 100644 src/test/ui/pattern/issue-68394-let-pat-runtime-value.stderr create mode 100644 src/test/ui/pattern/non-constant-in-const-path.rs create mode 100644 src/test/ui/pattern/non-constant-in-const-path.stderr diff --git a/compiler/rustc_mir_build/src/thir/pattern/mod.rs b/compiler/rustc_mir_build/src/thir/pattern/mod.rs index 3225d302cb3..a07786eb842 100644 --- a/compiler/rustc_mir_build/src/thir/pattern/mod.rs +++ b/compiler/rustc_mir_build/src/thir/pattern/mod.rs @@ -423,6 +423,7 @@ impl<'a, 'tcx> PatCtxt<'a, 'tcx> { _ => { let pattern_error = match res { Res::Def(DefKind::ConstParam, _) => PatternError::ConstParamInPattern(span), + Res::Def(DefKind::Static, _) => PatternError::StaticInPattern(span), _ => PatternError::NonConstPath(span), }; self.errors.push(pattern_error); @@ -468,11 +469,9 @@ impl<'a, 'tcx> PatCtxt<'a, 'tcx> { let instance = match ty::Instance::resolve(self.tcx, param_env_reveal_all, def_id, substs) { Ok(Some(i)) => i, Ok(None) => { - self.errors.push(if is_associated_const { - PatternError::AssocConstInPattern(span) - } else { - PatternError::StaticInPattern(span) - }); + if is_associated_const { + self.errors.push(PatternError::AssocConstInPattern(span)); + } return pat_from_kind(PatKind::Wild); } diff --git a/src/test/ui/issues/issue-27895.rs b/src/test/ui/issues/issue-27895.rs deleted file mode 100644 index 0018ac1bdf1..00000000000 --- a/src/test/ui/issues/issue-27895.rs +++ /dev/null @@ -1,10 +0,0 @@ -fn main() { - let i = 5; - let index = 6; - - match i { - 0..=index => println!("winner"), - //~^ ERROR runtime values cannot be referenced in patterns - _ => println!("hello"), - } -} diff --git a/src/test/ui/issues/issue-27895.stderr b/src/test/ui/issues/issue-27895.stderr deleted file mode 100644 index 55bd938b0bf..00000000000 --- a/src/test/ui/issues/issue-27895.stderr +++ /dev/null @@ -1,9 +0,0 @@ -error[E0080]: runtime values cannot be referenced in patterns - --> $DIR/issue-27895.rs:6:13 - | -LL | 0..=index => println!("winner"), - | ^^^^^ - -error: aborting due to previous error - -For more information about this error, try `rustc --explain E0080`. diff --git a/src/test/ui/non-constant-in-const-path.rs b/src/test/ui/non-constant-in-const-path.rs deleted file mode 100644 index 343bb98aeb6..00000000000 --- a/src/test/ui/non-constant-in-const-path.rs +++ /dev/null @@ -1,7 +0,0 @@ -fn main() { - let x = 0; - match 1 { - 0 ..= x => {} - //~^ ERROR runtime values cannot be referenced in patterns - }; -} diff --git a/src/test/ui/non-constant-in-const-path.stderr b/src/test/ui/non-constant-in-const-path.stderr deleted file mode 100644 index 5936f76b2e0..00000000000 --- a/src/test/ui/non-constant-in-const-path.stderr +++ /dev/null @@ -1,9 +0,0 @@ -error[E0080]: runtime values cannot be referenced in patterns - --> $DIR/non-constant-in-const-path.rs:4:15 - | -LL | 0 ..= x => {} - | ^ - -error: aborting due to previous error - -For more information about this error, try `rustc --explain E0080`. diff --git a/src/test/ui/pattern/issue-68394-let-pat-runtime-value.rs b/src/test/ui/pattern/issue-68394-let-pat-runtime-value.rs deleted file mode 100644 index f10a7f2d8a5..00000000000 --- a/src/test/ui/pattern/issue-68394-let-pat-runtime-value.rs +++ /dev/null @@ -1,5 +0,0 @@ -fn main() { - let x = 255u8; - let 0u8..=x = 0; - //~^ ERROR runtime values cannot be referenced in patterns -} diff --git a/src/test/ui/pattern/issue-68394-let-pat-runtime-value.stderr b/src/test/ui/pattern/issue-68394-let-pat-runtime-value.stderr deleted file mode 100644 index c1508bd71ff..00000000000 --- a/src/test/ui/pattern/issue-68394-let-pat-runtime-value.stderr +++ /dev/null @@ -1,9 +0,0 @@ -error[E0080]: runtime values cannot be referenced in patterns - --> $DIR/issue-68394-let-pat-runtime-value.rs:3:15 - | -LL | let 0u8..=x = 0; - | ^ - -error: aborting due to previous error - -For more information about this error, try `rustc --explain E0080`. diff --git a/src/test/ui/pattern/non-constant-in-const-path.rs b/src/test/ui/pattern/non-constant-in-const-path.rs new file mode 100644 index 00000000000..3918485bacb --- /dev/null +++ b/src/test/ui/pattern/non-constant-in-const-path.rs @@ -0,0 +1,18 @@ +// Checks if we emit `PatternError`s correctly. +// This is also a regression test for #27895 and #68394. + +static FOO: u8 = 10; + +fn main() { + let x = 0; + let 0u8..=x = 0; + //~^ ERROR: runtime values cannot be referenced in patterns + let 0u8..=FOO = 0; + //~^ ERROR: statics cannot be referenced in patterns + match 1 { + 0 ..= x => {} + //~^ ERROR: runtime values cannot be referenced in patterns + 0 ..= FOO => {} + //~^ ERROR: statics cannot be referenced in patterns + }; +} diff --git a/src/test/ui/pattern/non-constant-in-const-path.stderr b/src/test/ui/pattern/non-constant-in-const-path.stderr new file mode 100644 index 00000000000..53c3974f780 --- /dev/null +++ b/src/test/ui/pattern/non-constant-in-const-path.stderr @@ -0,0 +1,28 @@ +error[E0080]: runtime values cannot be referenced in patterns + --> $DIR/non-constant-in-const-path.rs:8:15 + | +LL | let 0u8..=x = 0; + | ^ + +error[E0158]: statics cannot be referenced in patterns + --> $DIR/non-constant-in-const-path.rs:10:15 + | +LL | let 0u8..=FOO = 0; + | ^^^ + +error[E0080]: runtime values cannot be referenced in patterns + --> $DIR/non-constant-in-const-path.rs:13:15 + | +LL | 0 ..= x => {} + | ^ + +error[E0158]: statics cannot be referenced in patterns + --> $DIR/non-constant-in-const-path.rs:15:15 + | +LL | 0 ..= FOO => {} + | ^^^ + +error: aborting due to 4 previous errors + +Some errors have detailed explanations: E0080, E0158. +For more information about an error, try `rustc --explain E0080`. From e44e65e8881f75e981ceb843d0e189ad55c6b4e3 Mon Sep 17 00:00:00 2001 From: Yuki Okushi Date: Sat, 19 Jun 2021 13:54:33 +0900 Subject: [PATCH 2/2] Assert `is_associated_const` when resolving --- compiler/rustc_mir_build/src/thir/pattern/mod.rs | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/compiler/rustc_mir_build/src/thir/pattern/mod.rs b/compiler/rustc_mir_build/src/thir/pattern/mod.rs index a07786eb842..3ea76fb99d5 100644 --- a/compiler/rustc_mir_build/src/thir/pattern/mod.rs +++ b/compiler/rustc_mir_build/src/thir/pattern/mod.rs @@ -469,9 +469,10 @@ impl<'a, 'tcx> PatCtxt<'a, 'tcx> { let instance = match ty::Instance::resolve(self.tcx, param_env_reveal_all, def_id, substs) { Ok(Some(i)) => i, Ok(None) => { - if is_associated_const { - self.errors.push(PatternError::AssocConstInPattern(span)); - } + // It should be assoc consts if there's no error but we cannot resolve it. + debug_assert!(is_associated_const); + + self.errors.push(PatternError::AssocConstInPattern(span)); return pat_from_kind(PatKind::Wild); }