File tree 6 files changed +97
-4
lines changed
6 files changed +97
-4
lines changed Original file line number Diff line number Diff line change @@ -478,6 +478,11 @@ parse_missing_for_in_trait_impl = missing `for` in a trait impl
478
478
479
479
parse_expected_trait_in_trait_impl_found_type = expected a trait, found type
480
480
481
+ parse_extra_impl_keyword_in_trait_impl = unexpected `impl` keyword
482
+ .suggestion = remove the extra `impl`
483
+ .note = this is parsed as an `impl Trait` type, but a trait is expected at this position
484
+
485
+
481
486
parse_non_item_in_item_list = non-item in item list
482
487
.suggestion_use_const_not_let = consider using `const` instead of `let` for associated const
483
488
.label_list_start = item list starts here
Original file line number Diff line number Diff line change @@ -1519,6 +1519,16 @@ pub(crate) struct ExpectedTraitInTraitImplFoundType {
1519
1519
pub span : Span ,
1520
1520
}
1521
1521
1522
+ #[ derive( Diagnostic ) ]
1523
+ #[ diag( parse_extra_impl_keyword_in_trait_impl) ]
1524
+ pub ( crate ) struct ExtraImplKeywordInTraitImpl {
1525
+ #[ primary_span]
1526
+ #[ suggestion( code = "" , applicability = "maybe-incorrect" ) ]
1527
+ pub extra_impl_kw : Span ,
1528
+ #[ note]
1529
+ pub impl_trait_span : Span ,
1530
+ }
1531
+
1522
1532
#[ derive( Diagnostic ) ]
1523
1533
#[ diag( parse_bounds_not_allowed_on_trait_aliases) ]
1524
1534
pub ( crate ) struct BoundsNotAllowedOnTraitAliases {
Original file line number Diff line number Diff line change @@ -603,10 +603,24 @@ impl<'a> Parser<'a> {
603
603
let path = match ty_first. kind {
604
604
// This notably includes paths passed through `ty` macro fragments (#46438).
605
605
TyKind :: Path ( None , path) => path,
606
- _ => {
607
- self . sess . emit_err ( errors:: ExpectedTraitInTraitImplFoundType {
608
- span : ty_first. span ,
609
- } ) ;
606
+ other => {
607
+ if let TyKind :: ImplTrait ( _, bounds) = other
608
+ && let [ bound] = bounds. as_slice ( )
609
+ {
610
+ // Suggest removing extra `impl` keyword:
611
+ // `impl<T: Default> impl Default for Wrapper<T>`
612
+ // ^^^^^
613
+ let extra_impl_kw = ty_first. span . until ( bound. span ( ) ) ;
614
+ self . sess
615
+ . emit_err ( errors:: ExtraImplKeywordInTraitImpl {
616
+ extra_impl_kw,
617
+ impl_trait_span : ty_first. span
618
+ } ) ;
619
+ } else {
620
+ self . sess . emit_err ( errors:: ExpectedTraitInTraitImplFoundType {
621
+ span : ty_first. span ,
622
+ } ) ;
623
+ }
610
624
err_path ( ty_first. span )
611
625
}
612
626
} ;
Original file line number Diff line number Diff line change
1
+ // run-rustfix
2
+
3
+ struct S<T>(T);
4
+ struct S2;
5
+
6
+ impl<T: Default> Default for S<T> {
7
+ //~^ ERROR: unexpected `impl` keyword
8
+ //~| HELP: remove the extra `impl`
9
+ fn default() -> Self { todo!() }
10
+ }
11
+
12
+ impl Default for S2 {
13
+ //~^ ERROR: unexpected `impl` keyword
14
+ //~| HELP: remove the extra `impl`
15
+ fn default() -> Self { todo!() }
16
+ }
17
+
18
+
19
+ fn main() {}
Original file line number Diff line number Diff line change
1
+ // run-rustfix
2
+
3
+ struct S < T > ( T ) ;
4
+ struct S2 ;
5
+
6
+ impl < T : Default > impl Default for S < T > {
7
+ //~^ ERROR: unexpected `impl` keyword
8
+ //~| HELP: remove the extra `impl`
9
+ fn default ( ) -> Self { todo ! ( ) }
10
+ }
11
+
12
+ impl impl Default for S2 {
13
+ //~^ ERROR: unexpected `impl` keyword
14
+ //~| HELP: remove the extra `impl`
15
+ fn default ( ) -> Self { todo ! ( ) }
16
+ }
17
+
18
+
19
+ fn main ( ) { }
Original file line number Diff line number Diff line change
1
+ error: unexpected `impl` keyword
2
+ --> $DIR/extra-impl-in-trait-impl.rs:6:18
3
+ |
4
+ LL | impl<T: Default> impl Default for S<T> {
5
+ | ^^^^^ help: remove the extra `impl`
6
+ |
7
+ note: this is parsed as an `impl Trait` type, but a trait is expected at this position
8
+ --> $DIR/extra-impl-in-trait-impl.rs:6:18
9
+ |
10
+ LL | impl<T: Default> impl Default for S<T> {
11
+ | ^^^^^^^^^^^^
12
+
13
+ error: unexpected `impl` keyword
14
+ --> $DIR/extra-impl-in-trait-impl.rs:12:6
15
+ |
16
+ LL | impl impl Default for S2 {
17
+ | ^^^^^ help: remove the extra `impl`
18
+ |
19
+ note: this is parsed as an `impl Trait` type, but a trait is expected at this position
20
+ --> $DIR/extra-impl-in-trait-impl.rs:12:6
21
+ |
22
+ LL | impl impl Default for S2 {
23
+ | ^^^^^^^^^^^^
24
+
25
+ error: aborting due to 2 previous errors
26
+
You can’t perform that action at this time.
0 commit comments