Refactor
- don't take `&self` as receiver for `Copy` types - simplify `hir::Module::nearest_non_block_module()` - style changes for consistency
This commit is contained in:
parent
a02846343f
commit
56dd5368f5
@ -3,12 +3,12 @@ mod block;
|
||||
use base_db::{fixture::WithFixture, SourceDatabase};
|
||||
use expect_test::Expect;
|
||||
|
||||
use crate::ModuleDefId;
|
||||
use crate::{test_db::TestDB, ModuleDefId};
|
||||
|
||||
use super::*;
|
||||
|
||||
fn lower(ra_fixture: &str) -> Arc<Body> {
|
||||
let db = crate::test_db::TestDB::with_files(ra_fixture);
|
||||
let db = TestDB::with_files(ra_fixture);
|
||||
|
||||
let krate = db.crate_graph().iter().next().unwrap();
|
||||
let def_map = db.crate_def_map(krate);
|
||||
@ -25,15 +25,15 @@ fn lower(ra_fixture: &str) -> Arc<Body> {
|
||||
db.body(fn_def.unwrap().into())
|
||||
}
|
||||
|
||||
fn block_def_map_at(ra_fixture: &str) -> String {
|
||||
let (db, position) = crate::test_db::TestDB::with_position(ra_fixture);
|
||||
fn def_map_at(ra_fixture: &str) -> String {
|
||||
let (db, position) = TestDB::with_position(ra_fixture);
|
||||
|
||||
let module = db.module_at_position(position);
|
||||
module.def_map(&db).dump(&db)
|
||||
}
|
||||
|
||||
fn check_block_scopes_at(ra_fixture: &str, expect: Expect) {
|
||||
let (db, position) = crate::test_db::TestDB::with_position(ra_fixture);
|
||||
let (db, position) = TestDB::with_position(ra_fixture);
|
||||
|
||||
let module = db.module_at_position(position);
|
||||
let actual = module.def_map(&db).dump_block_scopes(&db);
|
||||
@ -41,7 +41,7 @@ fn check_block_scopes_at(ra_fixture: &str, expect: Expect) {
|
||||
}
|
||||
|
||||
fn check_at(ra_fixture: &str, expect: Expect) {
|
||||
let actual = block_def_map_at(ra_fixture);
|
||||
let actual = def_map_at(ra_fixture);
|
||||
expect.assert_eq(&actual);
|
||||
}
|
||||
|
||||
|
@ -145,24 +145,28 @@ pub struct ModuleId {
|
||||
}
|
||||
|
||||
impl ModuleId {
|
||||
pub fn def_map(&self, db: &dyn db::DefDatabase) -> Arc<DefMap> {
|
||||
pub fn def_map(self, db: &dyn db::DefDatabase) -> Arc<DefMap> {
|
||||
match self.block {
|
||||
Some(block) => db.block_def_map(block),
|
||||
None => db.crate_def_map(self.krate),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn krate(&self) -> CrateId {
|
||||
pub fn krate(self) -> CrateId {
|
||||
self.krate
|
||||
}
|
||||
|
||||
pub fn containing_module(&self, db: &dyn db::DefDatabase) -> Option<ModuleId> {
|
||||
pub fn containing_module(self, db: &dyn db::DefDatabase) -> Option<ModuleId> {
|
||||
self.def_map(db).containing_module(self.local_id)
|
||||
}
|
||||
|
||||
pub fn containing_block(&self) -> Option<BlockId> {
|
||||
pub fn containing_block(self) -> Option<BlockId> {
|
||||
self.block
|
||||
}
|
||||
|
||||
pub fn is_block_module(self) -> bool {
|
||||
self.block.is_some() && self.local_id == DefMap::ROOT
|
||||
}
|
||||
}
|
||||
|
||||
/// An ID of a module, **local** to a `DefMap`.
|
||||
|
@ -808,11 +808,8 @@ impl DefCollector<'_> {
|
||||
}
|
||||
}
|
||||
|
||||
// Check whether all namespace is resolved
|
||||
if def.take_types().is_some()
|
||||
&& def.take_values().is_some()
|
||||
&& def.take_macros().is_some()
|
||||
{
|
||||
// Check whether all namespaces are resolved.
|
||||
if def.is_full() {
|
||||
PartialResolvedImport::Resolved(def)
|
||||
} else {
|
||||
PartialResolvedImport::Indeterminate(def)
|
||||
@ -821,7 +818,7 @@ impl DefCollector<'_> {
|
||||
}
|
||||
|
||||
fn resolve_extern_crate(&self, name: &Name) -> Option<CrateRootModuleId> {
|
||||
if *name == name!(self) {
|
||||
if *name == name![self] {
|
||||
cov_mark::hit!(extern_crate_self_as);
|
||||
Some(self.def_map.crate_root())
|
||||
} else {
|
||||
|
@ -192,7 +192,7 @@ impl DefMap {
|
||||
));
|
||||
|
||||
let mut segments = path.segments().iter().enumerate();
|
||||
let mut curr_per_ns: PerNs = match path.kind {
|
||||
let mut curr_per_ns = match path.kind {
|
||||
PathKind::DollarCrate(krate) => {
|
||||
if krate == self.krate {
|
||||
cov_mark::hit!(macro_dollar_crate_self);
|
||||
|
@ -47,7 +47,7 @@ use hir_def::{
|
||||
lang_item::LangItemTarget,
|
||||
layout::{self, ReprOptions, TargetDataLayout},
|
||||
macro_id_to_def_id,
|
||||
nameres::{self, diagnostics::DefDiagnostic, ModuleOrigin},
|
||||
nameres::{self, diagnostics::DefDiagnostic},
|
||||
per_ns::PerNs,
|
||||
resolver::{HasResolver, Resolver},
|
||||
src::HasSource as _,
|
||||
@ -505,15 +505,10 @@ impl Module {
|
||||
/// Finds nearest non-block ancestor `Module` (`self` included).
|
||||
pub fn nearest_non_block_module(self, db: &dyn HirDatabase) -> Module {
|
||||
let mut id = self.id;
|
||||
loop {
|
||||
let def_map = id.def_map(db.upcast());
|
||||
let origin = def_map[id.local_id].origin;
|
||||
if matches!(origin, ModuleOrigin::BlockExpr { .. }) {
|
||||
id = id.containing_module(db.upcast()).expect("block without parent module")
|
||||
} else {
|
||||
return Module { id };
|
||||
}
|
||||
while id.is_block_module() {
|
||||
id = id.containing_module(db.upcast()).expect("block without parent module");
|
||||
}
|
||||
Module { id }
|
||||
}
|
||||
|
||||
pub fn path_to_root(self, db: &dyn HirDatabase) -> Vec<Module> {
|
||||
|
Loading…
x
Reference in New Issue
Block a user