Skip to content

Commit e674e18

Browse files
authored
Merge pull request #22313 from ChayimFriedman2/visit-traits
internal: Refactor visiting and tracking of placeholder types
2 parents 20a5e4f + da71dd6 commit e674e18

11 files changed

Lines changed: 361 additions & 263 deletions

File tree

crates/hir-def/src/expr_store.rs

Lines changed: 210 additions & 81 deletions
Large diffs are not rendered by default.

crates/hir-def/src/hir/type_ref.rs

Lines changed: 1 addition & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,7 @@ use thin_vec::ThinVec;
88

99
use crate::{
1010
LifetimeParamId, TypeParamId,
11-
expr_store::{
12-
ExpressionStore,
13-
path::{GenericArg, Path},
14-
},
11+
expr_store::{ExpressionStore, path::Path},
1512
hir::ExprId,
1613
};
1714

@@ -191,71 +188,6 @@ impl TypeRef {
191188
pub(crate) fn unit() -> TypeRef {
192189
TypeRef::Tuple(ThinVec::new())
193190
}
194-
195-
pub fn walk(this: TypeRefId, map: &ExpressionStore, f: &mut impl FnMut(TypeRefId, &TypeRef)) {
196-
go(this, f, map);
197-
198-
fn go(
199-
type_ref_id: TypeRefId,
200-
f: &mut impl FnMut(TypeRefId, &TypeRef),
201-
map: &ExpressionStore,
202-
) {
203-
let type_ref = &map[type_ref_id];
204-
f(type_ref_id, type_ref);
205-
match type_ref {
206-
TypeRef::Fn(fn_) => {
207-
fn_.params.iter().for_each(|&(_, param_type)| go(param_type, f, map))
208-
}
209-
TypeRef::Tuple(types) => types.iter().for_each(|&t| go(t, f, map)),
210-
TypeRef::RawPtr(type_ref, _) | TypeRef::Slice(type_ref) => go(*type_ref, f, map),
211-
TypeRef::Reference(it) => go(it.ty, f, map),
212-
TypeRef::Array(it) => go(it.ty, f, map),
213-
TypeRef::ImplTrait(bounds) | TypeRef::DynTrait(bounds) => {
214-
for bound in bounds {
215-
match bound {
216-
&TypeBound::Path(path, _) | &TypeBound::ForLifetime(_, path) => {
217-
go_path(&map[path], f, map)
218-
}
219-
TypeBound::Lifetime(_) | TypeBound::Error | TypeBound::Use(_) => (),
220-
}
221-
}
222-
}
223-
TypeRef::Path(path) => go_path(path, f, map),
224-
TypeRef::Never | TypeRef::Placeholder | TypeRef::Error | TypeRef::TypeParam(_) => {}
225-
};
226-
}
227-
228-
fn go_path(path: &Path, f: &mut impl FnMut(TypeRefId, &TypeRef), map: &ExpressionStore) {
229-
if let Some(type_ref) = path.type_anchor() {
230-
go(type_ref, f, map);
231-
}
232-
for segment in path.segments().iter() {
233-
if let Some(args_and_bindings) = segment.args_and_bindings {
234-
for arg in args_and_bindings.args.iter() {
235-
match arg {
236-
GenericArg::Type(type_ref) => {
237-
go(*type_ref, f, map);
238-
}
239-
GenericArg::Const(_) | GenericArg::Lifetime(_) => {}
240-
}
241-
}
242-
for binding in args_and_bindings.bindings.iter() {
243-
if let Some(type_ref) = binding.type_ref {
244-
go(type_ref, f, map);
245-
}
246-
for bound in binding.bounds.iter() {
247-
match bound {
248-
&TypeBound::Path(path, _) | &TypeBound::ForLifetime(_, path) => {
249-
go_path(&map[path], f, map)
250-
}
251-
TypeBound::Lifetime(_) | TypeBound::Error | TypeBound::Use(_) => (),
252-
}
253-
}
254-
}
255-
}
256-
}
257-
}
258-
}
259191
}
260192

