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 } ;
@@ -410,23 +410,27 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
410
410
}
411
411
}
412
412
413
- pub fn to_ty ( & self , ast_t : & hir:: Ty < ' _ > ) -> Ty < ' tcx > {
413
+ pub fn create_raw_ty ( & self , span : Span , ty : Ty < ' tcx > ) -> RawTy < ' tcx > {
414
+ RawTy { raw : ty, normalized : self . normalize ( span, ty) }
415
+ }
416
+
417
+ pub fn to_ty ( & self , ast_t : & hir:: Ty < ' _ > ) -> RawTy < ' tcx > {
414
418
let t = <dyn AstConv < ' _ > >:: ast_ty_to_ty ( self , ast_t) ;
415
419
self . register_wf_obligation ( t. into ( ) , ast_t. span , traits:: WellFormed ( None ) ) ;
416
- t
420
+ self . create_raw_ty ( ast_t . span , t )
417
421
}
418
422
419
423
pub fn to_ty_saving_user_provided_ty ( & self , ast_ty : & hir:: Ty < ' _ > ) -> Ty < ' tcx > {
420
424
let ty = self . to_ty ( ast_ty) ;
421
425
debug ! ( "to_ty_saving_user_provided_ty: ty={:?}" , ty) ;
422
426
423
- if Self :: can_contain_user_lifetime_bounds ( ty) {
424
- let c_ty = self . canonicalize_response ( UserType :: Ty ( ty) ) ;
427
+ if Self :: can_contain_user_lifetime_bounds ( ty. raw ) {
428
+ let c_ty = self . canonicalize_response ( UserType :: Ty ( ty. raw ) ) ;
425
429
debug ! ( "to_ty_saving_user_provided_ty: c_ty={:?}" , c_ty) ;
426
430
self . typeck_results . borrow_mut ( ) . user_provided_types_mut ( ) . insert ( ast_ty. hir_id , c_ty) ;
427
431
}
428
432
429
- ty
433
+ ty. normalized
430
434
}
431
435
432
436
pub fn array_length_to_const ( & self , length : & hir:: ArrayLen ) -> ty:: Const < ' tcx > {
@@ -780,7 +784,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
780
784
qpath : & ' tcx QPath < ' tcx > ,
781
785
hir_id : hir:: HirId ,
782
786
span : Span ,
783
- ) -> ( Res , Option < Ty < ' tcx > > , & ' tcx [ hir:: PathSegment < ' tcx > ] ) {
787
+ ) -> ( Res , Option < RawTy < ' tcx > > , & ' tcx [ hir:: PathSegment < ' tcx > ] ) {
784
788
debug ! (
785
789
"resolve_ty_and_res_fully_qualified_call: qpath={:?} hir_id={:?} span={:?}" ,
786
790
qpath, hir_id, span
@@ -803,23 +807,24 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
803
807
// to be object-safe.
804
808
// We manually call `register_wf_obligation` in the success path
805
809
// below.
806
- ( <dyn AstConv < ' _ > >:: ast_ty_to_ty_in_path ( self , qself) , qself, segment)
810
+ let ty = <dyn AstConv < ' _ > >:: ast_ty_to_ty_in_path ( self , qself) ;
811
+ ( self . create_raw_ty ( span, ty) , qself, segment)
807
812
}
808
813
QPath :: LangItem ( ..) => {
809
814
bug ! ( "`resolve_ty_and_res_fully_qualified_call` called on `LangItem`" )
810
815
}
811
816
} ;
812
817
if let Some ( & cached_result) = self . typeck_results . borrow ( ) . type_dependent_defs ( ) . get ( hir_id)
813
818
{
814
- self . register_wf_obligation ( ty. into ( ) , qself. span , traits:: WellFormed ( None ) ) ;
819
+ self . register_wf_obligation ( ty. raw . into ( ) , qself. span , traits:: WellFormed ( None ) ) ;
815
820
// Return directly on cache hit. This is useful to avoid doubly reporting
816
821
// errors with default match binding modes. See #44614.
817
822
let def = cached_result. map_or ( Res :: Err , |( kind, def_id) | Res :: Def ( kind, def_id) ) ;
818
823
return ( def, Some ( ty) , slice:: from_ref ( & * * item_segment) ) ;
819
824
}
820
825
let item_name = item_segment. ident ;
821
826
let result = self
822
- . resolve_fully_qualified_call ( span, item_name, ty, qself. span , hir_id)
827
+ . resolve_fully_qualified_call ( span, item_name, ty. normalized , qself. span , hir_id)
823
828
. or_else ( |error| {
824
829
let result = match error {
825
830
method:: MethodError :: PrivateMatch ( kind, def_id, _) => Ok ( ( kind, def_id) ) ,
@@ -830,13 +835,17 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
830
835
// a WF obligation for `dyn MyTrait` when method lookup fails. Otherwise,
831
836
// register a WF obligation so that we can detect any additional
832
837
// errors in the self type.
833
- if !( matches ! ( error, method:: MethodError :: NoMatch ( _) ) && ty. is_trait ( ) ) {
834
- self . register_wf_obligation ( ty. into ( ) , qself. span , traits:: WellFormed ( None ) ) ;
838
+ if !( matches ! ( error, method:: MethodError :: NoMatch ( _) ) && ty. normalized . is_trait ( ) ) {
839
+ self . register_wf_obligation (
840
+ ty. raw . into ( ) ,
841
+ qself. span ,
842
+ traits:: WellFormed ( None ) ,
843
+ ) ;
835
844
}
836
845
if item_name. name != kw:: Empty {
837
846
if let Some ( mut e) = self . report_method_error (
838
847
span,
839
- ty,
848
+ ty. normalized ,
840
849
item_name,
841
850
SelfSource :: QPath ( qself) ,
842
851
error,
@@ -849,7 +858,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
849
858
} ) ;
850
859
851
860
if result. is_ok ( ) {
852
- self . register_wf_obligation ( ty. into ( ) , qself. span , traits:: WellFormed ( None ) ) ;
861
+ self . register_wf_obligation ( ty. raw . into ( ) , qself. span , traits:: WellFormed ( None ) ) ;
853
862
}
854
863
855
864
// Write back the new resolution.
@@ -986,7 +995,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
986
995
pub fn instantiate_value_path (
987
996
& self ,
988
997
segments : & [ hir:: PathSegment < ' _ > ] ,
989
- self_ty : Option < Ty < ' tcx > > ,
998
+ self_ty : Option < RawTy < ' tcx > > ,
990
999
res : Res ,
991
1000
span : Span ,
992
1001
hir_id : hir:: HirId ,
@@ -996,7 +1005,11 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
996
1005
let path_segs = match res {
997
1006
Res :: Local ( _) | Res :: SelfCtor ( _) => vec ! [ ] ,
998
1007
Res :: Def ( kind, def_id) => <dyn AstConv < ' _ > >:: def_ids_for_value_path_segments (
999
- self , segments, self_ty, kind, def_id,
1008
+ self ,
1009
+ segments,
1010
+ self_ty. map ( |ty| ty. normalized ) ,
1011
+ kind,
1012
+ def_id,
1000
1013
) ,
1001
1014
_ => bug ! ( "instantiate_value_path on {:?}" , res) ,
1002
1015
} ;
@@ -1007,8 +1020,8 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
1007
1020
Res :: Def ( DefKind :: Ctor ( CtorOf :: Variant , _) , _)
1008
1021
if let Some ( self_ty) = self_ty =>
1009
1022
{
1010
- let adt_def = self_ty. ty_adt_def ( ) . unwrap ( ) ;
1011
- user_self_ty = Some ( UserSelfTy { impl_def_id : adt_def. did ( ) , self_ty } ) ;
1023
+ let adt_def = self_ty. normalized . ty_adt_def ( ) . unwrap ( ) ;
1024
+ user_self_ty = Some ( UserSelfTy { impl_def_id : adt_def. did ( ) , self_ty : self_ty . raw } ) ;
1012
1025
is_alias_variant_ctor = true ;
1013
1026
}
1014
1027
Res :: Def ( DefKind :: AssocFn | DefKind :: AssocConst , def_id) => {
@@ -1027,7 +1040,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
1027
1040
// inherent impl, we need to record the
1028
1041
// `T` for posterity (see `UserSelfTy` for
1029
1042
// details).
1030
- let self_ty = self_ty. expect ( "UFCS sugared assoc missing Self" ) ;
1043
+ let self_ty = self_ty. expect ( "UFCS sugared assoc missing Self" ) . raw ;
1031
1044
user_self_ty = Some ( UserSelfTy { impl_def_id : container_id, self_ty } ) ;
1032
1045
}
1033
1046
}
@@ -1109,7 +1122,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
1109
1122
. unwrap_or ( false ) ;
1110
1123
1111
1124
let ( res, self_ctor_substs) = if let Res :: SelfCtor ( impl_def_id) = res {
1112
- let ty = self . normalize_ty ( span, tcx. at ( span) . type_of ( impl_def_id) ) ;
1125
+ let ty = self . normalize_ty_2 ( span, tcx. at ( span) . type_of ( impl_def_id) ) ;
1113
1126
match * ty. kind ( ) {
1114
1127
ty:: Adt ( adt_def, substs) if adt_def. has_ctor ( ) => {
1115
1128
let variant = adt_def. non_enum_variant ( ) ;
@@ -1193,7 +1206,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
1193
1206
<dyn AstConv < ' _ > >:: ast_region_to_region ( self . fcx , lt, Some ( param) ) . into ( )
1194
1207
}
1195
1208
( GenericParamDefKind :: Type { .. } , GenericArg :: Type ( ty) ) => {
1196
- self . fcx . to_ty ( ty) . into ( )
1209
+ self . fcx . to_ty ( ty) . raw . into ( )
1197
1210
}
1198
1211
( GenericParamDefKind :: Const { .. } , GenericArg :: Const ( ct) ) => {
1199
1212
self . fcx . const_arg_to_const ( & ct. value , param. def_id ) . into ( )
@@ -1227,7 +1240,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
1227
1240
// is missing.
1228
1241
let default = tcx. bound_type_of ( param. def_id ) ;
1229
1242
self . fcx
1230
- . normalize_ty ( self . span , default. subst ( tcx, substs. unwrap ( ) ) )
1243
+ . normalize_ty_2 ( self . span , default. subst ( tcx, substs. unwrap ( ) ) )
1231
1244
. into ( )
1232
1245
} else {
1233
1246
// If no type arguments were provided, we have to infer them.
@@ -1250,13 +1263,13 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
1250
1263
}
1251
1264
}
1252
1265
1253
- let substs = self_ctor_substs. unwrap_or_else ( || {
1266
+ let substs_raw = self_ctor_substs. unwrap_or_else ( || {
1254
1267
<dyn AstConv < ' _ > >:: create_substs_for_generic_args (
1255
1268
tcx,
1256
1269
def_id,
1257
1270
& [ ] ,
1258
1271
has_self,
1259
- self_ty,
1272
+ self_ty. map ( |s| s . raw ) ,
1260
1273
& arg_count,
1261
1274
& mut CreateCtorSubstsContext {
1262
1275
fcx : self ,
@@ -1269,7 +1282,10 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
1269
1282
} ) ;
1270
1283
1271
1284
// First, store the "user substs" for later.
1272
- self . write_user_type_annotation_from_substs ( hir_id, def_id, substs, user_self_ty) ;
1285
+ self . write_user_type_annotation_from_substs ( hir_id, def_id, substs_raw, user_self_ty) ;
1286
+
1287
+ // Normalize only after registering type annotations.
1288
+ let substs = self . normalize ( span, substs_raw) ;
1273
1289
1274
1290
self . add_required_obligations_for_hir ( span, def_id, & substs, hir_id) ;
1275
1291
@@ -1287,6 +1303,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
1287
1303
// with the substituted impl type.
1288
1304
// This also occurs for an enum variant on a type alias.
1289
1305
let impl_ty = self . normalize ( span, tcx. bound_type_of ( impl_def_id) . subst ( tcx, substs) ) ;
1306
+ let self_ty = self . normalize ( span, self_ty) ;
1290
1307
match self . at ( & self . misc ( span) , self . param_env ) . eq ( impl_ty, self_ty) {
1291
1308
Ok ( ok) => self . register_infer_ok_obligations ( ok) ,
1292
1309
Err ( _) => {
0 commit comments