Skip to content

Commit 40aa9f4

Browse files
committed
avoid instantiating infer vars with infer
1 parent 7df0c21 commit 40aa9f4

File tree

2 files changed

+9
-4
lines changed

2 files changed

+9
-4
lines changed

compiler/rustc_infer/src/infer/combine.rs

+7-1
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ use rustc_middle::infer::unify_key::{ConstVarValue, ConstVariableValue, EffectVa
3434
use rustc_middle::infer::unify_key::{ConstVariableOrigin, ConstVariableOriginKind};
3535
use rustc_middle::ty::error::{ExpectedFound, TypeError};
3636
use rustc_middle::ty::relate::{RelateResult, TypeRelation};
37+
use rustc_middle::ty::TyVar;
3738
use rustc_middle::ty::{self, InferConst, ToPredicate, Ty, TyCtxt, TypeVisitableExt};
3839
use rustc_middle::ty::{IntType, UintType};
3940
use rustc_span::DUMMY_SP;
@@ -459,7 +460,12 @@ impl<'infcx, 'tcx> CombineFields<'infcx, 'tcx> {
459460
ambient_variance,
460461
)?;
461462

462-
self.infcx.inner.borrow_mut().type_variables().instantiate(b_vid, b_ty);
463+
// Constrain `b_vid` to the generalized type `b_ty`.
464+
if let &ty::Infer(TyVar(b_ty_vid)) = b_ty.kind() {
465+
self.infcx.inner.borrow_mut().type_variables().equate(b_vid, b_ty_vid);
466+
} else {
467+
self.infcx.inner.borrow_mut().type_variables().instantiate(b_vid, b_ty);
468+
}
463469

464470
if needs_wf {
465471
self.obligations.push(Obligation::new(

compiler/rustc_infer/src/infer/type_variable.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -229,12 +229,11 @@ impl<'tcx> TypeVariableTable<'_, 'tcx> {
229229
/// Precondition: `vid` must not have been previously instantiated.
230230
pub fn instantiate(&mut self, vid: ty::TyVid, ty: Ty<'tcx>) {
231231
let vid = self.root_var(vid);
232+
debug_assert!(!ty.is_ty_var(), "instantiating ty var with var: {vid:?} {ty:?}");
232233
debug_assert!(self.probe(vid).is_unknown());
233234
debug_assert!(
234235
self.eq_relations().probe_value(vid).is_unknown(),
235-
"instantiating type variable `{:?}` twice: new-value = {:?}, old-value={:?}",
236-
vid,
237-
ty,
236+
"instantiating type variable `{vid:?}` twice: new-value = {ty:?}, old-value={:?}",
238237
self.eq_relations().probe_value(vid)
239238
);
240239
self.eq_relations().union_value(vid, TypeVariableValue::Known { value: ty });

0 commit comments

Comments
 (0)