Skip to content

Commit 507f10b

Browse files
committed
suggest removing impl in generic trait bound position
1 parent 0b84f18 commit 507f10b

File tree

3 files changed

+66
-12
lines changed

3 files changed

+66
-12
lines changed

compiler/rustc_parse/src/parser/ty.rs

+26-12
Original file line numberDiff line numberDiff line change
@@ -891,18 +891,32 @@ impl<'a> Parser<'a> {
891891
// that we do not use the try operator when parsing the type because
892892
// if it fails then we get a parser error which we don't want (we're trying
893893
// to recover from errors, not make more).
894-
let path = if self.may_recover()
895-
&& matches!(ty.kind, TyKind::Ptr(..) | TyKind::Ref(..))
896-
&& let TyKind::Path(_, path) = &ty.peel_refs().kind {
897-
// Just get the indirection part of the type.
898-
let span = ty.span.until(path.span);
899-
900-
err.span_suggestion_verbose(
901-
span,
902-
"consider removing the indirection",
903-
"",
904-
Applicability::MaybeIncorrect,
905-
);
894+
let path = if self.may_recover() {
895+
let (span, message, sugg, path, applicability) = match &ty.kind {
896+
TyKind::Ptr(..) | TyKind::Ref(..) if let TyKind::Path(_, path) = &ty.peel_refs().kind => {
897+
(
898+
ty.span.until(path.span),
899+
"consider removing the indirection",
900+
"",
901+
path,
902+
Applicability::MaybeIncorrect
903+
)
904+
}
905+
TyKind::ImplTrait(_, bounds)
906+
if let [GenericBound::Trait(tr, ..), ..] = bounds.as_slice() =>
907+
{
908+
(
909+
ty.span.until(tr.span),
910+
"use the trait bounds directly",
911+
"",
912+
&tr.trait_ref.path,
913+
Applicability::MachineApplicable
914+
)
915+
}
916+
_ => return Err(err)
917+
};
918+
919+
err.span_suggestion_verbose(span, message, sugg, applicability);
906920

907921
path.clone()
908922
} else {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
trait Tr {
2+
type Assoc: impl Sized;
3+
//~^ ERROR expected a trait, found type
4+
//~| HELP use the trait bounds directly
5+
6+
fn fn_with_generics<T>()
7+
where
8+
T: impl Sized
9+
//~^ ERROR expected a trait, found type
10+
//~| HELP use the trait bounds directly
11+
{}
12+
}
13+
14+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
error: expected a trait, found type
2+
--> $DIR/suggest-removing-impl.rs:2:17
3+
|
4+
LL | type Assoc: impl Sized;
5+
| ^^^^^^^^^^
6+
|
7+
help: use the trait bounds directly
8+
|
9+
LL - type Assoc: impl Sized;
10+
LL + type Assoc: Sized;
11+
|
12+
13+
error: expected a trait, found type
14+
--> $DIR/suggest-removing-impl.rs:8:12
15+
|
16+
LL | T: impl Sized
17+
| ^^^^^^^^^^
18+
|
19+
help: use the trait bounds directly
20+
|
21+
LL - T: impl Sized
22+
LL + T: Sized
23+
|
24+
25+
error: aborting due to 2 previous errors
26+

0 commit comments

Comments
 (0)