Skip to content

Commit

Permalink
stop categorizing inference variables as diverging when created
Browse files Browse the repository at this point in the history
Instead, we now rely on the code that looks for a NeverToAny adjustment.
  • Loading branch information
nikomatsakis authored and Mark-Simulacrum committed Sep 17, 2021
1 parent 1c004f2 commit bc2ece2
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 63 deletions.
15 changes: 8 additions & 7 deletions compiler/rustc_infer/src/infer/combine.rs
Expand Up @@ -22,14 +22,14 @@
// is also useful to track which value is the "expected" value in
// terms of error reporting.

use super::equate::Equate;
use super::glb::Glb;
use super::lub::Lub;
use super::sub::Sub;
use super::type_variable::TypeVariableValue;
use super::unify_key::replace_if_possible;
use super::unify_key::{ConstVarValue, ConstVariableValue};
use super::unify_key::{ConstVariableOrigin, ConstVariableOriginKind};
use super::{equate::Equate, type_variable::Diverging};
use super::{InferCtxt, MiscVariable, TypeTrace};

use crate::traits::{Obligation, PredicateObligations};
Expand Down Expand Up @@ -645,7 +645,7 @@ impl TypeRelation<'tcx> for Generalizer<'_, 'tcx> {
.inner
.borrow_mut()
.type_variables()
.new_var(self.for_universe, Diverging::NotDiverging, origin);
.new_var(self.for_universe, origin);
let u = self.tcx().mk_ty_var(new_var_id);

// Record that we replaced `vid` with `new_var_id` as part of a generalization
Expand Down Expand Up @@ -885,11 +885,12 @@ impl TypeRelation<'tcx> for ConstInferUnifier<'_, 'tcx> {

let origin =
*self.infcx.inner.borrow_mut().type_variables().var_origin(vid);
let new_var_id = self.infcx.inner.borrow_mut().type_variables().new_var(
self.for_universe,
Diverging::NotDiverging,
origin,
);
let new_var_id = self
.infcx
.inner
.borrow_mut()
.type_variables()
.new_var(self.for_universe, origin);
let u = self.tcx().mk_ty_var(new_var_id);
debug!(
"ConstInferUnifier: replacing original vid={:?} with new={:?}",
Expand Down
36 changes: 5 additions & 31 deletions compiler/rustc_infer/src/infer/mod.rs
Expand Up @@ -46,7 +46,7 @@ use self::region_constraints::{GenericKind, RegionConstraintData, VarInfos, Veri
use self::region_constraints::{
RegionConstraintCollector, RegionConstraintStorage, RegionSnapshot,
};
use self::type_variable::{Diverging, TypeVariableOrigin, TypeVariableOriginKind};
use self::type_variable::{TypeVariableOrigin, TypeVariableOriginKind};

pub mod at;
pub mod canonical;
Expand Down Expand Up @@ -701,23 +701,6 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
t.fold_with(&mut self.freshener())
}

/// Returns whether `ty` is a diverging type variable or not.
/// (If `ty` is not a type variable at all, returns not diverging.)
///
/// No attempt is made to resolve `ty`.
pub fn type_var_diverges(&'a self, ty: Ty<'_>) -> Diverging {
match *ty.kind() {
ty::Infer(ty::TyVar(vid)) => self.ty_vid_diverges(vid),
_ => Diverging::NotDiverging,
}
}

/// Returns true if the type inference variable `vid` was created
/// as a diverging type variable. No attempt is made to resolve `vid`.
pub fn ty_vid_diverges(&'a self, vid: ty::TyVid) -> Diverging {
self.inner.borrow_mut().type_variables().var_diverges(vid)
}

/// Returns the origin of the type variable identified by `vid`, or `None`
/// if this is not a type variable.
///
Expand Down Expand Up @@ -1081,31 +1064,23 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
self.inner.borrow_mut().type_variables().num_vars()
}

pub fn next_ty_var_id(&self, diverging: Diverging, origin: TypeVariableOrigin) -> TyVid {
self.inner.borrow_mut().type_variables().new_var(self.universe(), diverging, origin)
pub fn next_ty_var_id(&self, origin: TypeVariableOrigin) -> TyVid {
self.inner.borrow_mut().type_variables().new_var(self.universe(), origin)
}

pub fn next_ty_var(&self, origin: TypeVariableOrigin) -> Ty<'tcx> {
self.tcx.mk_ty_var(self.next_ty_var_id(Diverging::NotDiverging, origin))
self.tcx.mk_ty_var(self.next_ty_var_id(origin))
}

