Skip to content

Commit 017944c

Browse files
authored
Rollup merge of #64859 - Centril:const-def-here-new-var, r=estebank
check_match: improve diagnostics for `let A = 2;` with `const A: i32 = 3` For example: ``` error[E0005]: refutable pattern in local binding: `std::i32::MIN..=1i32` and `3i32..=std::i32::MAX` not covered --> $DIR/const-pat-non-exaustive-let-new-var.rs:2:9 | LL | let A = 3; | ^ | | | interpreted as a constant pattern, not a new variable | help: introduce a variable instead: `a_var` ... LL | const A: i32 = 2; | ----------------- constant defined here ``` r? @estebank cc @matthiaskrgr @rpjohnst
2 parents f1a5bc5 + aa03f1f commit 017944c

File tree

7 files changed

+96
-26
lines changed

7 files changed

+96
-26
lines changed

src/librustc/hir/map/mod.rs

+8
Original file line numberDiff line numberDiff line change
@@ -1083,6 +1083,14 @@ impl<'hir> Map<'hir> {
10831083
self.as_local_hir_id(id).map(|id| self.span(id))
10841084
}
10851085

1086+
pub fn res_span(&self, res: Res) -> Option<Span> {
1087+
match res {
1088+
Res::Err => None,
1089+
Res::Local(id) => Some(self.span(id)),
1090+
res => self.span_if_local(res.opt_def_id()?),
1091+
}
1092+
}
1093+
10861094
pub fn node_to_string(&self, id: HirId) -> String {
10871095
hir_id_to_string(self, id, true)
10881096
}

src/librustc_mir/hair/pattern/check_match.rs

+39-8
Original file line numberDiff line numberDiff line change
@@ -266,20 +266,51 @@ impl<'tcx> MatchVisitor<'_, 'tcx> {
266266
"refutable pattern in {}: {} not covered",
267267
origin, joined_patterns
268268
);
269-
err.span_label(pat.span, match &pat.kind {
269+
match &pat.kind {
270270
hir::PatKind::Path(hir::QPath::Resolved(None, path))
271-
if path.segments.len() == 1 && path.segments[0].args.is_none() => {
272-
format!("interpreted as {} {} pattern, not new variable",
273-
path.res.article(), path.res.descr())
271+
if path.segments.len() == 1 && path.segments[0].args.is_none() =>
272+
{
273+
const_not_var(&mut err, cx.tcx, pat, path);
274274
}
275-
_ => pattern_not_convered_label(&witnesses, &joined_patterns),
276-
});
275+
_ => {
276+
err.span_label(
277+
pat.span,
278+
pattern_not_covered_label(&witnesses, &joined_patterns),
279+
);
280+
}
281+
}
282+
277283
adt_defined_here(cx, &mut err, pattern_ty, &witnesses);
278284
err.emit();
279285
});
280286
}
281287
}
282288

