diff options
| author | Matthias Krüger <matthias.krueger@famsik.de> | 2023-08-30 07:18:12 +0200 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2023-08-30 07:18:12 +0200 |
| commit | 58c690729ccaacb95f08323e77cc5f1dd74324f9 (patch) | |
| tree | eabf27d9dc92bd0a19761f4ec5367127671b4338 /compiler/rustc_parse/src/parser | |
| parent | 2128efd87fcbd165e0885512c0f5d830092bb1a4 (diff) | |
| parent | 507f10baee9ea0b884785aa4f54e3ffa6a1d82b2 (diff) | |
| download | rust-58c690729ccaacb95f08323e77cc5f1dd74324f9.tar.gz rust-58c690729ccaacb95f08323e77cc5f1dd74324f9.zip | |
Rollup merge of #115347 - y21:generic-bound-impl-trait-ty, r=compiler-errors
suggest removing `impl` in generic trait bound position
rustc already does this recovery in type param position (`<T: impl Trait>` -> `<T: Trait>`).
This PR also adds that suggestion in trait bound position (e.g. `where T: impl Trait` or `trait Trait { type Assoc: impl Trait; }`)
Diffstat (limited to 'compiler/rustc_parse/src/parser')
| -rw-r--r-- | compiler/rustc_parse/src/parser/ty.rs | 38 |
1 files changed, 26 insertions, 12 deletions
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); - - err.span_suggestion_verbose( - span, - "consider removing the indirection", - "", - Applicability::MaybeIncorrect, - ); + 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, message, sugg, applicability); path.clone() } else { |
