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; }`)
This commit is contained in:
commit
58c690729c
3 changed files with 65 additions and 11 deletions
|
@ -891,18 +891,32 @@ impl<'a> Parser<'a> {
|
||||||
// that we do not use the try operator when parsing the type because
|
// 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
|
// if it fails then we get a parser error which we don't want (we're trying
|
||||||
// to recover from errors, not make more).
|
// to recover from errors, not make more).
|
||||||
let path = if self.may_recover()
|
let path = if self.may_recover() {
|
||||||
&& matches!(ty.kind, TyKind::Ptr(..) | TyKind::Ref(..))
|
let (span, message, sugg, path, applicability) = match &ty.kind {
|
||||||
&& let TyKind::Path(_, path) = &ty.peel_refs().kind {
|
TyKind::Ptr(..) | TyKind::Ref(..) if let TyKind::Path(_, path) = &ty.peel_refs().kind => {
|
||||||
// Just get the indirection part of the type.
|
(
|
||||||
let span = ty.span.until(path.span);
|
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(
|
err.span_suggestion_verbose(span, message, sugg, applicability);
|
||||||
span,
|
|
||||||
"consider removing the indirection",
|
|
||||||
"",
|
|
||||||
Applicability::MaybeIncorrect,
|
|
||||||
);
|
|
||||||
|
|
||||||
path.clone()
|
path.clone()
|
||||||
} else {
|
} else {
|
||||||
|
|
14
tests/ui/associated-type-bounds/suggest-removing-impl.rs
Normal file
14
tests/ui/associated-type-bounds/suggest-removing-impl.rs
Normal file
|
@ -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<T>()
|
||||||
|
where
|
||||||
|
T: impl Sized
|
||||||
|
//~^ ERROR expected a trait, found type
|
||||||
|
//~| HELP use the trait bounds directly
|
||||||
|
{}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn main() {}
|
26
tests/ui/associated-type-bounds/suggest-removing-impl.stderr
Normal file
26
tests/ui/associated-type-bounds/suggest-removing-impl.stderr
Normal file
|
@ -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
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue