Skip to content

Commit 88e0bea

Browse files
authored
Rollup merge of #90395 - b-naber:const-expr-type-relation, r=oli-obk
Restrict liveness of mutable borrow of inner infcx in ConstInferUnifier::consts Fixes #89304 r? ``@oli-obk``
2 parents b531364 + a39c50b commit 88e0bea

File tree

2 files changed

+40
-9
lines changed

2 files changed

+40
-9
lines changed

compiler/rustc_infer/src/infer/combine.rs

+20-9
Original file line numberDiff line numberDiff line change
@@ -866,6 +866,7 @@ impl TypeRelation<'tcx> for ConstInferUnifier<'_, 'tcx> {
866866
Ok(a.rebind(self.relate(a.skip_binder(), b.skip_binder())?))
867867
}
868868

869+
#[tracing::instrument(level = "debug", skip(self))]
869870
fn tys(&mut self, t: Ty<'tcx>, _t: Ty<'tcx>) -> RelateResult<'tcx, Ty<'tcx>> {
870871
debug_assert_eq!(t, _t);
871872
debug!("ConstInferUnifier: t={:?}", t);
@@ -941,6 +942,7 @@ impl TypeRelation<'tcx> for ConstInferUnifier<'_, 'tcx> {
941942
}
942943
}
943944

945+
#[tracing::instrument(level = "debug", skip(self))]
944946
fn consts(
945947
&mut self,
946948
c: &'tcx ty::Const<'tcx>,
@@ -951,29 +953,38 @@ impl TypeRelation<'tcx> for ConstInferUnifier<'_, 'tcx> {
951953

952954
match c.val {
953955
ty::ConstKind::Infer(InferConst::Var(vid)) => {
954-
let mut inner = self.infcx.inner.borrow_mut();
955-
let variable_table = &mut inner.const_unification_table();
956-
957956
// Check if the current unification would end up
958957
// unifying `target_vid` with a const which contains
959958
// an inference variable which is unioned with `target_vid`.
960959
//
961960
// Not doing so can easily result in stack overflows.
962-
if variable_table.unioned(self.target_vid, vid) {
961+
if self
962+
.infcx
963+
.inner
964+
.borrow_mut()
965+
.const_unification_table()
966+
.unioned(self.target_vid, vid)
967+
{
963968
return Err(TypeError::CyclicConst(c));
964969
}
965970

966-
let var_value = variable_table.probe_value(vid);
971+
let var_value =
972+
self.infcx.inner.borrow_mut().const_unification_table().probe_value(vid);
967973
match var_value.val {
968974
ConstVariableValue::Known { value: u } => self.consts(u, u),
969975
ConstVariableValue::Unknown { universe } => {
970976
if self.for_universe.can_name(universe) {
971977
Ok(c)
972978
} else {
973-
let new_var_id = variable_table.new_key(ConstVarValue {
974-
origin: var_value.origin,
975-
val: ConstVariableValue::Unknown { universe: self.for_universe },
976-
});
979+
let new_var_id =
980+
self.infcx.inner.borrow_mut().const_unification_table().new_key(
981+
ConstVarValue {
982+
origin: var_value.origin,
983+
val: ConstVariableValue::Unknown {
984+
universe: self.for_universe,
985+
},
986+
},
987+
);
977988
Ok(self.tcx().mk_const_var(new_var_id, c.ty))
978989
}
979990
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// check-pass
2+
3+
#![feature(generic_const_exprs)]
4+
#![allow(incomplete_features)]
5+
6+
struct GenericStruct<const T: usize> { val: i64 }
7+
8+
impl<const T: usize> From<GenericStruct<T>> for GenericStruct<{T + 1}> {
9+
fn from(other: GenericStruct<T>) -> Self {
10+
Self { val: other.val }
11+
}
12+
}
13+
14+
impl<const T: usize> From<GenericStruct<{T + 1}>> for GenericStruct<T> {
15+
fn from(other: GenericStruct<{T + 1}>) -> Self {
16+
Self { val: other.val }
17+
}
18+
}
19+
20+
fn main() {}

0 commit comments

Comments
 (0)