Point at const definition when used instead of a binding in a let statement

After:

```
error[E0005]: refutable pattern in local binding
  --> $DIR/bad-pattern.rs:19:13
   |
LL |     const PAT: u32 = 0;
   |     -------------- missing patterns are not covered because `PAT` is interpreted as a constant pattern, not a new variable
...
LL |         let PAT = v1;
   |             ^^^
   |             |
   |             pattern `1_u32..=u32::MAX` not covered
   |             help: introduce a variable instead: `PAT_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 `u32`
```

Before:

```
error[E0005]: refutable pattern in local binding
  --> $DIR/bad-pattern.rs:19:13
   |
LL |         let PAT = v1;
   |             ^^^
   |             |
   |             pattern `1_u32..=u32::MAX` not covered
   |             missing patterns are not covered because `PAT` is interpreted as a constant pattern, not a new variable
   |             help: introduce a variable instead: `PAT_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 `u32`
```
This commit is contained in:
Esteban Küber 2024-11-06 19:46:54 +00:00
parent 917a50a039
commit ff2f7a7a83
18 changed files with 78 additions and 28 deletions

View File

@ -640,6 +640,7 @@ impl<'tcx> Pat<'tcx> {
| Range(..)
| Binding { subpattern: None, .. }
| Constant { .. }
| NamedConstant { .. }
| Error(_) => {}
AscribeUserType { subpattern, .. }
| Binding { subpattern: Some(subpattern), .. }
@ -788,6 +789,12 @@ pub enum PatKind<'tcx> {
value: mir::Const<'tcx>,
},
/// Same as `Constant`, but that came from a `const` that we can point at in diagnostics.
NamedConstant {
value: mir::Const<'tcx>,
span: Span,
},
/// Inline constant found while lowering a pattern.
InlineConstant {
/// [LocalDefId] of the constant, we need this so that we have a
@ -1084,8 +1091,8 @@ mod size_asserts {
static_assert_size!(Block, 48);
static_assert_size!(Expr<'_>, 64);
static_assert_size!(ExprKind<'_>, 40);
static_assert_size!(Pat<'_>, 64);
static_assert_size!(PatKind<'_>, 48);
static_assert_size!(Pat<'_>, 72);
static_assert_size!(PatKind<'_>, 56);
static_assert_size!(Stmt<'_>, 48);
static_assert_size!(StmtKind<'_>, 48);
// tidy-alphabetical-end

View File

@ -246,7 +246,7 @@ pub fn walk_pat<'thir, 'tcx: 'thir, V: Visitor<'thir, 'tcx>>(
visitor.visit_pat(&subpattern.pattern);
}
}
Constant { value: _ } => {}
Constant { value: _ } | NamedConstant { value: _, span: _ } => {}
InlineConstant { def: _, subpattern } => visitor.visit_pat(subpattern),
Range(_) => {}
Slice { prefix, slice, suffix } | Array { prefix, slice, suffix } => {

View File

@ -144,7 +144,9 @@ impl<'a, 'tcx> ParseCtxt<'a, 'tcx> {
let mut targets = Vec::new();
for arm in rest {
let arm = &self.thir[*arm];
let PatKind::Constant { value } = arm.pattern.kind else {
let (PatKind::Constant { value } | PatKind::NamedConstant { value, span: _ }) =
arm.pattern.kind
else {
return Err(ParseError {
span: arm.pattern.span,
item_description: format!("{:?}", arm.pattern.kind),

View File

@ -129,7 +129,9 @@ impl<'pat, 'tcx> MatchPairTree<'pat, 'tcx> {
}
}
PatKind::Constant { value } => TestCase::Constant { value },
PatKind::Constant { value } | PatKind::NamedConstant { value, span: _ } => {
TestCase::Constant { value }
}
PatKind::AscribeUserType {
ascription: thir::Ascription { ref annotation, variance },

View File

@ -882,6 +882,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
}
PatKind::Constant { .. }
| PatKind::NamedConstant { .. }
| PatKind::Range { .. }
| PatKind::Wild
| PatKind::Never

View File

@ -316,6 +316,7 @@ impl<'a, 'tcx> Visitor<'a, 'tcx> for UnsafetyVisitor<'a, 'tcx> {
PatKind::Binding { .. }
// match is conditional on having this value
| PatKind::Constant { .. }
| PatKind::NamedConstant { .. }
| PatKind::Variant { .. }
| PatKind::Leaf { .. }
| PatKind::Deref { .. }

View File

@ -860,8 +860,10 @@ pub(crate) struct PatternNotCovered<'s, 'tcx> {
pub(crate) uncovered: Uncovered,
#[subdiagnostic]
pub(crate) inform: Option<Inform>,
#[label(mir_build_confused)]
pub(crate) interpreted_as_const: Option<Span>,
#[subdiagnostic]
pub(crate) interpreted_as_const: Option<InterpretedAsConst>,
pub(crate) interpreted_as_const_sugg: Option<InterpretedAsConst>,
#[subdiagnostic]
pub(crate) adt_defined_here: Option<AdtDefinedHere<'tcx>>,
#[note(mir_build_privately_uninhabited)]
@ -913,7 +915,6 @@ impl<'tcx> Subdiagnostic for AdtDefinedHere<'tcx> {
code = "{variable}_var",
applicability = "maybe-incorrect"
)]
#[label(mir_build_confused)]
pub(crate) struct InterpretedAsConst {
#[primary_span]
pub(crate) span: Span,

View File

@ -668,8 +668,20 @@ impl<'p, 'tcx> MatchVisitor<'p, 'tcx> {
let mut let_suggestion = None;
let mut misc_suggestion = None;
let mut interpreted_as_const = None;
let mut interpreted_as_const_sugg = None;
if let PatKind::Constant { .. }
if let PatKind::NamedConstant { span, .. }
| PatKind::AscribeUserType {
subpattern: box Pat { kind: PatKind::NamedConstant { span, .. }, .. },
..
} = pat.kind
&& let Ok(snippet) = self.tcx.sess.source_map().span_to_snippet(pat.span)
{
// When we encounter a constant as the binding name, point at the `const` definition.
interpreted_as_const = Some(span);
interpreted_as_const_sugg =
Some(InterpretedAsConst { span: pat.span, variable: snippet });
} else if let PatKind::Constant { .. }
| PatKind::AscribeUserType {
subpattern: box Pat { kind: PatKind::Constant { .. }, .. },
..
@ -683,7 +695,8 @@ impl<'p, 'tcx> MatchVisitor<'p, 'tcx> {
start_span: pat.span.shrink_to_lo(),
});
} else if snippet.chars().all(|c| c.is_alphanumeric() || c == '_') {
interpreted_as_const =
interpreted_as_const = Some(pat.span);
interpreted_as_const_sugg =
Some(InterpretedAsConst { span: pat.span, variable: snippet });
}
}
@ -733,6 +746,7 @@ impl<'p, 'tcx> MatchVisitor<'p, 'tcx> {
uncovered: Uncovered::new(pat.span, &cx, witnesses),
inform,
interpreted_as_const,
interpreted_as_const_sugg,
witness_1_is_privately_uninhabited,
_p: (),
pattern_ty,

View File

@ -157,7 +157,9 @@ impl<'a, 'tcx> PatCtxt<'a, 'tcx> {
}
kind => (kind, None, None),
};
let value = if let PatKind::Constant { value } = kind {
let value = if let PatKind::Constant { value }
| PatKind::NamedConstant { value, span: _ } = kind
{
value
} else {
let msg = format!(
@ -560,9 +562,15 @@ impl<'a, 'tcx> PatCtxt<'a, 'tcx> {
_ => return pat_from_kind(self.lower_variant_or_leaf(res, id, span, ty, vec![])),
};
// HERE
let args = self.typeck_results.node_args(id);
let c = ty::Const::new_unevaluated(self.tcx, ty::UnevaluatedConst { def: def_id, args });
let pattern = self.const_to_pat(c, ty, id, span);
let def_span = self.tcx.def_span(def_id);
let mut pattern = self.const_to_pat(c, ty, id, span);
if let PatKind::Constant { value } = pattern.kind {
pattern.kind = PatKind::NamedConstant { value, span: def_span };
}
tracing::info!("pattern {pattern:#?} {c:?} {ty:?} {id:?}");
if !is_associated_const {
return pattern;

View File

@ -702,7 +702,7 @@ impl<'a, 'tcx> ThirPrinter<'a, 'tcx> {
self.print_pat(subpattern, depth_lvl + 2);
print_indented!(self, "}", depth_lvl + 1);
}
PatKind::Constant { value } => {
PatKind::Constant { value } | PatKind::NamedConstant { value, span: _ } => {
print_indented!(self, "Constant {", depth_lvl + 1);
print_indented!(self, format!("value: {:?}", value), depth_lvl + 2);
print_indented!(self, "}", depth_lvl + 1);

View File

@ -536,7 +536,7 @@ impl<'p, 'tcx: 'p> RustcPatCtxt<'p, 'tcx> {
),
}
}
PatKind::Constant { value } => {
PatKind::Constant { value } | PatKind::NamedConstant { value, span: _ } => {
match ty.kind() {
ty::Bool => {
ctor = match value.try_eval_bool(cx.tcx, cx.param_env) {

View File

@ -370,7 +370,9 @@ impl<'a, 'tcx> IsThirPolymorphic<'a, 'tcx> {
}
match pat.kind {
thir::PatKind::Constant { value } => value.has_non_region_param(),
thir::PatKind::Constant { value } | thir::PatKind::NamedConstant { value, span: _ } => {
value.has_non_region_param()
}
thir::PatKind::Range(box thir::PatRange { lo, hi, .. }) => {
lo.has_non_region_param() || hi.has_non_region_param()
}

View File

@ -97,11 +97,13 @@ LL | if let Refutable::A = v3 { todo!() };
error[E0005]: refutable pattern in local binding
--> $DIR/bad-pattern.rs:19:13
|
LL | const PAT: u32 = 0;
| -------------- missing patterns are not covered because `PAT` is interpreted as a constant pattern, not a new variable
...
LL | let PAT = v1;
| ^^^
| |
| pattern `1_u32..=u32::MAX` not covered
| missing patterns are not covered because `PAT` is interpreted as a constant pattern, not a new variable
| help: introduce a variable instead: `PAT_var`
|
= note: `let` bindings require an "irrefutable pattern", like a `struct` or an `enum` with only one variant

View File

@ -1,28 +1,28 @@
mod foo {
pub const b: u8 = 2;
//~^ missing patterns are not covered because `c` is interpreted as a constant pattern, not a new variable
pub const d: u8 = 2;
//~^ missing patterns are not covered because `d` is interpreted as a constant pattern, not a new variable
}
use foo::b as c;
use foo::d;
const a: u8 = 2;
//~^ missing patterns are not covered because `a` is interpreted as a constant pattern, not a new variable
fn main() {
let a = 4;
//~^ ERROR refutable pattern in local binding
//~| patterns `0_u8..=1_u8` and `3_u8..=u8::MAX` not covered
//~| missing patterns are not covered because `a` is interpreted as a constant pattern, not a new variable
//~| HELP introduce a variable instead
let c = 4;
//~^ ERROR refutable pattern in local binding
//~| patterns `0_u8..=1_u8` and `3_u8..=u8::MAX` not covered
//~| missing patterns are not covered because `c` is interpreted as a constant pattern, not a new variable
//~| HELP introduce a variable instead
let d = 4;
//~^ ERROR refutable pattern in local binding
//~| patterns `0_u8..=1_u8` and `3_u8..=u8::MAX` not covered
//~| missing patterns are not covered because `d` is interpreted as a constant pattern, not a new variable
//~| HELP introduce a variable instead
fn f() {} // Check that the `NOTE`s still work with an item here (cf. issue #35115).
}

View File

@ -1,11 +1,13 @@
error[E0005]: refutable pattern in local binding
--> $DIR/const-pattern-irrefutable.rs:12:9
--> $DIR/const-pattern-irrefutable.rs:15:9
|
LL | const a: u8 = 2;
| ----------- missing patterns are not covered because `a` is interpreted as a constant pattern, not a new variable
...
LL | let a = 4;
| ^
| |
| patterns `0_u8..=1_u8` and `3_u8..=u8::MAX` not covered
| missing patterns are not covered because `a` is interpreted as a constant pattern, not a new variable
| help: introduce a variable instead: `a_var`
|
= note: `let` bindings require an "irrefutable pattern", like a `struct` or an `enum` with only one variant
@ -13,13 +15,15 @@ LL | let a = 4;
= note: the matched value is of type `u8`
error[E0005]: refutable pattern in local binding
--> $DIR/const-pattern-irrefutable.rs:17:9
--> $DIR/const-pattern-irrefutable.rs:19:9
|
LL | pub const b: u8 = 2;
| --------------- missing patterns are not covered because `c` is interpreted as a constant pattern, not a new variable
...
LL | let c = 4;
| ^
| |
| patterns `0_u8..=1_u8` and `3_u8..=u8::MAX` not covered
| missing patterns are not covered because `c` is interpreted as a constant pattern, not a new variable
| help: introduce a variable instead: `c_var`
|
= note: `let` bindings require an "irrefutable pattern", like a `struct` or an `enum` with only one variant
@ -27,13 +31,15 @@ LL | let c = 4;
= note: the matched value is of type `u8`
error[E0005]: refutable pattern in local binding
--> $DIR/const-pattern-irrefutable.rs:22:9
--> $DIR/const-pattern-irrefutable.rs:23:9
|
LL | pub const d: u8 = 2;
| --------------- missing patterns are not covered because `d` is interpreted as a constant pattern, not a new variable
...
LL | let d = 4;
| ^
| |
| patterns `0_u8..=1_u8` and `3_u8..=u8::MAX` not covered
| missing patterns are not covered because `d` is interpreted as a constant pattern, not a new variable
| help: introduce a variable instead: `d_var`
|
= note: `let` bindings require an "irrefutable pattern", like a `struct` or an `enum` with only one variant

View File

@ -1,11 +1,12 @@
error[E0005]: refutable pattern in local binding
--> $DIR/issue-112269.rs:3:9
|
LL | const x: i32 = 4;
| ------------ missing patterns are not covered because `x` is interpreted as a constant pattern, not a new variable
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
@ -15,11 +16,12 @@ LL | let x: i32 = 3;
error[E0005]: refutable pattern in local binding
--> $DIR/issue-112269.rs:7:9
|
LL | const y: i32 = 3;
| ------------ missing patterns are not covered because `y` is interpreted as a constant pattern, not a new variable
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

View File

@ -2,9 +2,9 @@ fn main() {
let A = 3;
//~^ ERROR refutable pattern in local binding
//~| patterns `i32::MIN..=1_i32` and `3_i32..=i32::MAX` not covered
//~| missing patterns are not covered because `A` is interpreted as a constant pattern, not a new variable
//~| HELP introduce a variable instead
//~| SUGGESTION A_var
const A: i32 = 2;
//~^ missing patterns are not covered because `A` is interpreted as a constant pattern, not a new variable
}

View File

@ -5,8 +5,10 @@ LL | let A = 3;
| ^
| |
| patterns `i32::MIN..=1_i32` and `3_i32..=i32::MAX` not covered
| missing patterns are not covered because `A` is interpreted as a constant pattern, not a new variable
| help: introduce a variable instead: `A_var`
...
LL | const A: i32 = 2;
| ------------ missing patterns are not covered because `A` is interpreted as a constant pattern, not a new variable
|
= 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