Skip to content

Commit 710192d

Browse files
committed
const generics check subst in generalize
1 parent 1557fb0 commit 710192d

File tree

2 files changed

+24
-3
lines changed

2 files changed

+24
-3
lines changed

src/librustc_infer/infer/combine.rs

+22-3
Original file line numberDiff line numberDiff line change
@@ -343,7 +343,7 @@ impl<'infcx, 'tcx> CombineFields<'infcx, 'tcx> {
343343
for_vid: ty::TyVid,
344344
dir: RelationDir,
345345
) -> RelateResult<'tcx, Generalization<'tcx>> {
346-
debug!("generalize(ty={:?}, for_vid={:?}, dir={:?}", ty, for_vid, dir);
346+
debug!("generalize(ty={:?}, for_vid={:?}, dir={:?})", ty, for_vid, dir);
347347
// Determine the ambient variance within which `ty` appears.
348348
// The surrounding equation is:
349349
//
@@ -649,13 +649,17 @@ impl TypeRelation<'tcx> for Generalizer<'_, 'tcx> {
649649
) -> RelateResult<'tcx, &'tcx ty::Const<'tcx>> {
650650
assert_eq!(c, c2); // we are abusing TypeRelation here; both LHS and RHS ought to be ==
651651

652+
debug!("generalize: consts c={:?}", c);
652653
match c.val {
653654
ty::ConstKind::Infer(InferConst::Var(vid)) => {
654655
let mut inner = self.infcx.inner.borrow_mut();
655656
let variable_table = &mut inner.const_unification_table();
656657
let var_value = variable_table.probe_value(vid);
657658
match var_value.val {
658-
ConstVariableValue::Known { value: u } => self.relate(&u, &u),
659+
ConstVariableValue::Known { value: u } => {
660+
drop(inner);
661+
self.relate(&u, &u)
662+
}
659663
ConstVariableValue::Unknown { universe } => {
660664
if self.for_universe.can_name(universe) {
661665
Ok(c)
@@ -669,7 +673,22 @@ impl TypeRelation<'tcx> for Generalizer<'_, 'tcx> {
669673
}
670674
}
671675
}
672-
ty::ConstKind::Unevaluated(..) if self.tcx().lazy_normalization() => Ok(c),
676+
ty::ConstKind::Unevaluated(did, substs, promoted)
677+
if self.tcx().lazy_normalization() =>
678+
{
679+
// We have to generalize inference variables used in the generic substitutions,
680+
// as unevaluated consts may otherwise contain invalid inference variables.
681+
let new_substs =
682+
self.relate_with_variance(ty::Variance::Invariant, &substs, &substs)?;
683+
if new_substs != substs {
684+
Ok(self.tcx().mk_const(ty::Const {
685+
ty: c.ty,
686+
val: ty::ConstKind::Unevaluated(did, new_substs, promoted),
687+
}))
688+
} else {
689+
Ok(c)
690+
}
691+
}
673692
_ => relate::super_relate_consts(self, c, c),
674693
}
675694
}

src/librustc_middle/ty/structural_impls.rs

+2
Original file line numberDiff line numberDiff line change
@@ -885,6 +885,7 @@ impl<'tcx> TypeFoldable<'tcx> for interpret::GlobalId<'tcx> {
885885

886886
impl<'tcx> TypeFoldable<'tcx> for Ty<'tcx> {
887887
fn super_fold_with<F: TypeFolder<'tcx>>(&self, folder: &mut F) -> Self {
888+
debug!("Ty::super_fold_with({:?})", self);
888889
let kind = match self.kind {
889890
ty::RawPtr(tm) => ty::RawPtr(tm.fold_with(folder)),
890891
ty::Array(typ, sz) => ty::Array(typ.fold_with(folder), sz.fold_with(folder)),
@@ -1040,6 +1041,7 @@ impl<'tcx, T: TypeFoldable<'tcx>, I: Idx> TypeFoldable<'tcx> for IndexVec<I, T>
10401041

10411042
impl<'tcx> TypeFoldable<'tcx> for &'tcx ty::Const<'tcx> {
10421043
fn super_fold_with<F: TypeFolder<'tcx>>(&self, folder: &mut F) -> Self {
1044+
debug!("Const::super_fold_with({:?})", self);
10431045
let ty = self.ty.fold_with(folder);
10441046
let val = self.val.fold_with(folder);
10451047
if ty != self.ty || val != self.val {

0 commit comments

Comments
 (0)