2019-01-08 23:47:12 +00:00
|
|
|
//! Name resolution algorithm. The end result of the algorithm is an `ItemMap`:
|
|
|
|
//! a map which maps each module to its scope: the set of items visible in the
|
2018-11-28 00:42:26 +00:00
|
|
|
//! module. That is, we only resolve imports here, name resolution of item
|
|
|
|
//! bodies will be done in a separate step.
|
|
|
|
//!
|
2019-01-08 23:47:12 +00:00
|
|
|
//! Like Rustc, we use an interactive per-crate algorithm: we start with scopes
|
2018-11-28 00:42:26 +00:00
|
|
|
//! containing only directly defined items, and then iteratively resolve
|
|
|
|
//! imports.
|
|
|
|
//!
|
2019-01-08 23:47:12 +00:00
|
|
|
//! To make this work nicely in the IDE scenario, we place `InputModuleItems`
|
2018-11-28 00:42:26 +00:00
|
|
|
//! in between raw syntax and name resolution. `InputModuleItems` are computed
|
|
|
|
//! using only the module's syntax, and it is all directly defined items plus
|
2019-01-08 23:47:12 +00:00
|
|
|
//! imports. The plan is to make `InputModuleItems` independent of local
|
|
|
|
//! modifications (that is, typing inside a function should not change IMIs),
|
|
|
|
//! so that the results of name resolution can be preserved unless the module
|
2018-11-28 00:42:26 +00:00
|
|
|
//! structure itself is modified.
|
2019-01-18 11:34:13 +00:00
|
|
|
pub(crate) mod lower;
|
|
|
|
use lower::*;
|
|
|
|
|
2018-12-27 17:07:21 +00:00
|
|
|
use std::sync::Arc;
|
2018-11-28 00:42:26 +00:00
|
|
|
|
2019-01-07 18:40:31 +00:00
|
|
|
use rustc_hash::{FxHashMap, FxHashSet};
|
2019-01-18 11:34:13 +00:00
|
|
|
use ra_syntax::SyntaxKind::*;
|
|
|
|
use ra_db::SourceRootId;
|
2018-11-28 00:42:26 +00:00
|
|
|
|
|
|
|
use crate::{
|
2018-12-04 20:01:53 +00:00
|
|
|
DefId, DefLoc, DefKind,
|
2018-11-28 00:42:26 +00:00
|
|
|
Path, PathKind,
|
2018-12-09 09:45:47 +00:00
|
|
|
HirDatabase, Crate,
|
2019-01-18 11:34:13 +00:00
|
|
|
Name,
|
2019-01-06 14:33:27 +00:00
|
|
|
module_tree::{ModuleId, ModuleTree},
|
2018-11-28 00:42:26 +00:00
|
|
|
};
|
|
|
|
|
2019-01-08 23:47:12 +00:00
|
|
|
/// `ItemMap` is the result of name resolution. It contains, for each
|
2018-11-28 00:42:26 +00:00
|
|
|
/// module, the set of visible items.
|
2018-12-19 22:00:54 +00:00
|
|
|
// FIXME: currenty we compute item map per source-root. We should do it per crate instead.
|
2018-11-28 00:42:26 +00:00
|
|
|
#[derive(Default, Debug, PartialEq, Eq)]
|
2018-11-28 01:09:44 +00:00
|
|
|
pub struct ItemMap {
|
|
|
|
pub per_module: FxHashMap<ModuleId, ModuleScope>,
|
2018-11-28 00:42:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, Default, PartialEq, Eq, Clone)]
|
2018-11-28 01:09:44 +00:00
|
|
|
pub struct ModuleScope {
|
2018-12-27 17:07:21 +00:00
|
|
|
items: FxHashMap<Name, Resolution>,
|
2018-11-28 00:42:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl ModuleScope {
|
2018-12-27 17:07:21 +00:00
|
|
|
pub fn entries<'a>(&'a self) -> impl Iterator<Item = (&'a Name, &'a Resolution)> + 'a {
|
2018-11-28 00:42:26 +00:00
|
|
|
self.items.iter()
|
|
|
|
}
|
2018-12-27 17:07:21 +00:00
|
|
|
pub fn get(&self, name: &Name) -> Option<&Resolution> {
|
2018-11-28 00:42:26 +00:00
|
|
|
self.items.get(name)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-08 23:47:12 +00:00
|
|
|
/// `Resolution` is basically `DefId` atm, but it should account for stuff like
|
2018-11-28 00:42:26 +00:00
|
|
|
/// multiple namespaces, ambiguity and errors.
|
|
|
|
#[derive(Debug, Clone, PartialEq, Eq)]
|
2018-11-28 01:09:44 +00:00
|
|
|
pub struct Resolution {
|
2018-11-28 00:42:26 +00:00
|
|
|
/// None for unresolved
|
2018-12-24 19:32:39 +00:00
|
|
|
pub def_id: PerNs<DefId>,
|
2018-11-28 00:42:26 +00:00
|
|
|
/// ident by whitch this is imported into local scope.
|
2019-01-18 13:56:02 +00:00
|
|
|
pub import: Option<ImportId>,
|
2018-11-28 00:42:26 +00:00
|
|
|
}
|
|
|
|
|
2018-12-24 19:32:39 +00:00
|
|
|
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
|
|
|
|
pub enum Namespace {
|
|
|
|
Types,
|
|
|
|
Values,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
|
|
|
|
pub struct PerNs<T> {
|
|
|
|
pub types: Option<T>,
|
|
|
|
pub values: Option<T>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T> PerNs<T> {
|
|
|
|
pub fn none() -> PerNs<T> {
|
|
|
|
PerNs {
|
|
|
|
types: None,
|
|
|
|
values: None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn values(t: T) -> PerNs<T> {
|
|
|
|
PerNs {
|
|
|
|
types: None,
|
|
|
|
values: Some(t),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn types(t: T) -> PerNs<T> {
|
|
|
|
PerNs {
|
|
|
|
types: Some(t),
|
|
|
|
values: None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn both(types: T, values: T) -> PerNs<T> {
|
|
|
|
PerNs {
|
|
|
|
types: Some(types),
|
|
|
|
values: Some(values),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn is_none(&self) -> bool {
|
|
|
|
self.types.is_none() && self.values.is_none()
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn take(self, namespace: Namespace) -> Option<T> {
|
|
|
|
match namespace {
|
|
|
|
Namespace::Types => self.types,
|
|
|
|
Namespace::Values => self.values,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn take_types(self) -> Option<T> {
|
2019-01-06 11:05:03 +00:00
|
|
|
self.take(Namespace::Types)
|
2018-12-24 19:32:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn take_values(self) -> Option<T> {
|
2019-01-06 11:05:03 +00:00
|
|
|
self.take(Namespace::Values)
|
2018-12-24 19:32:39 +00:00
|
|
|
}
|
2018-11-28 00:42:26 +00:00
|
|
|
|
2018-12-24 19:32:39 +00:00
|
|
|
pub fn get(&self, namespace: Namespace) -> Option<&T> {
|
|
|
|
self.as_ref().take(namespace)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn as_ref(&self) -> PerNs<&T> {
|
|
|
|
PerNs {
|
|
|
|
types: self.types.as_ref(),
|
|
|
|
values: self.values.as_ref(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn and_then<U>(self, f: impl Fn(T) -> Option<U>) -> PerNs<U> {
|
|
|
|
PerNs {
|
|
|
|
types: self.types.and_then(&f),
|
|
|
|
values: self.values.and_then(&f),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn map<U>(self, f: impl Fn(T) -> U) -> PerNs<U> {
|
|
|
|
PerNs {
|
|
|
|
types: self.types.map(&f),
|
|
|
|
values: self.values.map(&f),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-11-28 00:42:26 +00:00
|
|
|
|
|
|
|
pub(crate) struct Resolver<'a, DB> {
|
2018-12-09 09:24:52 +00:00
|
|
|
db: &'a DB,
|
2019-01-18 13:36:56 +00:00
|
|
|
input: &'a FxHashMap<ModuleId, Arc<LoweredModule>>,
|
2018-12-09 09:24:52 +00:00
|
|
|
source_root: SourceRootId,
|
|
|
|
module_tree: Arc<ModuleTree>,
|
2019-01-18 13:56:02 +00:00
|
|
|
processed_imports: FxHashSet<(ModuleId, ImportId)>,
|
2018-12-09 09:24:52 +00:00
|
|
|
result: ItemMap,
|
2018-11-28 00:42:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a, DB> Resolver<'a, DB>
|
|
|
|
where
|
|
|
|
DB: HirDatabase,
|
|
|
|
{
|
2018-12-09 09:24:52 +00:00
|
|
|
pub(crate) fn new(
|
|
|
|
db: &'a DB,
|
2019-01-18 13:36:56 +00:00
|
|
|
input: &'a FxHashMap<ModuleId, Arc<LoweredModule>>,
|
2018-12-09 09:24:52 +00:00
|
|
|
source_root: SourceRootId,
|
|
|
|
module_tree: Arc<ModuleTree>,
|
|
|
|
) -> Resolver<'a, DB> {
|
|
|
|
Resolver {
|
2018-12-09 09:45:47 +00:00
|
|
|
db,
|
|
|
|
input,
|
2018-12-09 09:24:52 +00:00
|
|
|
source_root,
|
|
|
|
module_tree,
|
2019-01-07 18:40:31 +00:00
|
|
|
processed_imports: FxHashSet::default(),
|
2018-12-09 09:24:52 +00:00
|
|
|
result: ItemMap::default(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-15 16:15:01 +00:00
|
|
|
pub(crate) fn resolve(mut self) -> ItemMap {
|
2018-11-28 00:42:26 +00:00
|
|
|
for (&module_id, items) in self.input.iter() {
|
2019-01-15 16:15:01 +00:00
|
|
|
self.populate_module(module_id, Arc::clone(items));
|
2018-11-28 00:42:26 +00:00
|
|
|
}
|
|
|
|
|
2019-01-07 18:40:31 +00:00
|
|
|
loop {
|
|
|
|
let processed_imports_count = self.processed_imports.len();
|
|
|
|
for &module_id in self.input.keys() {
|
2019-01-15 12:45:48 +00:00
|
|
|
self.db.check_canceled();
|
2019-01-15 16:15:01 +00:00
|
|
|
self.resolve_imports(module_id);
|
2019-01-07 18:40:31 +00:00
|
|
|
}
|
|
|
|
if processed_imports_count == self.processed_imports.len() {
|
|
|
|
// no new imports resolved
|
|
|
|
break;
|
|
|
|
}
|
2018-11-28 00:42:26 +00:00
|
|
|
}
|
2019-01-15 16:15:01 +00:00
|
|
|
self.result
|
2018-11-28 00:42:26 +00:00
|
|
|
}
|
|
|
|
|
2019-01-18 13:36:56 +00:00
|
|
|
fn populate_module(&mut self, module_id: ModuleId, input: Arc<LoweredModule>) {
|
2018-11-28 00:42:26 +00:00
|
|
|
let mut module_items = ModuleScope::default();
|
|
|
|
|
2018-12-09 10:49:54 +00:00
|
|
|
// Populate extern crates prelude
|
|
|
|
{
|
|
|
|
let root_id = module_id.crate_root(&self.module_tree);
|
2019-01-06 16:58:10 +00:00
|
|
|
let file_id = root_id.source(&self.module_tree).file_id;
|
2018-12-09 10:49:54 +00:00
|
|
|
let crate_graph = self.db.crate_graph();
|
2019-01-01 20:21:16 +00:00
|
|
|
if let Some(crate_id) = crate_graph.crate_id_for_crate_root(file_id.as_original_file())
|
|
|
|
{
|
2018-12-09 10:49:54 +00:00
|
|
|
let krate = Crate::new(crate_id);
|
2019-01-15 15:33:26 +00:00
|
|
|
for dep in krate.dependencies(self.db) {
|
|
|
|
if let Some(module) = dep.krate.root_module(self.db) {
|
2019-01-04 22:37:40 +00:00
|
|
|
let def_id = module.def_id;
|
2018-12-27 17:07:21 +00:00
|
|
|
self.add_module_item(
|
|
|
|
&mut module_items,
|
|
|
|
dep.name.clone(),
|
|
|
|
PerNs::types(def_id),
|
|
|
|
);
|
2018-12-09 10:49:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
2019-01-18 13:36:56 +00:00
|
|
|
for (import_id, import_data) in input.imports.iter() {
|
2019-01-12 23:19:20 +00:00
|
|
|
if let Some(segment) = import_data.path.segments.iter().last() {
|
2019-01-18 13:36:56 +00:00
|
|
|
if !import_data.is_glob {
|
2018-11-28 00:42:26 +00:00
|
|
|
module_items.items.insert(
|
2019-01-12 23:19:20 +00:00
|
|
|
segment.name.clone(),
|
2018-11-28 00:42:26 +00:00
|
|
|
Resolution {
|
2018-12-24 19:32:39 +00:00
|
|
|
def_id: PerNs::none(),
|
2019-01-18 13:36:56 +00:00
|
|
|
import: Some(import_id),
|
2018-11-28 00:42:26 +00:00
|
|
|
},
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-12-23 16:13:11 +00:00
|
|
|
// Populate explicitly declared items, except modules
|
2018-11-28 00:42:26 +00:00
|
|
|
for item in input.items.iter() {
|
|
|
|
if item.kind == MODULE {
|
|
|
|
continue;
|
|
|
|
}
|
2018-12-24 19:32:39 +00:00
|
|
|
// depending on the item kind, the location can define something in
|
|
|
|
// the values namespace, the types namespace, or both
|
|
|
|
let kind = DefKind::for_syntax_kind(item.kind);
|
|
|
|
let def_id = kind.map(|k| {
|
|
|
|
let def_loc = DefLoc {
|
|
|
|
kind: k,
|
|
|
|
source_root_id: self.source_root,
|
|
|
|
module_id,
|
2019-01-01 18:17:52 +00:00
|
|
|
source_item_id: item.id,
|
2018-12-24 19:32:39 +00:00
|
|
|
};
|
|
|
|
def_loc.id(self.db)
|
|
|
|
});
|
2018-11-28 00:42:26 +00:00
|
|
|
let resolution = Resolution {
|
2018-12-24 19:32:39 +00:00
|
|
|
def_id,
|
2018-11-28 00:42:26 +00:00
|
|
|
import: None,
|
|
|
|
};
|
|
|
|
module_items.items.insert(item.name.clone(), resolution);
|
|
|
|
}
|
|
|
|
|
2018-12-09 10:49:54 +00:00
|
|
|
// Populate modules
|
2018-12-04 20:01:53 +00:00
|
|
|
for (name, module_id) in module_id.children(&self.module_tree) {
|
2018-12-19 15:04:17 +00:00
|
|
|
let def_loc = DefLoc {
|
|
|
|
kind: DefKind::Module,
|
|
|
|
source_root_id: self.source_root,
|
|
|
|
module_id,
|
2019-01-06 16:58:10 +00:00
|
|
|
source_item_id: module_id.source(&self.module_tree),
|
2018-12-19 15:04:17 +00:00
|
|
|
};
|
|
|
|
let def_id = def_loc.id(self.db);
|
2018-12-24 19:32:39 +00:00
|
|
|
self.add_module_item(&mut module_items, name, PerNs::types(def_id));
|
2018-11-28 00:42:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
self.result.per_module.insert(module_id, module_items);
|
2018-12-09 10:49:54 +00:00
|
|
|
}
|
|
|
|
|
2018-12-27 17:07:21 +00:00
|
|
|
fn add_module_item(&self, module_items: &mut ModuleScope, name: Name, def_id: PerNs<DefId>) {
|
2018-12-09 10:49:54 +00:00
|
|
|
let resolution = Resolution {
|
2018-12-24 19:32:39 +00:00
|
|
|
def_id,
|
2018-12-09 10:49:54 +00:00
|
|
|
import: None,
|
|
|
|
};
|
|
|
|
module_items.items.insert(name, resolution);
|
2018-11-28 00:42:26 +00:00
|
|
|
}
|
|
|
|
|
2019-01-15 16:15:01 +00:00
|
|
|
fn resolve_imports(&mut self, module_id: ModuleId) {
|
2019-01-18 13:36:56 +00:00
|
|
|
for (import_id, import_data) in self.input[&module_id].imports.iter() {
|
|
|
|
if self.processed_imports.contains(&(module_id, import_id)) {
|
2019-01-07 18:40:31 +00:00
|
|
|
// already done
|
|
|
|
continue;
|
|
|
|
}
|
2019-01-18 13:36:56 +00:00
|
|
|
if self.resolve_import(module_id, import_id, import_data) {
|
|
|
|
log::debug!("import {:?} resolved (or definite error)", import_id);
|
|
|
|
self.processed_imports.insert((module_id, import_id));
|
2019-01-07 18:40:31 +00:00
|
|
|
}
|
2018-11-28 00:42:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-18 13:36:56 +00:00
|
|
|
fn resolve_import(
|
|
|
|
&mut self,
|
|
|
|
module_id: ModuleId,
|
2019-01-18 13:56:02 +00:00
|
|
|
import_id: ImportId,
|
2019-01-18 13:36:56 +00:00
|
|
|
import: &ImportData,
|
|
|
|
) -> bool {
|
2019-01-08 13:40:41 +00:00
|
|
|
log::debug!("resolving import: {:?}", import);
|
2019-01-18 13:36:56 +00:00
|
|
|
if import.is_glob {
|
|
|
|
return false;
|
2018-11-28 00:42:26 +00:00
|
|
|
};
|
|
|
|
|
2018-12-19 15:04:17 +00:00
|
|
|
let mut curr: ModuleId = match import.path.kind {
|
2018-12-09 10:49:54 +00:00
|
|
|
PathKind::Plain | PathKind::Self_ => module_id,
|
2018-11-28 00:42:26 +00:00
|
|
|
PathKind::Super => {
|
|
|
|
match module_id.parent(&self.module_tree) {
|
|
|
|
Some(it) => it,
|
2019-01-08 13:40:41 +00:00
|
|
|
None => {
|
|
|
|
// TODO: error
|
|
|
|
log::debug!("super path in root module");
|
2019-01-15 16:15:01 +00:00
|
|
|
return true; // this can't suddenly resolve if we just resolve some other imports
|
2019-01-08 13:40:41 +00:00
|
|
|
}
|
2018-11-28 00:42:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
PathKind::Crate => module_id.crate_root(&self.module_tree),
|
|
|
|
};
|
|
|
|
|
2019-01-12 23:19:20 +00:00
|
|
|
for (i, segment) in import.path.segments.iter().enumerate() {
|
2018-11-28 00:42:26 +00:00
|
|
|
let is_last = i == import.path.segments.len() - 1;
|
|
|
|
|
2019-01-12 23:19:20 +00:00
|
|
|
let def_id = match self.result.per_module[&curr].items.get(&segment.name) {
|
2018-12-24 19:32:39 +00:00
|
|
|
Some(res) if !res.def_id.is_none() => res.def_id,
|
2019-01-08 13:40:41 +00:00
|
|
|
_ => {
|
2019-01-12 23:19:20 +00:00
|
|
|
log::debug!("path segment {:?} not found", segment.name);
|
2019-01-15 16:15:01 +00:00
|
|
|
return false;
|
2019-01-08 13:40:41 +00:00
|
|
|
}
|
2018-11-28 00:42:26 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
if !is_last {
|
2018-12-24 19:32:39 +00:00
|
|
|
let type_def_id = if let Some(d) = def_id.take(Namespace::Types) {
|
|
|
|
d
|
|
|
|
} else {
|
2019-01-08 13:40:41 +00:00
|
|
|
log::debug!(
|
|
|
|
"path segment {:?} resolved to value only, but is not last",
|
2019-01-12 23:19:20 +00:00
|
|
|
segment.name
|
2019-01-08 13:40:41 +00:00
|
|
|
);
|
2019-01-15 16:15:01 +00:00
|
|
|
return false;
|
2018-12-24 19:32:39 +00:00
|
|
|
};
|
|
|
|
curr = match type_def_id.loc(self.db) {
|
2018-12-04 20:01:53 +00:00
|
|
|
DefLoc {
|
|
|
|
kind: DefKind::Module,
|
2018-12-19 21:57:13 +00:00
|
|
|
module_id: target_module_id,
|
2018-12-19 15:04:17 +00:00
|
|
|
source_root_id,
|
2018-12-04 20:01:53 +00:00
|
|
|
..
|
2018-12-19 15:04:17 +00:00
|
|
|
} => {
|
|
|
|
if source_root_id == self.source_root {
|
2018-12-19 21:57:13 +00:00
|
|
|
target_module_id
|
2018-12-19 15:04:17 +00:00
|
|
|
} else {
|
2019-01-06 12:16:21 +00:00
|
|
|
let module = crate::code_model_api::Module::new(type_def_id);
|
2018-12-19 21:57:13 +00:00
|
|
|
let path = Path {
|
|
|
|
segments: import.path.segments[i + 1..].iter().cloned().collect(),
|
|
|
|
kind: PathKind::Crate,
|
|
|
|
};
|
2019-01-08 13:40:41 +00:00
|
|
|
log::debug!("resolving {:?} in other source root", path);
|
2019-01-15 16:15:01 +00:00
|
|
|
let def_id = module.resolve_path(self.db, &path);
|
2018-12-24 19:32:39 +00:00
|
|
|
if !def_id.is_none() {
|
2019-01-12 23:19:20 +00:00
|
|
|
let last_segment = path.segments.last().unwrap();
|
2018-12-19 21:57:13 +00:00
|
|
|
self.update(module_id, |items| {
|
|
|
|
let res = Resolution {
|
2019-01-08 13:40:41 +00:00
|
|
|
def_id,
|
2019-01-18 13:36:56 +00:00
|
|
|
import: Some(import_id),
|
2018-12-19 21:57:13 +00:00
|
|
|
};
|
2019-01-12 23:19:20 +00:00
|
|
|
items.items.insert(last_segment.name.clone(), res);
|
2019-01-07 18:40:31 +00:00
|
|
|
});
|
2019-01-08 13:40:41 +00:00
|
|
|
log::debug!(
|
|
|
|
"resolved import {:?} ({:?}) cross-source root to {:?}",
|
2019-01-12 23:19:20 +00:00
|
|
|
last_segment.name,
|
2019-01-08 13:40:41 +00:00
|
|
|
import,
|
|
|
|
def_id.map(|did| did.loc(self.db))
|
|
|
|
);
|
2019-01-15 16:15:01 +00:00
|
|
|
return true;
|
2019-01-07 18:40:31 +00:00
|
|
|
} else {
|
2019-01-08 13:40:41 +00:00
|
|
|
log::debug!("rest of path did not resolve in other source root");
|
2019-01-15 16:15:01 +00:00
|
|
|
return true;
|
2018-12-19 21:57:13 +00:00
|
|
|
}
|
2018-12-19 15:04:17 +00:00
|
|
|
}
|
|
|
|
}
|
2019-01-08 13:40:41 +00:00
|
|
|
_ => {
|
|
|
|
log::debug!(
|
|
|
|
"path segment {:?} resolved to non-module {:?}, but is not last",
|
2019-01-12 23:19:20 +00:00
|
|
|
segment.name,
|
2019-01-08 13:40:41 +00:00
|
|
|
type_def_id.loc(self.db)
|
|
|
|
);
|
2019-01-15 16:15:01 +00:00
|
|
|
return true; // this resolved to a non-module, so the path won't ever resolve
|
2019-01-08 13:40:41 +00:00
|
|
|
}
|
2018-11-28 00:42:26 +00:00
|
|
|
}
|
|
|
|
} else {
|
2019-01-08 13:40:41 +00:00
|
|
|
log::debug!(
|
|
|
|
"resolved import {:?} ({:?}) within source root to {:?}",
|
2019-01-12 23:19:20 +00:00
|
|
|
segment.name,
|
2019-01-08 13:40:41 +00:00
|
|
|
import,
|
|
|
|
def_id.map(|did| did.loc(self.db))
|
|
|
|
);
|
2018-11-28 00:42:26 +00:00
|
|
|
self.update(module_id, |items| {
|
|
|
|
let res = Resolution {
|
2019-01-08 13:40:41 +00:00
|
|
|
def_id,
|
2019-01-18 13:36:56 +00:00
|
|
|
import: Some(import_id),
|
2018-11-28 00:42:26 +00:00
|
|
|
};
|
2019-01-12 23:19:20 +00:00
|
|
|
items.items.insert(segment.name.clone(), res);
|
2018-11-28 00:42:26 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2019-01-15 16:15:01 +00:00
|
|
|
true
|
2018-11-28 00:42:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn update(&mut self, module_id: ModuleId, f: impl FnOnce(&mut ModuleScope)) {
|
|
|
|
let module_items = self.result.per_module.get_mut(&module_id).unwrap();
|
|
|
|
f(module_items)
|
|
|
|
}
|
|
|
|
}
|
2018-11-28 13:19:01 +00:00
|
|
|
|
|
|
|
#[cfg(test)]
|
2018-12-09 09:48:55 +00:00
|
|
|
mod tests;
|