Skip to content

Commit

Permalink
Use item_name instead of a span snippet when talking about const pa…
Browse files Browse the repository at this point in the history
…ttern
  • Loading branch information
estebank committed Nov 6, 2024
1 parent da93ce7 commit 3a25296
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 6 deletions.
5 changes: 2 additions & 3 deletions compiler/rustc_mir_build/src/thir/pattern/check_match.rs
Original file line number Diff line number Diff line change
Expand Up @@ -675,13 +675,12 @@ impl<'p, 'tcx> MatchVisitor<'p, 'tcx> {
subpattern: box Pat { kind: PatKind::Constant { opt_def: Some(def_id), .. }, .. },
..
} = pat.kind
&& let Ok(snippet) = self.tcx.sess.source_map().span_to_snippet(pat.span)
{
let span = self.tcx.def_span(def_id);
let variable = self.tcx.item_name(def_id).to_string();
// 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 });
interpreted_as_const_sugg = Some(InterpretedAsConst { span: pat.span, variable });
} else if let PatKind::Constant { .. }
| PatKind::AscribeUserType {
subpattern: box Pat { kind: PatKind::Constant { .. }, .. },
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/consts/const-pattern-irrefutable.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
mod foo {
pub const b: u8 = 2;
//~^ missing patterns are not covered because `c` is interpreted as a constant pattern, not a new variable
//~^ missing patterns are not covered because `b` 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
}
Expand Down
4 changes: 2 additions & 2 deletions tests/ui/consts/const-pattern-irrefutable.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,13 @@ error[E0005]: refutable pattern in local binding
--> $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
| --------------- missing patterns are not covered because `b` 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
| help: introduce a variable instead: `c_var`
| help: introduce a variable instead: `b_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
Expand Down

0 comments on commit 3a25296

Please sign in to comment.