Skip to content

Commit d21f5f7

Browse files
Merge #6911
6911: decl_check: don't pass `db` around so often r=matklad a=jonas-schievink Instead, store it in the `DeclValidator`. Also pass the `CrateId` that defines the checked item along. This is not yet needed, but will be once I've refactored `Attrs` to handle `cfg_attr` internally. We could also try to extract the crate from the "owner" `ModuleDefId` instead of passing it in, but then it might not be present for builtin types. Open to suggestions. Co-authored-by: Jonas Schievink <[email protected]>
2 parents 554dd21 + 6615fda commit d21f5f7

File tree

3 files changed

+68
-60
lines changed

3 files changed

+68
-60
lines changed

crates/hir/src/code_model.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,12 @@ impl ModuleDef {
267267
_ => return,
268268
};
269269

270-
hir_ty::diagnostics::validate_module_item(db, id, sink)
270+
let module = match self.module(db) {
271+
Some(it) => it,
272+
None => return,
273+
};
274+
275+
hir_ty::diagnostics::validate_module_item(db, module.id.krate, id, sink)
271276
}
272277
}
273278

@@ -780,8 +785,9 @@ impl Function {
780785
}
781786

782787
pub fn diagnostics(self, db: &dyn HirDatabase, sink: &mut DiagnosticSink) {
788+
let krate = self.module(db).id.krate;
783789
hir_def::diagnostics::validate_body(db.upcast(), self.id.into(), sink);
784-
hir_ty::diagnostics::validate_module_item(db, self.id.into(), sink);
790+
hir_ty::diagnostics::validate_module_item(db, krate, self.id.into(), sink);
785791
hir_ty::diagnostics::validate_body(db, self.id.into(), sink);
786792
}
787793

crates/hir_ty/src/diagnostics.rs

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ mod decl_check;
66

77
use std::{any::Any, fmt};
88

