diff options
| author | bors <bors@rust-lang.org> | 2023-06-05 01:33:58 +0000 |
|---|---|---|
| committer | bors <bors@rust-lang.org> | 2023-06-05 01:33:58 +0000 |
| commit | e4106065bf1bb515935d5d024e8d9c86454e2b22 (patch) | |
| tree | 2dc430f81aa163717f435f8f4ec6c82c8dffaeeb | |
| parent | dcf3571c513a29a60a5061ce84c3c618cf5e4cd5 (diff) | |
| parent | 55b4549602d2f1105aedd7a1357a62db4f128167 (diff) | |
| download | rust-e4106065bf1bb515935d5d024e8d9c86454e2b22.tar.gz rust-e4106065bf1bb515935d5d024e8d9c86454e2b22.zip | |
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.
| -rw-r--r-- | compiler/rustc_mir_build/src/thir/pattern/check_match.rs | 7 | ||||
| -rw-r--r-- | tests/ui/mir/issue-112269.rs | 9 | ||||
| -rw-r--r-- | tests/ui/mir/issue-112269.stderr | 31 |
3 files changed, 46 insertions, 1 deletions
diff --git a/compiler/rustc_mir_build/src/thir/pattern/check_match.rs b/compiler/rustc_mir_build/src/thir/pattern/check_match.rs index 1e51cb9aa96..e7e88351bbd 100644 --- a/compiler/rustc_mir_build/src/thir/pattern/check_match.rs +++ b/compiler/rustc_mir_build/src/thir/pattern/check_match.rs @@ -443,7 +443,12 @@ impl<'p, 'tcx> MatchVisitor<'_, 'p, 'tcx> { let mut let_suggestion = None; let mut misc_suggestion = None; let mut interpreted_as_const = None; - if let PatKind::Constant { .. } = pat.kind + + if let PatKind::Constant { .. } + | PatKind::AscribeUserType { + subpattern: box Pat { kind: PatKind::Constant { .. }, .. }, + .. + } = pat.kind && let Ok(snippet) = self.tcx.sess.source_map().span_to_snippet(pat.span) { // If the pattern to match is an integer literal: diff --git a/tests/ui/mir/issue-112269.rs b/tests/ui/mir/issue-112269.rs new file mode 100644 index 00000000000..8b9b16647e4 --- /dev/null +++ b/tests/ui/mir/issue-112269.rs @@ -0,0 +1,9 @@ +pub fn main() { + const x: i32 = 4; + let x: i32 = 3; + //~^ ERROR refutable pattern in local binding + + const y: i32 = 3; + let y = 4; + //~^ ERROR refutable pattern in local binding +} diff --git a/tests/ui/mir/issue-112269.stderr b/tests/ui/mir/issue-112269.stderr new file mode 100644 index 00000000000..f5b79602797 --- /dev/null +++ b/tests/ui/mir/issue-112269.stderr @@ -0,0 +1,31 @@ +error[E0005]: refutable pattern in local binding + --> $DIR/issue-112269.rs:3:9 + | +LL | let x: i32 = 3; + | ^ + | | + | patterns `i32::MIN..=3_i32` and `5_i32..=i32::MAX` not covered + | missing patterns are not covered because `x` is interpreted as a constant pattern, not a new variable + | help: introduce a variable instead: `x_var` + | + = note: `let` bindings require an "irrefutable pattern", like a `struct` or an `enum` with only one variant + = note: for more information, visit https://doc.rust-lang.org/book/ch18-02-refutability.html + = note: the matched value is of type `i32` + +error[E0005]: refutable pattern in local binding + --> $DIR/issue-112269.rs:7:9 + | +LL | let y = 4; + | ^ + | | + | patterns `i32::MIN..=2_i32` and `4_i32..=i32::MAX` not covered + | missing patterns are not covered because `y` is interpreted as a constant pattern, not a new variable + | help: introduce a variable instead: `y_var` + | + = note: `let` bindings require an "irrefutable pattern", like a `struct` or an `enum` with only one variant + = note: for more information, visit https://doc.rust-lang.org/book/ch18-02-refutability.html + = note: the matched value is of type `i32` + +error: aborting due to 2 previous errors + +For more information about this error, try `rustc --explain E0005`. |
