1
1
use crate :: callee:: { self , DeferredCallResolution } ;
2
2
use crate :: method:: { self , MethodCallee , SelfSource } ;
3
3
use crate :: rvalue_scopes;
4
- use crate :: { BreakableCtxt , Diverges , Expectation , FnCtxt , LocalTy } ;
4
+ use crate :: { BreakableCtxt , Diverges , Expectation , FnCtxt , LocalTy , RawTy } ;
5
5
use rustc_data_structures:: captures:: Captures ;
6
6
use rustc_data_structures:: fx:: FxHashSet ;
7
7
use rustc_errors:: { Applicability , Diagnostic , ErrorGuaranteed , MultiSpan } ;
@@ -464,23 +464,27 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
464
464
}
465
465
}
466
466
467
- pub fn to_ty ( & self , ast_t : & hir:: Ty < ' _ > ) -> Ty < ' tcx > {
467
+ pub fn create_raw_ty ( & self , span : Span , ty : Ty < ' tcx > ) -> RawTy < ' tcx > {
468
+ RawTy { raw : ty, normalized : self . normalize_associated_types_in ( span, ty) }
469
+ }
470
+
471
+ pub fn to_ty ( & self , ast_t : & hir:: Ty < ' _ > ) -> RawTy < ' tcx > {
468
472
let t = <dyn AstConv < ' _ > >:: ast_ty_to_ty ( self , ast_t) ;
469
473
self . register_wf_obligation ( t. into ( ) , ast_t. span , traits:: WellFormed ( None ) ) ;
470
- t
474
+ self . create_raw_ty ( ast_t . span , t )
471
475
}
472
476
473
477
pub fn to_ty_saving_user_provided_ty ( & self , ast_ty : & hir:: Ty < ' _ > ) -> Ty < ' tcx > {
474
478
let ty = self . to_ty ( ast_ty) ;
475
479
debug ! ( "to_ty_saving_user_provided_ty: ty={:?}" , ty) ;
476
480
477
- if Self :: can_contain_user_lifetime_bounds ( ty) {
478
- let c_ty = self . canonicalize_response ( UserType :: Ty ( ty) ) ;
481
+ if Self :: can_contain_user_lifetime_bounds ( ty. raw ) {
482
+ let c_ty = self . canonicalize_response ( UserType :: Ty ( ty. raw ) ) ;
479
483
debug ! ( "to_ty_saving_user_provided_ty: c_ty={:?}" , c_ty) ;
480
484
self . typeck_results . borrow_mut ( ) . user_provided_types_mut ( ) . insert ( ast_ty. hir_id , c_ty) ;
481
485
}
482
486
483
- ty
487
+ ty. normalized
484
488
}
485
489
486
490
pub fn array_length_to_const ( & self , length : & hir:: ArrayLen ) -> ty:: Const < ' tcx > {
@@ -831,7 +835,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
831
835
qpath : & ' tcx QPath < ' tcx > ,
832
836
hir_id : hir:: HirId ,
833
837
span : Span ,
834
- ) -> ( Res , Option < Ty < ' tcx > > , & ' tcx [ hir:: PathSegment < ' tcx > ] ) {
838
+ ) -> ( Res , Option < RawTy < ' tcx > > , & ' tcx [ hir:: PathSegment < ' tcx > ] ) {
835
839
debug ! (
836
840
"resolve_ty_and_res_fully_qualified_call: qpath={:?} hir_id={:?} span={:?}" ,
837
841
qpath, hir_id, span
@@ -854,23 +858,24 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
854
858
// to be object-safe.
855
859
// We manually call `register_wf_obligation` in the success path
856
860
// below.
857
- ( <dyn AstConv < ' _ > >:: ast_ty_to_ty_in_path ( self , qself) , qself, segment)
861
+ let ty = <dyn AstConv < ' _ > >:: ast_ty_to_ty_in_path ( self , qself) ;
862
+ ( self . create_raw_ty ( span, ty) , qself, segment)
858
863
}
859
864
QPath :: LangItem ( ..) => {
860
865
bug ! ( "`resolve_ty_and_res_fully_qualified_call` called on `LangItem`" )
861
866
}
862
867
} ;
863
868
if let Some ( & cached_result) = self . typeck_results . borrow ( ) . type_dependent_defs ( ) . get ( hir_id)
864
869
{
865
- self . register_wf_obligation ( ty. into ( ) , qself. span , traits:: WellFormed ( None ) ) ;
870
+ self . register_wf_obligation ( ty. raw . into ( ) , qself. span , traits:: WellFormed ( None ) ) ;
866
871
// Return directly on cache hit. This is useful to avoid doubly reporting
867
872
// errors with default match binding modes. See #44614.
868
873
let def = cached_result. map_or ( Res :: Err , |( kind, def_id) | Res :: Def ( kind, def_id) ) ;
869
874
return ( def, Some ( ty) , slice:: from_ref ( & * * item_segment) ) ;
870
875
}
871
876
let item_name = item_segment. ident ;
872
877
let result = self
873
- . resolve_fully_qualified_call ( span, item_name, ty, qself. span , hir_id)
878
+ . resolve_fully_qualified_call ( span, item_name, ty. normalized , qself. span , hir_id)
874
879
. or_else ( |error| {
875
880
let result = match error {
876
881
method:: MethodError :: PrivateMatch ( kind, def_id, _) => Ok ( ( kind, def_id) ) ,
@@ -881,13 +886,17 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
881
886
// a WF obligation for `dyn MyTrait` when method lookup fails. Otherwise,
882
887
// register a WF obligation so that we can detect any additional
883
888
// errors in the self type.
884
- if !( matches ! ( error, method:: MethodError :: NoMatch ( _) ) && ty. is_trait ( ) ) {
885
- self . register_wf_obligation ( ty. into ( ) , qself. span , traits:: WellFormed ( None ) ) ;
889
+ if !( matches ! ( error, method:: MethodError :: NoMatch ( _) ) && ty. normalized . is_trait ( ) ) {
890
+ self . register_wf_obligation (
891
+ ty. raw . into ( ) ,
892
+ qself. span ,
893
+ traits:: WellFormed ( None ) ,
894
+ ) ;
886
895
}
887
896
if item_name. name != kw:: Empty {
888
897
if let Some ( mut e) = self . report_method_error (
889
898
span,
890
- ty,
899
+ ty. normalized ,
891
900
item_name,
892
901
SelfSource :: QPath ( qself) ,
893
902
error,
@@ -900,7 +909,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
900
909
} ) ;
901
910
902
911
if result. is_ok ( ) {
903
- self . register_wf_obligation ( ty. into ( ) , qself. span , traits:: WellFormed ( None ) ) ;
912
+ self . register_wf_obligation ( ty. raw . into ( ) , qself. span , traits:: WellFormed ( None ) ) ;
904
913
}
905
914
906
915
// Write back the new resolution.
@@ -1037,17 +1046,22 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
1037
1046
pub fn instantiate_value_path (
1038
1047
& self ,
1039
1048
segments : & [ hir:: PathSegment < ' _ > ] ,
1040
- self_ty : Option < Ty < ' tcx > > ,
1049
+ self_ty : Option < RawTy < ' tcx > > ,
1041
1050
res : Res ,
1042
1051
span : Span ,
1043
1052
hir_id : hir:: HirId ,
1044
1053
) -> ( Ty < ' tcx > , Res ) {
1045
1054
let tcx = self . tcx ;
1055
+ assert_eq ! ( res. ns( ) , Some ( rustc_hir:: def:: Namespace :: ValueNS ) ) ;
1046
1056
1047
1057
let path_segs = match res {
1048
1058
Res :: Local ( _) | Res :: SelfCtor ( _) => vec ! [ ] ,
1049
1059
Res :: Def ( kind, def_id) => <dyn AstConv < ' _ > >:: def_ids_for_value_path_segments (
1050
- self , segments, self_ty, kind, def_id,
1060
+ self ,
1061
+ segments,
1062
+ self_ty. map ( |ty| ty. normalized ) ,
1063
+ kind,
1064
+ def_id,
1051
1065
) ,
1052
1066
_ => bug ! ( "instantiate_value_path on {:?}" , res) ,
1053
1067
} ;
@@ -1058,8 +1072,8 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
1058
1072
Res :: Def ( DefKind :: Ctor ( CtorOf :: Variant , _) , _)
1059
1073
if let Some ( self_ty) = self_ty =>
1060
1074
{
1061
- let adt_def = self_ty. ty_adt_def ( ) . unwrap ( ) ;
1062
- user_self_ty = Some ( UserSelfTy { impl_def_id : adt_def. did ( ) , self_ty } ) ;
1075
+ let adt_def = self_ty. normalized . ty_adt_def ( ) . unwrap ( ) ;
1076
+ user_self_ty = Some ( UserSelfTy { impl_def_id : adt_def. did ( ) , self_ty : self_ty . raw } ) ;
1063
1077
is_alias_variant_ctor = true ;
1064
1078
}
1065
1079
Res :: Def ( DefKind :: AssocFn | DefKind :: AssocConst , def_id) => {
@@ -1078,7 +1092,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
1078
1092
// inherent impl, we need to record the
1079
1093
// `T` for posterity (see `UserSelfTy` for
1080
1094
// details).
1081
- let self_ty = self_ty. expect ( "UFCS sugared assoc missing Self" ) ;
1095
+ let self_ty = self_ty. expect ( "UFCS sugared assoc missing Self" ) . raw ;
1082
1096
user_self_ty = Some ( UserSelfTy { impl_def_id : container_id, self_ty } ) ;
1083
1097
}
1084
1098
}
@@ -1160,7 +1174,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
1160
1174
. unwrap_or ( false ) ;
1161
1175
1162
1176
let ( res, self_ctor_substs) = if let Res :: SelfCtor ( impl_def_id) = res {
1163
- let ty = self . normalize_ty ( span, tcx. at ( span) . type_of ( impl_def_id) ) ;
1177
+ let ty = self . normalize_ty_2 ( span, tcx. at ( span) . type_of ( impl_def_id) ) ;
1164
1178
match * ty. kind ( ) {
1165
1179
ty:: Adt ( adt_def, substs) if adt_def. has_ctor ( ) => {
1166
1180
let variant = adt_def. non_enum_variant ( ) ;
@@ -1251,7 +1265,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
1251
1265
<dyn AstConv < ' _ > >:: ast_region_to_region ( self . fcx , lt, Some ( param) ) . into ( )
1252
1266
}
1253
1267
( GenericParamDefKind :: Type { .. } , GenericArg :: Type ( ty) ) => {
1254
- self . fcx . to_ty ( ty) . into ( )
1268
+ self . fcx . to_ty ( ty) . raw . into ( )
1255
1269
}
1256
1270
( GenericParamDefKind :: Const { .. } , GenericArg :: Const ( ct) ) => {
1257
1271
self . fcx . const_arg_to_const ( & ct. value , param. def_id ) . into ( )
@@ -1285,7 +1299,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
1285
1299
// is missing.
1286
1300
let default = tcx. bound_type_of ( param. def_id ) ;
1287
1301
self . fcx
1288
- . normalize_ty ( self . span , default. subst ( tcx, substs. unwrap ( ) ) )
1302
+ . normalize_ty_2 ( self . span , default. subst ( tcx, substs. unwrap ( ) ) )
1289
1303
. into ( )
1290
1304
} else {
1291
1305
// If no type arguments were provided, we have to infer them.
@@ -1308,13 +1322,13 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
1308
1322
}
1309
1323
}
1310
1324
1311
- let substs = self_ctor_substs. unwrap_or_else ( || {
1325
+ let substs_raw = self_ctor_substs. unwrap_or_else ( || {
1312
1326
<dyn AstConv < ' _ > >:: create_substs_for_generic_args (
1313
1327
tcx,
1314
1328
def_id,
1315
1329
& [ ] ,
1316
1330
has_self,
1317
- self_ty,
1331
+ self_ty. map ( |s| s . raw ) ,
1318
1332
& arg_count,
1319
1333
& mut CreateCtorSubstsContext {
1320
1334
fcx : self ,
@@ -1325,11 +1339,14 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
1325
1339
} ,
1326
1340
)
1327
1341
} ) ;
1328
- assert ! ( !substs . has_escaping_bound_vars( ) ) ;
1342
+ assert ! ( !substs_raw . has_escaping_bound_vars( ) ) ;
1329
1343
assert ! ( !ty. has_escaping_bound_vars( ) ) ;
1330
1344
1331
1345
// First, store the "user substs" for later.
1332
- self . write_user_type_annotation_from_substs ( hir_id, def_id, substs, user_self_ty) ;
1346
+ self . write_user_type_annotation_from_substs ( hir_id, def_id, substs_raw, user_self_ty) ;
1347
+
1348
+ // Normalize only after registering type annotations.
1349
+ let substs = self . normalize_associated_types_in ( span, substs_raw) ;
1333
1350
1334
1351
self . add_required_obligations_for_hir ( span, def_id, & substs, hir_id) ;
1335
1352
@@ -1346,6 +1363,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
1346
1363
let ty = tcx. type_of ( impl_def_id) ;
1347
1364
1348
1365
let impl_ty = self . instantiate_type_scheme ( span, & substs, ty) ;
1366
+ let self_ty = self . normalize_associated_types_in ( span, self_ty) ;
1349
1367
match self . at ( & self . misc ( span) , self . param_env ) . eq ( impl_ty, self_ty) {
1350
1368
Ok ( ok) => self . register_infer_ok_obligations ( ok) ,
1351
1369
Err ( _) => {
0 commit comments