@@ -31,6 +31,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
3131 }
3232
3333 self . annotate_alternative_method_deref ( err, expr, error) ;
34+ self . explain_self_literal ( err, expr, expected, expr_ty) ;
3435
3536 // Use `||` to give these suggestions a precedence
3637 let suggested = self . suggest_missing_parentheses ( err, expr)
@@ -1027,6 +1028,59 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
10271028 return false ;
10281029 }
10291030
1031+ fn explain_self_literal (
1032+ & self ,
1033+ err : & mut Diagnostic ,
1034+ expr : & hir:: Expr < ' tcx > ,
1035+ expected : Ty < ' tcx > ,
1036+ found : Ty < ' tcx > ,
1037+ ) {
1038+ match expr. peel_drop_temps ( ) . kind {
1039+ hir:: ExprKind :: Struct (
1040+ hir:: QPath :: Resolved (
1041+ None ,
1042+ hir:: Path { res : hir:: def:: Res :: SelfTyAlias { alias_to, .. } , span, .. } ,
1043+ ) ,
1044+ ..,
1045+ )
1046+ | hir:: ExprKind :: Call (
1047+ hir:: Expr {
1048+ kind :
1049+ hir:: ExprKind :: Path ( hir:: QPath :: Resolved (
1050+ None ,
1051+ hir:: Path {
1052+ res : hir:: def:: Res :: SelfTyAlias { alias_to, .. } ,
1053+ span,
1054+ ..
1055+ } ,
1056+ ) ) ,
1057+ ..
1058+ } ,
1059+ ..,
1060+ ) => {
1061+ if let Some ( hir:: Node :: Item ( hir:: Item {
1062+ kind : hir:: ItemKind :: Impl ( hir:: Impl { self_ty, .. } ) ,
1063+ ..
1064+ } ) ) = self . tcx . hir ( ) . get_if_local ( * alias_to)
1065+ {
1066+ err. span_label ( self_ty. span , "this is the type of the `Self` literal" ) ;
1067+ }
1068+ if let ty:: Adt ( e_def, e_args) = expected. kind ( )
1069+ && let ty:: Adt ( f_def, _f_args) = found. kind ( )
1070+ && e_def == f_def
1071+ {
1072+ err. span_suggestion_verbose (
1073+ * span,
1074+ "use the type name directly" ,
1075+ self . tcx . value_path_str_with_args ( * alias_to, e_args) ,
1076+ Applicability :: MaybeIncorrect ,
1077+ ) ;
1078+ }
1079+ }
1080+ _ => { }
1081+ }
1082+ }
1083+
10301084 fn note_wrong_return_ty_due_to_generic_arg (
10311085 & self ,
10321086 err : & mut Diagnostic ,
0 commit comments