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

Commit 56dd536

Browse files
committed
Refactor
- don't take `&self` as receiver for `Copy` types - simplify `hir::Module::nearest_non_block_module()` - style changes for consistency
1 parent a028463 commit 56dd536

File tree

5 files changed

+22
-26
lines changed

5 files changed

+22
-26
lines changed

crates/hir-def/src/body/tests.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@ mod block;
33
use base_db::{fixture::WithFixture, SourceDatabase};
44
use expect_test::Expect;
55

6-
use crate::ModuleDefId;
6+
use crate::{test_db::TestDB, ModuleDefId};
77

88
use super::*;
99

1010
fn lower(ra_fixture: &str) -> Arc<Body> {
11-
let db = crate::test_db::TestDB::with_files(ra_fixture);
11+
let db = TestDB::with_files(ra_fixture);
1212

1313
let krate = db.crate_graph().iter().next().unwrap();
1414
let def_map = db.crate_def_map(krate);
@@ -25,23 +25,23 @@ fn lower(ra_fixture: &str) -> Arc<Body> {
2525
db.body(fn_def.unwrap().into())
2626
}
2727

28-
fn block_def_map_at(ra_fixture: &str) -> String {
29-
let (db, position) = crate::test_db::TestDB::with_position(ra_fixture);
28+
fn def_map_at(ra_fixture: &str) -> String {
29+
let (db, position) = TestDB::with_position(ra_fixture);
3030

3131
let module = db.module_at_position(position);
3232
module.def_map(&db).dump(&db)
3333
}
3434

3535
fn check_block_scopes_at(ra_fixture: &str, expect: Expect) {
36-
let (db, position) = crate::test_db::TestDB::with_position(ra_fixture);
36+
let (db, position) = TestDB::with_position(ra_fixture);
3737

3838
let module = db.module_at_position(position);
3939
let actual = module.def_map(&db).dump_block_scopes(&db);
4040
expect.assert_eq(&actual);
4141
}
4242

4343
fn check_at(ra_fixture: &str, expect: Expect) {
44-
let actual = block_def_map_at(ra_fixture);
44+
let actual = def_map_at(ra_fixture);
4545
expect.assert_eq(&actual);
4646
}
4747

crates/hir-def/src/lib.rs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -145,24 +145,28 @@ pub struct ModuleId {
145145
}
146146

147147
impl ModuleId {
148-
pub fn def_map(&self, db: &dyn db::DefDatabase) -> Arc<DefMap> {
148+
pub fn def_map(self, db: &dyn db::DefDatabase) -> Arc<DefMap> {
149149
match self.block {
150150
Some(block) => db.block_def_map(block),
151151
None => db.crate_def_map(self.krate),
152152
}
153153
}
154154

155-
pub fn krate(&self) -> CrateId {
155+
pub fn krate(self) -> CrateId {
156156
self.krate
157157
}
158158

159-
pub fn containing_module(&self, db: &dyn db::DefDatabase) -> Option<ModuleId> {
159+
pub fn containing_module(self, db: &dyn db::DefDatabase) -> Option<ModuleId> {
160160
self.def_map(db).containing_module(self.local_id)
161161
}
162162

163-
pub fn containing_block(&self) -> Option<BlockId> {
163+
pub fn containing_block(self) -> Option<BlockId> {
164164
self.block
165165
}
166+
167+
pub fn is_block_module(self) -> bool {
168+
self.block.is_some() && self.local_id == DefMap::ROOT
169+
}
166170
}
167171

168172
/// An ID of a module, **local** to a `DefMap`.

crates/hir-def/src/nameres/collector.rs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -808,11 +808,8 @@ impl DefCollector<'_> {
808808
}
809809
}
810810

811-
// Check whether all namespace is resolved
812-
if def.take_types().is_some()
813-
&& def.take_values().is_some()
814-
&& def.take_macros().is_some()
815-
{
811+
// Check whether all namespaces are resolved.
812+
if def.is_full() {
816813
PartialResolvedImport::Resolved(def)
817814
} else {
818815
PartialResolvedImport::Indeterminate(def)
@@ -821,7 +818,7 @@ impl DefCollector<'_> {
821818
}
822819

823820
fn resolve_extern_crate(&self, name: &Name) -> Option<CrateRootModuleId> {
824-
if *name == name!(self) {
821+
if *name == name![self] {
825822
cov_mark::hit!(extern_crate_self_as);
826823
Some(self.def_map.crate_root())
827824
} else {

crates/hir-def/src/nameres/path_resolution.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ impl DefMap {
192192
));
193193

194194
let mut segments = path.segments().iter().enumerate();
195-
let mut curr_per_ns: PerNs = match path.kind {
195+
let mut curr_per_ns = match path.kind {
196196
PathKind::DollarCrate(krate) => {
197197
if krate == self.krate {
198198
cov_mark::hit!(macro_dollar_crate_self);

crates/hir/src/lib.rs

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ use hir_def::{
4747
lang_item::LangItemTarget,
4848
layout::{self, ReprOptions, TargetDataLayout},
4949
macro_id_to_def_id,
50-
nameres::{self, diagnostics::DefDiagnostic, ModuleOrigin},
50+
nameres::{self, diagnostics::DefDiagnostic},
5151
per_ns::PerNs,
5252
resolver::{HasResolver, Resolver},
5353
src::HasSource as _,
@@ -505,15 +505,10 @@ impl Module {
505505
/// Finds nearest non-block ancestor `Module` (`self` included).
506506
pub fn nearest_non_block_module(self, db: &dyn HirDatabase) -> Module {
507507
let mut id = self.id;
508-
loop {
509-
let def_map = id.def_map(db.upcast());
510-
let origin = def_map[id.local_id].origin;
511-
if matches!(origin, ModuleOrigin::BlockExpr { .. }) {
512-
id = id.containing_module(db.upcast()).expect("block without parent module")
513-
} else {
514-
return Module { id };
515-
}
508+
while id.is_block_module() {
509+
id = id.containing_module(db.upcast()).expect("block without parent module");
516510
}
511+
Module { id }
517512
}
518513

519514
pub fn path_to_root(self, db: &dyn HirDatabase) -> Vec<Module> {

0 commit comments

Comments
 (0)