@@ -35,7 +35,7 @@ use rustc::traits::query::type_op::custom::CustomTypeOp;
35
35
use rustc:: traits:: query:: { Fallible , NoSolution } ;
36
36
use rustc:: traits:: { ObligationCause , PredicateObligations } ;
37
37
use rustc:: ty:: fold:: TypeFoldable ;
38
- use rustc:: ty:: subst:: { Subst , Substs , UnpackedKind } ;
38
+ use rustc:: ty:: subst:: { Subst , Substs , UnpackedKind , UserSubsts } ;
39
39
use rustc:: ty:: {
40
40
self , RegionVid , ToPolyTraitRef , Ty , TyCtxt , TyKind , UserType ,
41
41
CanonicalUserTypeAnnotation , UserTypeAnnotationIndex ,
@@ -283,7 +283,7 @@ impl<'a, 'b, 'gcx, 'tcx> Visitor<'tcx> for TypeVerifier<'a, 'b, 'gcx, 'tcx> {
283
283
location. to_locations ( ) ,
284
284
ConstraintCategory :: Boring ,
285
285
) {
286
- let annotation = self . cx . instantiated_type_annotations [ & annotation_index] ;
286
+ let annotation = & self . mir . user_type_annotations [ annotation_index] ;
287
287
span_mirbug ! (
288
288
self ,
289
289
constant,
@@ -293,6 +293,39 @@ impl<'a, 'b, 'gcx, 'tcx> Visitor<'tcx> for TypeVerifier<'a, 'b, 'gcx, 'tcx> {
293
293
terr,
294
294
) ;
295
295
}
296
+ } else {
297
+ match * constant. literal {
298
+ ty:: LazyConst :: Unevaluated ( def_id, substs) => {
299
+ if let Err ( terr) = self . cx . fully_perform_op (
300
+ location. to_locations ( ) ,
301
+ ConstraintCategory :: Boring ,
302
+ self . cx . param_env . and ( type_op:: ascribe_user_type:: AscribeUserType :: new (
303
+ constant. ty , def_id, UserSubsts { substs, user_self_ty : None } ,
304
+ ) ) ,
305
+ ) {
306
+ span_mirbug ! (
307
+ self ,
308
+ constant,
309
+ "bad constant type {:?} ({:?})" ,
310
+ constant,
311
+ terr
312
+ ) ;
313
+ }
314
+ }
315
+ ty:: LazyConst :: Evaluated ( lit) => {
316
+ if let ty:: FnDef ( def_id, substs) = lit. ty . sty {
317
+ let tcx = self . tcx ( ) ;
318
+
319
+ let instantiated_predicates = tcx
320
+ . predicates_of ( def_id)
321
+ . instantiate ( tcx, substs) ;
322
+ self . cx . normalize_and_prove_instantiated_predicates (
323
+ instantiated_predicates,
324
+ location. to_locations ( ) ,
325
+ ) ;
326
+ }
327
+ }
328
+ }
296
329
}
297
330
}
298
331
@@ -374,8 +407,9 @@ impl<'a, 'b, 'gcx, 'tcx> TypeVerifier<'a, 'b, 'gcx, 'tcx> {
374
407
}
375
408
}
376
409
377
- /// Checks that the constant's `ty` field matches up with what
378
- /// would be expected from its literal.
410
+ /// Checks that the constant's `ty` field matches up with what would be
411
+ /// expected from its literal. Unevaluated constants and well-formed
412
+ /// constraints are checked by `visit_constant`.
379
413
fn sanitize_constant ( & mut self , constant : & Constant < ' tcx > , location : Location ) {
380
414
debug ! (
381
415
"sanitize_constant(constant={:?}, location={:?})" ,
@@ -387,35 +421,6 @@ impl<'a, 'b, 'gcx, 'tcx> TypeVerifier<'a, 'b, 'gcx, 'tcx> {
387
421
ty:: LazyConst :: Unevaluated ( ..) => return ,
388
422
} ;
389
423
390
- // FIXME(#46702) -- We need some way to get the predicates
391
- // associated with the "pre-evaluated" form of the
392
- // constant. For example, consider that the constant
393
- // may have associated constant projections (`<Foo as
394
- // Trait<'a, 'b>>::SOME_CONST`) that impose
395
- // constraints on `'a` and `'b`. These constraints
396
- // would be lost if we just look at the normalized
397
- // value.
398
- if let ty:: FnDef ( def_id, substs) = literal. ty . sty {
399
- let tcx = self . tcx ( ) ;
400
- let type_checker = & mut self . cx ;
401
-
402
- // FIXME -- For now, use the substitutions from
403
- // `value.ty` rather than `value.val`. The
404
- // renumberer will rewrite them to independent
405
- // sets of regions; in principle, we ought to
406
- // derive the type of the `value.val` from "first
407
- // principles" and equate with value.ty, but as we
408
- // are transitioning to the miri-based system, we
409
- // don't have a handy function for that, so for
410
- // now we just ignore `value.val` regions.
411
-
412
- let instantiated_predicates = tcx. predicates_of ( def_id) . instantiate ( tcx, substs) ;
413
- type_checker. normalize_and_prove_instantiated_predicates (
414
- instantiated_predicates,
415
- location. to_locations ( ) ,
416
- ) ;
417
- }
418
-
419
424
debug ! ( "sanitize_constant: expected_ty={:?}" , literal. ty) ;
420
425
421
426
if let Err ( terr) = self . cx . eq_types (
@@ -740,15 +745,6 @@ struct TypeChecker<'a, 'gcx: 'tcx, 'tcx: 'a> {
740
745
reported_errors : FxHashSet < ( Ty < ' tcx > , Span ) > ,
741
746
borrowck_context : Option < & ' a mut BorrowCheckContext < ' a , ' tcx > > ,
742
747
universal_region_relations : Option < & ' a UniversalRegionRelations < ' tcx > > ,
743
- /// For each user-type annotation (identified by a UserTypeAnnotationIndex), we create
744
- /// an "instantiated" version at the beginning of type check, which replaces each
745
- /// canonical variable with a fresh inference variable. These instantiated versions are
746
- /// stored either in this field or in user_substs, depending on the kind of user-type
747
- /// annotation. They are then referenced by the code which has the job of enforcing these
748
- /// annotations. Part of the reason for this setup is that it allows us to enforce basic
749
- /// WF criteria on the types even if the code that referenced them is dead
750
- /// code (see #54943).
751
- instantiated_type_annotations : FxHashMap < UserTypeAnnotationIndex , Ty < ' tcx > > ,
752
748
}
753
749
754
750
struct BorrowCheckContext < ' a , ' tcx : ' a > {
@@ -905,23 +901,19 @@ impl<'a, 'gcx, 'tcx> TypeChecker<'a, 'gcx, 'tcx> {
905
901
borrowck_context,
906
902
reported_errors : Default :: default ( ) ,
907
903
universal_region_relations,
908
- instantiated_type_annotations : Default :: default ( ) ,
909
904
} ;
910
- checker. instantiate_user_type_annotations ( ) ;
905
+ checker. check_user_type_annotations ( ) ;
911
906
checker
912
907
}
913
908
914
- /// Instantiate canonical types from user type annotations in the `Mir` into the
915
- /// `TypeChecker`. Used when relating user type annotations and when checking if
916
- /// annotations are well-formed.
917
- fn instantiate_user_type_annotations ( & mut self ) {
909
+ /// Equate the inferred type and the annotated type for user type annotations
910
+ fn check_user_type_annotations ( & mut self ) {
918
911
debug ! (
919
- "instantiate_user_type_annotations : user_type_annotations={:?}" ,
912
+ "check_user_type_annotations : user_type_annotations={:?}" ,
920
913
self . mir. user_type_annotations
921
914
) ;
922
- for annotation_index in self . mir . user_type_annotations . indices ( ) {
923
- let CanonicalUserTypeAnnotation { span, ref user_ty, inferred_ty } =
924
- self . mir . user_type_annotations [ annotation_index] ;
915
+ for user_annotation in & self . mir . user_type_annotations {
916
+ let CanonicalUserTypeAnnotation { span, ref user_ty, inferred_ty } = * user_annotation;
925
917
let ( annotation, _) = self . infcx . instantiate_canonical_with_fresh_inference_vars (
926
918
span, user_ty
927
919
) ;
@@ -937,7 +929,7 @@ impl<'a, 'gcx, 'tcx> TypeChecker<'a, 'gcx, 'tcx> {
937
929
) {
938
930
span_mirbug ! (
939
931
self ,
940
- self . mir . user_type_annotations [ annotation_index ] ,
932
+ user_annotation ,
941
933
"bad user type ({:?} = {:?}): {:?}" ,
942
934
ty,
943
935
inferred_ty,
@@ -961,7 +953,7 @@ impl<'a, 'gcx, 'tcx> TypeChecker<'a, 'gcx, 'tcx> {
961
953
) {
962
954
span_mirbug ! (
963
955
self ,
964
- self . mir . user_type_annotations [ annotation_index ] ,
956
+ user_annotation ,
965
957
"bad user type AscribeUserType({:?}, {:?} {:?}): {:?}" ,
966
958
inferred_ty,
967
959
def_id,
@@ -971,12 +963,7 @@ impl<'a, 'gcx, 'tcx> TypeChecker<'a, 'gcx, 'tcx> {
971
963
}
972
964
} ,
973
965
}
974
- self . instantiated_type_annotations . insert ( annotation_index, inferred_ty) ;
975
966
}
976
- debug ! (
977
- "instantiate_user_type_annotations: instantiated_type_annotations={:?}" ,
978
- self . instantiated_type_annotations,
979
- ) ;
980
967
}
981
968
982
969
/// Given some operation `op` that manipulates types, proves
@@ -1108,7 +1095,7 @@ impl<'a, 'gcx, 'tcx> TypeChecker<'a, 'gcx, 'tcx> {
1108
1095
a, v, user_ty, locations,
1109
1096
) ;
1110
1097
1111
- let annotated_type = self . instantiated_type_annotations [ & user_ty. base ] ;
1098
+ let annotated_type = self . mir . user_type_annotations [ user_ty. base ] . inferred_ty ;
1112
1099
let mut curr_projected_ty = PlaceTy :: from_ty ( annotated_type) ;
1113
1100
1114
1101
let tcx = self . infcx . tcx ;
@@ -1293,7 +1280,7 @@ impl<'a, 'gcx, 'tcx> TypeChecker<'a, 'gcx, 'tcx> {
1293
1280
location. to_locations ( ) ,
1294
1281
ConstraintCategory :: Boring ,
1295
1282
) {
1296
- let annotation = self . instantiated_type_annotations [ & annotation_index] ;
1283
+ let annotation = & mir . user_type_annotations [ annotation_index] ;
1297
1284
span_mirbug ! (
1298
1285
self ,
1299
1286
stmt,
@@ -1352,7 +1339,7 @@ impl<'a, 'gcx, 'tcx> TypeChecker<'a, 'gcx, 'tcx> {
1352
1339
Locations :: All ( stmt. source_info . span ) ,
1353
1340
ConstraintCategory :: TypeAnnotation ,
1354
1341
) {
1355
- let annotation = self . instantiated_type_annotations [ & projection. base ] ;
1342
+ let annotation = & mir . user_type_annotations [ projection. base ] ;
1356
1343
span_mirbug ! (
1357
1344
self ,
1358
1345
stmt,
0 commit comments