From 507f10baee9ea0b884785aa4f54e3ffa6a1d82b2 Mon Sep 17 00:00:00 2001 From: y21 <30553356+y21@users.noreply.github.com> Date: Tue, 29 Aug 2023 17:10:27 +0200 Subject: [PATCH] suggest removing `impl` in generic trait bound position --- compiler/rustc_parse/src/parser/ty.rs | 36 +++++++++++++------ .../suggest-removing-impl.rs | 14 ++++++++ .../suggest-removing-impl.stderr | 26 ++++++++++++++ 3 files changed, 65 insertions(+), 11 deletions(-) create mode 100644 tests/ui/associated-type-bounds/suggest-removing-impl.rs create mode 100644 tests/ui/associated-type-bounds/suggest-removing-impl.stderr diff --git a/compiler/rustc_parse/src/parser/ty.rs b/compiler/rustc_parse/src/parser/ty.rs index 661113666cd..a25b0f1f893 100644 --- a/compiler/rustc_parse/src/parser/ty.rs +++ b/compiler/rustc_parse/src/parser/ty.rs @@ -891,18 +891,32 @@ impl<'a> Parser<'a> { // that we do not use the try operator when parsing the type because // if it fails then we get a parser error which we don't want (we're trying // to recover from errors, not make more). - let path = if self.may_recover() - && matches!(ty.kind, TyKind::Ptr(..) | TyKind::Ref(..)) - && let TyKind::Path(_, path) = &ty.peel_refs().kind { - // Just get the indirection part of the type. - let span = ty.span.until(path.span); + let path = if self.may_recover() { + let (span, message, sugg, path, applicability) = match &ty.kind { + TyKind::Ptr(..) | TyKind::Ref(..) if let TyKind::Path(_, path) = &ty.peel_refs().kind => { + ( + ty.span.until(path.span), + "consider removing the indirection", + "", + path, + Applicability::MaybeIncorrect + ) + } + TyKind::ImplTrait(_, bounds) + if let [GenericBound::Trait(tr, ..), ..] = bounds.as_slice() => + { + ( + ty.span.until(tr.span), + "use the trait bounds directly", + "", + &tr.trait_ref.path, + Applicability::MachineApplicable + ) + } + _ => return Err(err) + }; - err.span_suggestion_verbose( - span, - "consider removing the indirection", - "", - Applicability::MaybeIncorrect, - ); + err.span_suggestion_verbose(span, message, sugg, applicability); path.clone() } else { diff --git a/tests/ui/associated-type-bounds/suggest-removing-impl.rs b/tests/ui/associated-type-bounds/suggest-removing-impl.rs new file mode 100644 index 00000000000..242cd85727d --- /dev/null +++ b/tests/ui/associated-type-bounds/suggest-removing-impl.rs @@ -0,0 +1,14 @@ +trait Tr { + type Assoc: impl Sized; + //~^ ERROR expected a trait, found type + //~| HELP use the trait bounds directly + + fn fn_with_generics() + where + T: impl Sized + //~^ ERROR expected a trait, found type + //~| HELP use the trait bounds directly + {} +} + +fn main() {} diff --git a/tests/ui/associated-type-bounds/suggest-removing-impl.stderr b/tests/ui/associated-type-bounds/suggest-removing-impl.stderr new file mode 100644 index 00000000000..875b2db6deb --- /dev/null +++ b/tests/ui/associated-type-bounds/suggest-removing-impl.stderr @@ -0,0 +1,26 @@ +error: expected a trait, found type + --> $DIR/suggest-removing-impl.rs:2:17 + | +LL | type Assoc: impl Sized; + | ^^^^^^^^^^ + | +help: use the trait bounds directly + | +LL - type Assoc: impl Sized; +LL + type Assoc: Sized; + | + +error: expected a trait, found type + --> $DIR/suggest-removing-impl.rs:8:12 + | +LL | T: impl Sized + | ^^^^^^^^^^ + | +help: use the trait bounds directly + | +LL - T: impl Sized +LL + T: Sized + | + +error: aborting due to 2 previous errors +