Skip to content

Commit e410606

Browse files
committed
Auto merge of #112272 - jieyouxu:issue-112269, r=compiler-errors
Show note for type ascription on a local binding interpreted as a constant pattern and not a new variable Given the code ```rust pub fn main() { const y: i32 = 4; let y: i32 = 3; } ``` `y` in the let binding is actually interpreted as a constant pattern and is not a new variable, causing confusing diagnostics about refutable patterns in local binding. This PR extends the note for type ascription of a constant pattern to `AscribeUserType` patterns which have `Constant` subpatterns. Fixes #112269.
2 parents dcf3571 + 55b4549 commit e410606

File tree

3 files changed

+46
-1
lines changed

3 files changed

+46
-1
lines changed

Diff for: compiler/rustc_mir_build/src/thir/pattern/check_match.rs

+6-1
Original file line numberDiff line numberDiff line change
@@ -443,7 +443,12 @@ impl<'p, 'tcx> MatchVisitor<'_, 'p, 'tcx> {
443443
let mut let_suggestion = None;
444444
let mut misc_suggestion = None;
445445
let mut interpreted_as_const = None;
446-
if let PatKind::Constant { .. } = pat.kind
446+
447+
if let PatKind::Constant { .. }
448+
| PatKind::AscribeUserType {
449+
subpattern: box Pat { kind: PatKind::Constant { .. }, .. },
450+
..
451+
} = pat.kind
447452
&& let Ok(snippet) = self.tcx.sess.source_map().span_to_snippet(pat.span)
448453
{
449454
// If the pattern to match is an integer literal:

Diff for: tests/ui/mir/issue-112269.rs

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
pub fn main() {
2+
const x: i32 = 4;
3+
let x: i32 = 3;
4+
//~^ ERROR refutable pattern in local binding
5+
6+
const y: i32 = 3;
7+
let y = 4;
8+
//~^ ERROR refutable pattern in local binding
9+
}

Diff for: tests/ui/mir/issue-112269.stderr

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
error[E0005]: refutable pattern in local binding
2+
--> $DIR/issue-112269.rs:3:9
3+
|
4+
LL | let x: i32 = 3;
5+
| ^
6+
| |
7+
| patterns `i32::MIN..=3_i32` and `5_i32..=i32::MAX` not covered
8+
| missing patterns are not covered because `x` is interpreted as a constant pattern, not a new variable
9+
| help: introduce a variable instead: `x_var`
10+
|
11+
= note: `let` bindings require an "irrefutable pattern", like a `struct` or an `enum` with only one variant
12+
= note: for more information, visit https://doc.rust-lang.org/book/ch18-02-refutability.html
13+
= note: the matched value is of type `i32`
14+
15+
error[E0005]: refutable pattern in local binding
16+
--> $DIR/issue-112269.rs:7:9
17+
|
18+
LL | let y = 4;
19+
| ^
20+
| |
21+
| patterns `i32::MIN..=2_i32` and `4_i32..=i32::MAX` not covered
22+
| missing patterns are not covered because `y` is interpreted as a constant pattern, not a new variable
23+
| help: introduce a variable instead: `y_var`
24+
|
25+
= note: `let` bindings require an "irrefutable pattern", like a `struct` or an `enum` with only one variant
26+
= note: for more information, visit https://doc.rust-lang.org/book/ch18-02-refutability.html
27+
= note: the matched value is of type `i32`
28+
29+
error: aborting due to 2 previous errors
30+
31+
For more information about this error, try `rustc --explain E0005`.

0 commit comments

Comments
 (0)