Skip to content

Commit

Permalink
Improve "covered_by_many" error
Browse files Browse the repository at this point in the history
  • Loading branch information
Nadrieril committed Jul 24, 2024
1 parent 64ac2b8 commit 940769a
Show file tree
Hide file tree
Showing 13 changed files with 178 additions and 49 deletions.
6 changes: 5 additions & 1 deletion compiler/rustc_mir_build/messages.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -325,12 +325,16 @@ mir_build_union_field_requires_unsafe_unsafe_op_in_unsafe_fn_allowed =
mir_build_union_pattern = cannot use unions in constant patterns
mir_build_unreachable_making_this_unreachable = collectively making this unreachable
mir_build_unreachable_matches_same_values = matches some of the same values
mir_build_unreachable_pattern = unreachable pattern
.label = unreachable pattern
.unreachable_matches_no_values = this pattern matches no values because `{$ty}` is uninhabited
.unreachable_covered_by_catchall = matches any value
.unreachable_covered_by_one = matches all the values already
.unreachable_covered_by_many = matches some of the same values
.unreachable_covered_by_many = these patterns collectively make the last one unreachable
mir_build_unsafe_fn_safe_body = an unsafe function restricts its caller, but its body is safe by default
mir_build_unsafe_not_inherited = items do not inherit unsafety from separate enclosing items
Expand Down
18 changes: 2 additions & 16 deletions compiler/rustc_mir_build/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -591,8 +591,8 @@ pub(crate) struct UnreachablePattern<'tcx> {
pub(crate) covered_by_catchall: Option<Span>,
#[label(mir_build_unreachable_covered_by_one)]
pub(crate) covered_by_one: Option<Span>,
#[subdiagnostic]
pub(crate) covered_by_many: Option<UnreachableCoveredByMany>,
#[note(mir_build_unreachable_covered_by_many)]
pub(crate) covered_by_many: Option<MultiSpan>,
}

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

pub(crate) struct UnreachableCoveredByMany(pub(crate) Vec<Span>);

impl Subdiagnostic for UnreachableCoveredByMany {
fn add_to_diag_with<G: EmissionGuarantee, F: SubdiagMessageOp<G>>(
self,
diag: &mut Diag<'_, G>,
_f: &F,
) {
for span in self.0 {
diag.span_label(span, fluent::mir_build_unreachable_covered_by_many);
}
}
}

#[derive(Diagnostic)]
#[diag(mir_build_const_pattern_depends_on_generic_parameter, code = E0158)]
pub(crate) struct ConstPatternDependsOnGenericParameter {
Expand Down
13 changes: 11 additions & 2 deletions compiler/rustc_mir_build/src/thir/pattern/check_match.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::errors::*;
use crate::fluent_generated as fluent;

use rustc_arena::{DroplessArena, TypedArena};
use rustc_ast::Mutability;
Expand Down Expand Up @@ -944,8 +945,16 @@ fn report_unreachable_pattern<'p, 'tcx>(
lint.covered_by_one = Some(covering_pat.data().span);
}
covering_pats => {
let covering_spans = covering_pats.iter().map(|p| p.data().span).collect();
lint.covered_by_many = Some(UnreachableCoveredByMany(covering_spans));
let mut multispan = MultiSpan::from_span(pat_span);
for p in covering_pats {
multispan.push_span_label(
p.data().span,
fluent::mir_build_unreachable_matches_same_values,
);
}
multispan
.push_span_label(pat_span, fluent::mir_build_unreachable_making_this_unreachable);
lint.covered_by_many = Some(multispan);
}
}
cx.tcx.emit_node_span_lint(UNREACHABLE_PATTERNS, hir_id, pat_span, lint);
Expand Down
9 changes: 7 additions & 2 deletions tests/ui/error-codes/E0001.stderr
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
error: unreachable pattern
--> $DIR/E0001.rs:8:9
|
LL | _ => {/* ... */}
| ^ unreachable pattern
|
note: these patterns collectively make the last one unreachable
--> $DIR/E0001.rs:8:9
|
LL | Some(_) => {/* ... */}
| ------- matches some of the same values
LL | None => {/* ... */}
| ---- matches some of the same values
LL | _ => {/* ... */}
| ^ unreachable pattern
|
| ^ collectively making this unreachable
note: the lint level is defined here
--> $DIR/E0001.rs:1:9
|
Expand Down
58 changes: 51 additions & 7 deletions tests/ui/or-patterns/exhaustiveness-unreachable-pattern.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,18 @@ LL | (2,) => {}
error: unreachable pattern
--> $DIR/exhaustiveness-unreachable-pattern.rs:19:9
|
LL | (1 | 2,) => {}
| ^^^^^^^^ unreachable pattern
|
note: these patterns collectively make the last one unreachable
--> $DIR/exhaustiveness-unreachable-pattern.rs:19:9
|
LL | (1,) => {}
| ---- matches some of the same values
LL | (2,) => {}
| ---- matches some of the same values
LL | (1 | 2,) => {}
| ^^^^^^^^ unreachable pattern
| ^^^^^^^^ collectively making this unreachable

