@@ -146,6 +146,8 @@ impl<'a: 'ast, 'ast> LateResolutionVisitor<'a, '_, 'ast> {
146
146
let is_expected = & |res| source. is_expected ( res) ;
147
147
let is_enum_variant = & |res| matches ! ( res, Res :: Def ( DefKind :: Variant , _) ) ;
148
148
149
+ debug ! ( ?res, ?source) ;
150
+
149
151
// Make the base error.
150
152
struct BaseError < ' a > {
151
153
msg : String ,
@@ -250,6 +252,8 @@ impl<'a: 'ast, 'ast> LateResolutionVisitor<'a, '_, 'ast> {
250
252
let mut err =
251
253
self . r . session . struct_span_err_with_code ( base_error. span , & base_error. msg , code) ;
252
254
255
+ self . suggest_swapping_misplaced_self_ty_and_trait ( & mut err, source, res, base_error. span ) ;
256
+
253
257
if let Some ( sugg) = base_error. suggestion {
254
258
err. span_suggestion_verbose ( sugg. 0 , sugg. 1 , sugg. 2 , Applicability :: MaybeIncorrect ) ;
255
259
}
@@ -692,6 +696,35 @@ impl<'a: 'ast, 'ast> LateResolutionVisitor<'a, '_, 'ast> {
692
696
}
693
697
}
694
698
699
+ fn suggest_swapping_misplaced_self_ty_and_trait (
700
+ & mut self ,
701
+ err : & mut Diagnostic ,
702
+ source : PathSource < ' _ > ,
703
+ res : Option < Res > ,
704
+ span : Span ,
705
+ ) {
706
+ if let Some ( ( trait_ref, self_ty) ) =
707
+ self . diagnostic_metadata . currently_processing_impl_trait . clone ( )
708
+ && let TyKind :: Path ( _, self_ty_path) = & self_ty. kind
709
+ && let PathResult :: Module ( ModuleOrUniformRoot :: Module ( module) ) =
710
+ self . resolve_path ( & Segment :: from_path ( self_ty_path) , Some ( TypeNS ) , None )
711
+ && let ModuleKind :: Def ( DefKind :: Trait , ..) = module. kind
712
+ && trait_ref. path . span == span
713
+ && let PathSource :: Trait ( _) = source
714
+ && let Some ( Res :: Def ( DefKind :: Struct | DefKind :: Enum | DefKind :: Union , _) ) = res
715
+ && let Ok ( self_ty_str) =
716
+ self . r . session . source_map ( ) . span_to_snippet ( self_ty. span )
717
+ && let Ok ( trait_ref_str) =
718
+ self . r . session . source_map ( ) . span_to_snippet ( trait_ref. path . span )
719
+ {
720
+ err. multipart_suggestion (
721
+ "`impl` items mention the trait being implemented first and the type it is being implemented for second" ,
722
+ vec ! [ ( trait_ref. path. span, self_ty_str) , ( self_ty. span, trait_ref_str) ] ,
723
+ Applicability :: MaybeIncorrect ,
724
+ ) ;
725
+ }
726
+ }
727
+
695
728
fn get_single_associated_item (
696
729
& mut self ,
697
730
path : & [ Segment ] ,
0 commit comments