Skip to content

Commit 150fad3

Browse files
committed
Auto merge of #86460 - JohnTitor:use-static-in-pattern-err, r=oli-obk
Refactor `PatternError` structure Now we emit the `StaticInPattern` error precisely. Fixes #68395 r? `@oli-obk`
2 parents 6b354a1 + e44e65e commit 150fad3

9 files changed

+51
-54
lines changed

compiler/rustc_mir_build/src/thir/pattern/mod.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -423,6 +423,7 @@ impl<'a, 'tcx> PatCtxt<'a, 'tcx> {
423423
_ => {
424424
let pattern_error = match res {
425425
Res::Def(DefKind::ConstParam, _) => PatternError::ConstParamInPattern(span),
426+
Res::Def(DefKind::Static, _) => PatternError::StaticInPattern(span),
426427
_ => PatternError::NonConstPath(span),
427428
};
428429
self.errors.push(pattern_error);
@@ -468,11 +469,10 @@ impl<'a, 'tcx> PatCtxt<'a, 'tcx> {
468469
let instance = match ty::Instance::resolve(self.tcx, param_env_reveal_all, def_id, substs) {
469470
Ok(Some(i)) => i,
470471
Ok(None) => {
471-
self.errors.push(if is_associated_const {
472-
PatternError::AssocConstInPattern(span)
473-
} else {
474-
PatternError::StaticInPattern(span)
475-
});
472+
// It should be assoc consts if there's no error but we cannot resolve it.
473+
debug_assert!(is_associated_const);
474+
475+
self.errors.push(PatternError::AssocConstInPattern(span));
476476

477477
return pat_from_kind(PatKind::Wild);
478478
}

src/test/ui/issues/issue-27895.rs

Lines changed: 0 additions & 10 deletions
This file was deleted.

src/test/ui/issues/issue-27895.stderr

Lines changed: 0 additions & 9 deletions
This file was deleted.

src/test/ui/non-constant-in-const-path.rs

Lines changed: 0 additions & 7 deletions
This file was deleted.

src/test/ui/non-constant-in-const-path.stderr

Lines changed: 0 additions & 9 deletions
This file was deleted.

src/test/ui/pattern/issue-68394-let-pat-runtime-value.rs

Lines changed: 0 additions & 5 deletions
This file was deleted.

src/test/ui/pattern/issue-68394-let-pat-runtime-value.stderr

Lines changed: 0 additions & 9 deletions
This file was deleted.
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// Checks if we emit `PatternError`s correctly.
2+
// This is also a regression test for #27895 and #68394.
3+
4+
static FOO: u8 = 10;
5+
6+
fn main() {
7+
let x = 0;
8+
let 0u8..=x = 0;
9+
//~^ ERROR: runtime values cannot be referenced in patterns
10+
let 0u8..=FOO = 0;
11+
//~^ ERROR: statics cannot be referenced in patterns
12+
match 1 {
13+
0 ..= x => {}
14+
//~^ ERROR: runtime values cannot be referenced in patterns
15+
0 ..= FOO => {}
16+
//~^ ERROR: statics cannot be referenced in patterns
17+
};
18+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
error[E0080]: runtime values cannot be referenced in patterns
2+
--> $DIR/non-constant-in-const-path.rs:8:15
3+
|
4+
LL | let 0u8..=x = 0;
5+
| ^
6+
7+
error[E0158]: statics cannot be referenced in patterns
8+
--> $DIR/non-constant-in-const-path.rs:10:15
9+
|
10+
LL | let 0u8..=FOO = 0;
11+
| ^^^
12+
13+
error[E0080]: runtime values cannot be referenced in patterns
14+
--> $DIR/non-constant-in-const-path.rs:13:15
15+
|
16+
LL | 0 ..= x => {}
17+
| ^
18+
19+
error[E0158]: statics cannot be referenced in patterns
20+
--> $DIR/non-constant-in-const-path.rs:15:15
21+
|
22+
LL | 0 ..= FOO => {}
23+
| ^^^
24+
25+
error: aborting due to 4 previous errors
26+
27+
Some errors have detailed explanations: E0080, E0158.
28+
For more information about an error, try `rustc --explain E0080`.

0 commit comments

Comments
 (0)