Skip to content

Commit 73a73c7

Browse files
committed
Auto merge of rust-lang#123537 - compiler-errors:shallow, r=<try>
Simplify shallow resolver to just fold ty/consts Probably faster than using a whole folder?
2 parents 23d47db + ce2f870 commit 73a73c7

File tree

12 files changed

+86
-124
lines changed

12 files changed

+86
-124
lines changed

compiler/rustc_borrowck/src/type_check/relate_tys.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -437,7 +437,7 @@ impl<'bccx, 'tcx> TypeRelation<'tcx> for NllTypeRelating<'_, 'bccx, 'tcx> {
437437
a: ty::Const<'tcx>,
438438
b: ty::Const<'tcx>,
439439
) -> RelateResult<'tcx, ty::Const<'tcx>> {
440-
let a = self.type_checker.infcx.shallow_resolve(a);
440+
let a = self.type_checker.infcx.shallow_resolve_const(a);
441441
assert!(!a.has_non_region_infer(), "unexpected inference var {:?}", a);
442442
assert!(!b.has_non_region_infer(), "unexpected inference var {:?}", b);
443443

compiler/rustc_infer/src/infer/canonical/canonicalizer.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -790,7 +790,7 @@ impl<'cx, 'tcx> Canonicalizer<'cx, 'tcx> {
790790
const_var: ty::Const<'tcx>,
791791
) -> ty::Const<'tcx> {
792792
debug_assert!(
793-
!self.infcx.is_some_and(|infcx| const_var != infcx.shallow_resolve(const_var))
793+
!self.infcx.is_some_and(|infcx| const_var != infcx.shallow_resolve_const(const_var))
794794
);
795795
let var = self.canonical_var(info, const_var.into());
796796
ty::Const::new_bound(self.tcx, self.binder_index, var, self.fold_ty(const_var.ty()))

compiler/rustc_infer/src/infer/mod.rs

+61-96
Original file line numberDiff line numberDiff line change
@@ -1245,19 +1245,67 @@ impl<'tcx> InferCtxt<'tcx> {
12451245
}
12461246
}
12471247

