Skip to content

Commit 6d2a803

Browse files
committed
Improve "covered_by_many" error
1 parent 64ac2b8 commit 6d2a803

12 files changed

+171
-47
lines changed

Diff for: compiler/rustc_mir_build/messages.ftl

+5-1
Original file line numberDiff line numberDiff line change
@@ -325,12 +325,16 @@ mir_build_union_field_requires_unsafe_unsafe_op_in_unsafe_fn_allowed =
325325
326326
mir_build_union_pattern = cannot use unions in constant patterns
327327
328+
mir_build_unreachable_making_this_unreachable = collectively making this unreachable
329+
330+
mir_build_unreachable_matches_same_values = matches some of the same values
331+
328332
mir_build_unreachable_pattern = unreachable pattern
329333
.label = unreachable pattern
330334
.unreachable_matches_no_values = this pattern matches no values because `{$ty}` is uninhabited
331335
.unreachable_covered_by_catchall = matches any value
332336
.unreachable_covered_by_one = matches all the values already
333-
.unreachable_covered_by_many = matches some of the same values
337+
.unreachable_covered_by_many = these patterns collectively make the last one unreachable
334338
335339
mir_build_unsafe_fn_safe_body = an unsafe function restricts its caller, but its body is safe by default
336340
mir_build_unsafe_not_inherited = items do not inherit unsafety from separate enclosing items

Diff for: compiler/rustc_mir_build/src/errors.rs

+2-16
Original file line numberDiff line numberDiff line change
@@ -591,8 +591,8 @@ pub(crate) struct UnreachablePattern<'tcx> {
591591
pub(crate) covered_by_catchall: Option<Span>,
592592
#[label(mir_build_unreachable_covered_by_one)]
593593
pub(crate) covered_by_one: Option<Span>,
594-
#[subdiagnostic]
595-
pub(crate) covered_by_many: Option<UnreachableCoveredByMany>,
594+
#[note(mir_build_unreachable_covered_by_many)]
595+
pub(crate) covered_by_many: Option<MultiSpan>,
596596
}
597597

598598
#[derive(Subdiagnostic)]
@@ -601,20 +601,6 @@ pub(crate) struct UnreachableMatchesNoValues<'tcx> {
601601
pub(crate) ty: Ty<'tcx>,
602602
}
603603

604-
pub(crate) struct UnreachableCoveredByMany(pub(crate) Vec<Span>);
605-
606-
impl Subdiagnostic for UnreachableCoveredByMany {
607-
fn add_to_diag_with<G: EmissionGuarantee, F: SubdiagMessageOp<G>>(
608-
self,
609-
diag: &mut Diag<'_, G>,
610-
_f: &F,
611-
) {
612-
for span in self.0 {
613-
diag.span_label(span, fluent::mir_build_unreachable_covered_by_many);
614-
}
615-
}
616-
}
617-
618604
#[derive(Diagnostic)]
619605
#[diag(mir_build_const_pattern_depends_on_generic_parameter, code = E0158)]
620606
pub(crate) struct ConstPatternDependsOnGenericParameter {

Diff for: compiler/rustc_mir_build/src/thir/pattern/check_match.rs

+11-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use crate::errors::*;
2+
use crate::fluent_generated as fluent;
23

34
use rustc_arena::{DroplessArena, TypedArena};
45
use rustc_ast::Mutability;
@@ -944,8 +945,16 @@ fn report_unreachable_pattern<'p, 'tcx>(
944945
lint.covered_by_one = Some(covering_pat.data().span);
945946
}
946947
covering_pats => {
947-
let covering_spans = covering_pats.iter().map(|p| p.data().span).collect();
948-
lint.covered_by_many = Some(UnreachableCoveredByMany(covering_spans));
948+
let mut multispan = MultiSpan::from_span(pat_span);
949+
for p in covering_pats {
950+
multispan.push_span_label(
951+
p.data().span,
952+
fluent::mir_build_unreachable_matches_same_values,
953+
);
954+
}
955+
multispan
956+
.push_span_label(pat_span, fluent::mir_build_unreachable_making_this_unreachable);
957+
lint.covered_by_many = Some(multispan);
949958
}
950959
}
951960
cx.tcx.emit_node_span_lint(UNREACHABLE_PATTERNS, hir_id, pat_span, lint);

