Skip to content

Commit 4f97540

Browse files
committed
Reinstate confusion note.
1 parent 4d8ed58 commit 4f97540

File tree

8 files changed

+61
-59
lines changed

8 files changed

+61
-59
lines changed

compiler/rustc_mir_build/messages.ftl

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -347,15 +347,13 @@ mir_build_inform_irrefutable = `let` bindings require an "irrefutable pattern",
347347
348348
mir_build_more_information = for more information, visit https://doc.rust-lang.org/book/ch18-02-refutability.html
349349
350-
mir_build_res_defined_here = {$res} defined here
351-
352350
mir_build_adt_defined_here = `{$ty}` defined here
353351
354352
mir_build_variant_defined_here = not covered
355353
356354
mir_build_interpreted_as_const = introduce a variable instead
357355
358-
mir_build_confused = missing patterns are not covered because `{$variable}` is interpreted as {$article} {$res} pattern, not a new variable
356+
mir_build_confused = missing patterns are not covered because `{$variable}` is interpreted as a constant pattern, not a new variable
359357
360358
mir_build_suggest_if_let = you might want to use `if let` to ignore the {$count ->
361359
[one] variant that isn't

compiler/rustc_mir_build/src/errors.rs

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ use rustc_errors::{
66
error_code, AddToDiagnostic, Applicability, Diagnostic, DiagnosticBuilder, ErrorGuaranteed,
77
Handler, IntoDiagnostic, MultiSpan, SubdiagnosticMessage,
88
};
9-
use rustc_hir::def::Res;
109
use rustc_macros::{Diagnostic, LintDiagnostic, Subdiagnostic};
1110
use rustc_middle::thir::Pat;
1211
use rustc_middle::ty::{self, Ty};
@@ -795,8 +794,6 @@ pub(crate) struct PatternNotCovered<'s, 'tcx> {
795794
pub let_suggestion: Option<SuggestLet>,
796795
#[subdiagnostic]
797796
pub misc_suggestion: Option<MiscPatternSuggestion>,
798-
#[subdiagnostic]
799-
pub res_defined_here: Option<ResDefinedHere>,
800797
}
801798

802799
#[derive(Subdiagnostic)]
@@ -830,14 +827,6 @@ impl<'tcx> AddToDiagnostic for AdtDefinedHere<'tcx> {
830827
}
831828
}
832829

