Skip to content

Commit 6dc79f6

Browse files
committed
Use item_name instead of a span snippet when talking about const pattern
1 parent c25b44b commit 6dc79f6

File tree

3 files changed

+5
-6
lines changed

3 files changed

+5
-6
lines changed

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

+2-3
Original file line numberDiff line numberDiff line change
@@ -675,13 +675,12 @@ impl<'p, 'tcx> MatchVisitor<'p, 'tcx> {
675675
subpattern: box Pat { kind: PatKind::Constant { opt_def: Some(def_id), .. }, .. },
676676
..
677677
} = pat.kind
678-
&& let Ok(snippet) = self.tcx.sess.source_map().span_to_snippet(pat.span)
679678
{
680679
let span = self.tcx.def_span(def_id);
680+
let variable = self.tcx.item_name(def_id).to_string();
681681
// When we encounter a constant as the binding name, point at the `const` definition.
682682
interpreted_as_const = Some(span);
683-
interpreted_as_const_sugg =
684-
Some(InterpretedAsConst { span: pat.span, variable: snippet });
683+
interpreted_as_const_sugg = Some(InterpretedAsConst { span: pat.span, variable });
685684
} else if let PatKind::Constant { .. }
686685
| PatKind::AscribeUserType {
687686
subpattern: box Pat { kind: PatKind::Constant { .. }, .. },

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
mod foo {
22
pub const b: u8 = 2;
3-
//~^ missing patterns are not covered because `c` is interpreted as a constant pattern, not a new variable
3+
//~^ missing patterns are not covered because `b` is interpreted as a constant pattern, not a new variable
44
pub const d: u8 = 2;
55
//~^ missing patterns are not covered because `d` is interpreted as a constant pattern, not a new variable
66
}

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,13 @@ error[E0005]: refutable pattern in local binding
1818
--> $DIR/const-pattern-irrefutable.rs:19:9
1919
|
2020
LL | pub const b: u8 = 2;
21-
| --------------- missing patterns are not covered because `c` is interpreted as a constant pattern, not a new variable
21+
| --------------- missing patterns are not covered because `b` is interpreted as a constant pattern, not a new variable
2222
...
2323
LL | let c = 4;
2424
| ^
2525
| |
2626
| patterns `0_u8..=1_u8` and `3_u8..=u8::MAX` not covered
27-
| help: introduce a variable instead: `c_var`
27+
| help: introduce a variable instead: `b_var`
2828
|
2929
= note: `let` bindings require an "irrefutable pattern", like a `struct` or an `enum` with only one variant
3030
= note: for more information, visit https://doc.rust-lang.org/book/ch18-02-refutability.html

0 commit comments

Comments
 (0)