Diff for: tests/ui/or-patterns/exhaustiveness-unreachable-pattern.stderr

+51-7
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,18 @@ LL | (2,) => {}
2323
error: unreachable pattern
2424
--> $DIR/exhaustiveness-unreachable-pattern.rs:19:9
2525
|
26+
LL | (1 | 2,) => {}
27+
| ^^^^^^^^ unreachable pattern
28+
|
29+
note: these patterns collectively make the last one unreachable
30+
--> $DIR/exhaustiveness-unreachable-pattern.rs:19:9
31+
|
2632
LL | (1,) => {}
2733
| ---- matches some of the same values
2834
LL | (2,) => {}
2935
| ---- matches some of the same values
3036
LL | (1 | 2,) => {}
31-
| ^^^^^^^^ unreachable pattern
37+
| ^^^^^^^^ collectively making this unreachable
3238

3339
error: unreachable pattern
3440
--> $DIR/exhaustiveness-unreachable-pattern.rs:24:9
@@ -68,13 +74,19 @@ LL | (2 | 1, 4) => {}
6874
error: unreachable pattern
6975
--> $DIR/exhaustiveness-unreachable-pattern.rs:29:9
7076
|
77+
LL | (1, 4 | 5) => {}
78+
| ^^^^^^^^^^ unreachable pattern
79+
|
80+
note: these patterns collectively make the last one unreachable
81+
--> $DIR/exhaustiveness-unreachable-pattern.rs:29:9
82+
|
7183
LL | (1 | 2, 3 | 4) => {}
7284
| -------------- matches some of the same values
7385
...
7486
LL | (1, 5 | 6) => {}
7587
| ---------- matches some of the same values
7688
LL | (1, 4 | 5) => {}
77-
| ^^^^^^^^^^ unreachable pattern
89+
| ^^^^^^^^^^ collectively making this unreachable
7890

7991
error: unreachable pattern
8092
--> $DIR/exhaustiveness-unreachable-pattern.rs:34:13
@@ -177,10 +189,16 @@ LL | (true, 0 | 0) => {}
177189
error: unreachable pattern
178190
--> $DIR/exhaustiveness-unreachable-pattern.rs:84:17
179191
|
192+
LL | (_, 0 | 0) => {}
193+
| ^ unreachable pattern
194+
|
195+
note: these patterns collectively make the last one unreachable
196+
--> $DIR/exhaustiveness-unreachable-pattern.rs:84:17
197+
|
180198
LL | (true, 0 | 0) => {}
181199
| - matches some of the same values
182200
LL | (_, 0 | 0) => {}
183-
| - ^ unreachable pattern
201+
| - ^ collectively making this unreachable
184202
| |
185203
| matches some of the same values
186204

