2019-11-08 21:17:17 +00:00
|
|
|
//! This modules implements a function to resolve a path `foo::bar::baz` to a
|
|
|
|
//! def, which is used within the name resolution.
|
|
|
|
//!
|
|
|
|
//! When name resolution is finished, the result of resolving a path is either
|
|
|
|
//! `Some(def)` or `None`. However, when we are in process of resolving imports
|
|
|
|
//! or macros, there's a third possibility:
|
|
|
|
//!
|
|
|
|
//! I can't resolve this path right now, but I might be resolve this path
|
|
|
|
//! later, when more macros are expanded.
|
|
|
|
//!
|
|
|
|
//! `ReachedFixedPoint` signals about this.
|
|
|
|
|
2020-08-13 14:25:38 +00:00
|
|
|
use base_db::Edition;
|
2019-11-08 21:17:17 +00:00
|
|
|
use hir_expand::name::Name;
|
|
|
|
|
|
|
|
use crate::{
|
2019-11-23 11:44:43 +00:00
|
|
|
db::DefDatabase,
|
2020-02-21 14:34:06 +00:00
|
|
|
item_scope::BUILTIN_SCOPE,
|
2021-01-18 19:18:05 +00:00
|
|
|
nameres::{BuiltinShadowMode, DefMap},
|
2019-12-13 11:12:36 +00:00
|
|
|
path::{ModPath, PathKind},
|
2019-11-23 13:53:16 +00:00
|
|
|
per_ns::PerNs,
|
2019-12-26 15:00:10 +00:00
|
|
|
visibility::{RawVisibility, Visibility},
|
2022-03-22 14:54:46 +00:00
|
|
|
AdtId, CrateId, EnumVariantId, LocalModuleId, ModuleDefId, ModuleId,
|
2019-11-08 21:17:17 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
|
|
|
pub(super) enum ResolveMode {
|
|
|
|
Import,
|
|
|
|
Other,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
|
|
|
pub(super) enum ReachedFixedPoint {
|
|
|
|
Yes,
|
|
|
|
No,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, Clone)]
|
|
|
|
pub(super) struct ResolvePathResult {
|
|
|
|
pub(super) resolved_def: PerNs,
|
|
|
|
pub(super) segment_index: Option<usize>,
|
|
|
|
pub(super) reached_fixedpoint: ReachedFixedPoint,
|
2019-12-07 11:20:41 +00:00
|
|
|
pub(super) krate: Option<CrateId>,
|
2019-11-08 21:17:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl ResolvePathResult {
|
|
|
|
fn empty(reached_fixedpoint: ReachedFixedPoint) -> ResolvePathResult {
|
2019-12-07 11:20:41 +00:00
|
|
|
ResolvePathResult::with(PerNs::none(), reached_fixedpoint, None, None)
|
2019-11-08 21:17:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn with(
|
|
|
|
resolved_def: PerNs,
|
|
|
|
reached_fixedpoint: ReachedFixedPoint,
|
|
|
|
segment_index: Option<usize>,
|
2019-12-07 11:20:41 +00:00
|
|
|
krate: Option<CrateId>,
|
2019-11-08 21:17:17 +00:00
|
|
|
) -> ResolvePathResult {
|
2021-06-07 11:59:01 +00:00
|
|
|
ResolvePathResult { resolved_def, segment_index, reached_fixedpoint, krate }
|
2019-11-08 21:17:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-18 19:18:05 +00:00
|
|
|
impl DefMap {
|
2021-04-09 13:25:12 +00:00
|
|
|
pub(super) fn resolve_name_in_extern_prelude(
|
|
|
|
&self,
|
|
|
|
db: &dyn DefDatabase,
|
|
|
|
name: &Name,
|
2022-03-22 14:54:46 +00:00
|
|
|
) -> Option<ModuleId> {
|
|
|
|
match self.block {
|
|
|
|
Some(_) => self.crate_root(db).def_map(db).extern_prelude.get(name).copied(),
|
|
|
|
None => self.extern_prelude.get(name).copied(),
|
|
|
|
}
|
2019-11-08 21:17:17 +00:00
|
|
|
}
|
|
|
|
|
2019-12-24 22:45:14 +00:00
|
|
|
pub(crate) fn resolve_visibility(
|
|
|
|
&self,
|
2020-03-13 15:05:46 +00:00
|
|
|
db: &dyn DefDatabase,
|
2019-12-24 22:45:14 +00:00
|
|
|
original_module: LocalModuleId,
|
2019-12-26 14:57:14 +00:00
|
|
|
visibility: &RawVisibility,
|
2019-12-26 15:00:10 +00:00
|
|
|
) -> Option<Visibility> {
|
2021-02-28 03:39:38 +00:00
|
|
|
let mut vis = match visibility {
|
2019-12-26 14:57:14 +00:00
|
|
|
RawVisibility::Module(path) => {
|
2019-12-24 22:45:14 +00:00
|
|
|
let (result, remaining) =
|
2021-06-13 03:54:16 +00:00
|
|
|
self.resolve_path(db, original_module, path, BuiltinShadowMode::Module);
|
2019-12-24 22:45:14 +00:00
|
|
|
if remaining.is_some() {
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
let types = result.take_types()?;
|
|
|
|
match types {
|
2021-02-28 03:39:38 +00:00
|
|
|
ModuleDefId::ModuleId(m) => Visibility::Module(m),
|
2019-12-24 22:45:14 +00:00
|
|
|
_ => {
|
|
|
|
// error: visibility needs to refer to module
|
2021-02-28 03:39:38 +00:00
|
|
|
return None;
|
2019-12-24 22:45:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-02-28 03:39:38 +00:00
|
|
|
RawVisibility::Public => Visibility::Public,
|
|
|
|
};
|
|
|
|
|
|
|
|
// In block expressions, `self` normally refers to the containing non-block module, and
|
|
|
|
// `super` to its parent (etc.). However, visibilities must only refer to a module in the
|
|
|
|
// DefMap they're written in, so we restrict them when that happens.
|
|
|
|
if let Visibility::Module(m) = vis {
|
|
|
|
if self.block_id() != m.block {
|
2021-03-08 20:19:44 +00:00
|
|
|
cov_mark::hit!(adjust_vis_in_block_def_map);
|
2021-02-28 03:39:38 +00:00
|
|
|
vis = Visibility::Module(self.module_id(self.root()));
|
2021-08-15 12:46:13 +00:00
|
|
|
tracing::debug!("visibility {:?} points outside DefMap, adjusting to {:?}", m, vis);
|
2021-02-28 03:39:38 +00:00
|
|
|
}
|
2019-12-24 22:45:14 +00:00
|
|
|
}
|
2021-02-28 03:39:38 +00:00
|
|
|
|
|
|
|
Some(vis)
|
2019-12-24 22:45:14 +00:00
|
|
|
}
|
|
|
|
|
2019-11-08 21:17:17 +00:00
|
|
|
// Returns Yes if we are sure that additions to `ItemMap` wouldn't change
|
|
|
|
// the result.
|
|
|
|
pub(super) fn resolve_path_fp_with_macro(
|
|
|
|
&self,
|
2020-03-13 15:05:46 +00:00
|
|
|
db: &dyn DefDatabase,
|
2021-01-21 14:24:15 +00:00
|
|
|
mode: ResolveMode,
|
2021-01-28 17:27:27 +00:00
|
|
|
mut original_module: LocalModuleId,
|
2021-01-21 14:24:15 +00:00
|
|
|
path: &ModPath,
|
|
|
|
shadow: BuiltinShadowMode,
|
|
|
|
) -> ResolvePathResult {
|
|
|
|
let mut result = ResolvePathResult::empty(ReachedFixedPoint::No);
|
|
|
|
|
2021-02-04 12:44:54 +00:00
|
|
|
let mut arc;
|
2021-01-21 14:24:15 +00:00
|
|
|
let mut current_map = self;
|
|
|
|
loop {
|
|
|
|
let new = current_map.resolve_path_fp_with_macro_single(
|
|
|
|
db,
|
|
|
|
mode,
|
|
|
|
original_module,
|
|
|
|
path,
|
|
|
|
shadow,
|
|
|
|
);
|
|
|
|
|
|
|
|
// Merge `new` into `result`.
|
|
|
|
result.resolved_def = result.resolved_def.or(new.resolved_def);
|
|
|
|
if result.reached_fixedpoint == ReachedFixedPoint::No {
|
|
|
|
result.reached_fixedpoint = new.reached_fixedpoint;
|
|
|
|
}
|
|
|
|
// FIXME: this doesn't seem right; what if the different namespace resolutions come from different crates?
|
|
|
|
result.krate = result.krate.or(new.krate);
|
2021-02-05 14:14:33 +00:00
|
|
|
result.segment_index = match (result.segment_index, new.segment_index) {
|
|
|
|
(Some(idx), None) => Some(idx),
|
|
|
|
(Some(old), Some(new)) => Some(old.max(new)),
|
|
|
|
(None, new) => new,
|
|
|
|
};
|
2021-01-21 14:24:15 +00:00
|
|
|
|
2021-01-25 18:02:05 +00:00
|
|
|
match ¤t_map.block {
|
2021-01-28 17:27:27 +00:00
|
|
|
Some(block) => {
|
2021-02-04 12:44:54 +00:00
|
|
|
original_module = block.parent.local_id;
|
|
|
|
arc = block.parent.def_map(db);
|
|
|
|
current_map = &*arc;
|
2021-01-28 17:27:27 +00:00
|
|
|
}
|
2021-01-21 14:24:15 +00:00
|
|
|
None => return result,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-22 17:47:19 +00:00
|
|
|
pub(super) fn resolve_path_fp_with_macro_single(
|
2021-01-21 14:24:15 +00:00
|
|
|
&self,
|
|
|
|
db: &dyn DefDatabase,
|
2019-11-08 21:17:17 +00:00
|
|
|
mode: ResolveMode,
|
2019-11-23 13:49:53 +00:00
|
|
|
original_module: LocalModuleId,
|
2019-12-13 11:12:36 +00:00
|
|
|
path: &ModPath,
|
2019-11-30 15:29:21 +00:00
|
|
|
shadow: BuiltinShadowMode,
|
2019-11-08 21:17:17 +00:00
|
|
|
) -> ResolvePathResult {
|
2021-09-05 16:48:34 +00:00
|
|
|
let graph = db.crate_graph();
|
2021-08-31 11:42:46 +00:00
|
|
|
let _cx = stdx::panic_context::enter(format!(
|
2021-09-05 16:48:34 +00:00
|
|
|
"DefMap {:?} crate_name={:?} block={:?} path={}",
|
|
|
|
self.krate, graph[self.krate].display_name, self.block, path
|
2021-08-31 11:42:46 +00:00
|
|
|
));
|
|
|
|
|
2021-02-04 19:49:24 +00:00
|
|
|
let mut segments = path.segments().iter().enumerate();
|
2019-11-08 21:17:17 +00:00
|
|
|
let mut curr_per_ns: PerNs = match path.kind {
|
|
|
|
PathKind::DollarCrate(krate) => {
|
|
|
|
if krate == self.krate {
|
2021-03-08 20:19:44 +00:00
|
|
|
cov_mark::hit!(macro_dollar_crate_self);
|
2021-02-04 12:44:54 +00:00
|
|
|
PerNs::types(self.crate_root(db).into(), Visibility::Public)
|
2019-11-08 21:17:17 +00:00
|
|
|
} else {
|
|
|
|
let def_map = db.crate_def_map(krate);
|
2021-01-25 14:21:33 +00:00
|
|
|
let module = def_map.module_id(def_map.root);
|
2021-03-08 20:19:44 +00:00
|
|
|
cov_mark::hit!(macro_dollar_crate_other);
|
2019-12-26 15:00:10 +00:00
|
|
|
PerNs::types(module.into(), Visibility::Public)
|
2019-11-08 21:17:17 +00:00
|
|
|
}
|
|
|
|
}
|
2021-02-04 12:44:54 +00:00
|
|
|
PathKind::Crate => PerNs::types(self.crate_root(db).into(), Visibility::Public),
|
2019-11-08 21:17:17 +00:00
|
|
|
// plain import or absolute path in 2015: crate-relative with
|
|
|
|
// fallback to extern prelude (with the simplification in
|
|
|
|
// rust-lang/rust#57745)
|
|
|
|
// FIXME there must be a nicer way to write this condition
|
|
|
|
PathKind::Plain | PathKind::Abs
|
|
|
|
if self.edition == Edition::Edition2015
|
|
|
|
&& (path.kind == PathKind::Abs || mode == ResolveMode::Import) =>
|
|
|
|
{
|
2020-02-21 14:34:06 +00:00
|
|
|
let (_, segment) = match segments.next() {
|
2019-12-01 04:14:35 +00:00
|
|
|
Some((idx, segment)) => (idx, segment),
|
2019-11-08 21:17:17 +00:00
|
|
|
None => return ResolvePathResult::empty(ReachedFixedPoint::Yes),
|
|
|
|
};
|
2021-08-15 12:46:13 +00:00
|
|
|
tracing::debug!("resolving {:?} in crate root (+ extern prelude)", segment);
|
2021-06-13 03:54:16 +00:00
|
|
|
self.resolve_name_in_crate_root_or_extern_prelude(db, segment)
|
2019-11-08 21:17:17 +00:00
|
|
|
}
|
|
|
|
PathKind::Plain => {
|
2020-02-21 14:34:06 +00:00
|
|
|
let (_, segment) = match segments.next() {
|
2019-12-01 04:14:35 +00:00
|
|
|
Some((idx, segment)) => (idx, segment),
|
2019-11-08 21:17:17 +00:00
|
|
|
None => return ResolvePathResult::empty(ReachedFixedPoint::Yes),
|
|
|
|
};
|
2020-02-21 14:34:06 +00:00
|
|
|
// The first segment may be a builtin type. If the path has more
|
|
|
|
// than one segment, we first try resolving it as a module
|
|
|
|
// anyway.
|
|
|
|
// FIXME: If the next segment doesn't resolve in the module and
|
|
|
|
// BuiltinShadowMode wasn't Module, then we need to try
|
|
|
|
// resolving it as a builtin.
|
|
|
|
let prefer_module =
|
2021-02-04 19:49:24 +00:00
|
|
|
if path.segments().len() == 1 { shadow } else { BuiltinShadowMode::Module };
|
2020-02-21 14:34:06 +00:00
|
|
|
|
2021-08-15 12:46:13 +00:00
|
|
|
tracing::debug!("resolving {:?} in module", segment);
|
2021-06-13 03:54:16 +00:00
|
|
|
self.resolve_name_in_module(db, original_module, segment, prefer_module)
|
2019-11-08 21:17:17 +00:00
|
|
|
}
|
2019-12-17 14:38:28 +00:00
|
|
|
PathKind::Super(lvl) => {
|
2021-01-25 18:15:39 +00:00
|
|
|
let mut module = original_module;
|
|
|
|
for i in 0..lvl {
|
|
|
|
match self.modules[module].parent {
|
|
|
|
Some(it) => module = it,
|
|
|
|
None => match &self.block {
|
|
|
|
Some(block) => {
|
|
|
|
// Look up remaining path in parent `DefMap`
|
2021-02-04 19:49:24 +00:00
|
|
|
let new_path = ModPath::from_segments(
|
|
|
|
PathKind::Super(lvl - i),
|
|
|
|
path.segments().to_vec(),
|
|
|
|
);
|
2021-08-15 12:46:13 +00:00
|
|
|
tracing::debug!(
|
|
|
|
"`super` path: {} -> {} in parent map",
|
|
|
|
path,
|
|
|
|
new_path
|
|
|
|
);
|
2021-02-04 12:44:54 +00:00
|
|
|
return block.parent.def_map(db).resolve_path_fp_with_macro(
|
2021-01-25 18:15:39 +00:00
|
|
|
db,
|
|
|
|
mode,
|
2021-02-04 12:44:54 +00:00
|
|
|
block.parent.local_id,
|
2021-01-25 18:15:39 +00:00
|
|
|
&new_path,
|
|
|
|
shadow,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
None => {
|
2021-08-15 12:46:13 +00:00
|
|
|
tracing::debug!("super path in root module");
|
2021-01-25 18:15:39 +00:00
|
|
|
return ResolvePathResult::empty(ReachedFixedPoint::Yes);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
}
|
2019-11-08 21:17:17 +00:00
|
|
|
}
|
2021-01-25 18:15:39 +00:00
|
|
|
|
2021-02-05 18:24:03 +00:00
|
|
|
// Resolve `self` to the containing crate-rooted module if we're a block
|
|
|
|
self.with_ancestor_maps(db, module, &mut |def_map, module| {
|
|
|
|
if def_map.block.is_some() {
|
|
|
|
None // keep ascending
|
|
|
|
} else {
|
|
|
|
Some(PerNs::types(def_map.module_id(module).into(), Visibility::Public))
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.expect("block DefMap not rooted in crate DefMap")
|
2019-11-08 21:17:17 +00:00
|
|
|
}
|
|
|
|
PathKind::Abs => {
|
|
|
|
// 2018-style absolute path -- only extern prelude
|
|
|
|
let segment = match segments.next() {
|
|
|
|
Some((_, segment)) => segment,
|
|
|
|
None => return ResolvePathResult::empty(ReachedFixedPoint::Yes),
|
|
|
|
};
|
2022-03-22 14:54:46 +00:00
|
|
|
if let Some(&def) = self.extern_prelude.get(segment) {
|
2021-08-15 12:46:13 +00:00
|
|
|
tracing::debug!("absolute path {:?} resolved to crate {:?}", path, def);
|
2022-03-22 14:54:46 +00:00
|
|
|
PerNs::types(def.into(), Visibility::Public)
|
2019-11-08 21:17:17 +00:00
|
|
|
} else {
|
|
|
|
return ResolvePathResult::empty(ReachedFixedPoint::No); // extern crate declarations can add to the extern prelude
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2019-12-01 04:14:35 +00:00
|
|
|
for (i, segment) in segments {
|
2019-12-25 14:00:10 +00:00
|
|
|
let (curr, vis) = match curr_per_ns.take_types_vis() {
|
2019-11-08 21:17:17 +00:00
|
|
|
Some(r) => r,
|
|
|
|
None => {
|
|
|
|
// we still have path segments left, but the path so far
|
|
|
|
// didn't resolve in the types namespace => no resolution
|
|
|
|
// (don't break here because `curr_per_ns` might contain
|
|
|
|
// something in the value namespace, and it would be wrong
|
|
|
|
// to return that)
|
|
|
|
return ResolvePathResult::empty(ReachedFixedPoint::No);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
// resolve segment in curr
|
|
|
|
|
|
|
|
curr_per_ns = match curr {
|
|
|
|
ModuleDefId::ModuleId(module) => {
|
|
|
|
if module.krate != self.krate {
|
2021-02-04 19:49:24 +00:00
|
|
|
let path = ModPath::from_segments(
|
|
|
|
PathKind::Super(0),
|
|
|
|
path.segments()[i..].iter().cloned(),
|
|
|
|
);
|
2021-08-15 12:46:13 +00:00
|
|
|
tracing::debug!("resolving {:?} in other crate", path);
|
2021-01-22 15:31:40 +00:00
|
|
|
let defp_map = module.def_map(db);
|
2019-11-30 15:29:21 +00:00
|
|
|
let (def, s) = defp_map.resolve_path(db, module.local_id, &path, shadow);
|
2019-11-08 21:17:17 +00:00
|
|
|
return ResolvePathResult::with(
|
|
|
|
def,
|
|
|
|
ReachedFixedPoint::Yes,
|
|
|
|
s.map(|s| s + i),
|
2019-12-07 11:20:41 +00:00
|
|
|
Some(module.krate),
|
2019-11-08 21:17:17 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-02-02 11:25:13 +00:00
|
|
|
let def_map;
|
|
|
|
let module_data = if module.block == self.block_id() {
|
|
|
|
&self[module.local_id]
|
|
|
|
} else {
|
|
|
|
def_map = module.def_map(db);
|
|
|
|
&def_map[module.local_id]
|
|
|
|
};
|
|
|
|
|
2019-11-08 21:17:17 +00:00
|
|
|
// Since it is a qualified path here, it should not contains legacy macros
|
2021-06-13 03:54:16 +00:00
|
|
|
module_data.scope.get(segment)
|
2019-11-08 21:17:17 +00:00
|
|
|
}
|
|
|
|
ModuleDefId::AdtId(AdtId::EnumId(e)) => {
|
|
|
|
// enum variant
|
2021-03-08 20:19:44 +00:00
|
|
|
cov_mark::hit!(can_import_enum_variant);
|
2019-11-08 21:17:17 +00:00
|
|
|
let enum_data = db.enum_data(e);
|
2021-06-13 03:54:16 +00:00
|
|
|
match enum_data.variant(segment) {
|
2019-11-08 21:17:17 +00:00
|
|
|
Some(local_id) => {
|
|
|
|
let variant = EnumVariantId { parent: e, local_id };
|
2020-07-16 11:00:56 +00:00
|
|
|
match &*enum_data.variants[local_id].variant_data {
|
|
|
|
crate::adt::VariantData::Record(_) => {
|
|
|
|
PerNs::types(variant.into(), Visibility::Public)
|
|
|
|
}
|
|
|
|
crate::adt::VariantData::Tuple(_)
|
|
|
|
| crate::adt::VariantData::Unit => {
|
|
|
|
PerNs::both(variant.into(), variant.into(), Visibility::Public)
|
|
|
|
}
|
|
|
|
}
|
2019-11-08 21:17:17 +00:00
|
|
|
}
|
|
|
|
None => {
|
|
|
|
return ResolvePathResult::with(
|
2019-12-25 14:00:10 +00:00
|
|
|
PerNs::types(e.into(), vis),
|
2019-11-08 21:17:17 +00:00
|
|
|
ReachedFixedPoint::Yes,
|
|
|
|
Some(i),
|
2019-12-07 11:20:41 +00:00
|
|
|
Some(self.krate),
|
2019-11-08 21:17:17 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
s => {
|
|
|
|
// could be an inherent method call in UFCS form
|
|
|
|
// (`Struct::method`), or some other kind of associated item
|
2021-08-15 12:46:13 +00:00
|
|
|
tracing::debug!(
|
2019-11-08 21:17:17 +00:00
|
|
|
"path segment {:?} resolved to non-module {:?}, but is not last",
|
2019-12-13 11:12:36 +00:00
|
|
|
segment,
|
2019-11-08 21:17:17 +00:00
|
|
|
curr,
|
|
|
|
);
|
|
|
|
|
|
|
|
return ResolvePathResult::with(
|
2019-12-25 14:00:10 +00:00
|
|
|
PerNs::types(s, vis),
|
2019-11-08 21:17:17 +00:00
|
|
|
ReachedFixedPoint::Yes,
|
|
|
|
Some(i),
|
2019-12-07 11:20:41 +00:00
|
|
|
Some(self.krate),
|
2019-11-08 21:17:17 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
2019-11-30 15:29:21 +00:00
|
|
|
|
2019-12-07 11:20:41 +00:00
|
|
|
ResolvePathResult::with(curr_per_ns, ReachedFixedPoint::Yes, None, Some(self.krate))
|
2019-11-08 21:17:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn resolve_name_in_module(
|
|
|
|
&self,
|
2020-03-13 15:05:46 +00:00
|
|
|
db: &dyn DefDatabase,
|
2019-11-23 13:49:53 +00:00
|
|
|
module: LocalModuleId,
|
2019-11-08 21:17:17 +00:00
|
|
|
name: &Name,
|
2019-11-30 15:29:21 +00:00
|
|
|
shadow: BuiltinShadowMode,
|
2019-11-08 21:17:17 +00:00
|
|
|
) -> PerNs {
|
|
|
|
// Resolve in:
|
|
|
|
// - legacy scope of macro
|
|
|
|
// - current module / scope
|
|
|
|
// - extern prelude
|
|
|
|
// - std prelude
|
2019-12-25 14:00:10 +00:00
|
|
|
let from_legacy_macro = self[module]
|
|
|
|
.scope
|
|
|
|
.get_legacy_macro(name)
|
2022-07-05 09:28:47 +00:00
|
|
|
// FIXME: shadowing
|
|
|
|
.and_then(|it| it.last())
|
|
|
|
.map_or_else(PerNs::none, |&m| PerNs::macros(m.into(), Visibility::Public));
|
2020-02-21 14:34:06 +00:00
|
|
|
let from_scope = self[module].scope.get(name);
|
2021-04-16 17:28:22 +00:00
|
|
|
let from_builtin = match self.block {
|
|
|
|
Some(_) => {
|
|
|
|
// Only resolve to builtins in the root `DefMap`.
|
|
|
|
PerNs::none()
|
|
|
|
}
|
|
|
|
None => BUILTIN_SCOPE.get(name).copied().unwrap_or_else(PerNs::none),
|
|
|
|
};
|
2020-02-21 14:34:06 +00:00
|
|
|
let from_scope_or_builtin = match shadow {
|
|
|
|
BuiltinShadowMode::Module => from_scope.or(from_builtin),
|
2021-10-03 12:53:01 +00:00
|
|
|
BuiltinShadowMode::Other => match from_scope.take_types() {
|
|
|
|
Some(ModuleDefId::ModuleId(_)) => from_builtin.or(from_scope),
|
|
|
|
Some(_) | None => from_scope.or(from_builtin),
|
|
|
|
},
|
2020-02-21 14:34:06 +00:00
|
|
|
};
|
2021-03-10 15:33:18 +00:00
|
|
|
|
2022-08-15 14:41:51 +00:00
|
|
|
let extern_prelude = || {
|
|
|
|
self.extern_prelude
|
|
|
|
.get(name)
|
|
|
|
.map_or(PerNs::none(), |&it| PerNs::types(it.into(), Visibility::Public))
|
|
|
|
};
|
|
|
|
let prelude = || self.resolve_in_prelude(db, name);
|
2019-11-08 21:17:17 +00:00
|
|
|
|
2022-08-15 14:41:51 +00:00
|
|
|
from_legacy_macro.or(from_scope_or_builtin).or_else(extern_prelude).or_else(prelude)
|
2019-11-08 21:17:17 +00:00
|
|
|
}
|
|
|
|
|
2021-04-09 13:25:12 +00:00
|
|
|
fn resolve_name_in_crate_root_or_extern_prelude(
|
|
|
|
&self,
|
|
|
|
db: &dyn DefDatabase,
|
|
|
|
name: &Name,
|
|
|
|
) -> PerNs {
|
2022-08-15 14:41:51 +00:00
|
|
|
let from_crate_root = match self.block {
|
2021-04-09 13:25:12 +00:00
|
|
|
Some(_) => {
|
2022-08-15 14:41:51 +00:00
|
|
|
let def_map = self.crate_root(db).def_map(db);
|
|
|
|
def_map[def_map.root].scope.get(name)
|
2021-04-09 13:25:12 +00:00
|
|
|
}
|
2022-08-15 14:41:51 +00:00
|
|
|
None => self[self.root].scope.get(name),
|
|
|
|
};
|
|
|
|
let from_extern_prelude = || {
|
|
|
|
self.resolve_name_in_extern_prelude(db, name)
|
|
|
|
.map_or(PerNs::none(), |it| PerNs::types(it.into(), Visibility::Public))
|
2021-04-09 13:25:12 +00:00
|
|
|
};
|
2019-11-08 21:17:17 +00:00
|
|
|
|
2022-08-15 14:41:51 +00:00
|
|
|
from_crate_root.or_else(from_extern_prelude)
|
2019-11-08 21:17:17 +00:00
|
|
|
}
|
|
|
|
|
2020-03-13 15:05:46 +00:00
|
|
|
fn resolve_in_prelude(&self, db: &dyn DefDatabase, name: &Name) -> PerNs {
|
2019-11-08 21:17:17 +00:00
|
|
|
if let Some(prelude) = self.prelude {
|
|
|
|
let keep;
|
|
|
|
let def_map = if prelude.krate == self.krate {
|
|
|
|
self
|
|
|
|
} else {
|
|
|
|
// Extend lifetime
|
2021-01-22 15:31:40 +00:00
|
|
|
keep = prelude.def_map(db);
|
2019-11-08 21:17:17 +00:00
|
|
|
&keep
|
|
|
|
};
|
2020-02-21 14:34:06 +00:00
|
|
|
def_map[prelude.local_id].scope.get(name)
|
2019-11-08 21:17:17 +00:00
|
|
|
} else {
|
|
|
|
PerNs::none()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|