Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit 1aae522

Browse files
Complete diagnostics in ty lowering groundwork
Implement diagnostics in all places left: generics (predicates, defaults, const params' types), fields, and type aliases. Unfortunately this results in a 20mb addition in `analysis-stats .` due to many type methods returning an addition diagnostics result now (even if it's `None` in most cases). I'm not sure if this can be improved. An alternative strategy that can prevent the memory usage growth is to never produce diagnostics in hir-ty methods. Instead, lower all types in the hir crate when computing diagnostics from scratch (with diagnostics this time). But this has two serious disadvantages: 1. This can cause code duplication (although it can probably be not that bad, it will still mean a lot more code). 2. I believe we eventually want to compute diagnostics for the *entire* workspace (either on-type or on-save or something alike), so users can know when they have diagnostics even in inactive files. Choosing this approach will mean we lose all precomputed salsa queries. For one file this is fine, for the whole workspace this will be very slow.
1 parent 63acf60 commit 1aae522

File tree

8 files changed

+621
-103
lines changed

8 files changed

+621
-103
lines changed

src/tools/rust-analyzer/crates/hir-def/src/generics.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,11 @@ impl GenericParams {
224224
self.len() == 0
225225
}
226226

227+
#[inline]
228+
pub fn no_predicates(&self) -> bool {
229+
self.where_predicates.is_empty()
230+
}
231+
227232
#[inline]
228233
pub fn where_predicates(&self) -> std::slice::Iter<'_, WherePredicate> {
229234
self.where_predicates.iter()