error: unreachable pattern
--> $DIR/exhaustiveness-unreachable-pattern.rs:24:9
Expand Down Expand Up @@ -68,13 +74,19 @@ LL | (2 | 1, 4) => {}
error: unreachable pattern
--> $DIR/exhaustiveness-unreachable-pattern.rs:29:9
|
LL | (1, 4 | 5) => {}
| ^^^^^^^^^^ unreachable pattern
|
note: these patterns collectively make the last one unreachable
--> $DIR/exhaustiveness-unreachable-pattern.rs:29:9
|
LL | (1 | 2, 3 | 4) => {}
| -------------- matches some of the same values
...
LL | (1, 5 | 6) => {}
| ---------- matches some of the same values
LL | (1, 4 | 5) => {}
| ^^^^^^^^^^ unreachable pattern
| ^^^^^^^^^^ collectively making this unreachable

error: unreachable pattern
--> $DIR/exhaustiveness-unreachable-pattern.rs:34:13
Expand Down Expand Up @@ -177,10 +189,16 @@ LL | (true, 0 | 0) => {}
error: unreachable pattern
--> $DIR/exhaustiveness-unreachable-pattern.rs:84:17
|
LL | (_, 0 | 0) => {}
| ^ unreachable pattern
|
note: these patterns collectively make the last one unreachable
--> $DIR/exhaustiveness-unreachable-pattern.rs:84:17
|
LL | (true, 0 | 0) => {}
| - matches some of the same values
LL | (_, 0 | 0) => {}
| - ^ unreachable pattern
| - ^ collectively making this unreachable
| |
| matches some of the same values

