@@ -12,7 +12,6 @@ use rustc_hir::{self as hir, AmbigArg, GenericParamKind, ImplItemKind, intravisi
1212use rustc_infer:: infer:: { self , InferCtxt , TyCtxtInferExt } ;
1313use rustc_infer:: traits:: util;
1414use rustc_middle:: ty:: error:: { ExpectedFound , TypeError } ;
15- use rustc_middle:: ty:: util:: ExplicitSelf ;
1615use rustc_middle:: ty:: {
1716 self , BottomUpFolder , GenericArgs , GenericParamDefKind , Ty , TyCtxt , TypeFoldable , TypeFolder ,
1817 TypeSuperFoldable , TypeVisitableExt , TypingMode , Upcast ,
@@ -995,6 +994,26 @@ impl<'tcx> ty::FallibleTypeFolder<TyCtxt<'tcx>> for RemapHiddenTyRegions<'tcx> {
995994 }
996995}
997996
997+ /// Gets the string for an explicit self declaration, e.g. "self", "&self",
998+ /// etc.
999+ fn get_self_string < ' tcx , P > ( self_arg_ty : Ty < ' tcx > , is_self_ty : P ) -> String
1000+ where
1001+ P : Fn ( Ty < ' tcx > ) -> bool ,
1002+ {
1003+ if is_self_ty ( self_arg_ty) {
1004+ "self" . to_owned ( )
1005+ } else if let ty:: Ref ( _, ty, mutbl) = self_arg_ty. kind ( )
1006+ && is_self_ty ( * ty)
1007+ {
1008+ match mutbl {
1009+ hir:: Mutability :: Not => "&self" . to_owned ( ) ,
1010+ hir:: Mutability :: Mut => "&mut self" . to_owned ( ) ,
1011+ }
1012+ } else {
1013+ format ! ( "self: {self_arg_ty}" )
1014+ }
1015+ }
1016+
9981017fn report_trait_method_mismatch < ' tcx > (
9991018 infcx : & InferCtxt < ' tcx > ,
10001019 mut cause : ObligationCause < ' tcx > ,
@@ -1020,12 +1039,7 @@ fn report_trait_method_mismatch<'tcx>(
10201039 if trait_m. fn_has_self_parameter =>
10211040 {
10221041 let ty = trait_sig. inputs ( ) [ 0 ] ;
1023- let sugg = match ExplicitSelf :: determine ( ty, |ty| ty == impl_trait_ref. self_ty ( ) ) {
1024- ExplicitSelf :: ByValue => "self" . to_owned ( ) ,
1025- ExplicitSelf :: ByReference ( _, hir:: Mutability :: Not ) => "&self" . to_owned ( ) ,
1026- ExplicitSelf :: ByReference ( _, hir:: Mutability :: Mut ) => "&mut self" . to_owned ( ) ,
1027- _ => format ! ( "self: {ty}" ) ,
1028- } ;
1042+ let sugg = get_self_string ( ty, |ty| ty == impl_trait_ref. self_ty ( ) ) ;
10291043
10301044 // When the `impl` receiver is an arbitrary self type, like `self: Box<Self>`, the
10311045 // span points only at the type `Box<Self`>, but we want to cover the whole
@@ -1238,12 +1252,7 @@ fn compare_self_type<'tcx>(
12381252 . build_with_typing_env ( ty:: TypingEnv :: non_body_analysis ( tcx, method. def_id ) ) ;
12391253 let self_arg_ty = tcx. liberate_late_bound_regions ( method. def_id , self_arg_ty) ;
12401254 let can_eq_self = |ty| infcx. can_eq ( param_env, untransformed_self_ty, ty) ;
1241- match ExplicitSelf :: determine ( self_arg_ty, can_eq_self) {
1242- ExplicitSelf :: ByValue => "self" . to_owned ( ) ,
1243- ExplicitSelf :: ByReference ( _, hir:: Mutability :: Not ) => "&self" . to_owned ( ) ,
1244- ExplicitSelf :: ByReference ( _, hir:: Mutability :: Mut ) => "&mut self" . to_owned ( ) ,
1245- _ => format ! ( "self: {self_arg_ty}" ) ,
1246- }
1255+ get_self_string ( self_arg_ty, can_eq_self)
12471256 } ;
12481257
12491258 match ( trait_m. fn_has_self_parameter , impl_m. fn_has_self_parameter ) {
0 commit comments