Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 9dd390f

Browse files
committedDec 16, 2021
Point at uncovered variants in enum definition in note instead of a span_label
This makes the order of the output always consistent: 1. Place of the `match` missing arms 2. The `enum` definition span 3. The structured suggestion to add a fallthrough arm
1 parent 37cd037 commit 9dd390f

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

45 files changed

+1033
-696
lines changed
 

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

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ use rustc_session::lint::builtin::{
1717
BINDINGS_WITH_VARIANT_NAME, IRREFUTABLE_LET_PATTERNS, UNREACHABLE_PATTERNS,
1818
};
1919
use rustc_session::Session;
20-
use rustc_span::{DesugaringKind, ExpnKind, Span};
20+
use rustc_span::{DesugaringKind, ExpnKind, MultiSpan, Span};
2121

2222
crate fn check_match(tcx: TyCtxt<'_>, def_id: DefId) {
2323
let body_id = match def_id.as_local() {
@@ -671,15 +671,27 @@ fn adt_defined_here<'p, 'tcx>(
671671
) {
672672
let ty = ty.peel_refs();
673673
if let ty::Adt(def, _) = ty.kind() {
674-
if let Some(sp) = cx.tcx.hir().span_if_local(def.did) {
675-
err.span_label(sp, format!("`{}` defined here", ty));
676-
}
677-
678-
if witnesses.len() < 4 {
674+
let mut spans = vec![];
675+
if witnesses.len() < 5 {
679676
for sp in maybe_point_at_variant(cx, def, witnesses.iter()) {
680-
err.span_label(sp, "not covered");
677+
spans.push(sp);
681678
}
682679
}
680+
let def_span = cx
681+
.tcx
682+
.hir()
683+
.get_if_local(def.did)
684+
.and_then(|node| node.ident())
685+
.map(|ident| ident.span)
686+
.unwrap_or_else(|| cx.tcx.def_span(def.did));
687+
let mut span: MultiSpan =
688+
if spans.is_empty() { def_span.into() } else { spans.clone().into() };
689+
690+
span.push_span_label(def_span, String::new());
691+
for pat in spans {
692+
span.push_span_label(pat, "not covered".to_string());
693+
}
694+
err.span_note(span, &format!("`{}` defined here", ty));
683695
}
684696
}
685697

‎src/test/ui/closures/2229_closure_analysis/match/issue-88331.stderr

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
error[E0004]: non-exhaustive patterns: `Opcode(0_u8)` and `Opcode(2_u8..=u8::MAX)` not covered
22
--> $DIR/issue-88331.rs:11:20
33
|
4-
LL | pub struct Opcode(pub u8);
5-
| -------------------------- `Opcode` defined here
6-
...
74
LL | move |i| match msg_type {
85
| ^^^^^^^^ patterns `Opcode(0_u8)` and `Opcode(2_u8..=u8::MAX)` not covered
96
|
7+
note: `Opcode` defined here
8+
--> $DIR/issue-88331.rs:4:12
9+
|
10+
LL | pub struct Opcode(pub u8);
11+
| ^^^^^^
1012
= note: the matched value is of type `Opcode`
1113
help: ensure that all possible cases are being handled, possibly by adding wildcards or more match arms
1214
|
@@ -17,12 +19,14 @@ LL ~ Opcode(0_u8) | Opcode(2_u8..=u8::MAX) => todo!(),
1719
error[E0004]: non-exhaustive patterns: `Opcode2(Opcode(0_u8))` and `Opcode2(Opcode(2_u8..=u8::MAX))` not covered
1820
--> $DIR/issue-88331.rs:27:20
1921
|
20-
LL | pub struct Opcode2(Opcode);
21-
| --------------------------- `Opcode2` defined here
22-
...
2322
LL | move |i| match msg_type {
2423
| ^^^^^^^^ patterns `Opcode2(Opcode(0_u8))` and `Opcode2(Opcode(2_u8..=u8::MAX))` not covered
2524
|
25+
note: `Opcode2` defined here
26+
--> $DIR/issue-88331.rs:18:12
27+
|
28+
LL | pub struct Opcode2(Opcode);
29+
| ^^^^^^^
2630
= note: the matched value is of type `Opcode2`
2731
help: ensure that all possible cases are being handled, possibly by adding wildcards or more match arms
2832
|

0 commit comments

Comments
 (0)
Please sign in to comment.