2019-09-06 06:30:24 -05:00
|
|
|
//! This module resolves `mod foo;` declaration to file.
|
2020-12-09 10:01:15 -06:00
|
|
|
use base_db::{AnchoredPath, FileId};
|
2019-10-31 02:31:29 -05:00
|
|
|
use hir_expand::name::Name;
|
2021-07-10 15:49:17 -05:00
|
|
|
use limit::Limit;
|
2020-08-12 11:26:51 -05:00
|
|
|
use syntax::SmolStr;
|
2019-09-05 12:27:10 -05:00
|
|
|
|
2019-11-23 05:44:43 -06:00
|
|
|
use crate::{db::DefDatabase, HirFileId};
|
2019-09-05 12:27:10 -05:00
|
|
|
|
2021-07-10 15:49:17 -05:00
|
|
|
const MOD_DEPTH_LIMIT: Limit = Limit::new(32);
|
2020-11-04 08:31:35 -06:00
|
|
|
|
2019-10-10 06:45:05 -05:00
|
|
|
#[derive(Clone, Debug)]
|
2019-11-04 12:42:25 -06:00
|
|
|
pub(super) struct ModDir {
|
2020-07-08 12:09:42 -05:00
|
|
|
/// `` for `mod.rs`, `lib.rs`
|
|
|
|
/// `foo/` for `foo.rs`
|
|
|
|
/// `foo/bar/` for `mod bar { mod x; }` nested in `foo.rs`
|
|
|
|
/// Invariant: path.is_empty() || path.ends_with('/')
|
|
|
|
dir_path: DirPath,
|
2019-10-10 06:45:05 -05:00
|
|
|
/// inside `./foo.rs`, mods with `#[path]` should *not* be relative to `./foo/`
|
|
|
|
root_non_dir_owner: bool,
|
2020-11-04 08:31:35 -06:00
|
|
|
depth: u32,
|
2019-09-05 12:27:10 -05:00
|
|
|
}
|
|
|
|
|
2019-10-10 06:45:05 -05:00
|
|
|
impl ModDir {
|
2019-11-04 12:42:25 -06:00
|
|
|
pub(super) fn root() -> ModDir {
|
2020-11-04 08:31:35 -06:00
|
|
|
ModDir { dir_path: DirPath::empty(), root_non_dir_owner: false, depth: 0 }
|
|
|
|
}
|
2019-09-05 12:27:10 -05:00
|
|
|
|
2019-11-04 12:42:25 -06:00
|
|
|
pub(super) fn descend_into_definition(
|
|
|
|
&self,
|
|
|
|
name: &Name,
|
|
|
|
attr_path: Option<&SmolStr>,
|
2020-11-04 08:31:35 -06:00
|
|
|
) -> Option<ModDir> {
|
2021-11-18 14:43:54 -06:00
|
|
|
let path = match attr_path.map(SmolStr::as_str) {
|
2020-07-08 12:09:42 -05:00
|
|
|
None => {
|
|
|
|
let mut path = self.dir_path.clone();
|
2021-11-04 12:12:05 -05:00
|
|
|
path.push(&name.to_smol_str());
|
2020-07-08 12:09:42 -05:00
|
|
|
path
|
|
|
|
}
|
2019-10-10 06:45:05 -05:00
|
|
|
Some(attr_path) => {
|
2020-07-08 12:09:42 -05:00
|
|
|
let mut path = self.dir_path.join_attr(attr_path, self.root_non_dir_owner);
|
|
|
|
if !(path.is_empty() || path.ends_with('/')) {
|
|
|
|
path.push('/')
|
2019-09-05 12:27:10 -05:00
|
|
|
}
|
2020-07-08 12:09:42 -05:00
|
|
|
DirPath::new(path)
|
2019-09-05 12:27:10 -05:00
|
|
|
}
|
2020-07-08 12:09:42 -05:00
|
|
|
};
|
2020-11-04 08:31:35 -06:00
|
|
|
self.child(path, false)
|
2019-10-10 06:45:05 -05:00
|
|
|
}
|
|
|
|
|
2021-11-18 14:43:54 -06:00
|
|
|
fn child(&self, dir_path: DirPath, root_non_dir_owner: bool) -> Option<ModDir> {
|
|
|
|
let depth = self.depth + 1;
|
|
|
|
if MOD_DEPTH_LIMIT.check(depth as usize).is_err() {
|
|
|
|
tracing::error!("MOD_DEPTH_LIMIT exceeded");
|
|
|
|
cov_mark::hit!(circular_mods);
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
Some(ModDir { dir_path, root_non_dir_owner, depth })
|
|
|
|
}
|
|
|
|
|
2019-11-04 12:42:25 -06:00
|
|
|
pub(super) fn resolve_declaration(
|
2019-10-10 06:45:05 -05:00
|
|
|
&self,
|
2020-03-13 10:05:46 -05:00
|
|
|
db: &dyn DefDatabase,
|
2019-10-10 06:45:05 -05:00
|
|
|
file_id: HirFileId,
|
|
|
|
name: &Name,
|
|
|
|
attr_path: Option<&SmolStr>,
|
2020-06-11 04:04:09 -05:00
|
|
|
) -> Result<(FileId, bool, ModDir), String> {
|
2021-03-21 12:54:05 -05:00
|
|
|
let orig_file_id = file_id.original_file(db.upcast());
|
2019-10-10 06:45:05 -05:00
|
|
|
|
|
|
|
let mut candidate_files = Vec::new();
|
2020-07-08 12:09:42 -05:00
|
|
|
match attr_path {
|
2019-10-09 06:27:37 -05:00
|
|
|
Some(attr_path) => {
|
2020-07-08 12:09:42 -05:00
|
|
|
candidate_files.push(self.dir_path.join_attr(attr_path, self.root_non_dir_owner))
|
2019-09-05 12:27:10 -05:00
|
|
|
}
|
|
|
|
None => {
|
2021-03-21 12:54:05 -05:00
|
|
|
if file_id.is_include_macro(db.upcast()) {
|
2021-03-21 10:02:01 -05:00
|
|
|
candidate_files.push(format!("{}.rs", name));
|
|
|
|
candidate_files.push(format!("{}/mod.rs", name));
|
|
|
|
} else {
|
|
|
|
candidate_files.push(format!("{}{}.rs", self.dir_path.0, name));
|
|
|
|
candidate_files.push(format!("{}{}/mod.rs", self.dir_path.0, name));
|
|
|
|
}
|
2019-09-05 12:27:10 -05:00
|
|
|
}
|
2019-10-10 06:45:05 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
for candidate in candidate_files.iter() {
|
2021-03-21 12:54:05 -05:00
|
|
|
let path = AnchoredPath { anchor: orig_file_id, path: candidate.as_str() };
|
2020-12-09 10:01:15 -06:00
|
|
|
if let Some(file_id) = db.resolve_path(path) {
|
2020-12-22 10:18:45 -06:00
|
|
|
let is_mod_rs = candidate.ends_with("/mod.rs");
|
2020-07-08 12:09:42 -05:00
|
|
|
|
|
|
|
let (dir_path, root_non_dir_owner) = if is_mod_rs || attr_path.is_some() {
|
|
|
|
(DirPath::empty(), false)
|
|
|
|
} else {
|
|
|
|
(DirPath::new(format!("{}/", name)), true)
|
|
|
|
};
|
2020-11-04 08:31:35 -06:00
|
|
|
if let Some(mod_dir) = self.child(dir_path, root_non_dir_owner) {
|
|
|
|
return Ok((file_id, is_mod_rs, mod_dir));
|
|
|
|
}
|
2019-09-05 12:27:10 -05:00
|
|
|
}
|
|
|
|
}
|
2019-10-09 06:27:37 -05:00
|
|
|
Err(candidate_files.remove(0))
|
2019-10-10 06:45:05 -05:00
|
|
|
}
|
2019-09-05 12:27:10 -05:00
|
|
|
}
|
|
|
|
|
2020-07-08 12:09:42 -05:00
|
|
|
#[derive(Clone, Debug)]
|
|
|
|
struct DirPath(String);
|
|
|
|
|
|
|
|
impl DirPath {
|
|
|
|
fn assert_invariant(&self) {
|
|
|
|
assert!(self.0.is_empty() || self.0.ends_with('/'));
|
|
|
|
}
|
|
|
|
fn new(repr: String) -> DirPath {
|
|
|
|
let res = DirPath(repr);
|
|
|
|
res.assert_invariant();
|
|
|
|
res
|
|
|
|
}
|
|
|
|
fn empty() -> DirPath {
|
|
|
|
DirPath::new(String::new())
|
|
|
|
}
|
|
|
|
fn push(&mut self, name: &str) {
|
|
|
|
self.0.push_str(name);
|
|
|
|
self.0.push('/');
|
|
|
|
self.assert_invariant();
|
|
|
|
}
|
|
|
|
fn parent(&self) -> Option<&str> {
|
|
|
|
if self.0.is_empty() {
|
|
|
|
return None;
|
|
|
|
};
|
|
|
|
let idx =
|
|
|
|
self.0[..self.0.len() - '/'.len_utf8()].rfind('/').map_or(0, |it| it + '/'.len_utf8());
|
|
|
|
Some(&self.0[..idx])
|
|
|
|
}
|
|
|
|
/// So this is the case which doesn't really work I think if we try to be
|
|
|
|
/// 100% platform agnostic:
|
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
/// mod a {
|
|
|
|
/// #[path="C://sad/face"]
|
|
|
|
/// mod b { mod c; }
|
|
|
|
/// }
|
|
|
|
/// ```
|
|
|
|
///
|
|
|
|
/// Here, we need to join logical dir path to a string path from an
|
|
|
|
/// attribute. Ideally, we should somehow losslessly communicate the whole
|
|
|
|
/// construction to `FileLoader`.
|
|
|
|
fn join_attr(&self, mut attr: &str, relative_to_parent: bool) -> String {
|
|
|
|
let base = if relative_to_parent { self.parent().unwrap() } else { &self.0 };
|
|
|
|
|
|
|
|
if attr.starts_with("./") {
|
|
|
|
attr = &attr["./".len()..];
|
|
|
|
}
|
|
|
|
let tmp;
|
|
|
|
let attr = if attr.contains('\\') {
|
|
|
|
tmp = attr.replace('\\', "/");
|
|
|
|
&tmp
|
|
|
|
} else {
|
|
|
|
attr
|
|
|
|
};
|
|
|
|
let res = format!("{}{}", base, attr);
|
|
|
|
res
|
|
|
|
}
|
2019-09-05 12:27:10 -05:00
|
|
|
}
|