@@ -12,7 +12,6 @@ use rustc_hir::{self as hir, AmbigArg, GenericParamKind, ImplItemKind, intravisi
12
12
use rustc_infer:: infer:: { self , InferCtxt , TyCtxtInferExt } ;
13
13
use rustc_infer:: traits:: util;
14
14
use rustc_middle:: ty:: error:: { ExpectedFound , TypeError } ;
15
- use rustc_middle:: ty:: util:: ExplicitSelf ;
16
15
use rustc_middle:: ty:: {
17
16
self , BottomUpFolder , GenericArgs , GenericParamDefKind , Ty , TyCtxt , TypeFoldable , TypeFolder ,
18
17
TypeSuperFoldable , TypeVisitableExt , TypingMode , Upcast ,
@@ -995,6 +994,26 @@ impl<'tcx> ty::FallibleTypeFolder<TyCtxt<'tcx>> for RemapHiddenTyRegions<'tcx> {
995
994
}
996
995
}
997
996
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
+
998
1017
fn report_trait_method_mismatch < ' tcx > (
999
1018
infcx : & InferCtxt < ' tcx > ,
1000
1019
mut cause : ObligationCause < ' tcx > ,
@@ -1020,12 +1039,7 @@ fn report_trait_method_mismatch<'tcx>(
1020
1039
if trait_m. fn_has_self_parameter =>
1021
1040
{
1022
1041
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 ( ) ) ;
1029
1043
1030
1044
// When the `impl` receiver is an arbitrary self type, like `self: Box<Self>`, the
1031
1045
// span points only at the type `Box<Self`>, but we want to cover the whole
@@ -1238,12 +1252,7 @@ fn compare_self_type<'tcx>(
1238
1252
. build_with_typing_env ( ty:: TypingEnv :: non_body_analysis ( tcx, method. def_id ) ) ;
1239
1253
let self_arg_ty = tcx. liberate_late_bound_regions ( method. def_id , self_arg_ty) ;
1240
1254
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)
1247
1256
} ;
1248
1257
1249
1258
match ( trait_m. fn_has_self_parameter , impl_m. fn_has_self_parameter ) {
0 commit comments