Skip to content

Commit 4d917fa

Browse files
committed
Reduce destructuring and re-interning where possible
1 parent 019dba0 commit 4d917fa

File tree

1 file changed

+18
-15
lines changed

1 file changed

+18
-15
lines changed

compiler/rustc_middle/src/ty/relate.rs

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -506,18 +506,21 @@ pub fn super_relate_consts<R: TypeRelation<'tcx>>(
506506
"cannot relate constants of different types"
507507
);
508508

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);
510512

511513
// Currently, the values that can be unified are primitive types,
512514
// and those that derive both `PartialEq` and `Eq`, corresponding
513515
// to structural-match types.
514-
let new_const_val = match (eagerly_eval(a), eagerly_eval(b)) {
516+
match (a.val, b.val) {
515517
(ty::ConstKind::Infer(_), _) | (_, ty::ConstKind::Infer(_)) => {
516518
// The caller should handle these cases!
517519
bug!("var types encountered in super_relate_consts: {:?} {:?}", a, b)
518520
}
519521

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),
521524

522525
(ty::ConstKind::Param(a_p), ty::ConstKind::Param(b_p)) if a_p.index == b_p.index => {
523526
return Ok(a);
@@ -526,15 +529,15 @@ pub fn super_relate_consts<R: TypeRelation<'tcx>>(
526529
return Ok(a);
527530
}
528531
(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) {
530533
(ConstValue::Scalar(a_val), ConstValue::Scalar(b_val)) => {
531534
if a_val == b_val {
532-
Ok(ConstValue::Scalar(a_val))
535+
Ok(a)
533536
} else if let ty::FnPtr(_) = a.ty.kind() {
534537
let a_instance = tcx.global_alloc(a_val.assert_ptr().alloc_id).unwrap_fn();
535538
let b_instance = tcx.global_alloc(b_val.assert_ptr().alloc_id).unwrap_fn();
536539
if a_instance == b_instance {
537-
Ok(ConstValue::Scalar(a_val))
540+
Ok(a)
538541
} else {
539542
Err(TypeError::ConstMismatch(expected_found(relation, a, b)))
540543
}
@@ -547,7 +550,7 @@ pub fn super_relate_consts<R: TypeRelation<'tcx>>(
547550
let a_bytes = get_slice_bytes(&tcx, a_val);
548551
let b_bytes = get_slice_bytes(&tcx, b_val);
549552
if a_bytes == b_bytes {
550-
Ok(a_val)
553+
Ok(a)
551554
} else {
552555
Err(TypeError::ConstMismatch(expected_found(relation, a, b)))
553556
}
@@ -567,7 +570,7 @@ pub fn super_relate_consts<R: TypeRelation<'tcx>>(
567570
relation.consts(a_field, b_field)?;
568571
}
569572

570-
Ok(a_val)
573+
Ok(a)
571574
} else {
572575
Err(TypeError::ConstMismatch(expected_found(relation, a, b)))
573576
}
@@ -585,17 +588,15 @@ pub fn super_relate_consts<R: TypeRelation<'tcx>>(
585588
}
586589

587590
_ => Err(TypeError::ConstMismatch(expected_found(relation, a, b))),
588-
};
589-
590-
new_val.map(ty::ConstKind::Value)
591+
}
591592
}
592593

593594
(
594595
ty::ConstKind::Unevaluated(a_def, a_substs, None),
595596
ty::ConstKind::Unevaluated(b_def, b_substs, None),
596597
) if tcx.features().const_evaluatable_checked && !relation.visit_ct_substs() => {
597598
if tcx.try_unify_abstract_consts(((a_def, a_substs), (b_def, b_substs))) {
598-
Ok(a.val)
599+
Ok(a)
599600
} else {
600601
Err(TypeError::ConstMismatch(expected_found(relation, a, b)))
601602
}
@@ -610,11 +611,13 @@ pub fn super_relate_consts<R: TypeRelation<'tcx>>(
610611
) if a_def == b_def && a_promoted == b_promoted => {
611612
let substs =
612613
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+
}))
614618
}
615619
_ => 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+
}
618621
}
619622

620623
impl<'tcx> Relate<'tcx> for &'tcx ty::List<ty::Binder<ty::ExistentialPredicate<'tcx>>> {

0 commit comments

Comments
 (0)