Skip to content

Commit c8d19fe

Browse files
bors[bot]matklad
andauthored
Merge #9582
9582: internal: remove erroneous default impl r=matklad a=matklad bors r+ 🤖 Co-authored-by: Aleksey Kladov <[email protected]>
2 parents 745be39 + a9d0d14 commit c8d19fe

File tree

3 files changed

+35
-20
lines changed

3 files changed

+35
-20
lines changed

crates/hir_def/src/nameres.rs

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -145,12 +145,6 @@ pub enum ModuleOrigin {
145145
},
146146
}
147147

148-
impl Default for ModuleOrigin {
149-
fn default() -> Self {
150-
ModuleOrigin::CrateRoot { definition: FileId(0) }
151-
}
152-
}
153-
154148
impl ModuleOrigin {
155149
fn declaration(&self) -> Option<AstId<ast::Module>> {
156150
match self {
@@ -196,7 +190,7 @@ impl ModuleOrigin {
196190
}
197191
}
198192

199-
#[derive(Default, Debug, PartialEq, Eq)]
193+
#[derive(Debug, PartialEq, Eq)]
200194
pub struct ModuleData {
201195
pub parent: Option<LocalModuleId>,
202196
pub children: FxHashMap<Name, LocalModuleId>,
@@ -211,9 +205,14 @@ impl DefMap {
211205
let _p = profile::span("crate_def_map_query").detail(|| {
212206
db.crate_graph()[krate].display_name.as_deref().unwrap_or_default().to_string()
213207
});
214-
let edition = db.crate_graph()[krate].edition;
215-
let def_map = DefMap::empty(krate, edition);
208+
209+
let crate_graph = db.crate_graph();
210+
211+
let edition = crate_graph[krate].edition;
212+
let origin = ModuleOrigin::CrateRoot { definition: crate_graph[krate].root_file_id };
213+
let def_map = DefMap::empty(krate, edition, origin);
216214
let def_map = collector::collect_defs(db, def_map, None);
215+
217216
Arc::new(def_map)
218217
}
219218

@@ -231,16 +230,20 @@ impl DefMap {
231230
let block_info = BlockInfo { block: block_id, parent: block.module };
232231

233232
let parent_map = block.module.def_map(db);
234-
let mut def_map = DefMap::empty(block.module.krate, parent_map.edition);
233+
let mut def_map = DefMap::empty(
234+
block.module.krate,
235+
parent_map.edition,
236+
ModuleOrigin::BlockExpr { block: block.ast_id },
237+
);
235238
def_map.block = Some(block_info);
236239

237240
let def_map = collector::collect_defs(db, def_map, Some(block.ast_id));
238241
Some(Arc::new(def_map))
239242
}
240243

241-
fn empty(krate: CrateId, edition: Edition) -> DefMap {
244+
fn empty(krate: CrateId, edition: Edition, root_module_origin: ModuleOrigin) -> DefMap {
242245
let mut modules: Arena<ModuleData> = Arena::default();
243-
let root = modules.alloc(ModuleData::default());
246+
let root = modules.alloc(ModuleData::new(root_module_origin));
244247
DefMap {
245248
_c: Count::new(),
246249
block: None,
@@ -445,6 +448,15 @@ impl DefMap {
445448
}
446449

447450
impl ModuleData {
451+
pub(crate) fn new(origin: ModuleOrigin) -> Self {
452+
ModuleData {
453+
parent: None,
454+
children: FxHashMap::default(),
455+
scope: ItemScope::default(),
456+
origin,
457+
}
458+
}
459+
448460
/// Returns a node which defines this module. That is, a file or a `mod foo {}` with items.
449461
pub fn definition_source(&self, db: &dyn DefDatabase) -> InFile<ModuleSource> {
450462
self.origin.definition_source(db)

crates/hir_def/src/nameres/collector.rs

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -272,7 +272,6 @@ impl DefCollector<'_> {
272272
let file_id = self.db.crate_graph()[self.def_map.krate].root_file_id;
273273
let item_tree = self.db.file_item_tree(file_id.into());
274274
let module_id = self.def_map.root;
275-
self.def_map.modules[module_id].origin = ModuleOrigin::CrateRoot { definition: file_id };
276275

277276
let attrs = item_tree.top_level_attrs(self.db, self.def_map.krate);
278277
if attrs.cfg().map_or(true, |cfg| self.cfg_options.check(&cfg) != Some(false)) {
@@ -323,7 +322,6 @@ impl DefCollector<'_> {
323322
fn seed_with_inner(&mut self, block: AstId<ast::BlockExpr>) {
324323
let item_tree = self.db.file_item_tree(block.file_id);
325324
let module_id = self.def_map.root;
326-
self.def_map.modules[module_id].origin = ModuleOrigin::BlockExpr { block };
327325
if item_tree
328326
.top_level_attrs(self.db, self.def_map.krate)
329327
.cfg()
@@ -1625,14 +1623,14 @@ impl ModCollector<'_, '_> {
16251623
.resolve_visibility(self.def_collector.db, self.module_id, visibility)
16261624
.unwrap_or(Visibility::Public);
16271625
let modules = &mut self.def_collector.def_map.modules;
1628-
let res = modules.alloc(ModuleData::default());
1629-
modules[res].parent = Some(self.module_id);
1630-
modules[res].origin = match definition {
1626+
let origin = match definition {
16311627
None => ModuleOrigin::Inline { definition: declaration },
16321628
Some((definition, is_mod_rs)) => {
16331629
ModuleOrigin::File { declaration, definition, is_mod_rs }
16341630
}
16351631
};
1632+
let res = modules.alloc(ModuleData::new(origin));
1633+
modules[res].parent = Some(self.module_id);
16361634
for (name, mac) in modules[self.module_id].scope.collect_legacy_macros() {
16371635
modules[res].scope.define_legacy_macro(name, mac)
16381636
}
@@ -2005,11 +2003,12 @@ mod tests {
20052003
}
20062004

20072005
fn do_resolve(not_ra_fixture: &str) -> DefMap {
2008-
let (db, _file_id) = TestDB::with_single_file(not_ra_fixture);
2006+
let (db, file_id) = TestDB::with_single_file(not_ra_fixture);
20092007
let krate = db.test_crate();
20102008

20112009
let edition = db.crate_graph()[krate].edition;
2012-
let def_map = DefMap::empty(krate, edition);
2010+
let module_origin = ModuleOrigin::CrateRoot { definition: file_id };
2011+
let def_map = DefMap::empty(krate, edition, module_origin);
20132012
do_collect_defs(&db, def_map)
20142013
}
20152014

docs/dev/style.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -312,7 +312,7 @@ Even when generality is not required, consistency is important.
312312

313313
## Constructors
314314

315-
Prefer `Default` to zero-argument `new` function
315+
Prefer `Default` to zero-argument `new` function.
316316

317317
```rust
318318
// GOOD
@@ -341,6 +341,10 @@ Use `Vec::new` rather than `vec![]`.
341341

342342
**Rationale:** uniformity, strength reduction.
343343

344+
Avoid using "dummy" states to implement a `Default`.
345+
If a type doesn't have a sensible default, empty value, don't hide it.
346+
Let the caller explicitly decide what's the right initial state is.
347+
344348
## Functions Over Objects
345349

346350
Avoid creating "doer" objects.

0 commit comments

Comments
 (0)