src/tools/rust-analyzer/crates/hir-ty/src/db.rs

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ use crate::{
2222
consteval::ConstEvalError,
2323
dyn_compatibility::DynCompatibilityViolation,
2424
layout::{Layout, LayoutError},
25-
lower::{GenericDefaults, GenericPredicates},
25+
lower::{Diagnostics, GenericDefaults, GenericPredicates},
2626
method_resolution::{InherentImpls, TraitImpls, TyFingerprint},
2727
mir::{BorrowckResult, MirBody, MirLowerError},
2828
Binders, ClosureId, Const, FnDefId, ImplTraitId, ImplTraits, InferenceResult, Interner,
@@ -115,21 +115,35 @@ pub trait HirDatabase: DefDatabase + Upcast<dyn DefDatabase> {
115115
#[ra_salsa::cycle(crate::lower::ty_recover)]
116116
fn ty(&self, def: TyDefId) -> Binders<Ty>;
117117

118+
#[ra_salsa::invoke(crate::lower::type_for_type_alias_with_diagnostics_query)]
119+
fn type_for_type_alias_with_diagnostics(&self, def: TypeAliasId) -> (Binders<Ty>, Diagnostics);
120+
118121
/// Returns the type of the value of the given constant, or `None` if the `ValueTyDefId` is
119122
/// a `StructId` or `EnumVariantId` with a record constructor.
120123
#[ra_salsa::invoke(crate::lower::value_ty_query)]
121124
fn value_ty(&self, def: ValueTyDefId) -> Option<Binders<Ty>>;
122125

126+
#[ra_salsa::invoke(crate::lower::impl_self_ty_with_diagnostics_query)]
127+
#[ra_salsa::cycle(crate::lower::impl_self_ty_with_diagnostics_recover)]
128+
fn impl_self_ty_with_diagnostics(&self, def: ImplId) -> (Binders<Ty>, Diagnostics);
123129
#[ra_salsa::invoke(crate::lower::impl_self_ty_query)]
124-
#[ra_salsa::cycle(crate::lower::impl_self_ty_recover)]
125130
fn impl_self_ty(&self, def: ImplId) -> Binders<Ty>;
126131

132+
#[ra_salsa::invoke(crate::lower::const_param_ty_with_diagnostics_query)]
133+
fn const_param_ty_with_diagnostics(&self, def: ConstParamId) -> (Ty, Diagnostics);
127134
#[ra_salsa::invoke(crate::lower::const_param_ty_query)]
128135
fn const_param_ty(&self, def: ConstParamId) -> Ty;
129136

137+
#[ra_salsa::invoke(crate::lower::impl_trait_with_diagnostics_query)]
138+
fn impl_trait_with_diagnostics(&self, def: ImplId) -> Option<(Binders<TraitRef>, Diagnostics)>;
130139
#[ra_salsa::invoke(crate::lower::impl_trait_query)]
131140
fn impl_trait(&self, def: ImplId) -> Option<Binders<TraitRef>>;
132141

142+
#[ra_salsa::invoke(crate::lower::field_types_with_diagnostics_query)]
143+
fn field_types_with_diagnostics(
144+
&self,
145+
var: VariantId,
146+
) -> (Arc<ArenaMap<LocalFieldId, Binders<Ty>>>, Diagnostics);
133147
#[ra_salsa::invoke(crate::lower::field_types_query)]
134148
fn field_types(&self, var: VariantId) -> Arc<ArenaMap<LocalFieldId, Binders<Ty>>>;
135149

@@ -154,6 +168,11 @@ pub trait HirDatabase: DefDatabase + Upcast<dyn DefDatabase> {
154168
#[ra_salsa::invoke(crate::lower::generic_predicates_query)]
155169
fn generic_predicates(&self, def: GenericDefId) -> GenericPredicates;
156170

171+
#[ra_salsa::invoke(crate::lower::generic_predicates_without_parent_with_diagnostics_query)]
172+
fn generic_predicates_without_parent_with_diagnostics(
173+
&self,
174+
def: GenericDefId,
175+
) -> (GenericPredicates, Diagnostics);
157176
#[ra_salsa::invoke(crate::lower::generic_predicates_without_parent_query)]
158177
fn generic_predicates_without_parent(&self, def: GenericDefId) -> GenericPredicates;
159178

@@ -164,8 +183,13 @@ pub trait HirDatabase: DefDatabase + Upcast<dyn DefDatabase> {
164183
#[ra_salsa::invoke(crate::lower::trait_environment_query)]
165184
fn trait_environment(&self, def: GenericDefId) -> Arc<TraitEnvironment>;
166185

186+
#[ra_salsa::invoke(crate::lower::generic_defaults_with_diagnostics_query)]
187+
#[ra_salsa::cycle(crate::lower::generic_defaults_with_diagnostics_recover)]
188+
fn generic_defaults_with_diagnostics(
189+
&self,
190+
def: GenericDefId,
191+
) -> (GenericDefaults, Diagnostics);
167192
#[ra_salsa::invoke(crate::lower::generic_defaults_query)]
168-
#[ra_salsa::cycle(crate::lower::generic_defaults_recover)]
169193
fn generic_defaults(&self, def: GenericDefId) -> GenericDefaults;
170194

171195
#[ra_salsa::invoke(InherentImpls::inherent_impls_in_crate_query)]

src/tools/rust-analyzer/crates/hir-ty/src/generics.rs

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,10 @@ impl Generics {
5555
self.def
5656
}
5757

58+
pub(crate) fn self_types_map(&self) -> &TypesMap {
59+
&self.params.types_map
60+
}
61+
5862
pub(crate) fn iter_id(&self) -> impl Iterator<Item = GenericParamId> + '_ {
5963
self.iter_self_id().chain(self.iter_parent_id())
6064
}
@@ -86,15 +90,13 @@ impl Generics {
8690
self.iter_self().chain(self.iter_parent())
8791
}
8892

89-
pub(crate) fn iter_with_types_map(
93+
pub(crate) fn iter_parents_with_types_map(
9094
&self,
9195
) -> impl Iterator<Item = ((GenericParamId, GenericParamDataRef<'_>), &TypesMap)> + '_ {
92-
self.iter_self().zip(std::iter::repeat(&self.params.types_map)).chain(
93-
self.iter_parent().zip(
94-
self.parent_generics()
95-
.into_iter()
96-
.flat_map(|it| std::iter::repeat(&it.params.types_map)),
97-
),
96+
self.iter_parent().zip(
97+
self.parent_generics()
98+
.into_iter()
99+
.flat_map(|it| std::iter::repeat(&it.params.types_map)),
98100
)
99101
}
100102

src/tools/rust-analyzer/crates/hir-ty/src/lib.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,8 @@ pub use infer::{
9090
pub use interner::Interner;
9191
pub use lower::{
9292
associated_type_shorthand_candidates, GenericArgsProhibitedReason, ImplTraitLoweringMode,
93-
ParamLoweringMode, TyDefId, TyLoweringContext, TyLoweringDiagnosticKind, ValueTyDefId,
93+
ParamLoweringMode, TyDefId, TyLoweringContext, TyLoweringDiagnostic, TyLoweringDiagnosticKind,
94+
ValueTyDefId,
9495
};
9596
pub use mapping::{
9697
from_assoc_type_id, from_chalk_trait_id, from_foreign_def_id, from_placeholder_idx,

0 commit comments

Comments
 (0)