diff --git a/compiler/rustc_errors/src/lib.rs b/compiler/rustc_errors/src/lib.rs index 9b3dba15f55e..8c57544c54b8 100644 --- a/compiler/rustc_errors/src/lib.rs +++ b/compiler/rustc_errors/src/lib.rs @@ -138,6 +138,14 @@ pub fn unwrap_tag(self) -> Vec { Suggestions::Disabled => Vec::new(), } } + + pub fn len(&self) -> usize { + match self { + Suggestions::Enabled(suggestions) => suggestions.len(), + Suggestions::Sealed(suggestions) => suggestions.len(), + Suggestions::Disabled => 0, + } + } } impl Default for Suggestions { diff --git a/compiler/rustc_hir_typeck/src/pat.rs b/compiler/rustc_hir_typeck/src/pat.rs index 314b50a12fc3..26f7d1ccffc9 100644 --- a/compiler/rustc_hir_typeck/src/pat.rs +++ b/compiler/rustc_hir_typeck/src/pat.rs @@ -1644,7 +1644,9 @@ fn emit_bad_pat_path( return; } }; - if let [hir::PathSegment { ident, args: None, .. }] = segments { + if let [hir::PathSegment { ident, args: None, .. }] = segments + && e.suggestions.len() == 0 + { e.span_label(span, format!("{} defined here", res.descr())); e.span_label( pat_span, diff --git a/tests/ui/did_you_mean/compatible-variants-in-pat.rs b/tests/ui/did_you_mean/compatible-variants-in-pat.rs index 09e12dab2d3f..5633c28be208 100644 --- a/tests/ui/did_you_mean/compatible-variants-in-pat.rs +++ b/tests/ui/did_you_mean/compatible-variants-in-pat.rs @@ -21,7 +21,6 @@ fn b(s: Option) { S => { //~^ ERROR mismatched types //~| HELP try wrapping - //~| HELP introduce a new binding instead } _ => {} } @@ -32,7 +31,6 @@ fn c(s: Result) { S => { //~^ ERROR mismatched types //~| HELP try wrapping - //~| HELP introduce a new binding instead } _ => {} } diff --git a/tests/ui/did_you_mean/compatible-variants-in-pat.stderr b/tests/ui/did_you_mean/compatible-variants-in-pat.stderr index 09cf094e6bd7..f18965f5d080 100644 --- a/tests/ui/did_you_mean/compatible-variants-in-pat.stderr +++ b/tests/ui/did_you_mean/compatible-variants-in-pat.stderr @@ -14,16 +14,10 @@ LL | Foo::Bar(Bar { x }) => { error[E0308]: mismatched types --> $DIR/compatible-variants-in-pat.rs:21:9 | -LL | struct S; - | -------- unit struct defined here -... LL | match s { | - this expression has type `Option` LL | S => { - | ^ - | | - | expected `Option`, found `S` - | `S` is interpreted as a unit struct, not a new binding + | ^ expected `Option`, found `S` | = note: expected enum `Option` found struct `S` @@ -31,25 +25,14 @@ help: try wrapping the pattern in `Some` | LL | Some(S) => { | +++++ + -help: introduce a new binding instead - | -LL - S => { -LL + other_s => { - | error[E0308]: mismatched types - --> $DIR/compatible-variants-in-pat.rs:32:9 + --> $DIR/compatible-variants-in-pat.rs:31:9 | -LL | struct S; - | -------- unit struct defined here -... LL | match s { | - this expression has type `Result` LL | S => { - | ^ - | | - | expected `Result`, found `S` - | `S` is interpreted as a unit struct, not a new binding + | ^ expected `Result`, found `S` | = note: expected enum `Result` found struct `S` @@ -59,11 +42,6 @@ LL | Ok(S) => { | +++ + LL | Err(S) => { | ++++ + -help: introduce a new binding instead - | -LL - S => { -LL + other_s => { - | error: aborting due to 3 previous errors diff --git a/tests/ui/match/issue-12552.stderr b/tests/ui/match/issue-12552.stderr index 397265cd540f..195192fbd824 100644 --- a/tests/ui/match/issue-12552.stderr +++ b/tests/ui/match/issue-12552.stderr @@ -20,14 +20,7 @@ LL | match t { | - this expression has type `Result<_, {integer}>` ... LL | None => () - | ^^^^ - | | - | expected `Result<_, {integer}>`, found `Option<_>` - | `None` is interpreted as a unit variant, not a new binding - | - --> $SRC_DIR/core/src/option.rs:LL:COL - | - = note: unit variant defined here + | ^^^^ expected `Result<_, {integer}>`, found `Option<_>` | = note: expected enum `Result<_, {integer}>` found enum `Option<_>` @@ -35,11 +28,6 @@ help: try wrapping the pattern in `Ok` | LL | Ok(None) => () | +++ + -help: introduce a new binding instead - | -LL - None => () -LL + other_none => () - | error: aborting due to 2 previous errors diff --git a/tests/ui/suggestions/suggest-deref-in-match-issue-132784.rs b/tests/ui/suggestions/suggest-deref-in-match-issue-132784.rs index 77074e8c3a3b..205e57f4a9ff 100644 --- a/tests/ui/suggestions/suggest-deref-in-match-issue-132784.rs +++ b/tests/ui/suggestions/suggest-deref-in-match-issue-132784.rs @@ -8,7 +8,6 @@ fn main() { //~^ ERROR mismatched types None => {} //~^ ERROR mismatched types - //~| HELP introduce a new binding instead } match &x { @@ -18,7 +17,6 @@ fn main() { //~^ ERROR mismatched types None => {} //~^ ERROR mismatched types - //~| HELP introduce a new binding instead } let mut y = Box::new(Some(1)); @@ -29,7 +27,6 @@ fn main() { //~^ ERROR mismatched types None => {} //~^ ERROR mismatched types - //~| HELP introduce a new binding instead } let mut z = Arc::new(Some(1)); @@ -40,7 +37,6 @@ fn main() { //~^ ERROR mismatched types None => {} //~^ ERROR mismatched types - //~| HELP introduce a new binding instead } let z_const: &Arc> = &z; @@ -51,7 +47,6 @@ fn main() { //~^ ERROR mismatched types None => {} //~^ ERROR mismatched types - //~| HELP introduce a new binding instead } // Normal reference because Arc doesn't implement DerefMut. @@ -63,7 +58,6 @@ fn main() { //~^ ERROR mismatched types None => {} //~^ ERROR mismatched types - //~| HELP introduce a new binding instead } // Mutable reference because Box does implement DerefMut. @@ -75,7 +69,6 @@ fn main() { //~^ ERROR mismatched types None => {} //~^ ERROR mismatched types - //~| HELP introduce a new binding instead } // Difficult expression. @@ -87,6 +80,5 @@ fn main() { //~^ ERROR mismatched types None => {} //~^ ERROR mismatched types - //~| HELP introduce a new binding instead } } diff --git a/tests/ui/suggestions/suggest-deref-in-match-issue-132784.stderr b/tests/ui/suggestions/suggest-deref-in-match-issue-132784.stderr index a5ce44b25600..6092272aa8c5 100644 --- a/tests/ui/suggestions/suggest-deref-in-match-issue-132784.stderr +++ b/tests/ui/suggestions/suggest-deref-in-match-issue-132784.stderr @@ -21,14 +21,7 @@ LL | match x { | - this expression has type `Arc>` ... LL | None => {} - | ^^^^ - | | - | expected `Arc>`, found `Option<_>` - | `None` is interpreted as a unit variant, not a new binding - | - --> $SRC_DIR/core/src/option.rs:LL:COL - | - = note: unit variant defined here + | ^^^^ expected `Arc>`, found `Option<_>` | = note: expected struct `Arc>` found enum `Option<_>` @@ -36,14 +29,9 @@ help: consider dereferencing to access the inner value using the Deref trait | LL | match *x { | + -help: introduce a new binding instead - | -LL - None => {} -LL + other_none => {} - | error[E0308]: mismatched types - --> $DIR/suggest-deref-in-match-issue-132784.rs:17:9 + --> $DIR/suggest-deref-in-match-issue-132784.rs:16:9 | LL | match &x { | -- this expression has type `&Arc>` @@ -59,20 +47,13 @@ LL | match &*x { | + error[E0308]: mismatched types - --> $DIR/suggest-deref-in-match-issue-132784.rs:19:9 + --> $DIR/suggest-deref-in-match-issue-132784.rs:18:9 | LL | match &x { | -- this expression has type `&Arc>` ... LL | None => {} - | ^^^^ - | | - | expected `Arc>`, found `Option<_>` - | `None` is interpreted as a unit variant, not a new binding - | - --> $SRC_DIR/core/src/option.rs:LL:COL - | - = note: unit variant defined here + | ^^^^ expected `Arc>`, found `Option<_>` | = note: expected struct `Arc>` found enum `Option<_>` @@ -80,14 +61,9 @@ help: consider dereferencing to access the inner value using the Deref trait | LL | match &*x { | + -help: introduce a new binding instead - | -LL - None => {} -LL + other_none => {} - | error[E0308]: mismatched types - --> $DIR/suggest-deref-in-match-issue-132784.rs:28:9 + --> $DIR/suggest-deref-in-match-issue-132784.rs:26:9 | LL | match y { | - this expression has type `Box>` @@ -103,20 +79,13 @@ LL | match *y { | + error[E0308]: mismatched types - --> $DIR/suggest-deref-in-match-issue-132784.rs:30:9 + --> $DIR/suggest-deref-in-match-issue-132784.rs:28:9 | LL | match y { | - this expression has type `Box>` ... LL | None => {} - | ^^^^ - | | - | expected `Box>`, found `Option<_>` - | `None` is interpreted as a unit variant, not a new binding - | - --> $SRC_DIR/core/src/option.rs:LL:COL - | - = note: unit variant defined here + | ^^^^ expected `Box>`, found `Option<_>` | = note: expected struct `Box>` found enum `Option<_>` @@ -124,14 +93,9 @@ help: consider dereferencing to access the inner value using the Deref trait | LL | match *y { | + -help: introduce a new binding instead - | -LL - None => {} -LL + other_none => {} - | error[E0308]: mismatched types - --> $DIR/suggest-deref-in-match-issue-132784.rs:39:9 + --> $DIR/suggest-deref-in-match-issue-132784.rs:36:9 | LL | match z as Arc> { | --------------------- this expression has type `Arc>` @@ -148,20 +112,13 @@ LL + match *(z as Arc>) { | error[E0308]: mismatched types - --> $DIR/suggest-deref-in-match-issue-132784.rs:41:9 + --> $DIR/suggest-deref-in-match-issue-132784.rs:38:9 | LL | match z as Arc> { | --------------------- this expression has type `Arc>` ... LL | None => {} - | ^^^^ - | | - | expected `Arc>`, found `Option<_>` - | `None` is interpreted as a unit variant, not a new binding - | - --> $SRC_DIR/core/src/option.rs:LL:COL - | - = note: unit variant defined here + | ^^^^ expected `Arc>`, found `Option<_>` | = note: expected struct `Arc>` found enum `Option<_>` @@ -170,14 +127,9 @@ help: consider dereferencing to access the inner value using the Deref trait LL - match z as Arc> { LL + match *(z as Arc>) { | -help: introduce a new binding instead - | -LL - None => {} -LL + other_none => {} - | error[E0308]: mismatched types - --> $DIR/suggest-deref-in-match-issue-132784.rs:50:9 + --> $DIR/suggest-deref-in-match-issue-132784.rs:46:9 | LL | match z_const { | ------- this expression has type `&Arc>` @@ -193,20 +145,13 @@ LL | match &**z_const { | +++ error[E0308]: mismatched types - --> $DIR/suggest-deref-in-match-issue-132784.rs:52:9 + --> $DIR/suggest-deref-in-match-issue-132784.rs:48:9 | LL | match z_const { | ------- this expression has type `&Arc>` ... LL | None => {} - | ^^^^ - | | - | expected `Arc>`, found `Option<_>` - | `None` is interpreted as a unit variant, not a new binding - | - --> $SRC_DIR/core/src/option.rs:LL:COL - | - = note: unit variant defined here + | ^^^^ expected `Arc>`, found `Option<_>` | = note: expected struct `Arc>` found enum `Option<_>` @@ -214,14 +159,9 @@ help: consider dereferencing to access the inner value using the Deref trait | LL | match &**z_const { | +++ -help: introduce a new binding instead - | -LL - None => {} -LL + other_none => {} - | error[E0308]: mismatched types - --> $DIR/suggest-deref-in-match-issue-132784.rs:62:9 + --> $DIR/suggest-deref-in-match-issue-132784.rs:57:9 | LL | match z_mut { | ----- this expression has type `&mut Arc>` @@ -237,20 +177,13 @@ LL | match &**z_mut { | +++ error[E0308]: mismatched types - --> $DIR/suggest-deref-in-match-issue-132784.rs:64:9 + --> $DIR/suggest-deref-in-match-issue-132784.rs:59:9 | LL | match z_mut { | ----- this expression has type `&mut Arc>` ... LL | None => {} - | ^^^^ - | | - | expected `Arc>`, found `Option<_>` - | `None` is interpreted as a unit variant, not a new binding - | - --> $SRC_DIR/core/src/option.rs:LL:COL - | - = note: unit variant defined here + | ^^^^ expected `Arc>`, found `Option<_>` | = note: expected struct `Arc>` found enum `Option<_>` @@ -258,14 +191,9 @@ help: consider dereferencing to access the inner value using the Deref trait | LL | match &**z_mut { | +++ -help: introduce a new binding instead - | -LL - None => {} -LL + other_none => {} - | error[E0308]: mismatched types - --> $DIR/suggest-deref-in-match-issue-132784.rs:74:9 + --> $DIR/suggest-deref-in-match-issue-132784.rs:68:9 | LL | match y_mut { | ----- this expression has type `&mut Box>` @@ -281,20 +209,13 @@ LL | match &**y_mut { | +++ error[E0308]: mismatched types - --> $DIR/suggest-deref-in-match-issue-132784.rs:76:9 + --> $DIR/suggest-deref-in-match-issue-132784.rs:70:9 | LL | match y_mut { | ----- this expression has type `&mut Box>` ... LL | None => {} - | ^^^^ - | | - | expected `Box>`, found `Option<_>` - | `None` is interpreted as a unit variant, not a new binding - | - --> $SRC_DIR/core/src/option.rs:LL:COL - | - = note: unit variant defined here + | ^^^^ expected `Box>`, found `Option<_>` | = note: expected struct `Box>` found enum `Option<_>` @@ -302,14 +223,9 @@ help: consider dereferencing to access the inner value using the Deref trait | LL | match &**y_mut { | +++ -help: introduce a new binding instead - | -LL - None => {} -LL + other_none => {} - | error[E0308]: mismatched types - --> $DIR/suggest-deref-in-match-issue-132784.rs:86:9 + --> $DIR/suggest-deref-in-match-issue-132784.rs:79:9 | LL | match (& (&difficult) ) { | ------------------ this expression has type `&&Arc>` @@ -326,20 +242,13 @@ LL + match &*difficult { | error[E0308]: mismatched types - --> $DIR/suggest-deref-in-match-issue-132784.rs:88:9 + --> $DIR/suggest-deref-in-match-issue-132784.rs:81:9 | LL | match (& (&difficult) ) { | ------------------ this expression has type `&&Arc>` ... LL | None => {} - | ^^^^ - | | - | expected `Arc>`, found `Option<_>` - | `None` is interpreted as a unit variant, not a new binding - | - --> $SRC_DIR/core/src/option.rs:LL:COL - | - = note: unit variant defined here + | ^^^^ expected `Arc>`, found `Option<_>` | = note: expected struct `Arc>` found enum `Option<_>` @@ -348,11 +257,6 @@ help: consider dereferencing to access the inner value using the Deref trait LL - match (& (&difficult) ) { LL + match &*difficult { | -help: introduce a new binding instead - | -LL - None => {} -LL + other_none => {} - | error: aborting due to 16 previous errors