1248-
/// Resolve any type variables found in `value` -- but only one
1249-
/// level. So, if the variable `?X` is bound to some type
1250-
/// `Foo<?Y>`, then this would return `Foo<?Y>` (but `?Y` may
1251-
/// itself be bound to a type).
1252-
///
1253-
/// Useful when you only need to inspect the outermost level of
1254-
/// the type and don't care about nested types (or perhaps you
1255-
/// will be resolving them as well, e.g. in a loop).
1256-
pub fn shallow_resolve<T>(&self, value: T) -> T
1257-
where
1258-
T: TypeFoldable<TyCtxt<'tcx>>,
1259-
{
1260-
value.fold_with(&mut ShallowResolver { infcx: self })
1248+
pub fn shallow_resolve(&self, ty: Ty<'tcx>) -> Ty<'tcx> {
1249+
if let ty::Infer(v) = ty.kind() { self.fold_infer_ty(*v).unwrap_or(ty) } else { ty }
1250+
}
1251+
1252+
// This is separate from `fold_ty` to keep that method small and inlinable.
1253+
#[inline(never)]
1254+
fn fold_infer_ty(&self, v: InferTy) -> Option<Ty<'tcx>> {
1255+
match v {
1256+
ty::TyVar(v) => {
1257+
// Not entirely obvious: if `typ` is a type variable,
1258+
// it can be resolved to an int/float variable, which
1259+
// can then be recursively resolved, hence the
1260+
// recursion. Note though that we prevent type
1261+
// variables from unifying to other type variables
1262+
// directly (though they may be embedded
1263+
// structurally), and we prevent cycles in any case,
1264+
// so this recursion should always be of very limited
1265+
// depth.
1266+
//
1267+
// Note: if these two lines are combined into one we get
1268+
// dynamic borrow errors on `self.inner`.
1269+
let known = self.inner.borrow_mut().type_variables().probe(v).known();
1270+
known.map(|t| self.shallow_resolve(t))
1271+
}
1272+
1273+
ty::IntVar(v) => self
1274+
.inner
1275+
.borrow_mut()
1276+
.int_unification_table()
1277+
.probe_value(v)
1278+
.map(|v| v.to_type(self.tcx)),
1279+
1280+
ty::FloatVar(v) => self
1281+
.inner
1282+
.borrow_mut()
1283+
.float_unification_table()
1284+
.probe_value(v)
1285+
.map(|v| v.to_type(self.tcx)),
1286+
1287+
ty::FreshTy(_) | ty::FreshIntTy(_) | ty::FreshFloatTy(_) => None,
1288+
}
1289+
}
1290+
1291+
pub fn shallow_resolve_const(&self, ct: ty::Const<'tcx>) -> ty::Const<'tcx> {
1292+
match ct.kind() {
1293+
ty::ConstKind::Infer(InferConst::Var(vid)) => self
1294+
.inner
1295+
.borrow_mut()
1296+
.const_unification_table()
1297+
.probe_value(vid)
1298+
.known()
1299+
.unwrap_or(ct),
1300+
ty::ConstKind::Infer(InferConst::EffectVar(vid)) => self
1301+
.inner
1302+
.borrow_mut()
1303+
.effect_unification_table()
1304+
.probe_value(vid)
1305+
.known()
1306+
.unwrap_or(ct),
1307+
_ => ct,
1308+
}
12611309
}
12621310

12631311
pub fn root_var(&self, var: ty::TyVid) -> ty::TyVid {
@@ -1774,89 +1822,6 @@ impl<'tcx> TypeFolder<TyCtxt<'tcx>> for InferenceLiteralEraser<'tcx> {
17741822
}
17751823
}
17761824

1777-
struct ShallowResolver<'a, 'tcx> {
1778-
infcx: &'a InferCtxt<'tcx>,
1779-
}
1780-
1781-
impl<'a, 'tcx> TypeFolder<TyCtxt<'tcx>> for ShallowResolver<'a, 'tcx> {
1782-
fn interner(&self) -> TyCtxt<'tcx> {
1783-
self.infcx.tcx
1784-
}
1785-
1786-
/// If `ty` is a type variable of some kind, resolve it one level
1787-
/// (but do not resolve types found in the result). If `typ` is
1788-
/// not a type variable, just return it unmodified.
1789-
#[inline]
1790-
fn fold_ty(&mut self, ty: Ty<'tcx>) -> Ty<'tcx> {
1791-
if let ty::Infer(v) = ty.kind() { self.fold_infer_ty(*v).unwrap_or(ty) } else { ty }
1792-
}
1793-
1794-
fn fold_const(&mut self, ct: ty::Const<'tcx>) -> ty::Const<'tcx> {
1795-
match ct.kind() {
1796-
ty::ConstKind::Infer(InferConst::Var(vid)) => self
1797-
.infcx
1798-
.inner
1799-
.borrow_mut()
1800-
.const_unification_table()
1801-
.probe_value(vid)
1802-
.known()
1803-
.unwrap_or(ct),
1804-
ty::ConstKind::Infer(InferConst::EffectVar(vid)) => self
1805-
.infcx
1806-
.inner
1807-
.borrow_mut()
1808-
.effect_unification_table()
1809-
.probe_value(vid)
1810-
.known()
1811-
.unwrap_or(ct),
1812-
_ => ct,
1813-
}
1814-
}
1815-
}
1816-
1817-
impl<'a, 'tcx> ShallowResolver<'a, 'tcx> {
1818-
// This is separate from `fold_ty` to keep that method small and inlinable.
1819-
#[inline(never)]
1820-
fn fold_infer_ty(&mut self, v: InferTy) -> Option<Ty<'tcx>> {
1821-
match v {
1822-
ty::TyVar(v) => {
1823-
// Not entirely obvious: if `typ` is a type variable,
1824-
// it can be resolved to an int/float variable, which
1825-
// can then be recursively resolved, hence the
1826-
// recursion. Note though that we prevent type
1827-
// variables from unifying to other type variables
1828-
// directly (though they may be embedded
1829-
// structurally), and we prevent cycles in any case,
1830-
// so this recursion should always be of very limited
1831-
// depth.
1832-
//
1833-
// Note: if these two lines are combined into one we get
1834-
// dynamic borrow errors on `self.inner`.
1835-
let known = self.infcx.inner.borrow_mut().type_variables().probe(v).known();
1836-
known.map(|t| self.fold_ty(t))
1837-
}
1838-
1839-
ty::IntVar(v) => self
1840-
.infcx
1841-
.inner
1842-
.borrow_mut()
1843-
.int_unification_table()
1844-
.probe_value(v)
1845-
.map(|v| v.to_type(self.infcx.tcx)),
1846-
1847-
ty::FloatVar(v) => self
1848-
.infcx
1849-
.inner
1850-
.borrow_mut()
1851-
.float_unification_table()
1852-
.probe_value(v)
1853-
.map(|v| v.to_type(self.infcx.tcx)),
1854-
1855-
ty::FreshTy(_) | ty::FreshIntTy(_) | ty::FreshFloatTy(_) => None,
1856-
}
1857-
}
1858-
}
1859-
18601825
impl<'tcx> TypeTrace<'tcx> {
18611826
pub fn span(&self) -> Span {
18621827
self.cause.span

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -155,8 +155,8 @@ impl<'tcx> InferCtxt<'tcx> {
155155
return Ok(a);
156156
}
157157

158-
let a = self.shallow_resolve(a);
159-
let b = self.shallow_resolve(b);
158+
let a = self.shallow_resolve_const(a);
159+
let b = self.shallow_resolve_const(b);
160160

161161
// We should never have to relate the `ty` field on `Const` as it is checked elsewhere that consts have the
162162
// correct type for the generic param they are an argument for. However there have been a number of cases

compiler/rustc_infer/src/infer/resolve.rs

+6-8
Original file line numberDiff line numberDiff line change
@@ -12,29 +12,27 @@ use rustc_middle::ty::{self, Const, InferConst, Ty, TyCtxt, TypeFoldable};
1212
/// useful for printing messages etc but also required at various
1313
/// points for correctness.
1414
pub struct OpportunisticVarResolver<'a, 'tcx> {
15-
// The shallow resolver is used to resolve inference variables at every
16-
// level of the type.
17-
shallow_resolver: crate::infer::ShallowResolver<'a, 'tcx>,
15+
infcx: &'a InferCtxt<'tcx>,
1816
}
1917

2018
impl<'a, 'tcx> OpportunisticVarResolver<'a, 'tcx> {
2119
#[inline]
2220
pub fn new(infcx: &'a InferCtxt<'tcx>) -> Self {
23-
OpportunisticVarResolver { shallow_resolver: crate::infer::ShallowResolver { infcx } }
21+
OpportunisticVarResolver { infcx }
2422
}
2523
}
2624

2725
impl<'a, 'tcx> TypeFolder<TyCtxt<'tcx>> for OpportunisticVarResolver<'a, 'tcx> {
2826
fn interner(&self) -> TyCtxt<'tcx> {
29-
TypeFolder::interner(&self.shallow_resolver)
27+
self.infcx.tcx
3028
}
3129

3230
#[inline]
3331
fn fold_ty(&mut self, t: Ty<'tcx>) -> Ty<'tcx> {
3432
if !t.has_non_region_infer() {
3533
t // micro-optimize -- if there is nothing in this type that this fold affects...
3634
} else {
37-
let t = self.shallow_resolver.fold_ty(t);
35+
let t = self.infcx.shallow_resolve(t);
3836
t.super_fold_with(self)
3937
}
4038
}
@@ -43,7 +41,7 @@ impl<'a, 'tcx> TypeFolder<TyCtxt<'tcx>> for OpportunisticVarResolver<'a, 'tcx> {
4341
if !ct.has_non_region_infer() {
4442
ct // micro-optimize -- if there is nothing in this const that this fold affects...
4543
} else {
46-
let ct = self.shallow_resolver.fold_const(ct);
44+
let ct = self.infcx.shallow_resolve_const(ct);
4745
ct.super_fold_with(self)
4846
}
4947
}
@@ -160,7 +158,7 @@ impl<'a, 'tcx> FallibleTypeFolder<TyCtxt<'tcx>> for FullTypeResolver<'a, 'tcx> {
160158
if !c.has_infer() {
161159
Ok(c) // micro-optimize -- if there is nothing in this const that this fold affects...
162160
} else {
163-
let c = self.infcx.shallow_resolve(c);
161+
let c = self.infcx.shallow_resolve_const(c);
164162
match c.kind() {
165163
ty::ConstKind::Infer(InferConst::Var(vid)) => {
166164
return Err(FixupError::UnresolvedConst(vid));

compiler/rustc_trait_selection/src/solve/normalize.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ impl<'tcx> FallibleTypeFolder<TyCtxt<'tcx>> for NormalizationFolder<'_, 'tcx> {
203203
#[instrument(level = "debug", skip(self), ret)]
204204
fn try_fold_const(&mut self, ct: ty::Const<'tcx>) -> Result<ty::Const<'tcx>, Self::Error> {
205205
let infcx = self.at.infcx;
206-
debug_assert_eq!(ct, infcx.shallow_resolve(ct));
206+
debug_assert_eq!(ct, infcx.shallow_resolve_const(ct));
207207
if !ct.has_aliases() {
208208
return Ok(ct);
209209
}

compiler/rustc_trait_selection/src/traits/coherence.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -501,7 +501,7 @@ fn plug_infer_with_placeholders<'tcx>(
501501
}
502502

503503
fn visit_const(&mut self, ct: ty::Const<'tcx>) {
504-
let ct = self.infcx.shallow_resolve(ct);
504+
let ct = self.infcx.shallow_resolve_const(ct);
505505
if ct.is_ct_infer() {
506506
let Ok(InferOk { value: (), obligations }) =
507507
self.infcx.at(&ObligationCause::dummy(), ty::ParamEnv::empty()).eq(

compiler/rustc_trait_selection/src/traits/select/candidate_assembly.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -1166,8 +1166,8 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
11661166
return;
11671167
}
11681168

1169-
let self_ty = self.infcx.shallow_resolve(obligation.self_ty());
1170-
match self_ty.skip_binder().kind() {
1169+
let self_ty = self.infcx.shallow_resolve(obligation.self_ty().skip_binder());
1170+
match self_ty.kind() {
11711171
ty::Alias(..)
11721172
| ty::Dynamic(..)
11731173
| ty::Error(_)
@@ -1316,7 +1316,7 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
13161316
obligation: &PolyTraitObligation<'tcx>,
13171317
candidates: &mut SelectionCandidateSet<'tcx>,
13181318
) {
1319-
let self_ty = self.infcx.shallow_resolve(obligation.self_ty());
1319+
let self_ty = self.infcx.resolve_vars_if_possible(obligation.self_ty());
13201320

13211321
match self_ty.skip_binder().kind() {
13221322
ty::FnPtr(_) => candidates.vec.push(BuiltinCandidate { has_nested: false }),

compiler/rustc_trait_selection/src/traits/select/confirmation.rs

+4-5
Original file line numberDiff line numberDiff line change
@@ -157,10 +157,9 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
157157
) -> Result<Vec<PredicateObligation<'tcx>>, SelectionError<'tcx>> {
158158
let tcx = self.tcx();
159159

160-
let trait_predicate = self.infcx.shallow_resolve(obligation.predicate);
161160
let placeholder_trait_predicate =
162-
self.infcx.enter_forall_and_leak_universe(trait_predicate).trait_ref;
163-
let placeholder_self_ty = placeholder_trait_predicate.self_ty();
161+
self.infcx.enter_forall_and_leak_universe(obligation.predicate).trait_ref;
162+
let placeholder_self_ty = self.infcx.shallow_resolve(placeholder_trait_predicate.self_ty());
164163
let candidate_predicate = self
165164
.for_each_item_bound(
166165
placeholder_self_ty,
@@ -394,7 +393,7 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
394393
) -> Result<Vec<PredicateObligation<'tcx>>, SelectionError<'tcx>> {
395394
debug!(?obligation, "confirm_auto_impl_candidate");
396395

397-
let self_ty = self.infcx.shallow_resolve(obligation.predicate.self_ty());
396+
let self_ty = obligation.predicate.self_ty().map_bound(|ty| self.infcx.shallow_resolve(ty));
398397
let types = self.constituent_types_for_ty(self_ty)?;
399398
Ok(self.vtable_auto_impl(obligation, obligation.predicate.def_id(), types))
400399
}
@@ -1350,7 +1349,7 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
13501349
let drop_trait = self.tcx().require_lang_item(LangItem::Drop, None);
13511350

13521351
let tcx = self.tcx();
1353-
let self_ty = self.infcx.shallow_resolve(obligation.self_ty());
1352+
let self_ty = obligation.self_ty().map_bound(|ty| self.infcx.shallow_resolve(ty));
13541353

13551354
let mut nested = vec![];
13561355
let cause = obligation.derived_cause(BuiltinDerivedObligation);

compiler/rustc_trait_selection/src/traits/select/mod.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -571,7 +571,7 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
571571
)?;
572572
// If the predicate has done any inference, then downgrade the
573573
// result to ambiguous.
574-
if this.infcx.shallow_resolve(goal) != goal {
574+
if this.infcx.resolve_vars_if_possible(goal) != goal {
575575
result = result.max(EvaluatedToAmbig);
576576
}
577577
Ok(result)
@@ -1772,9 +1772,9 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
17721772
// that means that we must have newly inferred something about the GAT.
17731773
// We should give up in that case.
17741774
if !generics.params.is_empty()
1775-
&& obligation.predicate.args[generics.parent_count..]
1776-
.iter()
1777-
.any(|&p| p.has_non_region_infer() && self.infcx.shallow_resolve(p) != p)
1775+
&& obligation.predicate.args[generics.parent_count..].iter().any(|&p| {
1776+
p.has_non_region_infer() && self.infcx.resolve_vars_if_possible(p) != p
1777+
})
17781778
{
17791779
ProjectionMatchesProjection::Ambiguous
17801780
} else {

compiler/rustc_trait_selection/src/traits/util.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -649,7 +649,7 @@ impl<'tcx> TypeFolder<TyCtxt<'tcx>> for PlaceholderReplacer<'_, 'tcx> {
649649
}
650650

651651
fn fold_const(&mut self, ct: ty::Const<'tcx>) -> ty::Const<'tcx> {
652-
let ct = self.infcx.shallow_resolve(ct);
652+
let ct = self.infcx.shallow_resolve_const(ct);
653653
if let ty::ConstKind::Placeholder(p) = ct.kind() {
654654
let replace_var = self.mapped_consts.get(&p);
655655
match replace_var {

compiler/rustc_trait_selection/src/traits/wf.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ pub fn obligations<'tcx>(
4444
GenericArgKind::Const(ct) => {
4545
match ct.kind() {
4646
ty::ConstKind::Infer(_) => {
47-
let resolved = infcx.shallow_resolve(ct);
47+
let resolved = infcx.shallow_resolve_const(ct);
4848
if resolved == ct {
4949
// No progress.
5050
return None;

0 commit comments

Comments
 (0)