Skip to content

Commit 38b1872

Browse files
authored
Rollup merge of rust-lang#120021 - lcnr:const-var-value, r=compiler-errors
don't store const var origins for known vars r? types
2 parents 01b029c + 290651b commit 38b1872

File tree

8 files changed

+70
-165
lines changed

8 files changed

+70
-165
lines changed

compiler/rustc_infer/src/infer/error_reporting/need_type_info.rs

+23-10
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@ use rustc_hir::def_id::{DefId, LocalDefId};
1313
use rustc_hir::intravisit::{self, Visitor};
1414
use rustc_hir::{Body, Closure, Expr, ExprKind, FnRetTy, HirId, Local, LocalSource};
1515
use rustc_middle::hir::nested_filter;
16-
use rustc_middle::infer::unify_key::{ConstVariableOrigin, ConstVariableOriginKind};
16+
use rustc_middle::infer::unify_key::{
17+
ConstVariableOrigin, ConstVariableOriginKind, ConstVariableValue,
18+
};
1719
use rustc_middle::ty::adjustment::{Adjust, Adjustment, AutoBorrow};
1820
use rustc_middle::ty::print::{FmtPrinter, PrettyPrinter, Print, Printer};
1921
use rustc_middle::ty::{self, InferConst};
@@ -178,17 +180,23 @@ fn fmt_printer<'a, 'tcx>(infcx: &'a InferCtxt<'tcx>, ns: Namespace) -> FmtPrinte
178180
}
179181
};
180182
printer.ty_infer_name_resolver = Some(Box::new(ty_getter));
181-
let const_getter = move |ct_vid| {
182-
if infcx.probe_const_var(ct_vid).is_ok() {
183+
let const_getter = move |ct_vid| match infcx
184+
.inner
185+
.borrow_mut()
186+
.const_unification_table()
187+
.probe_value(ct_vid)
188+
{
189+
ConstVariableValue::Known { value: _ } => {
183190
warn!("resolved const var in error message");
184-
}
185-
if let ConstVariableOriginKind::ConstParameterDefinition(name, _) =
186-
infcx.inner.borrow_mut().const_unification_table().probe_value(ct_vid).origin.kind
187-
{
188-
return Some(name);
189-
} else {
190191
None
191192
}
193+
ConstVariableValue::Unknown { origin, universe: _ } => {
194+
if let ConstVariableOriginKind::ConstParameterDefinition(name, _) = origin.kind {
195+
return Some(name);
196+
} else {
197+
None
198+
}
199+
}
192200
};
193201
printer.const_infer_name_resolver = Some(Box::new(const_getter));
194202
printer
@@ -303,7 +311,12 @@ impl<'tcx> InferCtxt<'tcx> {
303311
GenericArgKind::Const(ct) => {
304312
if let ty::ConstKind::Infer(InferConst::Var(vid)) = ct.kind() {
305313
let origin =
306-
self.inner.borrow_mut().const_unification_table().probe_value(vid).origin;
314+
match self.inner.borrow_mut().const_unification_table().probe_value(vid) {
315+
ConstVariableValue::Known { value } => {
316+
bug!("resolved infer var: {vid:?} {value}")
317+
}
318+
ConstVariableValue::Unknown { origin, universe: _ } => origin,
319+
};
307320
if let ConstVariableOriginKind::ConstParameterDefinition(name, def_id) =
308321
origin.kind
309322
{

compiler/rustc_infer/src/infer/freshen.rs

+2-8
Original file line numberDiff line numberDiff line change
@@ -146,14 +146,8 @@ impl<'a, 'tcx> TypeFolder<TyCtxt<'tcx>> for TypeFreshener<'a, 'tcx> {
146146
fn fold_const(&mut self, ct: ty::Const<'tcx>) -> ty::Const<'tcx> {
147147
match ct.kind() {
148148
ty::ConstKind::Infer(ty::InferConst::Var(v)) => {
149-
let opt_ct = self
150-
.infcx
151-
.inner
152-
.borrow_mut()
153-
.const_unification_table()
154-
.probe_value(v)
155-
.val
156-
.known();
149+
let opt_ct =
150+
self.infcx.inner.borrow_mut().const_unification_table().probe_value(v).known();
157151
self.freshen_const(opt_ct, ty::InferConst::Var(v), ty::InferConst::Fresh, ct.ty())
158152
}
159153
ty::ConstKind::Infer(ty::InferConst::EffectVar(v)) => {

compiler/rustc_infer/src/infer/fudge.rs

+9-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use rustc_middle::infer::unify_key::ConstVidKey;
1+
use rustc_middle::infer::unify_key::{ConstVariableOriginKind, ConstVariableValue, ConstVidKey};
22
use rustc_middle::ty::fold::{TypeFoldable, TypeFolder, TypeSuperFoldable};
33
use rustc_middle::ty::{self, ConstVid, FloatVid, IntVid, RegionVid, Ty, TyCtxt, TyVid};
44

@@ -28,10 +28,17 @@ fn const_vars_since_snapshot<'tcx>(
2828
snapshot_var_len: usize,
2929
) -> (Range<ConstVid>, Vec<ConstVariableOrigin>) {
3030
let range = vars_since_snapshot(table, snapshot_var_len);
31+
3132
(
3233
range.start.vid..range.end.vid,
3334
(range.start.index()..range.end.index())
34-
.map(|index| table.probe_value(ConstVid::from_u32(index)).origin)
35+
.map(|index| match table.probe_value(ConstVid::from_u32(index)) {
36+
ConstVariableValue::Known { value: _ } => ConstVariableOrigin {
37+
kind: ConstVariableOriginKind::MiscVariable,
38+
span: rustc_span::DUMMY_SP,
39+
},
40+
ConstVariableValue::Unknown { origin, universe: _ } => origin,
41+
})
3542
.collect(),
3643
)
3744
}

compiler/rustc_infer/src/infer/mod.rs

+7-14
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@ use rustc_data_structures::unify as ut;
2323
use rustc_errors::{DiagCtxt, DiagnosticBuilder, ErrorGuaranteed};
2424
use rustc_hir::def_id::{DefId, LocalDefId};
2525
use rustc_middle::infer::canonical::{Canonical, CanonicalVarValues};
26-
use rustc_middle::infer::unify_key::{ConstVarValue, ConstVariableValue, EffectVarValue};
2726
use rustc_middle::infer::unify_key::{ConstVariableOrigin, ConstVariableOriginKind, ToType};
27+
use rustc_middle::infer::unify_key::{ConstVariableValue, EffectVarValue};
2828
use rustc_middle::mir::interpret::{ErrorHandled, EvalToValTreeResult};
2929
use rustc_middle::mir::ConstraintCategory;
3030
use rustc_middle::traits::{select, DefiningAnchor};
@@ -1086,7 +1086,7 @@ impl<'tcx> InferCtxt<'tcx> {
10861086
.inner
10871087
.borrow_mut()
10881088
.const_unification_table()
1089-
.new_key(ConstVarValue { origin, val: ConstVariableValue::Unknown { universe } })
1089+
.new_key(ConstVariableValue::Unknown { origin, universe })
10901090
.vid;
10911091
ty::Const::new_var(self.tcx, vid, ty)
10921092
}
@@ -1095,10 +1095,7 @@ impl<'tcx> InferCtxt<'tcx> {
10951095
self.inner
10961096
.borrow_mut()
10971097
.const_unification_table()
1098-
.new_key(ConstVarValue {
1099-
origin,
1100-
val: ConstVariableValue::Unknown { universe: self.universe() },
1101-
})
1098+
.new_key(ConstVariableValue::Unknown { origin, universe: self.universe() })
11021099
.vid
11031100
}
11041101

@@ -1217,10 +1214,7 @@ impl<'tcx> InferCtxt<'tcx> {
12171214
.inner
12181215
.borrow_mut()
12191216
.const_unification_table()
1220-
.new_key(ConstVarValue {
1221-
origin,
1222-
val: ConstVariableValue::Unknown { universe: self.universe() },
1223-
})
1217+
.new_key(ConstVariableValue::Unknown { origin, universe: self.universe() })
12241218
.vid;
12251219
ty::Const::new_var(
12261220
self.tcx,
@@ -1410,9 +1404,9 @@ impl<'tcx> InferCtxt<'tcx> {
14101404
}
14111405

14121406
pub fn probe_const_var(&self, vid: ty::ConstVid) -> Result<ty::Const<'tcx>, ty::UniverseIndex> {
1413-
match self.inner.borrow_mut().const_unification_table().probe_value(vid).val {
1407+
match self.inner.borrow_mut().const_unification_table().probe_value(vid) {
14141408
ConstVariableValue::Known { value } => Ok(value),
1415-
ConstVariableValue::Unknown { universe } => Err(universe),
1409+
ConstVariableValue::Unknown { origin: _, universe } => Err(universe),
14161410
}
14171411
}
14181412

@@ -1709,7 +1703,7 @@ impl<'tcx> InferCtxt<'tcx> {
17091703
// `ty::ConstKind::Infer(ty::InferConst::Var(v))`.
17101704
//
17111705
// Not `inlined_probe_value(v)` because this call site is colder.
1712-
match self.inner.borrow_mut().const_unification_table().probe_value(v).val {
1706+
match self.inner.borrow_mut().const_unification_table().probe_value(v) {
17131707
ConstVariableValue::Unknown { .. } => false,
17141708
ConstVariableValue::Known { .. } => true,
17151709
}
@@ -1876,7 +1870,6 @@ impl<'a, 'tcx> TypeFolder<TyCtxt<'tcx>> for ShallowResolver<'a, 'tcx> {
18761870
.borrow_mut()
18771871
.const_unification_table()
18781872
.probe_value(vid)
1879-
.val
18801873
.known()
18811874
.unwrap_or(ct),
18821875
ty::ConstKind::Infer(InferConst::EffectVar(vid)) => self

compiler/rustc_infer/src/infer/relate/combine.rs

+11-15
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,12 @@ use super::sub::Sub;
3030
use crate::infer::{DefineOpaqueTypes, InferCtxt, TypeTrace};
3131
use crate::traits::{Obligation, PredicateObligations};
3232
use rustc_middle::infer::canonical::OriginalQueryValues;
33-
use rustc_middle::infer::unify_key::{ConstVarValue, ConstVariableValue, EffectVarValue};
34-
use rustc_middle::infer::unify_key::{ConstVariableOrigin, ConstVariableOriginKind};
33+
use rustc_middle::infer::unify_key::{ConstVariableValue, EffectVarValue};
3534
use rustc_middle::ty::error::{ExpectedFound, TypeError};
3635
use rustc_middle::ty::relate::{RelateResult, TypeRelation};
3736
use rustc_middle::ty::{self, InferConst, ToPredicate, Ty, TyCtxt, TypeVisitableExt};
3837
use rustc_middle::ty::{AliasRelationDirection, TyVar};
3938
use rustc_middle::ty::{IntType, UintType};
40-
use rustc_span::DUMMY_SP;
4139

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

343-
self.inner.borrow_mut().const_unification_table().union_value(
344-
target_vid,
345-
ConstVarValue {
346-
origin: ConstVariableOrigin {
347-
kind: ConstVariableOriginKind::ConstInference,
348-
span: DUMMY_SP,
349-
},
350-
val: ConstVariableValue::Known { value },
351-
},
352-
);
345+
self.inner
346+
.borrow_mut()
347+
.const_unification_table()
348+
.union_value(target_vid, ConstVariableValue::Known { value });
353349
Ok(value)
354350
}
355351

compiler/rustc_infer/src/infer/relate/generalize.rs

+6-9
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use std::mem;
33
use rustc_data_structures::sso::SsoHashMap;
44
use rustc_data_structures::stack::ensure_sufficient_stack;
55
use rustc_hir::def_id::DefId;
6-
use rustc_middle::infer::unify_key::{ConstVarValue, ConstVariableValue};
6+
use rustc_middle::infer::unify_key::ConstVariableValue;
77
use rustc_middle::ty::error::TypeError;
88
use rustc_middle::ty::relate::{self, Relate, RelateResult, TypeRelation};
99
use rustc_middle::ty::visit::MaxUniverse;
@@ -431,22 +431,19 @@ where
431431

432432
let mut inner = self.infcx.inner.borrow_mut();
433433
let variable_table = &mut inner.const_unification_table();
434-
let var_value = variable_table.probe_value(vid);
435-
match var_value.val {
434+
match variable_table.probe_value(vid) {
436435
ConstVariableValue::Known { value: u } => {
437436
drop(inner);
438437
self.relate(u, u)
439438
}
440-
ConstVariableValue::Unknown { universe } => {
439+
ConstVariableValue::Unknown { origin, universe } => {
441440
if self.for_universe.can_name(universe) {
442441
Ok(c)
443442
} else {
444443
let new_var_id = variable_table
445-
.new_key(ConstVarValue {
446-
origin: var_value.origin,
447-
val: ConstVariableValue::Unknown {
448-
universe: self.for_universe,
449-
},
444+
.new_key(ConstVariableValue::Unknown {
445+
origin,
446+
universe: self.for_universe,
450447
})
451448
.vid;
452449
Ok(ty::Const::new_var(self.tcx(), new_var_id, c.ty()))

compiler/rustc_infer/src/infer/resolve.rs

+2-88
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,8 @@
1-
use super::type_variable::{TypeVariableOrigin, TypeVariableOriginKind};
2-
use super::{FixupError, FixupResult, InferCtxt, Span};
3-
use rustc_middle::infer::unify_key::{ConstVariableOrigin, ConstVariableOriginKind};
1+
use super::{FixupError, FixupResult, InferCtxt};
42
use rustc_middle::ty::fold::{FallibleTypeFolder, TypeFolder, TypeSuperFoldable};
5-
use rustc_middle::ty::visit::{TypeSuperVisitable, TypeVisitableExt, TypeVisitor};
3+
use rustc_middle::ty::visit::TypeVisitableExt;
64
use rustc_middle::ty::{self, Const, InferConst, Ty, TyCtxt, TypeFoldable};
75

8-
use std::ops::ControlFlow;
9-
106
///////////////////////////////////////////////////////////////////////////
117
// OPPORTUNISTIC VAR RESOLVER
128

@@ -104,88 +100,6 @@ impl<'a, 'tcx> TypeFolder<TyCtxt<'tcx>> for OpportunisticRegionResolver<'a, 'tcx
104100
}
105101
}
106102

107-
///////////////////////////////////////////////////////////////////////////
108-
// UNRESOLVED TYPE FINDER
109-
110-
/// The unresolved type **finder** walks a type searching for
111-
/// type variables that don't yet have a value. The first unresolved type is stored.
112-
/// It does not construct the fully resolved type (which might
113-
/// involve some hashing and so forth).
114-
pub struct UnresolvedTypeOrConstFinder<'a, 'tcx> {
115-
infcx: &'a InferCtxt<'tcx>,
116-
}
117-
118-
impl<'a, 'tcx> UnresolvedTypeOrConstFinder<'a, 'tcx> {
119-
pub fn new(infcx: &'a InferCtxt<'tcx>) -> Self {
120-
UnresolvedTypeOrConstFinder { infcx }
121-
}
122-
}
123-
124-
impl<'a, 'tcx> TypeVisitor<TyCtxt<'tcx>> for UnresolvedTypeOrConstFinder<'a, 'tcx> {
125-
type BreakTy = (ty::Term<'tcx>, Option<Span>);
126-
fn visit_ty(&mut self, t: Ty<'tcx>) -> ControlFlow<Self::BreakTy> {
127-
let t = self.infcx.shallow_resolve(t);
128-
if let ty::Infer(infer_ty) = *t.kind() {
129-
// Since we called `shallow_resolve` above, this must
130-
// be an (as yet...) unresolved inference variable.
131-
let ty_var_span = if let ty::TyVar(ty_vid) = infer_ty {
132-
let mut inner = self.infcx.inner.borrow_mut();
133-
let ty_vars = &inner.type_variables();
134-
if let TypeVariableOrigin {
135-
kind: TypeVariableOriginKind::TypeParameterDefinition(_, _),
136-
span,
137-
} = ty_vars.var_origin(ty_vid)
138-
{
139-
Some(span)
140-
} else {
141-
None
142-
}
143-
} else {
144-
None
145-
};
146-
ControlFlow::Break((t.into(), ty_var_span))
147-
} else if !t.has_non_region_infer() {
148-
// All const/type variables in inference types must already be resolved,
149-
// no need to visit the contents.
150-
ControlFlow::Continue(())
151-
} else {
152-
// Otherwise, keep visiting.
153-
t.super_visit_with(self)
154-
}
155-
}
156-
157-
fn visit_const(&mut self, ct: ty::Const<'tcx>) -> ControlFlow<Self::BreakTy> {
158-
let ct = self.infcx.shallow_resolve(ct);
159-
if let ty::ConstKind::Infer(i) = ct.kind() {
160-
// Since we called `shallow_resolve` above, this must
161-
// be an (as yet...) unresolved inference variable.
162-
let ct_var_span = if let ty::InferConst::Var(vid) = i {
163-
let mut inner = self.infcx.inner.borrow_mut();
164-
let ct_vars = &mut inner.const_unification_table();
165-
if let ConstVariableOrigin {
166-
span,
167-
kind: ConstVariableOriginKind::ConstParameterDefinition(_, _),
168-
} = ct_vars.probe_value(vid).origin
169-
{
170-
Some(span)
171-
} else {
172-
None
173-
}
174-
} else {
175-
None
176-
};
177-
ControlFlow::Break((ct.into(), ct_var_span))
178-
} else if !ct.has_non_region_infer() {
179-
// All const/type variables in inference types must already be resolved,
180-
// no need to visit the contents.
181-
ControlFlow::Continue(())
182-
} else {
183-
// Otherwise, keep visiting.
184-
ct.super_visit_with(self)
185-
}
186-
}
187-
}
188-
189103
///////////////////////////////////////////////////////////////////////////
190104
// FULL TYPE RESOLUTION
191105

0 commit comments

Comments
 (0)