pub fn next_ty_var_in_universe(
&self,
origin: TypeVariableOrigin,
universe: ty::UniverseIndex,
) -> Ty<'tcx> {
let vid = self.inner.borrow_mut().type_variables().new_var(
universe,
Diverging::NotDiverging,
origin,
);
let vid = self.inner.borrow_mut().type_variables().new_var(universe, origin);
self.tcx.mk_ty_var(vid)
}

pub fn next_diverging_ty_var(&self, origin: TypeVariableOrigin) -> Ty<'tcx> {
self.tcx.mk_ty_var(self.next_ty_var_id(Diverging::Diverges, origin))
}

pub fn next_const_var(
&self,
ty: Ty<'tcx>,
Expand Down Expand Up @@ -1217,7 +1192,6 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
// as the substitutions for the default, `(T, U)`.
let ty_var_id = self.inner.borrow_mut().type_variables().new_var(
self.universe(),
Diverging::NotDiverging,
TypeVariableOrigin {
kind: TypeVariableOriginKind::TypeParameterDefinition(
param.name,
Expand Down
4 changes: 1 addition & 3 deletions compiler/rustc_infer/src/infer/nll_relate/mod.rs
Expand Up @@ -22,7 +22,6 @@
//! constituents)

use crate::infer::combine::ConstEquateRelation;
use crate::infer::type_variable::Diverging;
use crate::infer::InferCtxt;
use crate::infer::{ConstVarValue, ConstVariableValue};
use rustc_data_structures::fx::FxHashMap;
Expand Down Expand Up @@ -927,8 +926,7 @@ where
// Replacing with a new variable in the universe `self.universe`,
// it will be unified later with the original type variable in
// the universe `_universe`.
let new_var_id =
variables.new_var(self.universe, Diverging::NotDiverging, origin);
let new_var_id = variables.new_var(self.universe, origin);

let u = self.tcx().mk_ty_var(new_var_id);
debug!("generalize: replacing original vid={:?} with new={:?}", vid, u);
Expand Down
23 changes: 2 additions & 21 deletions compiler/rustc_infer/src/infer/type_variable.rs
Expand Up @@ -139,13 +139,6 @@ pub enum TypeVariableOriginKind {

pub(crate) struct TypeVariableData {
origin: TypeVariableOrigin,
diverging: Diverging,
}

#[derive(Copy, Clone, Debug)]
pub enum Diverging {
NotDiverging,
Diverges,
}

#[derive(Copy, Clone, Debug)]
Expand Down Expand Up @@ -195,14 +188,6 @@ impl<'tcx> TypeVariableStorage<'tcx> {
}

impl<'tcx> TypeVariableTable<'_, 'tcx> {
/// Returns the diverges flag given when `vid` was created.
///
/// Note that this function does not return care whether
/// `vid` has been unified with something else or not.
pub fn var_diverges(&self, vid: ty::TyVid) -> Diverging {
self.storage.values.get(vid.index()).diverging
}

/// Returns the origin that was given when `vid` was created.
///
/// Note that this function does not return care whether
Expand Down Expand Up @@ -264,21 +249,17 @@ impl<'tcx> TypeVariableTable<'_, 'tcx> {
pub fn new_var(
&mut self,
universe: ty::UniverseIndex,
diverging: Diverging,
origin: TypeVariableOrigin,
) -> ty::TyVid {
let eq_key = self.eq_relations().new_key(TypeVariableValue::Unknown { universe });

let sub_key = self.sub_relations().new_key(());
assert_eq!(eq_key.vid, sub_key);

let index = self.values().push(TypeVariableData { origin, diverging });
let index = self.values().push(TypeVariableData { origin });
assert_eq!(eq_key.vid.as_u32(), index as u32);

debug!(
"new_var(index={:?}, universe={:?}, diverging={:?}, origin={:?}",
eq_key.vid, universe, diverging, origin,
);
debug!("new_var(index={:?}, universe={:?}, origin={:?}", eq_key.vid, universe, origin,);

eq_key.vid
}
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_typeck/src/check/expr.rs
Expand Up @@ -77,7 +77,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
!self.typeck_results.borrow().adjustments().contains_key(expr.hir_id),
"expression with never type wound up being adjusted"
);
let adj_ty = self.next_diverging_ty_var(TypeVariableOrigin {
let adj_ty = self.next_ty_var(TypeVariableOrigin {
kind: TypeVariableOriginKind::AdjustmentType,
span: expr.span,
});
Expand Down

0 comments on commit bc2ece2

Please sign in to comment.