261193
impl TypeBound {

crates/hir-ty/src/consteval.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,15 @@ use rustc_type_ir::inherent::{Const as _, GenericArgs as _, IntoKind, Ty as _};
1919
use stdx::never;
2020

2121
use crate::{
22-
ParamEnvAndCrate,
22+
ParamEnvAndCrate, Span,
2323
db::{AnonConstId, AnonConstLoc, GeneralConstId, HirDatabase},
2424
display::DisplayTarget,
2525
generics::Generics,
2626
mir::{MirEvalError, MirLowerError, pad16},
2727
next_solver::{
2828
Allocation, Const, ConstKind, Consts, DbInterner, DefaultAny, GenericArgs, ParamConst,
2929
ScalarInt, StoredAllocation, StoredEarlyBinder, StoredGenericArgs, Ty, TyKind,
30-
UnevaluatedConst, ValTreeKind, default_types, infer::InferCtxt,
30+
UnevaluatedConst, ValTreeKind, default_types,
3131
},
3232
traits::StoredParamEnvAndCrate,
3333
};
@@ -351,13 +351,13 @@ pub(crate) fn create_anon_const<'a, 'db>(
351351
resolver: &Resolver<'db>,
352352
expected_ty: Ty<'db>,
353353
generics: &dyn Fn() -> &'a Generics<'db>,
354-
infcx: Option<&InferCtxt<'db>>,
354+
create_var: Option<&mut dyn FnMut(Span) -> Const<'db>>,
355355
forbid_params_after: Option<u32>,
356356
) -> Result<Const<'db>, CreateConstError<'db>> {
357357
match &store[expr] {
358358
Expr::Literal(literal) => intern_const_ref(interner, literal, expected_ty),
359-
Expr::Underscore => match infcx {
360-
Some(infcx) => Ok(infcx.next_const_var(expr.into())),
359+
Expr::Underscore => match create_var {
360+
Some(create_var) => Ok(create_var(expr.into())),
361361
None => Err(CreateConstError::UnderscoreExpr),
362362
},
363363
Expr::Path(path)

crates/hir-ty/src/infer.rs

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -82,14 +82,16 @@ use crate::{
8282
expr_use_visitor::{FakeReadCause, Place},
8383
},
8484
coerce::{CoerceMany, DynamicCoerceMany},
85-
diagnostics::{Diagnostics, InferenceTyLoweringContext as TyLoweringContext},
85+
diagnostics::{
86+
Diagnostics, InferenceTyLoweringContext as TyLoweringContext,
87+
InferenceTyLoweringVarsCtx,
88+
},
8689
expr::ExprIsRead,
8790
pat::PatOrigin,
8891
unify::resolve_completely::WriteBackCtxt,
8992
},
9093
lower::{
91-
ImplTraitIdx, ImplTraitLoweringMode, LifetimeElisionKind, TyLoweringInferVarsCtx,
92-
diagnostics::TyLoweringDiagnostic,
94+
ImplTraitIdx, ImplTraitLoweringMode, LifetimeElisionKind, diagnostics::TyLoweringDiagnostic,
9395
},
9496
method_resolution::CandidateId,
9597
next_solver::{
@@ -1834,10 +1836,10 @@ impl<'body, 'db> InferenceContext<'body, 'db> {
18341836
f: impl FnOnce(&mut TyLoweringContext<'db, '_>) -> R,
18351837
) -> R {
18361838
let infer_vars = match types_source {
1837-
InferenceTyDiagnosticSource::Body => Some(TyLoweringInferVarsCtx {
1839+
InferenceTyDiagnosticSource::Body => Some(&mut InferenceTyLoweringVarsCtx {
18381840
table: &mut self.table,
1839-
type_of_placeholder: &mut self.result.type_of_type_placeholder,
1840-
}),
1841+
type_of_type_placeholder: &mut self.result.type_of_type_placeholder,
1842+
} as _),
18411843
InferenceTyDiagnosticSource::Signature => None,
18421844
};
18431845
let mut ctx = TyLoweringContext::new(
@@ -1919,7 +1921,7 @@ impl<'body, 'db> InferenceContext<'body, 'db> {
19191921
&self.resolver,
19201922
expected_ty,
19211923
&|| self.generics(),
1922-
Some(self.infcx()),
1924+
Some(&mut |span| self.table.next_const_var(span)),
19231925
(!(allow_using_generic_params && self.allow_using_generic_params)).then_some(0),
19241926
);
19251927

@@ -2169,6 +2171,10 @@ impl<'body, 'db> InferenceContext<'body, 'db> {
21692171
value_ns: bool,
21702172
) -> (Ty<'db>, Option<VariantId>) {
21712173
let interner = self.interner();
2174+
let mut vars_ctx = InferenceTyLoweringVarsCtx {
2175+
table: &mut self.table,
2176+
type_of_type_placeholder: &mut self.result.type_of_type_placeholder,
2177+
};
21722178
let mut ctx = TyLoweringContext::new(
21732179
self.db,
21742180
&self.resolver,
@@ -2180,10 +2186,7 @@ impl<'body, 'db> InferenceContext<'body, 'db> {
21802186
&self.generics,
21812187
LifetimeElisionKind::Infer,
21822188
self.allow_using_generic_params,
2183-
Some(TyLoweringInferVarsCtx {
2184-
table: &mut self.table,
2185-
type_of_placeholder: &mut self.result.type_of_type_placeholder,
2186-
}),
2189+
Some(&mut vars_ctx),
21872190
&self.defined_anon_consts,
21882191
);
21892192

crates/hir-ty/src/infer/diagnostics.rs

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,21 +6,24 @@ use std::cell::{OnceCell, RefCell};
66
use std::ops::{Deref, DerefMut};
77

88
use either::Either;
9-
use hir_def::expr_store::ExpressionStore;
109
use hir_def::expr_store::path::Path;
1110
use hir_def::{ExpressionStoreOwnerId, GenericDefId};
11+
use hir_def::{expr_store::ExpressionStore, type_ref::TypeRefId};
1212
use hir_def::{hir::ExprOrPatId, resolver::Resolver};
1313
use la_arena::{Idx, RawIdx};
14+
use rustc_hash::FxHashMap;
1415
use thin_vec::ThinVec;
1516

1617
use crate::{
17-
InferenceDiagnostic, InferenceTyDiagnosticSource, TyLoweringDiagnostic,
18+
InferenceDiagnostic, InferenceTyDiagnosticSource, Span, TyLoweringDiagnostic,
1819
db::{AnonConstId, HirDatabase},
1920
generics::Generics,
21+
infer::unify::InferenceTable,
2022
lower::{
2123
ForbidParamsAfterReason, LifetimeElisionKind, TyLoweringContext, TyLoweringInferVarsCtx,
2224
path::{PathDiagnosticCallback, PathLoweringContext},
2325
},
26+
next_solver::{Const, Region, StoredTy, Ty},
2427
};
2528

2629
// Unfortunately, this struct needs to use interior mutability (but we encapsulate it)
@@ -55,6 +58,33 @@ pub(crate) struct PathDiagnosticCallbackData<'a> {
5558
diagnostics: &'a Diagnostics,
5659
}
5760

61+
pub(super) struct InferenceTyLoweringVarsCtx<'a, 'db> {
62+
pub(super) table: &'a mut InferenceTable<'db>,
63+
pub(super) type_of_type_placeholder: &'a mut FxHashMap<TypeRefId, StoredTy>,
64+
}
65+
66+
impl<'db> TyLoweringInferVarsCtx<'db> for InferenceTyLoweringVarsCtx<'_, 'db> {
67+
fn next_ty_var(&mut self, span: Span) -> Ty<'db> {
68+
let ty = self.table.infer_ctxt.next_ty_var(span);
69+
70+
if let Span::TypeRefId(type_ref) = span {
71+
self.type_of_type_placeholder.insert(type_ref, ty.store());
72+
}
73+
74+
ty
75+
}
76+
fn next_const_var(&mut self, span: Span) -> Const<'db> {
77+
self.table.infer_ctxt.next_const_var(span)
78+
}
79+
fn next_region_var(&mut self, span: Span) -> Region<'db> {
80+
self.table.infer_ctxt.next_region_var(span)
81+
}
82+
83+
fn as_table(&mut self) -> Option<&mut InferenceTable<'db>> {
84+
Some(self.table)
85+
}
86+
}
87+
5888
pub(super) struct InferenceTyLoweringContext<'db, 'a> {
5989
ctx: TyLoweringContext<'db, 'a>,
6090
diagnostics: &'a Diagnostics,
@@ -75,7 +105,7 @@ impl<'db, 'a> InferenceTyLoweringContext<'db, 'a> {
75105
generics: &'a OnceCell<Generics<'db>>,
76106
lifetime_elision: LifetimeElisionKind<'db>,
77107
allow_using_generic_params: bool,
78-
infer_vars: Option<TyLoweringInferVarsCtx<'a, 'db>>,
108+
infer_vars: Option<&'a mut dyn TyLoweringInferVarsCtx<'db>>,
79109
defined_anon_consts: &'a RefCell<ThinVec<AnonConstId>>,
80110
) -> Self {
81111
let mut ctx = TyLoweringContext::new(

crates/hir-ty/src/infer/path.rs

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,10 @@ use stdx::never;
1212

1313
use crate::{
1414
InferenceDiagnostic, Span, ValueTyDefId,
15-
infer::diagnostics::InferenceTyLoweringContext as TyLoweringContext,
16-
lower::{GenericPredicates, LifetimeElisionKind, TyLoweringInferVarsCtx},
15+
infer::{
16+
InferenceTyLoweringVarsCtx, diagnostics::InferenceTyLoweringContext as TyLoweringContext,
17+
},
18+
lower::{GenericPredicates, LifetimeElisionKind},
1719
method_resolution::{self, CandidateId, MethodError},
1820
next_solver::{
1921
GenericArg, GenericArgs, TraitRef, Ty, Unnormalized, infer::traits::ObligationCause,
@@ -139,6 +141,10 @@ impl<'db> InferenceContext<'_, 'db> {
139141
no_diagnostics: bool,
140142
) -> Option<(ValueNs, Option<GenericArgs<'db>>)> {
141143
// Don't use `self.make_ty()` here as we need `orig_ns`.
144+
let mut vars_ctx = InferenceTyLoweringVarsCtx {
145+
table: &mut self.table,
146+
type_of_type_placeholder: &mut self.result.type_of_type_placeholder,
147+
};
142148
let mut ctx = TyLoweringContext::new(
143149
self.db,
144150
&self.resolver,
@@ -150,10 +156,7 @@ impl<'db> InferenceContext<'_, 'db> {
150156
&self.generics,
151157
LifetimeElisionKind::Infer,
152158
self.allow_using_generic_params,
153-
Some(TyLoweringInferVarsCtx {
154-
table: &mut self.table,
155-
type_of_placeholder: &mut self.result.type_of_type_placeholder,
156-
}),
159+
Some(&mut vars_ctx),
157160
&self.defined_anon_consts,
158161
);
159162
let mut path_ctx = if no_diagnostics {

crates/hir-ty/src/lib.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,8 @@ pub use infer::{
110110
};
111111
pub use lower::{
112112
GenericDefaults, GenericDefaultsRef, GenericPredicates, ImplTraits, LifetimeElisionKind,
113-
TyDefId, TyLoweringContext, TyLoweringResult, ValueTyDefId, diagnostics::*,
113+
TyDefId, TyLoweringContext, TyLoweringInferVarsCtx, TyLoweringResult, ValueTyDefId,
114+
diagnostics::*,
114115
};
115116
pub use next_solver::interner::{attach_db, attach_db_allow_change, with_attached_db};
116117
pub use target_feature::TargetFeatures;

0 commit comments

Comments
 (0)