Skip to content

Commit f32fe40

Browse files
committed
Hookup analysis_stats
1 parent 408301a commit f32fe40

8 files changed

Lines changed: 214 additions & 73 deletions

File tree

crates/hir-def/src/expr_store.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -319,7 +319,7 @@ impl ExpressionStoreBuilder {
319319
mut bindings,
320320
mut binding_owners,
321321
mut ident_hygiene,
322-
const_expr_origins,
322+
mut const_expr_origins,
323323
mut types,
324324
mut lifetimes,
325325

@@ -379,6 +379,9 @@ impl ExpressionStoreBuilder {
379379

380380
let store = {
381381
let expr_only = if has_exprs {
382+
if let Some(const_expr_origins) = &mut const_expr_origins {
383+
const_expr_origins.shrink_to_fit();
384+
}
382385
Some(Box::new(ExpressionOnlyStore {
383386
exprs,
384387
pats,

crates/hir-def/src/resolver.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -860,7 +860,15 @@ impl<'db> Resolver<'db> {
860860
owner: impl Into<ExpressionStoreOwner>,
861861
expr_id: ExprId,
862862
) -> UpdateGuard {
863-
let owner = owner.into();
863+
self.update_to_inner_scope_(db, owner.into(), expr_id)
864+
}
865+
866+
fn update_to_inner_scope_(
867+
&mut self,
868+
db: &'db dyn DefDatabase,
869+
owner: ExpressionStoreOwner,
870+
expr_id: ExprId,
871+
) -> UpdateGuard {
864872
#[inline(always)]
865873
fn append_expr_scope<'db>(
866874
db: &'db dyn DefDatabase,

crates/hir-ty/src/infer.rs

Lines changed: 9 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,9 @@ use std::{cell::OnceCell, convert::identity, iter};
3333
use base_db::Crate;
3434
use either::Either;
3535
use hir_def::{
36-
AdtId, AnonConstId, AssocItemId, ConstId, ConstParamId, DefWithBodyId, ExpressionStoreOwner,
37-
FieldId, FunctionId, GenericDefId, GenericParamId, ItemContainerId, LocalFieldId, Lookup,
38-
TraitId, TupleFieldId, TupleId, TypeAliasId, TypeOrConstParamId, VariantId,
36+
AdtId, AssocItemId, ConstId, ConstParamId, DefWithBodyId, ExpressionStoreOwner, FieldId,
37+
FunctionId, GenericDefId, GenericParamId, ItemContainerId, LocalFieldId, Lookup, TraitId,
38+
TupleFieldId, TupleId, TypeAliasId, TypeOrConstParamId, VariantId,
3939
expr_store::{ConstExprOrigin, ExpressionStore, HygieneId, path::Path},
4040
hir::{BindingAnnotation, BindingId, ExprId, ExprOrPatId, LabelId, PatId},
4141
lang_item::LangItems,
@@ -146,6 +146,10 @@ pub fn infer_query_with_inspect<'db>(
146146

147147
ctx.infer_mut_body(body.body_expr);
148148

149+
finalize_infer(ctx)
150+
}
151+
152+
fn finalize_infer(mut ctx: InferenceContext<'_, '_>) -> InferenceResult {
149153
ctx.handle_opaque_type_uses();
150154

151155
ctx.type_inference_fallback();
@@ -213,9 +217,7 @@ fn infer_signature_query(db: &dyn HirDatabase, def: GenericDefId) -> InferenceRe
213217
ctx.infer_expr(root_expr, &expected, ExprIsRead::Yes);
214218
}
215219

216-
ctx.type_inference_fallback();
217-
ctx.table.select_obligations_where_possible();
218-
ctx.resolve_all()
220+
finalize_infer(ctx)
219221
}
220222

221223
fn infer_signature_cycle_result(
@@ -618,24 +620,6 @@ impl InferenceResult {
618620
}
619621

620622
impl InferenceResult {
621-
/// Look up inference results for a specific anonymous const in a signature.
622-
///
623-
/// This delegates to [`Self::for_signature`] on the anon const's owner.
624-
/// The returned `InferenceResult` contains types for *all* expressions in
625-
/// the owner's signature store, not just this anon const's sub-tree.
626-
/// Callers should index into it with `loc.expr` to get the root expression's
627-
/// type.
628-
// FIXME: This function doesn't make sense in that we can't return a full inference result here
629-
// as the anon const is just part of an inference result.
630-
pub fn for_anon_const(db: &dyn HirDatabase, id: AnonConstId) -> &InferenceResult {
631-
match id.lookup(db).owner {
632-
ExpressionStoreOwner::Signature(generic_def_id) => {
633-
Self::for_signature(db, generic_def_id)
634-
}
635-
ExpressionStoreOwner::Body(def_with_body_id) => Self::for_body(db, def_with_body_id),
636-
}
637-
}
638-
639623
fn new(error_ty: Ty<'_>) -> Self {
640624
Self {
641625
method_resolutions: Default::default(),
@@ -943,8 +927,7 @@ impl<'body, 'db> InferenceContext<'body, 'db> {
943927
db.trait_environment_for_body(def_with_body_id)
944928
}
945929
};
946-
let table =
947-
unify::InferenceTable::new(db, trait_env, resolver.krate(), owner.as_def_with_body());
930+
let table = unify::InferenceTable::new(db, trait_env, resolver.krate(), Some(owner));
948931
let types = crate::next_solver::default_types(db);
949932
InferenceContext {
950933
result: InferenceResult::new(types.types.error),

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
use std::fmt;
44

55
use base_db::Crate;
6-
use hir_def::{AdtId, DefWithBodyId, GenericParamId};
6+
use hir_def::{AdtId, ExpressionStoreOwner, GenericParamId};
77
use hir_expand::name::Name;
88
use intern::sym;
99
use rustc_hash::FxHashSet;
@@ -147,7 +147,7 @@ impl<'db> InferenceTable<'db> {
147147
db: &'db dyn HirDatabase,
148148
trait_env: ParamEnv<'db>,
149149
krate: Crate,
150-
owner: Option<DefWithBodyId>,
150+
owner: Option<ExpressionStoreOwner>,
151151
) -> Self {
152152
let interner = DbInterner::new_with(db, krate);
153153
let typing_mode = match owner {

crates/hir-ty/src/next_solver/def_id.rs

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
33
use hir_def::{
44
AdtId, AnonConstId, AttrDefId, BuiltinDeriveImplId, CallableDefId, ConstId, DefWithBodyId,
5-
EnumId, EnumVariantId, FunctionId, GeneralConstId, GenericDefId, ImplId, StaticId, StructId,
6-
TraitId, TypeAliasId, UnionId,
5+
EnumId, EnumVariantId, ExpressionStoreOwner, FunctionId, GeneralConstId, GenericDefId, ImplId,
6+
StaticId, StructId, TraitId, TypeAliasId, UnionId,
77
};
88
use rustc_type_ir::inherent;
99
use stdx::impl_from;
@@ -12,13 +12,13 @@ use crate::db::{InternedClosureId, InternedCoroutineId, InternedOpaqueTyId};
1212

1313
use super::DbInterner;
1414

15-
#[derive(Debug, PartialOrd, Ord, Clone, Copy, PartialEq, Eq, Hash, salsa::Supertype)]
15+
#[derive(Debug, PartialOrd, Ord, Clone, Copy, PartialEq, Eq, Hash)]
1616
pub enum Ctor {
1717
Struct(StructId),
1818
Enum(EnumVariantId),
1919
}
2020

21-
#[derive(PartialOrd, Ord, Clone, Copy, PartialEq, Eq, Hash, salsa::Supertype)]
21+
#[derive(PartialOrd, Ord, Clone, Copy, PartialEq, Eq, Hash)]
2222
pub enum SolverDefId {
2323
AdtId(AdtId),
2424
ConstId(ConstId),
@@ -33,7 +33,6 @@ pub enum SolverDefId {
3333
InternedCoroutineId(InternedCoroutineId),
3434
InternedOpaqueTyId(InternedOpaqueTyId),
3535
EnumVariantId(EnumVariantId),
36-
// FIXME(next-solver): Do we need the separation of `Ctor`? It duplicates some variants.
3736
Ctor(Ctor),
3837
}
3938

@@ -139,6 +138,15 @@ impl From<GenericDefId> for SolverDefId {
139138
}
140139
}
141140

141+
impl From<ExpressionStoreOwner> for SolverDefId {
142+
fn from(value: ExpressionStoreOwner) -> Self {
143+
match value {
144+
ExpressionStoreOwner::Signature(generic_def_id) => generic_def_id.into(),
145+
ExpressionStoreOwner::Body(def_with_body_id) => def_with_body_id.into(),
146+
}
147+
}
148+
}
149+
142150
impl From<GeneralConstId> for SolverDefId {
143151
#[inline]
144152
fn from(value: GeneralConstId) -> Self {

crates/hir/src/lib.rs

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -508,6 +508,21 @@ impl ModuleDef {
508508
}
509509
}
510510

511+
pub fn as_generic_def(self) -> Option<GenericDef> {
512+
match self {
513+
ModuleDef::Function(it) => Some(it.into()),
514+
ModuleDef::Adt(it) => Some(it.into()),
515+
ModuleDef::Trait(it) => Some(it.into()),
516+
ModuleDef::TypeAlias(it) => Some(it.into()),
517+
ModuleDef::Static(it) => Some(it.into()),
518+
ModuleDef::Const(it) => Some(it.into()),
519+
ModuleDef::Variant(_)
520+
| ModuleDef::Module(_)
521+
| ModuleDef::BuiltinType(_)
522+
| ModuleDef::Macro(_) => None,
523+
}
524+
}
525+
511526
pub fn attrs(&self, db: &dyn HirDatabase) -> Option<AttrsWithOwner> {
512527
Some(match self {
513528
ModuleDef::Module(it) => it.attrs(db),
@@ -3972,6 +3987,30 @@ impl_from!(
39723987
);
39733988

39743989
impl GenericDef {
3990+
pub fn name(self, db: &dyn HirDatabase) -> Option<Name> {
3991+
match self {
3992+
GenericDef::Function(it) => Some(it.name(db)),
3993+
GenericDef::Adt(it) => Some(it.name(db)),
3994+
GenericDef::Trait(it) => Some(it.name(db)),
3995+
GenericDef::TypeAlias(it) => Some(it.name(db)),
3996+
GenericDef::Impl(_) => None,
3997+
GenericDef::Const(it) => it.name(db),
3998+
GenericDef::Static(it) => Some(it.name(db)),
3999+
}
4000+
}
4001+
4002+
pub fn module(self, db: &dyn HirDatabase) -> Module {
4003+
match self {
4004+
GenericDef::Function(it) => it.module(db),
4005+
GenericDef::Adt(it) => it.module(db),
4006+
GenericDef::Trait(it) => it.module(db),
4007+
GenericDef::TypeAlias(it) => it.module(db),
4008+
GenericDef::Impl(it) => it.module(db),
4009+
GenericDef::Const(it) => it.module(db),
4010+
GenericDef::Static(it) => it.module(db),
4011+
}
4012+
}
4013+
39754014
pub fn params(self, db: &dyn HirDatabase) -> Vec<GenericParam> {
39764015
let Ok(id) = self.try_into() else {
39774016
// Let's pretend builtin derive impls don't have generic parameters.

crates/hir/src/source_analyzer.rs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ impl<'db> SourceAnalyzer<'db> {
152152
node: InFile<&SyntaxNode>,
153153
offset: Option<TextSize>,
154154
) -> SourceAnalyzer<'db> {
155-
Self::new_generic_def_(db, def, node, offset, Some(InferenceResult::for_signature(db, def)))
155+
Self::new_generic_def_(db, def, node, offset, true)
156156
}
157157

158158
pub(crate) fn new_generic_def_no_infer(
@@ -161,15 +161,15 @@ impl<'db> SourceAnalyzer<'db> {
161161
node: InFile<&SyntaxNode>,
162162
offset: Option<TextSize>,
163163
) -> SourceAnalyzer<'db> {
164-
Self::new_generic_def_(db, def, node, offset, None)
164+
Self::new_generic_def_(db, def, node, offset, false)
165165
}
166166

167167
pub(crate) fn new_generic_def_(
168168
db: &'db dyn HirDatabase,
169169
def: GenericDefId,
170170
node @ InFile { file_id, .. }: InFile<&SyntaxNode>,
171171
offset: Option<TextSize>,
172-
infer: Option<&'db InferenceResult>,
172+
infer: bool,
173173
) -> SourceAnalyzer<'db> {
174174
let (generics, store, source_map) = db.generic_params_and_store_and_source_map(def);
175175
let scopes = db.expr_scopes(def.into());
@@ -186,6 +186,11 @@ impl<'db> SourceAnalyzer<'db> {
186186
}
187187
};
188188
let resolver = resolver_for_scope(db, def, scope);
189+
let infer = if infer && !Arc::ptr_eq(&store, &ExpressionStore::empty_singleton().0) {
190+
Some(InferenceResult::for_signature(db, def))
191+
} else {
192+
None
193+
};
189194
SourceAnalyzer {
190195
resolver,
191196
body_or_sig: Some(BodyOrSig::Sig { def, store, source_map, generics, infer }),

0 commit comments

Comments
 (0)