9+
use base_db::CrateId;
910
use hir_def::{DefWithBodyId, ModuleDefId};
1011
use hir_expand::diagnostics::{Diagnostic, DiagnosticCode, DiagnosticSink};
1112
use hir_expand::{name::Name, HirFileId, InFile};
@@ -18,12 +19,13 @@ pub use crate::diagnostics::expr::{record_literal_missing_fields, record_pattern
1819

1920
pub fn validate_module_item(
2021
db: &dyn HirDatabase,
22+
krate: CrateId,
2123
owner: ModuleDefId,
2224
sink: &mut DiagnosticSink<'_>,
2325
) {
2426
let _p = profile::span("validate_module_item");
25-
let mut validator = decl_check::DeclValidator::new(owner, sink);
26-
validator.validate_item(db);
27+
let mut validator = decl_check::DeclValidator::new(db, krate, sink);
28+
validator.validate_item(owner);
2729
}
2830

2931
pub fn validate_body(db: &dyn HirDatabase, owner: DefWithBodyId, sink: &mut DiagnosticSink<'_>) {
@@ -407,7 +409,7 @@ mod tests {
407409
for (module_id, _) in crate_def_map.modules.iter() {
408410
for decl in crate_def_map[module_id].scope.declarations() {
409411
let mut sink = DiagnosticSinkBuilder::new().build(&mut cb);
410-
validate_module_item(self, decl, &mut sink);
412+
validate_module_item(self, krate, decl, &mut sink);
411413

412414
if let ModuleDefId::FunctionId(f) = decl {
413415
fns.push(f)
@@ -419,7 +421,12 @@ mod tests {
419421
for item in impl_data.items.iter() {
420422
if let AssocItemId::FunctionId(f) = item {
421423
let mut sink = DiagnosticSinkBuilder::new().build(&mut cb);
422-
validate_module_item(self, ModuleDefId::FunctionId(*f), &mut sink);
424+
validate_module_item(
425+
self,
426+
krate,
427+
ModuleDefId::FunctionId(*f),
428+
&mut sink,
429+
);
423430
fns.push(*f)
424431
}
425432
}

crates/hir_ty/src/diagnostics/decl_check.rs

Lines changed: 49 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
1313
mod case_conv;
1414

15+
use base_db::CrateId;
1516
use hir_def::{
1617
adt::VariantData,
1718
expr::{Pat, PatId},
@@ -40,7 +41,8 @@ mod allow {
4041
}
4142

4243
pub(super) struct DeclValidator<'a, 'b: 'a> {
43-
owner: ModuleDefId,
44+
db: &'a dyn HirDatabase,
45+
krate: CrateId,
4446
sink: &'a mut DiagnosticSink<'b>,
4547
}
4648

@@ -53,26 +55,27 @@ struct Replacement {
5355

5456
impl<'a, 'b> DeclValidator<'a, 'b> {
5557
pub(super) fn new(
56-
owner: ModuleDefId,
58+
db: &'a dyn HirDatabase,
59+
krate: CrateId,
5760
sink: &'a mut DiagnosticSink<'b>,
5861
) -> DeclValidator<'a, 'b> {
59-
DeclValidator { owner, sink }
62+
DeclValidator { db, krate, sink }
6063
}
6164

62-
pub(super) fn validate_item(&mut self, db: &dyn HirDatabase) {
63-
match self.owner {
64-
ModuleDefId::FunctionId(func) => self.validate_func(db, func),
65-
ModuleDefId::AdtId(adt) => self.validate_adt(db, adt),
66-
ModuleDefId::ConstId(const_id) => self.validate_const(db, const_id),
67-
ModuleDefId::StaticId(static_id) => self.validate_static(db, static_id),
65+
pub(super) fn validate_item(&mut self, item: ModuleDefId) {
66+
match item {
67+
ModuleDefId::FunctionId(func) => self.validate_func(func),
68+
ModuleDefId::AdtId(adt) => self.validate_adt(adt),
69+
ModuleDefId::ConstId(const_id) => self.validate_const(const_id),
70+
ModuleDefId::StaticId(static_id) => self.validate_static(static_id),
6871
_ => return,
6972
}
7073
}
7174

72-
fn validate_adt(&mut self, db: &dyn HirDatabase, adt: AdtId) {
75+
fn validate_adt(&mut self, adt: AdtId) {
7376
match adt {
74-
AdtId::StructId(struct_id) => self.validate_struct(db, struct_id),
75-
AdtId::EnumId(enum_id) => self.validate_enum(db, enum_id),
77+
AdtId::StructId(struct_id) => self.validate_struct(struct_id),
78+
AdtId::EnumId(enum_id) => self.validate_enum(enum_id),
7679
AdtId::UnionId(_) => {
7780
// Unions aren't yet supported by this validator.
7881
}
@@ -82,27 +85,27 @@ impl<'a, 'b> DeclValidator<'a, 'b> {
8285
/// Checks whether not following the convention is allowed for this item.
8386
///
8487
/// Currently this method doesn't check parent attributes.
85-
fn allowed(&self, db: &dyn HirDatabase, id: AttrDefId, allow_name: &str) -> bool {
86-
db.attrs(id).by_key("allow").tt_values().any(|tt| tt.to_string().contains(allow_name))
88+
fn allowed(&self, id: AttrDefId, allow_name: &str) -> bool {
89+
self.db.attrs(id).by_key("allow").tt_values().any(|tt| tt.to_string().contains(allow_name))
8790
}
8891

89-
fn validate_func(&mut self, db: &dyn HirDatabase, func: FunctionId) {
90-
let data = db.function_data(func);
92+
fn validate_func(&mut self, func: FunctionId) {
93+
let data = self.db.function_data(func);
9194
if data.is_extern {
9295
mark::hit!(extern_func_incorrect_case_ignored);
9396
return;
9497
}
9598

96-
let body = db.body(func.into());
99+
let body = self.db.body(func.into());
97100

98101
// Recursively validate inner scope items, such as static variables and constants.
99102
for (item_id, _) in body.item_scope.values() {
100-
let mut validator = DeclValidator::new(item_id, self.sink);
101-
validator.validate_item(db);
103+
let mut validator = DeclValidator::new(self.db, self.krate, self.sink);
104+
validator.validate_item(item_id);
102105
}
103106

104107
// Check whether non-snake case identifiers are allowed for this function.
105-
if self.allowed(db, func.into(), allow::NON_SNAKE_CASE) {
108+
if self.allowed(func.into(), allow::NON_SNAKE_CASE) {
106109
return;
107110
}
108111

@@ -169,19 +172,17 @@ impl<'a, 'b> DeclValidator<'a, 'b> {
169172
// If there is at least one element to spawn a warning on, go to the source map and generate a warning.
170173
self.create_incorrect_case_diagnostic_for_func(
171174
func,
172-
db,
173175
fn_name_replacement,
174176
fn_param_replacements,
175177
);
176-
self.create_incorrect_case_diagnostic_for_variables(func, db, pats_replacements);
178+
self.create_incorrect_case_diagnostic_for_variables(func, pats_replacements);
177179
}
178180

179181
/// Given the information about incorrect names in the function declaration, looks up into the source code
180182
/// for exact locations and adds diagnostics into the sink.
181183
fn create_incorrect_case_diagnostic_for_func(
182184
&mut self,
183185
func: FunctionId,
184-
db: &dyn HirDatabase,
185186
fn_name_replacement: Option<Replacement>,
186187
fn_param_replacements: Vec<Replacement>,
187188
) {
@@ -190,8 +191,8 @@ impl<'a, 'b> DeclValidator<'a, 'b> {
190191
return;
191192
}
192193

193-
let fn_loc = func.lookup(db.upcast());
194-
let fn_src = fn_loc.source(db.upcast());
194+
let fn_loc = func.lookup(self.db.upcast());
195+
let fn_src = fn_loc.source(self.db.upcast());
195196

196197
// Diagnostic for function name.
197198
if let Some(replacement) = fn_name_replacement {
@@ -282,20 +283,19 @@ impl<'a, 'b> DeclValidator<'a, 'b> {
282283
fn create_incorrect_case_diagnostic_for_variables(
283284
&mut self,
284285
func: FunctionId,
285-
db: &dyn HirDatabase,
286286
pats_replacements: Vec<(PatId, Replacement)>,
287287
) {
288288
// XXX: only look at source_map if we do have missing fields
289289
if pats_replacements.is_empty() {
290290
return;
291291
}
292292

293-
let (_, source_map) = db.body_with_source_map(func.into());
293+
let (_, source_map) = self.db.body_with_source_map(func.into());
294294

295295
for (id, replacement) in pats_replacements {
296296
if let Ok(source_ptr) = source_map.pat_syntax(id) {
297297
if let Some(expr) = source_ptr.value.as_ref().left() {
298-
let root = source_ptr.file_syntax(db.upcast());
298+
let root = source_ptr.file_syntax(self.db.upcast());
299299
if let ast::Pat::IdentPat(ident_pat) = expr.to_node(&root) {
300300
let parent = match ident_pat.syntax().parent() {
301301
Some(parent) => parent,
@@ -333,12 +333,11 @@ impl<'a, 'b> DeclValidator<'a, 'b> {
333333
}
334334
}
335335

336-
fn validate_struct(&mut self, db: &dyn HirDatabase, struct_id: StructId) {
337-
let data = db.struct_data(struct_id);
336+
fn validate_struct(&mut self, struct_id: StructId) {
337+
let data = self.db.struct_data(struct_id);
338338

339-
let non_camel_case_allowed =
340-
self.allowed(db, struct_id.into(), allow::NON_CAMEL_CASE_TYPES);
341-
let non_snake_case_allowed = self.allowed(db, struct_id.into(), allow::NON_SNAKE_CASE);
339+
let non_camel_case_allowed = self.allowed(struct_id.into(), allow::NON_CAMEL_CASE_TYPES);
340+
let non_snake_case_allowed = self.allowed(struct_id.into(), allow::NON_SNAKE_CASE);
342341

343342
// Check the structure name.
344343
let struct_name = data.name.to_string();
@@ -379,7 +378,6 @@ impl<'a, 'b> DeclValidator<'a, 'b> {
379378
// If there is at least one element to spawn a warning on, go to the source map and generate a warning.
380379
self.create_incorrect_case_diagnostic_for_struct(
381380
struct_id,
382-
db,
383381
struct_name_replacement,
384382
struct_fields_replacements,
385383
);
@@ -390,7 +388,6 @@ impl<'a, 'b> DeclValidator<'a, 'b> {
390388
fn create_incorrect_case_diagnostic_for_struct(
391389
&mut self,
392390
struct_id: StructId,
393-
db: &dyn HirDatabase,
394391
struct_name_replacement: Option<Replacement>,
395392
struct_fields_replacements: Vec<Replacement>,
396393
) {
@@ -399,8 +396,8 @@ impl<'a, 'b> DeclValidator<'a, 'b> {
399396
return;
400397
}
401398

402-
let struct_loc = struct_id.lookup(db.upcast());
403-
let struct_src = struct_loc.source(db.upcast());
399+
let struct_loc = struct_id.lookup(self.db.upcast());
400+
let struct_src = struct_loc.source(self.db.upcast());
404401

405402
if let Some(replacement) = struct_name_replacement {
406403
let ast_ptr = match struct_src.value.name() {
@@ -473,11 +470,11 @@ impl<'a, 'b> DeclValidator<'a, 'b> {
473470
}
474471
}
475472

476-
fn validate_enum(&mut self, db: &dyn HirDatabase, enum_id: EnumId) {
477-
let data = db.enum_data(enum_id);
473+
fn validate_enum(&mut self, enum_id: EnumId) {
474+
let data = self.db.enum_data(enum_id);
478475

479476
// Check whether non-camel case names are allowed for this enum.
480-
if self.allowed(db, enum_id.into(), allow::NON_CAMEL_CASE_TYPES) {
477+
if self.allowed(enum_id.into(), allow::NON_CAMEL_CASE_TYPES) {
481478
return;
482479
}
483480

@@ -512,7 +509,6 @@ impl<'a, 'b> DeclValidator<'a, 'b> {
512509
// If there is at least one element to spawn a warning on, go to the source map and generate a warning.
513510
self.create_incorrect_case_diagnostic_for_enum(
514511
enum_id,
515-
db,
516512
enum_name_replacement,
517513
enum_fields_replacements,
518514
)
@@ -523,7 +519,6 @@ impl<'a, 'b> DeclValidator<'a, 'b> {
523519
fn create_incorrect_case_diagnostic_for_enum(
524520
&mut self,
525521
enum_id: EnumId,
526-
db: &dyn HirDatabase,
527522
enum_name_replacement: Option<Replacement>,
528523
enum_variants_replacements: Vec<Replacement>,
529524
) {
@@ -532,8 +527,8 @@ impl<'a, 'b> DeclValidator<'a, 'b> {
532527
return;
533528
}
534529

535-
let enum_loc = enum_id.lookup(db.upcast());
536-
let enum_src = enum_loc.source(db.upcast());
530+
let enum_loc = enum_id.lookup(self.db.upcast());
531+
let enum_src = enum_loc.source(self.db.upcast());
537532

538533
if let Some(replacement) = enum_name_replacement {
539534
let ast_ptr = match enum_src.value.name() {
@@ -608,10 +603,10 @@ impl<'a, 'b> DeclValidator<'a, 'b> {
608603
}
609604
}
610605

611-
fn validate_const(&mut self, db: &dyn HirDatabase, const_id: ConstId) {
612-
let data = db.const_data(const_id);
606+
fn validate_const(&mut self, const_id: ConstId) {
607+
let data = self.db.const_data(const_id);
613608

614-
if self.allowed(db, const_id.into(), allow::NON_UPPER_CASE_GLOBAL) {
609+
if self.allowed(const_id.into(), allow::NON_UPPER_CASE_GLOBAL) {
615610
return;
616611
}
617612

@@ -632,8 +627,8 @@ impl<'a, 'b> DeclValidator<'a, 'b> {
632627
return;
633628
};
634629

635-
let const_loc = const_id.lookup(db.upcast());
636-
let const_src = const_loc.source(db.upcast());
630+
let const_loc = const_id.lookup(self.db.upcast());
631+
let const_src = const_loc.source(self.db.upcast());
637632

638633
let ast_ptr = match const_src.value.name() {
639634
Some(name) => name,
@@ -652,14 +647,14 @@ impl<'a, 'b> DeclValidator<'a, 'b> {
652647
self.sink.push(diagnostic);
653648
}
654649

655-
fn validate_static(&mut self, db: &dyn HirDatabase, static_id: StaticId) {
656-
let data = db.static_data(static_id);
650+
fn validate_static(&mut self, static_id: StaticId) {
651+
let data = self.db.static_data(static_id);
657652
if data.is_extern {
658653
mark::hit!(extern_static_incorrect_case_ignored);
659654
return;
660655
}
661656

662-
if self.allowed(db, static_id.into(), allow::NON_UPPER_CASE_GLOBAL) {
657+
if self.allowed(static_id.into(), allow::NON_UPPER_CASE_GLOBAL) {
663658
return;
664659
}
665660

@@ -680,8 +675,8 @@ impl<'a, 'b> DeclValidator<'a, 'b> {
680675
return;
681676
};
682677

683-
let static_loc = static_id.lookup(db.upcast());
684-
let static_src = static_loc.source(db.upcast());
678+
let static_loc = static_id.lookup(self.db.upcast());
679+
let static_src = static_loc.source(self.db.upcast());
685680

686681
let ast_ptr = match static_src.value.name() {
687682
Some(name) => name,

0 commit comments

Comments
 (0)