Skip to content

Commit

Permalink
Auto merge of #120021 - lcnr:const-var-value, r=<try>
Browse files Browse the repository at this point in the history
don't store const var origins for known vars

r? types
  • Loading branch information
bors committed Jan 16, 2024
2 parents f9c2421 + 290651b commit c070016
Show file tree
Hide file tree
Showing 8 changed files with 70 additions and 165 deletions.
33 changes: 23 additions & 10 deletions compiler/rustc_infer/src/infer/error_reporting/need_type_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ use rustc_hir::def_id::{DefId, LocalDefId};
use rustc_hir::intravisit::{self, Visitor};
use rustc_hir::{Body, Closure, Expr, ExprKind, FnRetTy, HirId, Local, LocalSource};
use rustc_middle::hir::nested_filter;
use rustc_middle::infer::unify_key::{ConstVariableOrigin, ConstVariableOriginKind};
use rustc_middle::infer::unify_key::{
ConstVariableOrigin, ConstVariableOriginKind, ConstVariableValue,
};
use rustc_middle::ty::adjustment::{Adjust, Adjustment, AutoBorrow};
use rustc_middle::ty::print::{FmtPrinter, PrettyPrinter, Print, Printer};
use rustc_middle::ty::{self, InferConst};
Expand Down Expand Up @@ -178,17 +180,23 @@ fn fmt_printer<'a, 'tcx>(infcx: &'a InferCtxt<'tcx>, ns: Namespace) -> FmtPrinte
}
};
printer.ty_infer_name_resolver = Some(Box::new(ty_getter));
let const_getter = move |ct_vid| {
if infcx.probe_const_var(ct_vid).is_ok() {
let const_getter = move |ct_vid| match infcx
.inner
.borrow_mut()
.const_unification_table()
.probe_value(ct_vid)
{
ConstVariableValue::Known { value: _ } => {
warn!("resolved const var in error message");
}
if let ConstVariableOriginKind::ConstParameterDefinition(name, _) =
infcx.inner.borrow_mut().const_unification_table().probe_value(ct_vid).origin.kind
{
return Some(name);
} else {
None
}
ConstVariableValue::Unknown { origin, universe: _ } => {
if let ConstVariableOriginKind::ConstParameterDefinition(name, _) = origin.kind {
return Some(name);
} else {
None
}
}
};
printer.const_infer_name_resolver = Some(Box::new(const_getter));
printer
Expand Down Expand Up @@ -303,7 +311,12 @@ impl<'tcx> InferCtxt<'tcx> {
GenericArgKind::Const(ct) => {
if let ty::ConstKind::Infer(InferConst::Var(vid)) = ct.kind() {
let origin =
self.inner.borrow_mut().const_unification_table().probe_value(vid).origin;
match self.inner.borrow_mut().const_unification_table().probe_value(vid) {
ConstVariableValue::Known { value } => {
bug!("resolved infer var: {vid:?} {value}")
}
ConstVariableValue::Unknown { origin, universe: _ } => origin,
};
if let ConstVariableOriginKind::ConstParameterDefinition(name, def_id) =
origin.kind
{
Expand Down
10 changes: 2 additions & 8 deletions compiler/rustc_infer/src/infer/freshen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,14 +146,8 @@ impl<'a, 'tcx> TypeFolder<TyCtxt<'tcx>> for TypeFreshener<'a, 'tcx> {
fn fold_const(&mut self, ct: ty::Const<'tcx>) -> ty::Const<'tcx> {
match ct.kind() {
ty::ConstKind::Infer(ty::InferConst::Var(v)) => {
let opt_ct = self
.infcx
.inner
.borrow_mut()
.const_unification_table()
.probe_value(v)
.val
.known();
let opt_ct =
self.infcx.inner.borrow_mut().const_unification_table().probe_value(v).known();
self.freshen_const(opt_ct, ty::InferConst::Var(v), ty::InferConst::Fresh, ct.ty())
}
ty::ConstKind::Infer(ty::InferConst::EffectVar(v)) => {
Expand Down
11 changes: 9 additions & 2 deletions compiler/rustc_infer/src/infer/fudge.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use rustc_middle::infer::unify_key::ConstVidKey;
use rustc_middle::infer::unify_key::{ConstVariableOriginKind, ConstVariableValue, ConstVidKey};
use rustc_middle::ty::fold::{TypeFoldable, TypeFolder, TypeSuperFoldable};
use rustc_middle::ty::{self, ConstVid, FloatVid, IntVid, RegionVid, Ty, TyCtxt, TyVid};

Expand Down Expand Up @@ -28,10 +28,17 @@ fn const_vars_since_snapshot<'tcx>(
snapshot_var_len: usize,
) -> (Range<ConstVid>, Vec<ConstVariableOrigin>) {
let range = vars_since_snapshot(table, snapshot_var_len);

(
range.start.vid..range.end.vid,
(range.start.index()..range.end.index())
.map(|index| table.probe_value(ConstVid::from_u32(index)).origin)
.map(|index| match table.probe_value(ConstVid::from_u32(index)) {
ConstVariableValue::Known { value: _ } => ConstVariableOrigin {
kind: ConstVariableOriginKind::MiscVariable,
span: rustc_span::DUMMY_SP,
},
ConstVariableValue::Unknown { origin, universe: _ } => origin,
})
.collect(),
)
}
Expand Down
21 changes: 7 additions & 14 deletions compiler/rustc_infer/src/infer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ use rustc_data_structures::unify as ut;
use rustc_errors::{DiagCtxt, DiagnosticBuilder, ErrorGuaranteed};
use rustc_hir::def_id::{DefId, LocalDefId};
use rustc_middle::infer::canonical::{Canonical, CanonicalVarValues};
use rustc_middle::infer::unify_key::{ConstVarValue, ConstVariableValue, EffectVarValue};
use rustc_middle::infer::unify_key::{ConstVariableOrigin, ConstVariableOriginKind, ToType};
use rustc_middle::infer::unify_key::{ConstVariableValue, EffectVarValue};
use rustc_middle::mir::interpret::{ErrorHandled, EvalToValTreeResult};
use rustc_middle::mir::ConstraintCategory;
use rustc_middle::traits::{select, DefiningAnchor};
Expand Down Expand Up @@ -1086,7 +1086,7 @@ impl<'tcx> InferCtxt<'tcx> {
.inner
.borrow_mut()
.const_unification_table()
.new_key(ConstVarValue { origin, val: ConstVariableValue::Unknown { universe } })
.new_key(ConstVariableValue::Unknown { origin, universe })
.vid;
ty::Const::new_var(self.tcx, vid, ty)
}
Expand All @@ -1095,10 +1095,7 @@ impl<'tcx> InferCtxt<'tcx> {
self.inner
.borrow_mut()
.const_unification_table()
.new_key(ConstVarValue {
origin,
val: ConstVariableValue::Unknown { universe: self.universe() },
})
.new_key(ConstVariableValue::Unknown { origin, universe: self.universe() })
.vid
}

Expand Down Expand Up @@ -1217,10 +1214,7 @@ impl<'tcx> InferCtxt<'tcx> {
.inner
.borrow_mut()
.const_unification_table()
.new_key(ConstVarValue {
origin,
val: ConstVariableValue::Unknown { universe: self.universe() },
})
.new_key(ConstVariableValue::Unknown { origin, universe: self.universe() })
.vid;
ty::Const::new_var(
self.tcx,
Expand Down Expand Up @@ -1410,9 +1404,9 @@ impl<'tcx> InferCtxt<'tcx> {
}

pub fn probe_const_var(&self, vid: ty::ConstVid) -> Result<ty::Const<'tcx>, ty::UniverseIndex> {
match self.inner.borrow_mut().const_unification_table().probe_value(vid).val {
match self.inner.borrow_mut().const_unification_table().probe_value(vid) {
ConstVariableValue::Known { value } => Ok(value),
ConstVariableValue::Unknown { universe } => Err(universe),
ConstVariableValue::Unknown { origin: _, universe } => Err(universe),
}
}

Expand Down Expand Up @@ -1709,7 +1703,7 @@ impl<'tcx> InferCtxt<'tcx> {
// `ty::ConstKind::Infer(ty::InferConst::Var(v))`.
//
// Not `inlined_probe_value(v)` because this call site is colder.
match self.inner.borrow_mut().const_unification_table().probe_value(v).val {
match self.inner.borrow_mut().const_unification_table().probe_value(v) {
ConstVariableValue::Unknown { .. } => false,
ConstVariableValue::Known { .. } => true,
}
Expand Down Expand Up @@ -1876,7 +1870,6 @@ impl<'a, 'tcx> TypeFolder<TyCtxt<'tcx>> for ShallowResolver<'a, 'tcx> {
.borrow_mut()
.const_unification_table()
.probe_value(vid)
.val
.known()
.unwrap_or(ct),
ty::ConstKind::Infer(InferConst::EffectVar(vid)) => self
Expand Down
26 changes: 11 additions & 15 deletions compiler/rustc_infer/src/infer/relate/combine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,12 @@ use super::sub::Sub;
use crate::infer::{DefineOpaqueTypes, InferCtxt, TypeTrace};
use crate::traits::{Obligation, PredicateObligations};
use rustc_middle::infer::canonical::OriginalQueryValues;
use rustc_middle::infer::unify_key::{ConstVarValue, ConstVariableValue, EffectVarValue};
use rustc_middle::infer::unify_key::{ConstVariableOrigin, ConstVariableOriginKind};
use rustc_middle::infer::unify_key::{ConstVariableValue, EffectVarValue};
use rustc_middle::ty::error::{ExpectedFound, TypeError};
use rustc_middle::ty::relate::{RelateResult, TypeRelation};
use rustc_middle::ty::{self, InferConst, ToPredicate, Ty, TyCtxt, TypeVisitableExt};
use rustc_middle::ty::{AliasRelationDirection, TyVar};
use rustc_middle::ty::{IntType, UintType};
use rustc_span::DUMMY_SP;

#[derive(Clone)]
pub struct CombineFields<'infcx, 'tcx> {
Expand Down Expand Up @@ -328,8 +326,12 @@ impl<'tcx> InferCtxt<'tcx> {
ct: ty::Const<'tcx>,
param_env: ty::ParamEnv<'tcx>,
) -> RelateResult<'tcx, ty::Const<'tcx>> {
let span =
self.inner.borrow_mut().const_unification_table().probe_value(target_vid).origin.span;
let span = match self.inner.borrow_mut().const_unification_table().probe_value(target_vid) {
ConstVariableValue::Known { value } => {
bug!("instantiating a known const var: {target_vid:?} {value} {ct}")
}
ConstVariableValue::Unknown { origin, universe: _ } => origin.span,
};
// FIXME(generic_const_exprs): Occurs check failures for unevaluated
// constants and generic expressions are not yet handled correctly.
let Generalization { value_may_be_infer: value, needs_wf: _ } = generalize::generalize(
Expand All @@ -340,16 +342,10 @@ impl<'tcx> InferCtxt<'tcx> {
ty::Variance::Invariant,
)?;

self.inner.borrow_mut().const_unification_table().union_value(
target_vid,
ConstVarValue {
origin: ConstVariableOrigin {
kind: ConstVariableOriginKind::ConstInference,
span: DUMMY_SP,
},
val: ConstVariableValue::Known { value },
},
);
self.inner
.borrow_mut()
.const_unification_table()
.union_value(target_vid, ConstVariableValue::Known { value });
Ok(value)
}

Expand Down
15 changes: 6 additions & 9 deletions compiler/rustc_infer/src/infer/relate/generalize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::mem;
use rustc_data_structures::sso::SsoHashMap;
use rustc_data_structures::stack::ensure_sufficient_stack;
use rustc_hir::def_id::DefId;
use rustc_middle::infer::unify_key::{ConstVarValue, ConstVariableValue};
use rustc_middle::infer::unify_key::ConstVariableValue;
use rustc_middle::ty::error::TypeError;
use rustc_middle::ty::relate::{self, Relate, RelateResult, TypeRelation};
use rustc_middle::ty::visit::MaxUniverse;
Expand Down Expand Up @@ -431,22 +431,19 @@ where

let mut inner = self.infcx.inner.borrow_mut();
let variable_table = &mut inner.const_unification_table();
let var_value = variable_table.probe_value(vid);
match var_value.val {
match variable_table.probe_value(vid) {
ConstVariableValue::Known { value: u } => {
drop(inner);
self.relate(u, u)
}
ConstVariableValue::Unknown { universe } => {
ConstVariableValue::Unknown { origin, universe } => {
if self.for_universe.can_name(universe) {
Ok(c)
} else {
let new_var_id = variable_table
.new_key(ConstVarValue {
origin: var_value.origin,
val: ConstVariableValue::Unknown {
universe: self.for_universe,
},
.new_key(ConstVariableValue::Unknown {
origin,
universe: self.for_universe,
})
.vid;
Ok(ty::Const::new_var(self.tcx(), new_var_id, c.ty()))
Expand Down
90 changes: 2 additions & 88 deletions compiler/rustc_infer/src/infer/resolve.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
use super::type_variable::{TypeVariableOrigin, TypeVariableOriginKind};
use super::{FixupError, FixupResult, InferCtxt, Span};
use rustc_middle::infer::unify_key::{ConstVariableOrigin, ConstVariableOriginKind};
use super::{FixupError, FixupResult, InferCtxt};
use rustc_middle::ty::fold::{FallibleTypeFolder, TypeFolder, TypeSuperFoldable};
use rustc_middle::ty::visit::{TypeSuperVisitable, TypeVisitableExt, TypeVisitor};
use rustc_middle::ty::visit::TypeVisitableExt;
use rustc_middle::ty::{self, Const, InferConst, Ty, TyCtxt, TypeFoldable};

use std::ops::ControlFlow;

///////////////////////////////////////////////////////////////////////////
// OPPORTUNISTIC VAR RESOLVER

Expand Down Expand Up @@ -104,88 +100,6 @@ impl<'a, 'tcx> TypeFolder<TyCtxt<'tcx>> for OpportunisticRegionResolver<'a, 'tcx
}
}

///////////////////////////////////////////////////////////////////////////
// UNRESOLVED TYPE FINDER

/// The unresolved type **finder** walks a type searching for
/// type variables that don't yet have a value. The first unresolved type is stored.
/// It does not construct the fully resolved type (which might
/// involve some hashing and so forth).
pub struct UnresolvedTypeOrConstFinder<'a, 'tcx> {
infcx: &'a InferCtxt<'tcx>,
}

impl<'a, 'tcx> UnresolvedTypeOrConstFinder<'a, 'tcx> {
pub fn new(infcx: &'a InferCtxt<'tcx>) -> Self {
UnresolvedTypeOrConstFinder { infcx }
}
}

impl<'a, 'tcx> TypeVisitor<TyCtxt<'tcx>> for UnresolvedTypeOrConstFinder<'a, 'tcx> {
type BreakTy = (ty::Term<'tcx>, Option<Span>);
fn visit_ty(&mut self, t: Ty<'tcx>) -> ControlFlow<Self::BreakTy> {
let t = self.infcx.shallow_resolve(t);
if let ty::Infer(infer_ty) = *t.kind() {
// Since we called `shallow_resolve` above, this must
// be an (as yet...) unresolved inference variable.
let ty_var_span = if let ty::TyVar(ty_vid) = infer_ty {
let mut inner = self.infcx.inner.borrow_mut();
let ty_vars = &inner.type_variables();
if let TypeVariableOrigin {
kind: TypeVariableOriginKind::TypeParameterDefinition(_, _),
span,
} = ty_vars.var_origin(ty_vid)
{
Some(span)
} else {
None
}
} else {
None
};
ControlFlow::Break((t.into(), ty_var_span))
} else if !t.has_non_region_infer() {
// All const/type variables in inference types must already be resolved,
// no need to visit the contents.
ControlFlow::Continue(())
} else {
// Otherwise, keep visiting.
t.super_visit_with(self)
}
}

fn visit_const(&mut self, ct: ty::Const<'tcx>) -> ControlFlow<Self::BreakTy> {
let ct = self.infcx.shallow_resolve(ct);
if let ty::ConstKind::Infer(i) = ct.kind() {
// Since we called `shallow_resolve` above, this must
// be an (as yet...) unresolved inference variable.
let ct_var_span = if let ty::InferConst::Var(vid) = i {
let mut inner = self.infcx.inner.borrow_mut();
let ct_vars = &mut inner.const_unification_table();
if let ConstVariableOrigin {
span,
kind: ConstVariableOriginKind::ConstParameterDefinition(_, _),
} = ct_vars.probe_value(vid).origin
{
Some(span)
} else {
None
}
} else {
None
};
ControlFlow::Break((ct.into(), ct_var_span))
} else if !ct.has_non_region_infer() {
// All const/type variables in inference types must already be resolved,
// no need to visit the contents.
ControlFlow::Continue(())
} else {
// Otherwise, keep visiting.
ct.super_visit_with(self)
}
}
}

///////////////////////////////////////////////////////////////////////////
// FULL TYPE RESOLUTION

Expand Down
Loading

0 comments on commit c070016

Please sign in to comment.