avoid instantiating infer vars with infer

This commit is contained in:
lcnr 2023-12-07 16:13:09 +01:00
parent 7df0c211ac
commit 40aa9f4fd9
2 changed files with 9 additions and 4 deletions

View File

@ -34,6 +34,7 @@
use rustc_middle::infer::unify_key::{ConstVariableOrigin, ConstVariableOriginKind}; use rustc_middle::infer::unify_key::{ConstVariableOrigin, ConstVariableOriginKind};
use rustc_middle::ty::error::{ExpectedFound, TypeError}; use rustc_middle::ty::error::{ExpectedFound, TypeError};
use rustc_middle::ty::relate::{RelateResult, TypeRelation}; use rustc_middle::ty::relate::{RelateResult, TypeRelation};
use rustc_middle::ty::TyVar;
use rustc_middle::ty::{self, InferConst, ToPredicate, Ty, TyCtxt, TypeVisitableExt}; use rustc_middle::ty::{self, InferConst, ToPredicate, Ty, TyCtxt, TypeVisitableExt};
use rustc_middle::ty::{IntType, UintType}; use rustc_middle::ty::{IntType, UintType};
use rustc_span::DUMMY_SP; use rustc_span::DUMMY_SP;
@ -459,7 +460,12 @@ pub fn instantiate(
ambient_variance, ambient_variance,
)?; )?;
self.infcx.inner.borrow_mut().type_variables().instantiate(b_vid, b_ty); // Constrain `b_vid` to the generalized type `b_ty`.
if let &ty::Infer(TyVar(b_ty_vid)) = b_ty.kind() {
self.infcx.inner.borrow_mut().type_variables().equate(b_vid, b_ty_vid);
} else {
self.infcx.inner.borrow_mut().type_variables().instantiate(b_vid, b_ty);
}
if needs_wf { if needs_wf {
self.obligations.push(Obligation::new( self.obligations.push(Obligation::new(

View File

@ -229,12 +229,11 @@ pub fn sub(&mut self, a: ty::TyVid, b: ty::TyVid) {
/// Precondition: `vid` must not have been previously instantiated. /// Precondition: `vid` must not have been previously instantiated.
pub fn instantiate(&mut self, vid: ty::TyVid, ty: Ty<'tcx>) { pub fn instantiate(&mut self, vid: ty::TyVid, ty: Ty<'tcx>) {
let vid = self.root_var(vid); let vid = self.root_var(vid);
debug_assert!(!ty.is_ty_var(), "instantiating ty var with var: {vid:?} {ty:?}");
debug_assert!(self.probe(vid).is_unknown()); debug_assert!(self.probe(vid).is_unknown());
debug_assert!( debug_assert!(
self.eq_relations().probe_value(vid).is_unknown(), self.eq_relations().probe_value(vid).is_unknown(),
"instantiating type variable `{:?}` twice: new-value = {:?}, old-value={:?}", "instantiating type variable `{vid:?}` twice: new-value = {ty:?}, old-value={:?}",
vid,
ty,
self.eq_relations().probe_value(vid) self.eq_relations().probe_value(vid)
); );
self.eq_relations().union_value(vid, TypeVariableValue::Known { value: ty }); self.eq_relations().union_value(vid, TypeVariableValue::Known { value: ty });