@@ -203,26 +221,40 @@ LL | [true
203221
error: unreachable pattern
204222
--> $DIR/exhaustiveness-unreachable-pattern.rs:111:36
205223
|
224+
LL | (true | false, None | Some(true
225+
| ^^^^ unreachable pattern
226+
|
227+
note: these patterns collectively make the last one unreachable
228+
--> $DIR/exhaustiveness-unreachable-pattern.rs:111:36
229+
|
206230
LL | (true, Some(_)) => {}
207231
| - matches some of the same values
208232
LL | (false, Some(true)) => {}
209233
| ---- matches some of the same values
210234
LL | (true | false, None | Some(true
211-
| ^^^^ unreachable pattern
235+
| ^^^^ collectively making this unreachable
212236

213237
error: unreachable pattern
214238
--> $DIR/exhaustiveness-unreachable-pattern.rs:116:14
215239
|
216240
LL | (true
217241
| ^^^^ unreachable pattern
218242
...
243+
LL | (true | false, None | Some(t_or_f!())) => {}
244+
| --------- in this macro invocation
245+
|
246+
note: these patterns collectively make the last one unreachable
247+
--> $DIR/exhaustiveness-unreachable-pattern.rs:116:14
248+
|
249+
LL | (true
250+
| ^^^^ collectively making this unreachable
251+
...
219252
LL | (true, Some(_)) => {}
220253
| - matches some of the same values
221254
LL | (false, Some(true)) => {}
222255
| ---- matches some of the same values
223256
LL | (true | false, None | Some(t_or_f!())) => {}
224257
| --------- in this macro invocation
225-
|
226258
= note: this error originates in the macro `t_or_f` (in Nightly builds, run with -Z macro-backtrace for more info)
227259

228260
error: unreachable pattern
@@ -245,24 +277,36 @@ LL | | false) => {}
245277
error: unreachable pattern
246278
--> $DIR/exhaustiveness-unreachable-pattern.rs:154:15
247279
|
280+
LL | | true) => {}
281+
| ^^^^ unreachable pattern
282+
|
283+
note: these patterns collectively make the last one unreachable
284+
--> $DIR/exhaustiveness-unreachable-pattern.rs:154:15
285+
|
248286
LL | (false, true) => {}
249287
| ---- matches some of the same values
250288
LL | (true, true) => {}
251289
| ---- matches some of the same values
252290
LL | (false | true, false
253291
LL | | true) => {}
254-
| ^^^^ unreachable pattern
292+
| ^^^^ collectively making this unreachable
255293

256294
error: unreachable pattern
257295
--> $DIR/exhaustiveness-unreachable-pattern.rs:160:15
258296
|
297+
LL | | true,
298+
| ^^^^ unreachable pattern
299+
|
300+
note: these patterns collectively make the last one unreachable
301+
--> $DIR/exhaustiveness-unreachable-pattern.rs:160:15
302+
|
259303
LL | (true, false) => {}
260304
| ---- matches some of the same values
261305
LL | (true, true) => {}
262306
| ---- matches some of the same values
263307
LL | (false
264308
LL | | true,
265-
| ^^^^ unreachable pattern
309+
| ^^^^ collectively making this unreachable
266310

267311
error: unreachable pattern
268312
--> $DIR/exhaustiveness-unreachable-pattern.rs:165:15

Diff for: tests/ui/pattern/usefulness/consts-opaque.stderr

+7-1
Original file line numberDiff line numberDiff line change
@@ -122,12 +122,18 @@ LL | BAZ => {}
122122
error: unreachable pattern
123123
--> $DIR/consts-opaque.rs:87:9
124124
|
125+
LL | _ => {} // should not be emitting unreachable warning
126+
| ^ unreachable pattern
127+
|
128+
note: these patterns collectively make the last one unreachable
129+
--> $DIR/consts-opaque.rs:87:9
130+
|
125131
LL | BAZ => {}
126132
| --- matches some of the same values
127133
LL | Baz::Baz2 => {}
128134
| --------- matches some of the same values
129135
LL | _ => {} // should not be emitting unreachable warning
130-
| ^ unreachable pattern
136+
| ^ collectively making this unreachable
131137

132138
error: aborting due to 17 previous errors
133139

Diff for: tests/ui/pattern/usefulness/explain-unreachable-pats.rs

+4
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ fn main() {
2222
(1 | 2,) => {}
2323
//~^ ERROR unreachable pattern
2424
//~| NOTE unreachable pattern
25+
//~| NOTE these patterns collectively make the last one unreachable
26+
//~| NOTE collectively making this unreachable
2527
_ => {}
2628
}
2729

@@ -69,6 +71,8 @@ fn main() {
6971
(_, true) => {}
7072
//~^ ERROR unreachable pattern
7173
//~| NOTE unreachable pattern
74+
//~| NOTE these patterns collectively make the last one unreachable
75+
//~| NOTE collectively making this unreachable
7276
}
7377

7478
match (true, true) {

Diff for: tests/ui/pattern/usefulness/explain-unreachable-pats.stderr

+21-9
Original file line numberDiff line numberDiff line change
@@ -16,41 +16,47 @@ LL | #![deny(unreachable_patterns)]
1616
error: unreachable pattern
1717
--> $DIR/explain-unreachable-pats.rs:22:9
1818
|
19+
LL | (1 | 2,) => {}
20+
| ^^^^^^^^ unreachable pattern
21+
|
22+
note: these patterns collectively make the last one unreachable
23+
--> $DIR/explain-unreachable-pats.rs:22:9
24+
|
1925
LL | (1,) => {}
2026
| ---- matches some of the same values
2127
LL |
2228
LL | (2,) => {}
2329
| ---- matches some of the same values
2430
LL |
2531
LL | (1 | 2,) => {}
26-
| ^^^^^^^^ unreachable pattern
32+
| ^^^^^^^^ collectively making this unreachable
2733

2834
error: unreachable pattern
29-
--> $DIR/explain-unreachable-pats.rs:31:9
35+
--> $DIR/explain-unreachable-pats.rs:33:9
3036
|
3137
LL | Err(_) => {}
3238
| ^^^^^^
3339
|
3440
= note: this pattern matches no values because `!` is uninhabited
3541

3642
error: unreachable pattern
37-
--> $DIR/explain-unreachable-pats.rs:44:9
43+
--> $DIR/explain-unreachable-pats.rs:46:9
3844
|
3945
LL | (Err(_), Err(_)) => {}
4046
| ^^^^^^^^^^^^^^^^
4147
|
4248
= note: this pattern matches no values because `Void2` is uninhabited
4349

4450
error: unreachable pattern
45-
--> $DIR/explain-unreachable-pats.rs:50:9
51+
--> $DIR/explain-unreachable-pats.rs:52:9
4652
|
4753
LL | (Err(_), Err(_)) => {}
4854
| ^^^^^^^^^^^^^^^^
4955
|
5056
= note: this pattern matches no values because `Void1` is uninhabited
5157

5258
error: unreachable pattern
53-
--> $DIR/explain-unreachable-pats.rs:59:11
59+
--> $DIR/explain-unreachable-pats.rs:61:11
5460
|
5561
LL | if let (0
5662
| - matches all the values already
@@ -59,7 +65,13 @@ LL | | 0, _) = (0, 0) {}
5965
| ^ unreachable pattern
6066

6167
error: unreachable pattern
62-
--> $DIR/explain-unreachable-pats.rs:69:9
68+
--> $DIR/explain-unreachable-pats.rs:71:9
69+
|
70+
LL | (_, true) => {}
71+
| ^^^^^^^^^ unreachable pattern
72+
|
73+
note: these patterns collectively make the last one unreachable
74+
--> $DIR/explain-unreachable-pats.rs:71:9
6375
|
6476
LL | (true, _) => {}
6577
| --------- matches some of the same values
@@ -68,10 +80,10 @@ LL | (false, _) => {}
6880
| ---------- matches some of the same values
6981
LL |
7082
LL | (_, true) => {}
71-
| ^^^^^^^^^ unreachable pattern
83+
| ^^^^^^^^^ collectively making this unreachable
7284

7385
error: unreachable pattern
74-
--> $DIR/explain-unreachable-pats.rs:80:9
86+
--> $DIR/explain-unreachable-pats.rs:84:9
7587
|
7688
LL | (true, _) => {}
7789
| --------- matches all the values already
@@ -80,7 +92,7 @@ LL | (true, true) => {}
8092
| ^^^^^^^^^^^^ unreachable pattern
8193

8294
error: unreachable pattern
83-
--> $DIR/explain-unreachable-pats.rs:92:9
95+
--> $DIR/explain-unreachable-pats.rs:96:9
8496
|
8597
LL | (_, true, 0..10) => {}
8698
| ---------------- matches all the values already

0 commit comments

Comments
 (0)