Skip to content

Commit 624e34a

Browse files
committed
Account for const generalisation in nll_relate
1 parent 12f68e6 commit 624e34a

File tree

2 files changed

+21
-7
lines changed

2 files changed

+21
-7
lines changed

src/librustc/infer/combine.rs

+3-4
Original file line numberDiff line numberDiff line change
@@ -494,7 +494,7 @@ impl TypeRelation<'tcx> for Generalizer<'_, 'tcx> {
494494
if sub_vid == self.for_vid_sub_root {
495495
// If sub-roots are equal, then `for_vid` and
496496
// `vid` are related via subtyping.
497-
return Err(TypeError::CyclicTy(self.root_ty));
497+
Err(TypeError::CyclicTy(self.root_ty))
498498
} else {
499499
match variables.probe(vid) {
500500
TypeVariableValue::Known { value: u } => {
@@ -527,7 +527,7 @@ impl TypeRelation<'tcx> for Generalizer<'_, 'tcx> {
527527
let u = self.tcx().mk_ty_var(new_var_id);
528528
debug!("generalize: replacing original vid={:?} with new={:?}",
529529
vid, u);
530-
return Ok(u);
530+
Ok(u)
531531
}
532532
}
533533
}
@@ -616,8 +616,7 @@ impl TypeRelation<'tcx> for Generalizer<'_, 'tcx> {
616616
origin: var_value.origin,
617617
val: ConstVariableValue::Unknown { universe: self.for_universe },
618618
});
619-
let u = self.tcx().mk_const_var(new_var_id, c.ty);
620-
return Ok(u);
619+
Ok(self.tcx().mk_const_var(new_var_id, c.ty))
621620
}
622621
}
623622
}

src/librustc/infer/nll_relate/mod.rs

+18-3
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ use crate::ty::fold::{TypeFoldable, TypeVisitor};
2828
use crate::ty::relate::{self, Relate, RelateResult, TypeRelation};
2929
use crate::ty::subst::GenericArg;
3030
use crate::ty::{self, Ty, TyCtxt, InferConst};
31+
use crate::infer::{ConstVariableValue, ConstVarValue};
3132
use crate::mir::interpret::ConstValue;
3233
use rustc_data_structures::fx::FxHashMap;
3334
use std::fmt::Debug;
@@ -324,7 +325,7 @@ where
324325
let vid = pair.vid();
325326
let value_ty = pair.value_ty();
326327

327-
// FIXME -- this logic assumes invariance, but that is wrong.
328+
// FIXME(invariance) -- this logic assumes invariance, but that is wrong.
328329
// This only presently applies to chalk integration, as NLL
329330
// doesn't permit type variables to appear on both sides (and
330331
// doesn't use lazy norm).
@@ -629,6 +630,7 @@ where
629630
// Forbid inference variables in the RHS.
630631
bug!("unexpected inference var {:?}", b)
631632
}
633+
// FIXME(invariance): see the related FIXME above.
632634
_ => self.infcx.super_combine_consts(self, a, b)
633635
}
634636
}
@@ -997,11 +999,24 @@ where
997999
_: &'tcx ty::Const<'tcx>,
9981000
) -> RelateResult<'tcx, &'tcx ty::Const<'tcx>> {
9991001
match a.val {
1002+
ConstValue::Infer(InferConst::Var(_)) if D::forbid_inference_vars() => {
1003+
bug!(
1004+
"unexpected inference variable encountered in NLL generalization: {:?}",
1005+
a
1006+
);
1007+
}
10001008
ConstValue::Infer(InferConst::Var(vid)) => {
10011009
let mut variable_table = self.infcx.const_unification_table.borrow_mut();
1002-
match variable_table.probe_value(vid).val.known() {
1010+
let var_value = variable_table.probe_value(vid);
1011+
match var_value.val.known() {
10031012
Some(u) => self.relate(&u, &u),
1004-
None => Ok(a),
1013+
None => {
1014+
let new_var_id = variable_table.new_key(ConstVarValue {
1015+
origin: var_value.origin,
1016+
val: ConstVariableValue::Unknown { universe: self.universe },
1017+
});
1018+
Ok(self.tcx().mk_const_var(new_var_id, a.ty))
1019+
}
10051020
}
10061021
}
10071022
_ => relate::super_relate_consts(self, a, a),

0 commit comments

Comments
 (0)