833-
#[derive(Subdiagnostic)]
834-
#[label(mir_build_res_defined_here)]
835-
pub struct ResDefinedHere {
836-
#[primary_span]
837-
pub def_span: Span,
838-
pub res: Res,
839-
}
840-
841830
#[derive(Subdiagnostic)]
842831
#[suggestion(
843832
mir_build_interpreted_as_const,
@@ -848,9 +837,7 @@ pub struct ResDefinedHere {
848837
pub struct InterpretedAsConst {
849838
#[primary_span]
850839
pub span: Span,
851-
pub article: &'static str,
852840
pub variable: String,
853-
pub res: Res,
854841
}
855842

856843
#[derive(Subdiagnostic)]

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

Lines changed: 25 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -424,29 +424,38 @@ impl<'p, 'tcx> MatchVisitor<'_, 'p, 'tcx> {
424424
let inform = sp.is_some().then_some(Inform);
425425
let mut let_suggestion = None;
426426
let mut misc_suggestion = None;
427-
if let Some(span) = sp && self.tcx.sess.source_map().is_span_accessible(span) {
428-
let mut bindings= vec![];
429-
pat.each_binding(|name, _, _, _| {
430-
bindings.push(name);
431-
});
432-
let semi_span = span.shrink_to_hi();
433-
let start_span = span.shrink_to_lo();
434-
let end_span = semi_span.shrink_to_lo();
435-
let count = witnesses.len();
436-
427+
let mut interpreted_as_const = None;
428+
if let PatKind::Constant { .. } = pat.kind
429+
&& let Ok(snippet) = self.tcx.sess.source_map().span_to_snippet(pat.span)
430+
{
437431
// If the pattern to match is an integer literal:
438-
if bindings.is_empty()
439-
&& let Ok(snippet) = self.tcx.sess.source_map().span_to_snippet(pat.span)
440-
&& snippet.chars().all(|c| c.is_digit(10))
441-
{
432+
if snippet.chars().all(|c| c.is_digit(10)) {
442433
// Then give a suggestion, the user might've meant to create a binding instead.
443434
misc_suggestion = Some(MiscPatternSuggestion::AttemptedIntegerLiteral {
444435
start_span: pat.span.shrink_to_lo()
445436
});
437+
} else if snippet.chars().all(|c| c.is_alphanumeric() || c == '_') {
438+
interpreted_as_const = Some(InterpretedAsConst {
439+
span: pat.span,
440+
variable: snippet,
441+
});
446442
}
443+
}
444+
445+
if let Some(span) = sp
446+
&& self.tcx.sess.source_map().is_span_accessible(span)
447+
&& interpreted_as_const.is_none()
448+
{
449+
let mut bindings = vec![];
450+
pat.each_binding(|name, _, _, _| bindings.push(name));
451+
452+
let semi_span = span.shrink_to_hi();
453+
let start_span = span.shrink_to_lo();
454+
let end_span = semi_span.shrink_to_lo();
455+
let count = witnesses.len();
447456

448457
let_suggestion = Some(if bindings.is_empty() {
449-
SuggestLet::If {start_span, semi_span, count }
458+
SuggestLet::If { start_span, semi_span, count }
450459
} else {
451460
SuggestLet::Else { end_span, count }
452461
});
@@ -469,12 +478,11 @@ impl<'p, 'tcx> MatchVisitor<'_, 'p, 'tcx> {
469478
origin,
470479
uncovered: Uncovered::new(pat.span, &cx, witnesses),
471480
inform,
472-
interpreted_as_const: None,
481+
interpreted_as_const,
473482
_p: (),
474483
pattern_ty,
475484
let_suggestion,
476485
misc_suggestion,
477-
res_defined_here: None,
478486
adt_defined_here,
479487
});
480488
}

tests/ui/consts/const-pattern-irrefutable.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,17 @@ fn main() {
1212
let a = 4;
1313
//~^ ERROR refutable pattern in local binding
1414
//~| patterns `0_u8..=1_u8` and `3_u8..=u8::MAX` not covered
15-
//~| HELP you might want to use `if let` to ignore the variants that aren't matched
15+
//~| missing patterns are not covered because `a` is interpreted as a constant pattern, not a new variable
16+
//~| HELP introduce a variable instead
1617
let c = 4;
1718
//~^ ERROR refutable pattern in local binding
1819
//~| patterns `0_u8..=1_u8` and `3_u8..=u8::MAX` not covered
19-
//~| HELP you might want to use `if let` to ignore the variants that aren't matched
20+
//~| missing patterns are not covered because `c` is interpreted as a constant pattern, not a new variable
21+
//~| HELP introduce a variable instead
2022
let d = 4;
2123
//~^ ERROR refutable pattern in local binding
2224
//~| patterns `0_u8..=1_u8` and `3_u8..=u8::MAX` not covered
23-
//~| HELP you might want to use `if let` to ignore the variants that aren't matched
25+
//~| missing patterns are not covered because `d` is interpreted as a constant pattern, not a new variable
26+
//~| HELP introduce a variable instead
2427
fn f() {} // Check that the `NOTE`s still work with an item here (cf. issue #35115).
2528
}

tests/ui/consts/const-pattern-irrefutable.stderr

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,43 +2,43 @@ error[E0005]: refutable pattern in local binding
22
--> $DIR/const-pattern-irrefutable.rs:12:9
33
|
44
LL | let a = 4;
5-
| ^ patterns `0_u8..=1_u8` and `3_u8..=u8::MAX` not covered
5+
| ^
6+
| |
7+
| patterns `0_u8..=1_u8` and `3_u8..=u8::MAX` not covered
8+
| missing patterns are not covered because `a` is interpreted as a constant pattern, not a new variable
9+
| help: introduce a variable instead: `a_var`
610
|
711
= note: `let` bindings require an "irrefutable pattern", like a `struct` or an `enum` with only one variant
812
= note: for more information, visit https://doc.rust-lang.org/book/ch18-02-refutability.html
913
= note: the matched value is of type `u8`
10-
help: you might want to use `if let` to ignore the variants that aren't matched
11-
|
12-
LL | if let a = 4 { todo!() };
13-
| ++ +++++++++++
1414

1515
error[E0005]: refutable pattern in local binding
16-
--> $DIR/const-pattern-irrefutable.rs:16:9
16+
--> $DIR/const-pattern-irrefutable.rs:17:9
1717
|
1818
LL | let c = 4;
19-
| ^ patterns `0_u8..=1_u8` and `3_u8..=u8::MAX` not covered
19+
| ^
20+
| |
21+
| patterns `0_u8..=1_u8` and `3_u8..=u8::MAX` not covered
22+
| missing patterns are not covered because `c` is interpreted as a constant pattern, not a new variable
23+
| help: introduce a variable instead: `c_var`
2024
|
2125
= note: `let` bindings require an "irrefutable pattern", like a `struct` or an `enum` with only one variant
2226
= note: for more information, visit https://doc.rust-lang.org/book/ch18-02-refutability.html
2327
= note: the matched value is of type `u8`
24-
help: you might want to use `if let` to ignore the variants that aren't matched
25-
|
26-
LL | if let c = 4 { todo!() };
27-
| ++ +++++++++++
2828

2929
error[E0005]: refutable pattern in local binding
30-
--> $DIR/const-pattern-irrefutable.rs:20:9
30+
--> $DIR/const-pattern-irrefutable.rs:22:9
3131
|
3232
LL | let d = 4;
33-
| ^ patterns `0_u8..=1_u8` and `3_u8..=u8::MAX` not covered
33+
| ^
34+
| |
35+
| patterns `0_u8..=1_u8` and `3_u8..=u8::MAX` not covered
36+
| missing patterns are not covered because `d` is interpreted as a constant pattern, not a new variable
37+
| help: introduce a variable instead: `d_var`
3438
|
3539
= note: `let` bindings require an "irrefutable pattern", like a `struct` or an `enum` with only one variant
3640
= note: for more information, visit https://doc.rust-lang.org/book/ch18-02-refutability.html
3741
= note: the matched value is of type `u8`
38-
help: you might want to use `if let` to ignore the variants that aren't matched
39-
|
40-
LL | if let d = 4 { todo!() };
41-
| ++ +++++++++++
4242

4343
error: aborting due to 3 previous errors
4444

tests/ui/pattern/usefulness/refutable-pattern-in-fn-arg.stderr

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@ LL | let f = |3: isize| println!("hello");
55
| ^ pattern `_` not covered
66
|
77
= note: the matched value is of type `isize`
8+
help: alternatively, you could prepend the pattern with an underscore to define a new named variable; identifiers cannot begin with digits
9+
|
10+
LL | let f = |_3: isize| println!("hello");
11+
| +
812

913
error: aborting due to previous error
1014

tests/ui/suggestions/const-pat-non-exaustive-let-new-var.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@ fn main() {
22
let A = 3;
33
//~^ ERROR refutable pattern in local binding
44
//~| patterns `i32::MIN..=1_i32` and `3_i32..=i32::MAX` not covered
5-
//~| HELP you might want to use `if let` to ignore the variants that aren't matched
5+
//~| missing patterns are not covered because `A` is interpreted as a constant pattern, not a new variable
6+
//~| HELP introduce a variable instead
7+
//~| SUGGESTION A_var
68

79
const A: i32 = 2;
810
}

tests/ui/suggestions/const-pat-non-exaustive-let-new-var.stderr

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,15 @@ error[E0005]: refutable pattern in local binding
22
--> $DIR/const-pat-non-exaustive-let-new-var.rs:2:9
33
|
44
LL | let A = 3;
5-
| ^ patterns `i32::MIN..=1_i32` and `3_i32..=i32::MAX` not covered
5+
| ^
6+
| |
7+
| patterns `i32::MIN..=1_i32` and `3_i32..=i32::MAX` not covered
8+
| missing patterns are not covered because `A` is interpreted as a constant pattern, not a new variable
9+
| help: introduce a variable instead: `A_var`
610
|
711
= note: `let` bindings require an "irrefutable pattern", like a `struct` or an `enum` with only one variant
812
= note: for more information, visit https://doc.rust-lang.org/book/ch18-02-refutability.html
913
= note: the matched value is of type `i32`
10-
help: you might want to use `if let` to ignore the variants that aren't matched
11-
|
12-
LL | if let A = 3 { todo!() };
13-
| ++ +++++++++++
1414

1515
error: aborting due to previous error
1616

0 commit comments

Comments
 (0)