2019-11-08 15:17:17 -06: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 09:25:38 -05:00
|
|
|
use base_db::Edition;
|
2024-01-15 03:24:14 -06:00
|
|
|
use hir_expand::{name::Name, Lookup};
|
2023-06-27 01:22:05 -05:00
|
|
|
use triomphe::Arc;
|
2019-11-08 15:17:17 -06:00
|
|
|
|
|
|
|
use crate::{
|
2019-11-23 05:44:43 -06:00
|
|
|
db::DefDatabase,
|
2023-08-17 03:52:13 -05:00
|
|
|
item_scope::{ImportOrExternCrate, BUILTIN_SCOPE},
|
2024-01-15 03:24:14 -06:00
|
|
|
item_tree::Fields,
|
2023-06-27 01:22:05 -05:00
|
|
|
nameres::{sub_namespace_match, BlockInfo, BuiltinShadowMode, DefMap, MacroSubNs},
|
2019-12-13 05:12:36 -06:00
|
|
|
path::{ModPath, PathKind},
|
2019-11-23 07:53:16 -06:00
|
|
|
per_ns::PerNs,
|
2024-01-11 04:36:10 -06:00
|
|
|
visibility::{RawVisibility, Visibility},
|
2024-01-15 03:24:14 -06:00
|
|
|
AdtId, CrateId, LocalModuleId, ModuleDefId,
|
2019-11-08 15:17:17 -06: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 05:20:41 -06:00
|
|
|
pub(super) krate: Option<CrateId>,
|
2019-11-08 15:17:17 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
impl ResolvePathResult {
|
|
|
|
fn empty(reached_fixedpoint: ReachedFixedPoint) -> ResolvePathResult {
|
2019-12-07 05:20:41 -06:00
|
|
|
ResolvePathResult::with(PerNs::none(), reached_fixedpoint, None, None)
|
2019-11-08 15:17:17 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
fn with(
|
|
|
|
resolved_def: PerNs,
|
|
|
|
reached_fixedpoint: ReachedFixedPoint,
|
|
|
|
segment_index: Option<usize>,
|
2019-12-07 05:20:41 -06:00
|
|
|
krate: Option<CrateId>,
|
2019-11-08 15:17:17 -06:00
|
|
|
) -> ResolvePathResult {
|
2021-06-07 06:59:01 -05:00
|
|
|
ResolvePathResult { resolved_def, segment_index, reached_fixedpoint, krate }
|
2019-11-08 15:17:17 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-11 01:52:13 -05:00
|
|
|
impl PerNs {
|
2023-05-13 10:50:51 -05:00
|
|
|
pub(super) fn filter_macro(
|
|
|
|
mut self,
|
|
|
|
db: &dyn DefDatabase,
|
|
|
|
expected: Option<MacroSubNs>,
|
|
|
|
) -> Self {
|
2023-08-16 04:18:11 -05:00
|
|
|
self.macros = self.macros.filter(|&(id, _, _)| {
|
2023-05-11 01:52:13 -05:00
|
|
|
let this = MacroSubNs::from_id(db, id);
|
|
|
|
sub_namespace_match(Some(this), expected)
|
|
|
|
});
|
|
|
|
|
|
|
|
self
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-18 13:18:05 -06:00
|
|
|
impl DefMap {
|
2019-12-24 16:45:14 -06:00
|
|
|
pub(crate) fn resolve_visibility(
|
|
|
|
&self,
|
2020-03-13 10:05:46 -05:00
|
|
|
db: &dyn DefDatabase,
|
2022-11-11 04:50:26 -06:00
|
|
|
// module to import to
|
2019-12-24 16:45:14 -06:00
|
|
|
original_module: LocalModuleId,
|
2022-11-11 04:50:26 -06:00
|
|
|
// pub(path)
|
|
|
|
// ^^^^ this
|
2019-12-26 08:57:14 -06:00
|
|
|
visibility: &RawVisibility,
|
2023-02-19 08:30:49 -06:00
|
|
|
within_impl: bool,
|
2019-12-26 09:00:10 -06:00
|
|
|
) -> Option<Visibility> {
|
2021-02-27 21:39:38 -06:00
|
|
|
let mut vis = match visibility {
|
2024-02-02 04:22:54 -06:00
|
|
|
RawVisibility::Module(path, explicitness) => {
|
2019-12-24 16:45:14 -06:00
|
|
|
let (result, remaining) =
|
2023-05-11 01:52:13 -05:00
|
|
|
self.resolve_path(db, original_module, path, BuiltinShadowMode::Module, None);
|
2019-12-24 16:45:14 -06:00
|
|
|
if remaining.is_some() {
|
|
|
|
return None;
|
|
|
|
}
|
2023-12-08 04:59:44 -06:00
|
|
|
let types = result.take_types()?;
|
|
|
|
match types {
|
2024-02-02 04:22:54 -06:00
|
|
|
ModuleDefId::ModuleId(m) => Visibility::Module(m, *explicitness),
|
2023-12-08 04:59:44 -06:00
|
|
|
// error: visibility needs to refer to module
|
|
|
|
_ => {
|
2021-02-27 21:39:38 -06:00
|
|
|
return None;
|
2019-12-24 16:45:14 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-02-27 21:39:38 -06: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.
|
2024-01-05 04:00:29 -06:00
|
|
|
if let Visibility::Module(m, mv) = vis {
|
2023-02-19 08:30:49 -06:00
|
|
|
// ...unless we're resolving visibility for an associated item in an impl.
|
|
|
|
if self.block_id() != m.block && !within_impl {
|
2021-03-08 14:19:44 -06:00
|
|
|
cov_mark::hit!(adjust_vis_in_block_def_map);
|
2024-01-05 04:00:29 -06:00
|
|
|
vis = Visibility::Module(self.module_id(Self::ROOT), mv);
|
2021-08-15 07:46:13 -05:00
|
|
|
tracing::debug!("visibility {:?} points outside DefMap, adjusting to {:?}", m, vis);
|
2021-02-27 21:39:38 -06:00
|
|
|
}
|
2019-12-24 16:45:14 -06:00
|
|
|
}
|
2021-02-27 21:39:38 -06:00
|
|
|
|
|
|
|
Some(vis)
|
2019-12-24 16:45:14 -06:00
|
|
|
}
|
|
|
|
|
2019-11-08 15:17:17 -06: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 10:05:46 -05:00
|
|
|
db: &dyn DefDatabase,
|
2021-01-21 08:24:15 -06:00
|
|
|
mode: ResolveMode,
|
2022-11-11 04:50:26 -06:00
|
|
|
// module to import to
|
2021-01-28 11:27:27 -06:00
|
|
|
mut original_module: LocalModuleId,
|
2021-01-21 08:24:15 -06:00
|
|
|
path: &ModPath,
|
|
|
|
shadow: BuiltinShadowMode,
|
2023-05-11 01:52:13 -05:00
|
|
|
// Pass `MacroSubNs` if we know we're resolving macro names and which kind of macro we're
|
|
|
|
// resolving them to. Pass `None` otherwise, e.g. when we're resolving import paths.
|
|
|
|
expected_macro_subns: Option<MacroSubNs>,
|
2021-01-21 08:24:15 -06:00
|
|
|
) -> ResolvePathResult {
|
|
|
|
let mut result = ResolvePathResult::empty(ReachedFixedPoint::No);
|
|
|
|
|
2021-02-04 06:44:54 -06:00
|
|
|
let mut arc;
|
2021-01-21 08:24:15 -06:00
|
|
|
let mut current_map = self;
|
|
|
|
loop {
|
|
|
|
let new = current_map.resolve_path_fp_with_macro_single(
|
|
|
|
db,
|
|
|
|
mode,
|
|
|
|
original_module,
|
|
|
|
path,
|
|
|
|
shadow,
|
2023-05-11 01:52:13 -05:00
|
|
|
expected_macro_subns,
|
2021-01-21 08:24:15 -06:00
|
|
|
);
|
|
|
|
|
|
|
|
// 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 08:14:33 -06: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 08:24:15 -06:00
|
|
|
|
2023-06-27 01:22:05 -05:00
|
|
|
match current_map.block {
|
|
|
|
Some(block) if original_module == Self::ROOT => {
|
|
|
|
// Block modules "inherit" names from its parent module.
|
2021-02-04 06:44:54 -06:00
|
|
|
original_module = block.parent.local_id;
|
2023-06-01 07:46:36 -05:00
|
|
|
arc = block.parent.def_map(db, current_map.krate);
|
2023-06-27 01:22:05 -05:00
|
|
|
current_map = &arc;
|
2021-01-28 11:27:27 -06:00
|
|
|
}
|
2023-06-27 01:22:05 -05:00
|
|
|
// Proper (non-block) modules, including those in block `DefMap`s, don't.
|
|
|
|
_ => return result,
|
2021-01-21 08:24:15 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-22 12:47:19 -05:00
|
|
|
pub(super) fn resolve_path_fp_with_macro_single(
|
2021-01-21 08:24:15 -06:00
|
|
|
&self,
|
|
|
|
db: &dyn DefDatabase,
|
2019-11-08 15:17:17 -06:00
|
|
|
mode: ResolveMode,
|
2019-11-23 07:49:53 -06:00
|
|
|
original_module: LocalModuleId,
|
2019-12-13 05:12:36 -06:00
|
|
|
path: &ModPath,
|
2019-11-30 09:29:21 -06:00
|
|
|
shadow: BuiltinShadowMode,
|
2023-05-11 01:52:13 -05:00
|
|
|
expected_macro_subns: Option<MacroSubNs>,
|
2019-11-08 15:17:17 -06:00
|
|
|
) -> ResolvePathResult {
|
2021-02-04 13:49:24 -06:00
|
|
|
let mut segments = path.segments().iter().enumerate();
|
2023-06-27 01:22:18 -05:00
|
|
|
let mut curr_per_ns = match path.kind {
|
2019-11-08 15:17:17 -06:00
|
|
|
PathKind::DollarCrate(krate) => {
|
|
|
|
if krate == self.krate {
|
2021-03-08 14:19:44 -06:00
|
|
|
cov_mark::hit!(macro_dollar_crate_self);
|
2023-08-17 03:52:13 -05:00
|
|
|
PerNs::types(self.crate_root().into(), Visibility::Public, None)
|
2019-11-08 15:17:17 -06:00
|
|
|
} else {
|
|
|
|
let def_map = db.crate_def_map(krate);
|
2023-06-01 07:46:36 -05:00
|
|
|
let module = def_map.module_id(Self::ROOT);
|
2021-03-08 14:19:44 -06:00
|
|
|
cov_mark::hit!(macro_dollar_crate_other);
|
2023-08-17 03:52:13 -05:00
|
|
|
PerNs::types(module.into(), Visibility::Public, None)
|
2019-11-08 15:17:17 -06:00
|
|
|
}
|
|
|
|
}
|
2023-08-17 03:52:13 -05:00
|
|
|
PathKind::Crate => PerNs::types(self.crate_root().into(), Visibility::Public, None),
|
2019-11-08 15:17:17 -06: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
|
2023-06-01 08:04:38 -05:00
|
|
|
if self.data.edition == Edition::Edition2015
|
2019-11-08 15:17:17 -06:00
|
|
|
&& (path.kind == PathKind::Abs || mode == ResolveMode::Import) =>
|
|
|
|
{
|
2020-02-21 08:34:06 -06:00
|
|
|
let (_, segment) = match segments.next() {
|
2019-11-30 22:14:35 -06:00
|
|
|
Some((idx, segment)) => (idx, segment),
|
2019-11-08 15:17:17 -06:00
|
|
|
None => return ResolvePathResult::empty(ReachedFixedPoint::Yes),
|
|
|
|
};
|
2021-08-15 07:46:13 -05:00
|
|
|
tracing::debug!("resolving {:?} in crate root (+ extern prelude)", segment);
|
2021-06-12 22:54:16 -05:00
|
|
|
self.resolve_name_in_crate_root_or_extern_prelude(db, segment)
|
2019-11-08 15:17:17 -06:00
|
|
|
}
|
|
|
|
PathKind::Plain => {
|
2020-02-21 08:34:06 -06:00
|
|
|
let (_, segment) = match segments.next() {
|
2019-11-30 22:14:35 -06:00
|
|
|
Some((idx, segment)) => (idx, segment),
|
2019-11-08 15:17:17 -06:00
|
|
|
None => return ResolvePathResult::empty(ReachedFixedPoint::Yes),
|
|
|
|
};
|
2020-02-21 08:34:06 -06: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 13:49:24 -06:00
|
|
|
if path.segments().len() == 1 { shadow } else { BuiltinShadowMode::Module };
|
2020-02-21 08:34:06 -06:00
|
|
|
|
2021-08-15 07:46:13 -05:00
|
|
|
tracing::debug!("resolving {:?} in module", segment);
|
2023-05-11 01:52:13 -05:00
|
|
|
self.resolve_name_in_module(
|
|
|
|
db,
|
|
|
|
original_module,
|
|
|
|
segment,
|
|
|
|
prefer_module,
|
|
|
|
expected_macro_subns,
|
|
|
|
)
|
2019-11-08 15:17:17 -06:00
|
|
|
}
|
2019-12-17 08:38:28 -06:00
|
|
|
PathKind::Super(lvl) => {
|
2023-06-27 01:22:05 -05:00
|
|
|
let mut local_id = original_module;
|
|
|
|
let mut ext;
|
|
|
|
let mut def_map = self;
|
|
|
|
|
|
|
|
// Adjust `local_id` to `self`, i.e. the nearest non-block module.
|
|
|
|
if def_map.module_id(local_id).is_block_module() {
|
|
|
|
(ext, local_id) = adjust_to_nearest_non_block_module(db, def_map, local_id);
|
|
|
|
def_map = &ext;
|
2019-11-08 15:17:17 -06:00
|
|
|
}
|
2021-01-25 12:15:39 -06:00
|
|
|
|
2023-06-27 01:22:05 -05:00
|
|
|
// Go up the module tree but skip block modules as `super` always refers to the
|
|
|
|
// nearest non-block module.
|
|
|
|
for _ in 0..lvl {
|
|
|
|
// Loop invariant: at the beginning of each loop, `local_id` must refer to a
|
|
|
|
// non-block module.
|
|
|
|
if let Some(parent) = def_map.modules[local_id].parent {
|
|
|
|
local_id = parent;
|
|
|
|
if def_map.module_id(local_id).is_block_module() {
|
|
|
|
(ext, local_id) =
|
|
|
|
adjust_to_nearest_non_block_module(db, def_map, local_id);
|
|
|
|
def_map = &ext;
|
|
|
|
}
|
2021-02-05 12:24:03 -06:00
|
|
|
} else {
|
2023-06-27 01:22:05 -05:00
|
|
|
stdx::always!(def_map.block.is_none());
|
|
|
|
tracing::debug!("super path in root module");
|
|
|
|
return ResolvePathResult::empty(ReachedFixedPoint::Yes);
|
2021-02-05 12:24:03 -06:00
|
|
|
}
|
2023-06-27 01:22:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
let module = def_map.module_id(local_id);
|
|
|
|
stdx::never!(module.is_block_module());
|
|
|
|
|
|
|
|
if self.block != def_map.block {
|
2024-01-30 07:43:43 -06:00
|
|
|
// If we have a different `DefMap` from `self` (the original `DefMap` we started
|
2023-06-27 01:22:05 -05:00
|
|
|
// with), resolve the remaining path segments in that `DefMap`.
|
|
|
|
let path =
|
|
|
|
ModPath::from_segments(PathKind::Super(0), path.segments().iter().cloned());
|
|
|
|
return def_map.resolve_path_fp_with_macro(
|
|
|
|
db,
|
|
|
|
mode,
|
|
|
|
local_id,
|
|
|
|
&path,
|
|
|
|
shadow,
|
|
|
|
expected_macro_subns,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2023-08-17 03:52:13 -05:00
|
|
|
PerNs::types(module.into(), Visibility::Public, None)
|
2019-11-08 15:17:17 -06:00
|
|
|
}
|
|
|
|
PathKind::Abs => {
|
|
|
|
// 2018-style absolute path -- only extern prelude
|
|
|
|
let segment = match segments.next() {
|
|
|
|
Some((_, segment)) => segment,
|
|
|
|
None => return ResolvePathResult::empty(ReachedFixedPoint::Yes),
|
|
|
|
};
|
2023-08-17 03:52:13 -05:00
|
|
|
if let Some(&(def, extern_crate)) = self.data.extern_prelude.get(segment) {
|
2021-08-15 07:46:13 -05:00
|
|
|
tracing::debug!("absolute path {:?} resolved to crate {:?}", path, def);
|
2023-08-17 03:52:13 -05:00
|
|
|
PerNs::types(
|
|
|
|
def.into(),
|
|
|
|
Visibility::Public,
|
|
|
|
extern_crate.map(ImportOrExternCrate::ExternCrate),
|
|
|
|
)
|
2019-11-08 15:17:17 -06:00
|
|
|
} else {
|
|
|
|
return ResolvePathResult::empty(ReachedFixedPoint::No); // extern crate declarations can add to the extern prelude
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2019-11-30 22:14:35 -06:00
|
|
|
for (i, segment) in segments {
|
2023-08-17 03:52:13 -05:00
|
|
|
let (curr, vis, imp) = match curr_per_ns.take_types_full() {
|
2019-11-08 15:17:17 -06: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 13:49:24 -06:00
|
|
|
let path = ModPath::from_segments(
|
|
|
|
PathKind::Super(0),
|
|
|
|
path.segments()[i..].iter().cloned(),
|
|
|
|
);
|
2021-08-15 07:46:13 -05:00
|
|
|
tracing::debug!("resolving {:?} in other crate", path);
|
2021-01-22 09:31:40 -06:00
|
|
|
let defp_map = module.def_map(db);
|
2023-05-11 01:52:13 -05:00
|
|
|
// Macro sub-namespaces only matter when resolving single-segment paths
|
|
|
|
// because `macro_use` and other preludes should be taken into account. At
|
|
|
|
// this point, we know we're resolving a multi-segment path so macro kind
|
|
|
|
// expectation is discarded.
|
|
|
|
let (def, s) =
|
|
|
|
defp_map.resolve_path(db, module.local_id, &path, shadow, None);
|
2019-11-08 15:17:17 -06:00
|
|
|
return ResolvePathResult::with(
|
|
|
|
def,
|
|
|
|
ReachedFixedPoint::Yes,
|
|
|
|
s.map(|s| s + i),
|
2019-12-07 05:20:41 -06:00
|
|
|
Some(module.krate),
|
2019-11-08 15:17:17 -06:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-02-02 05:25:13 -06: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 15:17:17 -06:00
|
|
|
// Since it is a qualified path here, it should not contains legacy macros
|
2021-06-12 22:54:16 -05:00
|
|
|
module_data.scope.get(segment)
|
2019-11-08 15:17:17 -06:00
|
|
|
}
|
|
|
|
ModuleDefId::AdtId(AdtId::EnumId(e)) => {
|
|
|
|
// enum variant
|
2021-03-08 14:19:44 -06:00
|
|
|
cov_mark::hit!(can_import_enum_variant);
|
2024-01-15 05:03:31 -06:00
|
|
|
let def_map;
|
2024-01-15 03:24:14 -06:00
|
|
|
|
|
|
|
let loc = e.lookup(db);
|
|
|
|
let tree = loc.id.item_tree(db);
|
|
|
|
let current_def_map =
|
|
|
|
self.krate == loc.container.krate && self.block_id() == loc.container.block;
|
|
|
|
let res = if current_def_map {
|
2024-01-15 03:58:05 -06:00
|
|
|
&self.enum_definitions[&e]
|
2024-01-15 03:24:14 -06:00
|
|
|
} else {
|
2024-01-15 03:58:05 -06:00
|
|
|
def_map = loc.container.def_map(db);
|
|
|
|
&def_map.enum_definitions[&e]
|
|
|
|
}
|
|
|
|
.iter()
|
|
|
|
.find_map(|&variant| {
|
|
|
|
let variant_data = &tree[variant.lookup(db).id.value];
|
|
|
|
(variant_data.name == *segment).then(|| match variant_data.fields {
|
|
|
|
Fields::Record(_) => {
|
|
|
|
PerNs::types(variant.into(), Visibility::Public, None)
|
|
|
|
}
|
|
|
|
Fields::Tuple(_) | Fields::Unit => PerNs::both(
|
|
|
|
variant.into(),
|
|
|
|
variant.into(),
|
|
|
|
Visibility::Public,
|
|
|
|
None,
|
|
|
|
),
|
|
|
|
})
|
|
|
|
});
|
2024-01-15 03:24:14 -06:00
|
|
|
match res {
|
|
|
|
Some(res) => res,
|
2019-11-08 15:17:17 -06:00
|
|
|
None => {
|
|
|
|
return ResolvePathResult::with(
|
2023-08-17 03:52:13 -05:00
|
|
|
PerNs::types(e.into(), vis, imp),
|
2019-11-08 15:17:17 -06:00
|
|
|
ReachedFixedPoint::Yes,
|
|
|
|
Some(i),
|
2019-12-07 05:20:41 -06:00
|
|
|
Some(self.krate),
|
2024-01-15 03:24:14 -06:00
|
|
|
)
|
2019-11-08 15:17:17 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
s => {
|
|
|
|
// could be an inherent method call in UFCS form
|
|
|
|
// (`Struct::method`), or some other kind of associated item
|
2021-08-15 07:46:13 -05:00
|
|
|
tracing::debug!(
|
2019-11-08 15:17:17 -06:00
|
|
|
"path segment {:?} resolved to non-module {:?}, but is not last",
|
2019-12-13 05:12:36 -06:00
|
|
|
segment,
|
2019-11-08 15:17:17 -06:00
|
|
|
curr,
|
|
|
|
);
|
|
|
|
|
|
|
|
return ResolvePathResult::with(
|
2023-08-17 03:52:13 -05:00
|
|
|
PerNs::types(s, vis, imp),
|
2019-11-08 15:17:17 -06:00
|
|
|
ReachedFixedPoint::Yes,
|
|
|
|
Some(i),
|
2019-12-07 05:20:41 -06:00
|
|
|
Some(self.krate),
|
2019-11-08 15:17:17 -06:00
|
|
|
);
|
|
|
|
}
|
|
|
|
};
|
2022-11-11 04:50:26 -06:00
|
|
|
|
|
|
|
curr_per_ns = curr_per_ns
|
|
|
|
.filter_visibility(|vis| vis.is_visible_from_def_map(db, self, original_module));
|
2019-11-08 15:17:17 -06:00
|
|
|
}
|
2019-11-30 09:29:21 -06:00
|
|
|
|
2019-12-07 05:20:41 -06:00
|
|
|
ResolvePathResult::with(curr_per_ns, ReachedFixedPoint::Yes, None, Some(self.krate))
|
2019-11-08 15:17:17 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
fn resolve_name_in_module(
|
|
|
|
&self,
|
2020-03-13 10:05:46 -05:00
|
|
|
db: &dyn DefDatabase,
|
2019-11-23 07:49:53 -06:00
|
|
|
module: LocalModuleId,
|
2019-11-08 15:17:17 -06:00
|
|
|
name: &Name,
|
2019-11-30 09:29:21 -06:00
|
|
|
shadow: BuiltinShadowMode,
|
2023-05-11 01:52:13 -05:00
|
|
|
expected_macro_subns: Option<MacroSubNs>,
|
2019-11-08 15:17:17 -06:00
|
|
|
) -> PerNs {
|
|
|
|
// Resolve in:
|
|
|
|
// - legacy scope of macro
|
|
|
|
// - current module / scope
|
2023-05-11 01:41:24 -05:00
|
|
|
// - extern prelude / macro_use prelude
|
2019-11-08 15:17:17 -06:00
|
|
|
// - std prelude
|
2019-12-25 08:00:10 -06:00
|
|
|
let from_legacy_macro = self[module]
|
|
|
|
.scope
|
|
|
|
.get_legacy_macro(name)
|
2022-07-05 04:28:47 -05:00
|
|
|
// FIXME: shadowing
|
|
|
|
.and_then(|it| it.last())
|
2023-05-11 01:52:13 -05:00
|
|
|
.copied()
|
|
|
|
.filter(|&id| {
|
|
|
|
sub_namespace_match(Some(MacroSubNs::from_id(db, id)), expected_macro_subns)
|
|
|
|
})
|
2023-08-17 03:52:13 -05:00
|
|
|
.map_or_else(PerNs::none, |m| PerNs::macros(m, Visibility::Public, None));
|
2023-05-11 01:52:13 -05:00
|
|
|
let from_scope = self[module].scope.get(name).filter_macro(db, expected_macro_subns);
|
2021-04-16 12:28:22 -05: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 08:34:06 -06:00
|
|
|
let from_scope_or_builtin = match shadow {
|
|
|
|
BuiltinShadowMode::Module => from_scope.or(from_builtin),
|
2021-10-03 07:53:01 -05: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 08:34:06 -06:00
|
|
|
};
|
2021-03-10 09:33:18 -06:00
|
|
|
|
2022-08-15 09:41:51 -05:00
|
|
|
let extern_prelude = || {
|
2023-06-01 10:58:08 -05:00
|
|
|
if self.block.is_some() {
|
2023-08-17 03:52:13 -05:00
|
|
|
// Don't resolve extern prelude in block `DefMap`s, defer it to the crate def map so
|
|
|
|
// that blocks can properly shadow them
|
2023-06-01 10:58:08 -05:00
|
|
|
return PerNs::none();
|
|
|
|
}
|
2023-08-17 03:52:13 -05:00
|
|
|
self.data.extern_prelude.get(name).map_or(PerNs::none(), |&(it, extern_crate)| {
|
|
|
|
PerNs::types(
|
|
|
|
it.into(),
|
|
|
|
Visibility::Public,
|
|
|
|
extern_crate.map(ImportOrExternCrate::ExternCrate),
|
|
|
|
)
|
2023-08-09 08:20:42 -05:00
|
|
|
})
|
2022-08-15 09:41:51 -05:00
|
|
|
};
|
2023-05-11 01:41:24 -05:00
|
|
|
let macro_use_prelude = || {
|
2023-08-09 08:20:42 -05:00
|
|
|
self.macro_use_prelude.get(name).map_or(PerNs::none(), |&(it, _extern_crate)| {
|
2023-08-17 03:52:13 -05:00
|
|
|
PerNs::macros(
|
2024-01-21 18:31:50 -06:00
|
|
|
it,
|
2023-08-17 03:52:13 -05:00
|
|
|
Visibility::Public,
|
|
|
|
// FIXME?
|
|
|
|
None, // extern_crate.map(ImportOrExternCrate::ExternCrate),
|
|
|
|
)
|
2023-08-09 08:20:42 -05:00
|
|
|
})
|
2023-05-11 01:41:24 -05:00
|
|
|
};
|
2022-08-15 09:41:51 -05:00
|
|
|
let prelude = || self.resolve_in_prelude(db, name);
|
2019-11-08 15:17:17 -06:00
|
|
|
|
2023-05-11 01:41:24 -05:00
|
|
|
from_legacy_macro
|
|
|
|
.or(from_scope_or_builtin)
|
|
|
|
.or_else(extern_prelude)
|
|
|
|
.or_else(macro_use_prelude)
|
|
|
|
.or_else(prelude)
|
2019-11-08 15:17:17 -06:00
|
|
|
}
|
|
|
|
|
2021-04-09 08:25:12 -05:00
|
|
|
fn resolve_name_in_crate_root_or_extern_prelude(
|
|
|
|
&self,
|
|
|
|
db: &dyn DefDatabase,
|
|
|
|
name: &Name,
|
|
|
|
) -> PerNs {
|
2022-08-15 09:41:51 -05:00
|
|
|
let from_crate_root = match self.block {
|
2021-04-09 08:25:12 -05:00
|
|
|
Some(_) => {
|
2023-06-01 10:58:08 -05:00
|
|
|
let def_map = self.crate_root().def_map(db);
|
2023-06-01 07:46:36 -05:00
|
|
|
def_map[Self::ROOT].scope.get(name)
|
2021-04-09 08:25:12 -05:00
|
|
|
}
|
2023-06-01 07:46:36 -05:00
|
|
|
None => self[Self::ROOT].scope.get(name),
|
2022-08-15 09:41:51 -05:00
|
|
|
};
|
|
|
|
let from_extern_prelude = || {
|
2023-06-01 10:58:08 -05:00
|
|
|
if self.block.is_some() {
|
|
|
|
// Don't resolve extern prelude in block `DefMap`s.
|
|
|
|
return PerNs::none();
|
|
|
|
}
|
2023-08-17 03:52:13 -05:00
|
|
|
self.data.extern_prelude.get(name).copied().map_or(
|
|
|
|
PerNs::none(),
|
|
|
|
|(it, extern_crate)| {
|
|
|
|
PerNs::types(
|
|
|
|
it.into(),
|
|
|
|
Visibility::Public,
|
|
|
|
extern_crate.map(ImportOrExternCrate::ExternCrate),
|
|
|
|
)
|
|
|
|
},
|
|
|
|
)
|
2021-04-09 08:25:12 -05:00
|
|
|
};
|
2019-11-08 15:17:17 -06:00
|
|
|
|
2022-08-15 09:41:51 -05:00
|
|
|
from_crate_root.or_else(from_extern_prelude)
|
2019-11-08 15:17:17 -06:00
|
|
|
}
|
|
|
|
|
2020-03-13 10:05:46 -05:00
|
|
|
fn resolve_in_prelude(&self, db: &dyn DefDatabase, name: &Name) -> PerNs {
|
2023-08-09 08:20:42 -05:00
|
|
|
if let Some((prelude, _use)) = self.prelude {
|
2019-11-08 15:17:17 -06:00
|
|
|
let keep;
|
|
|
|
let def_map = if prelude.krate == self.krate {
|
|
|
|
self
|
|
|
|
} else {
|
|
|
|
// Extend lifetime
|
2021-01-22 09:31:40 -06:00
|
|
|
keep = prelude.def_map(db);
|
2019-11-08 15:17:17 -06:00
|
|
|
&keep
|
|
|
|
};
|
2020-02-21 08:34:06 -06:00
|
|
|
def_map[prelude.local_id].scope.get(name)
|
2019-11-08 15:17:17 -06:00
|
|
|
} else {
|
|
|
|
PerNs::none()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-06-27 01:22:05 -05:00
|
|
|
|
2024-01-30 07:43:43 -06:00
|
|
|
/// Given a block module, returns its nearest non-block module and the `DefMap` it belongs to.
|
2023-06-27 01:22:05 -05:00
|
|
|
fn adjust_to_nearest_non_block_module(
|
|
|
|
db: &dyn DefDatabase,
|
|
|
|
def_map: &DefMap,
|
|
|
|
mut local_id: LocalModuleId,
|
|
|
|
) -> (Arc<DefMap>, LocalModuleId) {
|
|
|
|
// INVARIANT: `local_id` in `def_map` must be a block module.
|
|
|
|
stdx::always!(def_map.module_id(local_id).is_block_module());
|
|
|
|
|
|
|
|
let mut ext;
|
|
|
|
// This needs to be a local variable due to our mighty lifetime.
|
|
|
|
let mut def_map = def_map;
|
|
|
|
loop {
|
|
|
|
let BlockInfo { parent, .. } = def_map.block.expect("block module without parent module");
|
|
|
|
|
|
|
|
ext = parent.def_map(db, def_map.krate);
|
|
|
|
def_map = &ext;
|
|
|
|
local_id = parent.local_id;
|
|
|
|
if !parent.is_block_module() {
|
|
|
|
return (ext, local_id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|