From d93f7f93c44a7106071411224c0615c7cfcb2468 Mon Sep 17 00:00:00 2001 From: Noah Lev Date: Thu, 4 Nov 2021 23:25:16 -0700 Subject: Suggest dereference of `Box` when inner type is expected For example: enum Ty { Unit, List(Box), } fn foo(x: Ty) -> Ty { match x { Ty::Unit => Ty::Unit, Ty::List(elem) => foo(elem), } } Before, the only suggestion was to rewrap `elem` with `Ty::List`, which is unhelpful and confusing: error[E0308]: mismatched types --> src/test/ui/suggestions/boxed-variant-field.rs:9:31 | 9 | Ty::List(elem) => foo(elem), | ^^^^ | | | expected enum `Ty`, found struct `Box` | help: try using a variant of the expected enum: `Ty::List(elem)` | = note: expected enum `Ty` found struct `Box` Now, rustc will first suggest dereferencing the `Box`, which is most likely what the user intended: error[E0308]: mismatched types --> src/test/ui/suggestions/boxed-variant-field.rs:9:31 | 9 | Ty::List(elem) => foo(elem), | ^^^^ expected enum `Ty`, found struct `Box` | = note: expected enum `Ty` found struct `Box` help: try dereferencing the `Box` | 9 | Ty::List(elem) => foo(*elem), | + help: try using a variant of the expected enum | 9 | Ty::List(elem) => foo(Ty::List(elem)), | ~~~~~~~~~~~~~~ --- src/test/ui/suggestions/boxed-variant-field.rs | 16 ++++++++++++++++ src/test/ui/suggestions/boxed-variant-field.stderr | 20 ++++++++++++++++++++ src/test/ui/terr-sorts.stderr | 4 ++++ 3 files changed, 40 insertions(+) create mode 100644 src/test/ui/suggestions/boxed-variant-field.rs create mode 100644 src/test/ui/suggestions/boxed-variant-field.stderr (limited to 'src') diff --git a/src/test/ui/suggestions/boxed-variant-field.rs b/src/test/ui/suggestions/boxed-variant-field.rs new file mode 100644 index 00000000000..d8f7fac1513 --- /dev/null +++ b/src/test/ui/suggestions/boxed-variant-field.rs @@ -0,0 +1,16 @@ +enum Ty { + Unit, + List(Box), +} + +fn foo(x: Ty) -> Ty { + match x { + Ty::Unit => Ty::Unit, + Ty::List(elem) => foo(elem), + //~^ ERROR mismatched types + //~| HELP try dereferencing the `Box` + //~| HELP try using a variant of the expected enum + } +} + +fn main() {} diff --git a/src/test/ui/suggestions/boxed-variant-field.stderr b/src/test/ui/suggestions/boxed-variant-field.stderr new file mode 100644 index 00000000000..d4ccb2ca490 --- /dev/null +++ b/src/test/ui/suggestions/boxed-variant-field.stderr @@ -0,0 +1,20 @@ +error[E0308]: mismatched types + --> $DIR/boxed-variant-field.rs:9:31 + | +LL | Ty::List(elem) => foo(elem), + | ^^^^ expected enum `Ty`, found struct `Box` + | + = note: expected enum `Ty` + found struct `Box` +help: try dereferencing the `Box` + | +LL | Ty::List(elem) => foo(*elem), + | + +help: try using a variant of the expected enum + | +LL | Ty::List(elem) => foo(Ty::List(elem)), + | ~~~~~~~~~~~~~~ + +error: aborting due to previous error + +For more information about this error, try `rustc --explain E0308`. diff --git a/src/test/ui/terr-sorts.stderr b/src/test/ui/terr-sorts.stderr index 869b3729659..65d90678040 100644 --- a/src/test/ui/terr-sorts.stderr +++ b/src/test/ui/terr-sorts.stderr @@ -6,6 +6,10 @@ LL | want_foo(b); | = note: expected struct `Foo` found struct `Box` +help: try dereferencing the `Box` + | +LL | want_foo(*b); + | + error: aborting due to previous error -- cgit 1.4.1-3-g733a5