@@ -506,18 +506,21 @@ pub fn super_relate_consts<R: TypeRelation<'tcx>>(
506
506
"cannot relate constants of different types"
507
507
) ;
508
508
509
- let eagerly_eval = |x : & ' tcx ty:: Const < ' tcx > | x. eval ( tcx, relation. param_env ( ) ) . val ;
509
+ let eagerly_eval = |x : & ' tcx ty:: Const < ' tcx > | x. eval ( tcx, relation. param_env ( ) ) ;
510
+ let a = eagerly_eval ( a) ;
511
+ let b = eagerly_eval ( b) ;
510
512
511
513
// Currently, the values that can be unified are primitive types,
512
514
// and those that derive both `PartialEq` and `Eq`, corresponding
513
515
// to structural-match types.
514
- let new_const_val = match ( eagerly_eval ( a ) , eagerly_eval ( b ) ) {
516
+ match ( a . val , b . val ) {
515
517
( ty:: ConstKind :: Infer ( _) , _) | ( _, ty:: ConstKind :: Infer ( _) ) => {
516
518
// The caller should handle these cases!
517
519
bug ! ( "var types encountered in super_relate_consts: {:?} {:?}" , a, b)
518
520
}
519
521
520
- ( ty:: ConstKind :: Error ( d) , _) | ( _, ty:: ConstKind :: Error ( d) ) => Ok ( ty:: ConstKind :: Error ( d) ) ,
522
+ ( ty:: ConstKind :: Error ( _) , _) => Ok ( a) ,
523
+ ( _, ty:: ConstKind :: Error ( _) ) => Ok ( b) ,
521
524
522
525
( ty:: ConstKind :: Param ( a_p) , ty:: ConstKind :: Param ( b_p) ) if a_p. index == b_p. index => {
523
526
return Ok ( a) ;
@@ -526,15 +529,15 @@ pub fn super_relate_consts<R: TypeRelation<'tcx>>(
526
529
return Ok ( a) ;
527
530
}
528
531
( ty:: ConstKind :: Value ( a_val) , ty:: ConstKind :: Value ( b_val) ) => {
529
- let new_val = match ( a_val, b_val) {
532
+ match ( a_val, b_val) {
530
533
( ConstValue :: Scalar ( a_val) , ConstValue :: Scalar ( b_val) ) => {
531
534
if a_val == b_val {
532
- Ok ( ConstValue :: Scalar ( a_val ) )
535
+ Ok ( a )
533
536
} else if let ty:: FnPtr ( _) = a. ty . kind ( ) {
534
537
let a_instance = tcx. global_alloc ( a_val. assert_ptr ( ) . alloc_id ) . unwrap_fn ( ) ;
535
538
let b_instance = tcx. global_alloc ( b_val. assert_ptr ( ) . alloc_id ) . unwrap_fn ( ) ;
536
539
if a_instance == b_instance {
537
- Ok ( ConstValue :: Scalar ( a_val ) )
540
+ Ok ( a )
538
541
} else {
539
542
Err ( TypeError :: ConstMismatch ( expected_found ( relation, a, b) ) )
540
543
}
@@ -547,7 +550,7 @@ pub fn super_relate_consts<R: TypeRelation<'tcx>>(
547
550
let a_bytes = get_slice_bytes ( & tcx, a_val) ;
548
551
let b_bytes = get_slice_bytes ( & tcx, b_val) ;
549
552
if a_bytes == b_bytes {
550
- Ok ( a_val )
553
+ Ok ( a )
551
554
} else {
552
555
Err ( TypeError :: ConstMismatch ( expected_found ( relation, a, b) ) )
553
556
}
@@ -567,7 +570,7 @@ pub fn super_relate_consts<R: TypeRelation<'tcx>>(
567
570
relation. consts ( a_field, b_field) ?;
568
571
}
569
572
570
- Ok ( a_val )
573
+ Ok ( a )
571
574
} else {
572
575
Err ( TypeError :: ConstMismatch ( expected_found ( relation, a, b) ) )
573
576
}
@@ -585,17 +588,15 @@ pub fn super_relate_consts<R: TypeRelation<'tcx>>(
585
588
}
586
589
587
590
_ => Err ( TypeError :: ConstMismatch ( expected_found ( relation, a, b) ) ) ,
588
- } ;
589
-
590
- new_val. map ( ty:: ConstKind :: Value )
591
+ }
591
592
}
592
593
593
594
(
594
595
ty:: ConstKind :: Unevaluated ( a_def, a_substs, None ) ,
595
596
ty:: ConstKind :: Unevaluated ( b_def, b_substs, None ) ,
596
597
) if tcx. features ( ) . const_evaluatable_checked && !relation. visit_ct_substs ( ) => {
597
598
if tcx. try_unify_abstract_consts ( ( ( a_def, a_substs) , ( b_def, b_substs) ) ) {
598
- Ok ( a. val )
599
+ Ok ( a)
599
600
} else {
600
601
Err ( TypeError :: ConstMismatch ( expected_found ( relation, a, b) ) )
601
602
}
@@ -610,11 +611,13 @@ pub fn super_relate_consts<R: TypeRelation<'tcx>>(
610
611
) if a_def == b_def && a_promoted == b_promoted => {
611
612
let substs =
612
613
relation. relate_with_variance ( ty:: Variance :: Invariant , a_substs, b_substs) ?;
613
- Ok ( ty:: ConstKind :: Unevaluated ( a_def, substs, a_promoted) )
614
+ Ok ( tcx. mk_const ( ty:: Const {
615
+ val : ty:: ConstKind :: Unevaluated ( a_def, substs, a_promoted) ,
616
+ ty : a. ty ,
617
+ } ) )
614
618
}
615
619
_ => Err ( TypeError :: ConstMismatch ( expected_found ( relation, a, b) ) ) ,
616
- } ;
617
- new_const_val. map ( |val| tcx. mk_const ( ty:: Const { val, ty : a. ty } ) )
620
+ }
618
621
}
619
622
620
623
impl < ' tcx > Relate < ' tcx > for & ' tcx ty:: List < ty:: Binder < ty:: ExistentialPredicate < ' tcx > > > {
0 commit comments