Skip to content

Commit 1d93048

Browse files
committed
give better suggestion when matching a const range
1 parent 5fae569 commit 1d93048

File tree

3 files changed

+73
-11
lines changed

3 files changed

+73
-11
lines changed

compiler/rustc_typeck/src/check/pat.rs

+44-9
Original file line numberDiff line numberDiff line change
@@ -772,12 +772,12 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
772772
);
773773
}
774774
_ => {
775-
let const_def_id = match pat_ty.kind() {
775+
let (type_def_id, item_def_id) = match pat_ty.kind() {
776776
Adt(def, _) => match res {
777-
Res::Def(DefKind::Const, _) => Some(def.did),
778-
_ => None,
777+
Res::Def(DefKind::Const, def_id) => (Some(def.did), Some(def_id)),
778+
_ => (None, None),
779779
},
780-
_ => None,
780+
_ => (None, None),
781781
};
782782

783783
let ranges = &[
@@ -788,11 +788,46 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
788788
self.tcx.lang_items().range_inclusive_struct(),
789789
self.tcx.lang_items().range_to_inclusive_struct(),
790790
];
791-
if const_def_id != None && ranges.contains(&const_def_id) {
792-
let msg = "constants only support matching by type, \
793-
if you meant to match against a range of values, \
794-
consider using a range pattern like `min ..= max` in the match block";
795-
e.note(msg);
791+
if type_def_id != None && ranges.contains(&type_def_id) {
792+
let generic_message = match item_def_id {
793+
Some(def_id) => match self.tcx.hir().get_if_local(def_id) {
794+
Some(hir::Node::Item(hir::Item {
795+
kind: hir::ItemKind::Const(_, body_id),
796+
..
797+
})) => match self.tcx.hir().get(body_id.hir_id) {
798+
hir::Node::Expr(expr) => {
799+
if hir::is_range_literal(expr) {
800+
let span = self.tcx.hir().span(body_id.hir_id);
801+
if let Ok(snip) =
802+
self.tcx.sess.source_map().span_to_snippet(span)
803+
{
804+
e.span_suggestion_verbose(
805+
span,
806+
"you may want to move the range into the match block",
807+
snip,
808+
Applicability::MachineApplicable
809+
);
810+
false
811+
} else {
812+
true
813+
}
814+
} else {
815+
true
816+
}
817+
}
818+
_ => true,
819+
},
820+
_ => true,
821+
},
822+
_ => true,
823+
};
824+
825+
if generic_message {
826+
let msg = "constants only support matching by type, \
827+
if you meant to match against a range of values, \
828+
consider using a range pattern like `min ..= max` in the match block";
829+
e.note(msg);
830+
}
796831
} else {
797832
let msg = "introduce a new binding instead";
798833
let sugg = format!("other_{}", ident.as_str().to_lowercase());

src/test/ui/issues/issue-76191.rs

+5
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,18 @@
22
#![allow(non_snake_case)]
33

44
use std::ops::RangeInclusive;
5+
56
const RANGE: RangeInclusive<i32> = 0..=255;
67

8+
const RANGE2: RangeInclusive<i32> = panic!();
9+
710
fn main() {
811
let n: i32 = 1;
912
match n {
1013
RANGE => {}
1114
//~^ ERROR mismatched types
15+
RANGE2 => {}
16+
//~^ ERROR mismatched types
1217
_ => {}
1318
}
1419
}

src/test/ui/issues/issue-76191.stderr

+24-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error[E0308]: mismatched types
2-
--> $DIR/issue-76191.rs:10:9
2+
--> $DIR/issue-76191.rs:13:9
33
|
44
LL | const RANGE: RangeInclusive<i32> = 0..=255;
55
| ------------------------------------------- constant defined here
@@ -14,8 +14,30 @@ LL | RANGE => {}
1414
|
1515
= note: expected type `i32`
1616
found struct `RangeInclusive<i32>`
17+
help: you may want to move the range into the match block
18+
|
19+
LL | const RANGE: RangeInclusive<i32> = 0..=255;
20+
| ^^^^^^^
21+
22+
error[E0308]: mismatched types
23+
--> $DIR/issue-76191.rs:15:9
24+
|
25+
LL | const RANGE2: RangeInclusive<i32> = panic!();
26+
| --------------------------------------------- constant defined here
27+
...
28+
LL | match n {
29+
| - this expression has type `i32`
30+
...
31+
LL | RANGE2 => {}
32+
| ^^^^^^
33+
| |
34+
| expected `i32`, found struct `RangeInclusive`
35+
| `RANGE2` is interpreted as a constant, not a new binding
36+
|
37+
= note: expected type `i32`
38+
found struct `RangeInclusive<i32>`
1739
= note: constants only support matching by type, if you meant to match against a range of values, consider using a range pattern like `min ..= max` in the match block
1840

19-
error: aborting due to previous error
41+
error: aborting due to 2 previous errors
2042

2143
For more information about this error, try `rustc --explain E0308`.

0 commit comments

Comments
 (0)