Expand All @@ -203,26 +221,40 @@ LL | [true
error: unreachable pattern
--> $DIR/exhaustiveness-unreachable-pattern.rs:111:36
|
LL | (true | false, None | Some(true
| ^^^^ unreachable pattern
|
note: these patterns collectively make the last one unreachable
--> $DIR/exhaustiveness-unreachable-pattern.rs:111:36
|
LL | (true, Some(_)) => {}
| - matches some of the same values
LL | (false, Some(true)) => {}
| ---- matches some of the same values
LL | (true | false, None | Some(true
| ^^^^ unreachable pattern
| ^^^^ collectively making this unreachable

error: unreachable pattern
--> $DIR/exhaustiveness-unreachable-pattern.rs:116:14
|
LL | (true
| ^^^^ unreachable pattern
...
LL | (true | false, None | Some(t_or_f!())) => {}
| --------- in this macro invocation
|
note: these patterns collectively make the last one unreachable
--> $DIR/exhaustiveness-unreachable-pattern.rs:116:14
|
LL | (true
| ^^^^ collectively making this unreachable
...
LL | (true, Some(_)) => {}
| - matches some of the same values
LL | (false, Some(true)) => {}
| ---- matches some of the same values
LL | (true | false, None | Some(t_or_f!())) => {}
| --------- in this macro invocation
|
= note: this error originates in the macro `t_or_f` (in Nightly builds, run with -Z macro-backtrace for more info)

error: unreachable pattern
Expand All @@ -245,24 +277,36 @@ LL | | false) => {}
error: unreachable pattern
--> $DIR/exhaustiveness-unreachable-pattern.rs:154:15
|
LL | | true) => {}
| ^^^^ unreachable pattern
|
note: these patterns collectively make the last one unreachable
--> $DIR/exhaustiveness-unreachable-pattern.rs:154:15
|
LL | (false, true) => {}
| ---- matches some of the same values
LL | (true, true) => {}
| ---- matches some of the same values
LL | (false | true, false
LL | | true) => {}
| ^^^^ unreachable pattern
| ^^^^ collectively making this unreachable

error: unreachable pattern
--> $DIR/exhaustiveness-unreachable-pattern.rs:160:15
|
LL | | true,
| ^^^^ unreachable pattern
|
note: these patterns collectively make the last one unreachable
--> $DIR/exhaustiveness-unreachable-pattern.rs:160:15
|
LL | (true, false) => {}
| ---- matches some of the same values
LL | (true, true) => {}
| ---- matches some of the same values
LL | (false
LL | | true,
| ^^^^ unreachable pattern
| ^^^^ collectively making this unreachable

error: unreachable pattern
--> $DIR/exhaustiveness-unreachable-pattern.rs:165:15
Expand Down
8 changes: 7 additions & 1 deletion tests/ui/pattern/usefulness/consts-opaque.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -122,12 +122,18 @@ LL | BAZ => {}
error: unreachable pattern
--> $DIR/consts-opaque.rs:87:9
|
LL | _ => {} // should not be emitting unreachable warning
| ^ unreachable pattern
|
note: these patterns collectively make the last one unreachable
--> $DIR/consts-opaque.rs:87:9
|
LL | BAZ => {}
| --- matches some of the same values
LL | Baz::Baz2 => {}
| --------- matches some of the same values
LL | _ => {} // should not be emitting unreachable warning
| ^ unreachable pattern
| ^ collectively making this unreachable

error: aborting due to 17 previous errors

4 changes: 4 additions & 0 deletions tests/ui/pattern/usefulness/explain-unreachable-pats.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ fn main() {
(1 | 2,) => {}
//~^ ERROR unreachable pattern
//~| NOTE unreachable pattern
//~| NOTE these patterns collectively make the last one unreachable
//~| NOTE collectively making this unreachable
_ => {}
}

Expand Down Expand Up @@ -69,6 +71,8 @@ fn main() {
(_, true) => {}
//~^ ERROR unreachable pattern
//~| NOTE unreachable pattern
//~| NOTE these patterns collectively make the last one unreachable
//~| NOTE collectively making this unreachable
}

match (true, true) {
Expand Down
30 changes: 21 additions & 9 deletions tests/ui/pattern/usefulness/explain-unreachable-pats.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -16,41 +16,47 @@ LL | #![deny(unreachable_patterns)]
error: unreachable pattern
--> $DIR/explain-unreachable-pats.rs:22:9
|
LL | (1 | 2,) => {}
| ^^^^^^^^ unreachable pattern
|
note: these patterns collectively make the last one unreachable
--> $DIR/explain-unreachable-pats.rs:22:9
|
LL | (1,) => {}
| ---- matches some of the same values
LL |
LL | (2,) => {}
| ---- matches some of the same values
LL |
LL | (1 | 2,) => {}
| ^^^^^^^^ unreachable pattern
| ^^^^^^^^ collectively making this unreachable

error: unreachable pattern
--> $DIR/explain-unreachable-pats.rs:31:9
--> $DIR/explain-unreachable-pats.rs:33:9
|
LL | Err(_) => {}
| ^^^^^^
|
= note: this pattern matches no values because `!` is uninhabited

error: unreachable pattern
--> $DIR/explain-unreachable-pats.rs:44:9
--> $DIR/explain-unreachable-pats.rs:46:9
|
LL | (Err(_), Err(_)) => {}
| ^^^^^^^^^^^^^^^^
|
= note: this pattern matches no values because `Void2` is uninhabited

error: unreachable pattern
--> $DIR/explain-unreachable-pats.rs:50:9
--> $DIR/explain-unreachable-pats.rs:52:9
|
LL | (Err(_), Err(_)) => {}
| ^^^^^^^^^^^^^^^^
|
= note: this pattern matches no values because `Void1` is uninhabited

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

error: unreachable pattern
--> $DIR/explain-unreachable-pats.rs:69:9
--> $DIR/explain-unreachable-pats.rs:71:9
|
LL | (_, true) => {}
| ^^^^^^^^^ unreachable pattern
|
note: these patterns collectively make the last one unreachable
--> $DIR/explain-unreachable-pats.rs:71:9
|
LL | (true, _) => {}
| --------- matches some of the same values
Expand All @@ -68,10 +80,10 @@ LL | (false, _) => {}
| ---------- matches some of the same values
LL |
LL | (_, true) => {}
| ^^^^^^^^^ unreachable pattern
| ^^^^^^^^^ collectively making this unreachable

error: unreachable pattern
--> $DIR/explain-unreachable-pats.rs:80:9
--> $DIR/explain-unreachable-pats.rs:84:9
|
LL | (true, _) => {}
| --------- matches all the values already
Expand All @@ -80,7 +92,7 @@ LL | (true, true) => {}
| ^^^^^^^^^^^^ unreachable pattern

error: unreachable pattern
--> $DIR/explain-unreachable-pats.rs:92:9
--> $DIR/explain-unreachable-pats.rs:96:9
|
LL | (_, true, 0..10) => {}
| ---------------- matches all the values already
Expand Down
Loading

0 comments on commit 940769a

Please sign in to comment.