289+
/// A path pattern was interpreted as a constant, not a new variable.
290+
/// This caused an irrefutable match failure in e.g. `let`.
291+
fn const_not_var(err: &mut DiagnosticBuilder<'_>, tcx: TyCtxt<'_>, pat: &Pat, path: &hir::Path) {
292+
let descr = path.res.descr();
293+
err.span_label(pat.span, format!(
294+
"interpreted as {} {} pattern, not a new variable",
295+
path.res.article(),
296+
descr,
297+
));
298+
299+
err.span_suggestion(
300+
pat.span,
301+
"introduce a variable instead",
302+
format!("{}_var", path.segments[0].ident).to_lowercase(),
303+
// Cannot use `MachineApplicable` as it's not really *always* correct
304+
// because there may be such an identifier in scope or the user maybe
305+
// really wanted to match against the constant. This is quite unlikely however.
306+
Applicability::MaybeIncorrect,
307+
);
308+
309+
if let Some(span) = tcx.hir().res_span(path.res) {
310+
err.span_label(span, format!("{} defined here", descr));
311+
}
312+
}
313+
283314
fn check_for_bindings_named_same_as_variants(cx: &MatchVisitor<'_, '_>, pat: &Pat) {
284315
pat.walk(|p| {
285316
if let hir::PatKind::Binding(_, _, ident, None) = p.kind {
@@ -445,7 +476,7 @@ fn check_exhaustive<'tcx>(
445476
cx.tcx.sess, sp,
446477
format!("non-exhaustive patterns: {} not covered", joined_patterns),
447478
);
448-
err.span_label(sp, pattern_not_convered_label(&witnesses, &joined_patterns));
479+
err.span_label(sp, pattern_not_covered_label(&witnesses, &joined_patterns));
449480
adt_defined_here(cx, &mut err, scrut_ty, &witnesses);
450481
err.help(
451482
"ensure that all possible cases are being handled, \
@@ -471,7 +502,7 @@ fn joined_uncovered_patterns(witnesses: &[super::Pat<'_>]) -> String {
471502
}
472503
}
473504

474-
fn pattern_not_convered_label(witnesses: &[super::Pat<'_>], joined_patterns: &str) -> String {
505+
fn pattern_not_covered_label(witnesses: &[super::Pat<'_>], joined_patterns: &str) -> String {
475506
format!("pattern{} {} not covered", rustc_errors::pluralise!(witnesses.len()), joined_patterns)
476507
}
477508

src/librustc_typeck/astconv.rs

+2-5
Original file line numberDiff line numberDiff line change
@@ -1368,11 +1368,8 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
13681368
span,
13691369
format!("associated type `{}` must be specified", assoc_item.ident),
13701370
);
1371-
if item_def_id.is_local() {
1372-
err.span_label(
1373-
tcx.def_span(*item_def_id),
1374-
format!("`{}` defined here", assoc_item.ident),
1375-
);
1371+
if let Some(sp) = tcx.hir().span_if_local(*item_def_id) {
1372+
err.span_label(sp, format!("`{}` defined here", assoc_item.ident));
13761373
}
13771374
if suggest {
13781375
if let Ok(snippet) = tcx.sess.source_map().span_to_snippet(

src/librustc_typeck/check/callee.rs

+1-10
Original file line numberDiff line numberDiff line change
@@ -351,16 +351,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
351351

352352
err.span_label(call_expr.span, "call expression requires function");
353353

354-
let def_span = match def {
355-
Res::Err => None,
356-
Res::Local(id) => {
357-
Some(self.tcx.hir().span(id))
358-
},
359-
_ => def
360-
.opt_def_id()
361-
.and_then(|did| self.tcx.hir().span_if_local(did)),
362-
};
363-
if let Some(span) = def_span {
354+
if let Some(span) = self.tcx.hir().res_span(def) {
364355
let label = match (unit_variant, inner_callee_path) {
365356
(Some(path), _) => format!("`{}` defined here", path),
366357
(_, Some(hir::QPath::Resolved(_, path))) => format!(

src/test/ui/consts/const-pattern-irrefutable.stderr

+21-3
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,38 @@
11
error[E0005]: refutable pattern in local binding: `0u8..=1u8` and `3u8..=std::u8::MAX` not covered
22
--> $DIR/const-pattern-irrefutable.rs:12:9
33
|
4+
LL | const a: u8 = 2;
5+
| ---------------- constant defined here
6+
...
47
LL | let a = 4;
5-
| ^ interpreted as a constant pattern, not new variable
8+
| ^
9+
| |
10+
| interpreted as a constant pattern, not a new variable
11+
| help: introduce a variable instead: `a_var`
612

713
error[E0005]: refutable pattern in local binding: `0u8..=1u8` and `3u8..=std::u8::MAX` not covered
814
--> $DIR/const-pattern-irrefutable.rs:13:9
915
|
16+
LL | pub const b: u8 = 2;
17+
| -------------------- constant defined here
18+
...
1019
LL | let c = 4;
11-
| ^ interpreted as a constant pattern, not new variable
20+
| ^
21+
| |
22+
| interpreted as a constant pattern, not a new variable
23+
| help: introduce a variable instead: `c_var`
1224

1325
error[E0005]: refutable pattern in local binding: `0u8..=1u8` and `3u8..=std::u8::MAX` not covered
1426
--> $DIR/const-pattern-irrefutable.rs:14:9
1527
|
28+
LL | pub const d: u8 = 2;
29+
| -------------------- constant defined here
30+
...
1631
LL | let d = 4;
17-
| ^ interpreted as a constant pattern, not new variable
32+
| ^
33+
| |
34+
| interpreted as a constant pattern, not a new variable
35+
| help: introduce a variable instead: `d_var`
1836

1937
error: aborting due to 3 previous errors
2038

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
fn main() {
2+
let A = 3;
3+
//~^ ERROR refutable pattern in local binding: `std::i32::MIN..=1i32` and
4+
//~| interpreted as a constant pattern, not a new variable
5+
//~| HELP introduce a variable instead
6+
//~| SUGGESTION a_var
7+
8+
const A: i32 = 2;
9+
//~^ constant defined here
10+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
error[E0005]: refutable pattern in local binding: `std::i32::MIN..=1i32` and `3i32..=std::i32::MAX` not covered
2+
--> $DIR/const-pat-non-exaustive-let-new-var.rs:2:9
3+
|
4+
LL | let A = 3;
5+
| ^
6+
| |
7+
| interpreted as a constant pattern, not a new variable
8+
| help: introduce a variable instead: `a_var`
9+
...
10+
LL | const A: i32 = 2;
11+
| ----------------- constant defined here
12+
13+
error: aborting due to previous error
14+
15+
For more information about this error, try `rustc --explain E0005`.

0 commit comments

Comments
 (0)