2019-12-30 07:25:19 -06:00
|
|
|
//! An algorithm to find a path to refer to a certain item.
|
|
|
|
|
2021-03-01 12:36:34 -06:00
|
|
|
use std::iter;
|
|
|
|
|
2020-05-19 09:43:26 -05:00
|
|
|
use hir_expand::name::{known, AsName, Name};
|
2020-06-05 06:15:16 -05:00
|
|
|
use rustc_hash::FxHashSet;
|
2020-05-19 09:43:26 -05:00
|
|
|
|
2019-12-30 10:31:15 -06:00
|
|
|
use crate::{
|
|
|
|
db::DefDatabase,
|
|
|
|
item_scope::ItemInNs,
|
2021-05-12 19:17:41 -05:00
|
|
|
nameres::DefMap,
|
2019-12-30 10:31:15 -06:00
|
|
|
path::{ModPath, PathKind},
|
2019-12-30 15:51:37 -06:00
|
|
|
visibility::Visibility,
|
2020-06-04 12:30:29 -05:00
|
|
|
ModuleDefId, ModuleId,
|
2019-12-30 10:31:15 -06:00
|
|
|
};
|
2019-12-30 07:25:19 -06:00
|
|
|
|
2020-05-19 09:46:33 -05:00
|
|
|
/// Find a path that can be used to refer to a certain item. This can depend on
|
|
|
|
/// *from where* you're referring to the item, hence the `from` parameter.
|
|
|
|
pub fn find_path(db: &dyn DefDatabase, item: ItemInNs, from: ModuleId) -> Option<ModPath> {
|
2020-08-12 09:32:36 -05:00
|
|
|
let _p = profile::span("find_path");
|
2021-03-21 16:45:00 -05:00
|
|
|
let mut visited_modules = FxHashSet::default();
|
|
|
|
find_path_inner(db, item, from, MAX_PATH_LEN, None, &mut visited_modules)
|
2020-09-25 08:19:22 -05:00
|
|
|
}
|
|
|
|
|
2020-10-05 10:12:37 -05:00
|
|
|
pub fn find_path_prefixed(
|
|
|
|
db: &dyn DefDatabase,
|
|
|
|
item: ItemInNs,
|
|
|
|
from: ModuleId,
|
|
|
|
prefix_kind: PrefixKind,
|
|
|
|
) -> Option<ModPath> {
|
|
|
|
let _p = profile::span("find_path_prefixed");
|
2021-03-21 16:45:00 -05:00
|
|
|
let mut visited_modules = FxHashSet::default();
|
|
|
|
find_path_inner(db, item, from, MAX_PATH_LEN, Some(prefix_kind), &mut visited_modules)
|
2020-05-19 09:46:33 -05:00
|
|
|
}
|
|
|
|
|
2019-12-30 16:08:40 -06:00
|
|
|
const MAX_PATH_LEN: usize = 15;
|
|
|
|
|
2020-01-28 09:19:41 -06:00
|
|
|
impl ModPath {
|
|
|
|
fn starts_with_std(&self) -> bool {
|
2021-02-04 13:49:24 -06:00
|
|
|
self.segments().first() == Some(&known::std)
|
2020-01-28 09:19:41 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// When std library is present, paths starting with `std::`
|
|
|
|
// should be preferred over paths starting with `core::` and `alloc::`
|
2020-04-25 09:24:44 -05:00
|
|
|
fn can_start_with_std(&self) -> bool {
|
2021-02-04 13:49:24 -06:00
|
|
|
let first_segment = self.segments().first();
|
2020-05-19 09:45:57 -05:00
|
|
|
first_segment == Some(&known::alloc) || first_segment == Some(&known::core)
|
2020-01-28 09:19:41 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-18 13:18:05 -06:00
|
|
|
fn check_self_super(def_map: &DefMap, item: ItemInNs, from: ModuleId) -> Option<ModPath> {
|
2020-10-05 10:12:37 -05:00
|
|
|
if item == ItemInNs::Types(from.into()) {
|
2020-09-25 08:19:22 -05:00
|
|
|
// - if the item is the module we're in, use `self`
|
|
|
|
Some(ModPath::from_segments(PathKind::Super(0), Vec::new()))
|
2021-01-20 08:41:18 -06:00
|
|
|
} else if let Some(parent_id) = def_map[from.local_id].parent {
|
2020-10-05 10:12:37 -05:00
|
|
|
// - if the item is the parent module, use `super` (this is not used recursively, since `super::super` is ugly)
|
2021-01-25 08:21:33 -06:00
|
|
|
let parent_id = def_map.module_id(parent_id);
|
|
|
|
if item == ItemInNs::Types(ModuleDefId::ModuleId(parent_id)) {
|
2020-10-05 10:12:37 -05:00
|
|
|
Some(ModPath::from_segments(PathKind::Super(1), Vec::new()))
|
|
|
|
} else {
|
|
|
|
None
|
2020-09-25 08:19:22 -05:00
|
|
|
}
|
2020-10-05 10:12:37 -05:00
|
|
|
} else {
|
2020-09-25 08:19:22 -05:00
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-05 10:12:37 -05:00
|
|
|
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
|
|
|
|
pub enum PrefixKind {
|
|
|
|
/// Causes paths to always start with either `self`, `super`, `crate` or a crate-name.
|
|
|
|
/// This is the same as plain, just that paths will start with `self` iprepended f the path
|
|
|
|
/// starts with an identifier that is not a crate.
|
2020-09-25 08:19:22 -05:00
|
|
|
BySelf,
|
2020-10-05 10:12:37 -05:00
|
|
|
/// Causes paths to ignore imports in the local module.
|
2020-09-25 08:19:22 -05:00
|
|
|
Plain,
|
2020-10-05 10:12:37 -05:00
|
|
|
/// Causes paths to start with `crate` where applicable, effectively forcing paths to be absolute.
|
|
|
|
ByCrate,
|
2020-09-25 08:19:22 -05:00
|
|
|
}
|
|
|
|
|
2020-10-05 10:12:37 -05:00
|
|
|
impl PrefixKind {
|
2020-09-25 08:19:22 -05:00
|
|
|
#[inline]
|
2020-10-05 10:12:37 -05:00
|
|
|
fn prefix(self) -> PathKind {
|
2020-09-25 08:19:22 -05:00
|
|
|
match self {
|
2020-10-05 10:12:37 -05:00
|
|
|
PrefixKind::BySelf => PathKind::Super(0),
|
|
|
|
PrefixKind::Plain => PathKind::Plain,
|
|
|
|
PrefixKind::ByCrate => PathKind::Crate,
|
2020-09-25 08:19:22 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
2020-10-05 10:12:37 -05:00
|
|
|
fn is_absolute(&self) -> bool {
|
|
|
|
self == &PrefixKind::ByCrate
|
2020-09-25 08:19:22 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-05 06:11:53 -05:00
|
|
|
fn find_path_inner(
|
2020-03-13 10:05:46 -05:00
|
|
|
db: &dyn DefDatabase,
|
2019-12-30 16:08:40 -06:00
|
|
|
item: ItemInNs,
|
|
|
|
from: ModuleId,
|
|
|
|
max_len: usize,
|
2021-03-01 12:36:34 -06:00
|
|
|
mut prefixed: Option<PrefixKind>,
|
2021-03-21 16:45:00 -05:00
|
|
|
visited_modules: &mut FxHashSet<ModuleId>,
|
2019-12-30 16:08:40 -06:00
|
|
|
) -> Option<ModPath> {
|
2019-12-30 16:07:47 -06:00
|
|
|
if max_len == 0 {
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
|
2019-12-30 16:08:40 -06:00
|
|
|
// Base cases:
|
|
|
|
|
2019-12-30 14:18:43 -06:00
|
|
|
// - if the item is already in scope, return the name under which it is
|
2021-01-22 09:31:40 -06:00
|
|
|
let def_map = from.def_map(db);
|
2021-02-09 10:25:03 -06:00
|
|
|
let scope_name = def_map.with_ancestor_maps(db, from.local_id, &mut |def_map, local_id| {
|
|
|
|
def_map[local_id].scope.name_of(item).map(|(name, _)| name.clone())
|
|
|
|
});
|
2020-10-05 10:12:37 -05:00
|
|
|
if prefixed.is_none() && scope_name.is_some() {
|
2020-09-25 08:19:22 -05:00
|
|
|
return scope_name
|
|
|
|
.map(|scope_name| ModPath::from_segments(PathKind::Plain, vec![scope_name]));
|
2019-12-30 14:18:43 -06:00
|
|
|
}
|
|
|
|
|
2020-10-05 10:12:37 -05:00
|
|
|
// - if the item is the crate root, return `crate`
|
2021-03-01 12:36:34 -06:00
|
|
|
let root = def_map.crate_root(db);
|
2021-04-19 12:50:11 -05:00
|
|
|
if item == ItemInNs::Types(ModuleDefId::ModuleId(root)) {
|
2020-10-05 10:12:37 -05:00
|
|
|
return Some(ModPath::from_segments(PathKind::Crate, Vec::new()));
|
|
|
|
}
|
|
|
|
|
|
|
|
if prefixed.filter(PrefixKind::is_absolute).is_none() {
|
|
|
|
if let modpath @ Some(_) = check_self_super(&def_map, item, from) {
|
|
|
|
return modpath;
|
|
|
|
}
|
2019-12-30 15:40:50 -06:00
|
|
|
}
|
|
|
|
|
2019-12-30 14:18:43 -06:00
|
|
|
// - if the item is the crate root of a dependency crate, return the name from the extern prelude
|
2021-04-30 11:41:18 -05:00
|
|
|
let root_def_map = root.def_map(db);
|
|
|
|
for (name, def_id) in root_def_map.extern_prelude() {
|
2019-12-30 14:18:43 -06:00
|
|
|
if item == ItemInNs::Types(*def_id) {
|
2020-09-25 08:19:22 -05:00
|
|
|
let name = scope_name.unwrap_or_else(|| name.clone());
|
2021-05-12 19:17:41 -05:00
|
|
|
|
|
|
|
let name_already_occupied_in_type_ns = def_map
|
|
|
|
.with_ancestor_maps(db, from.local_id, &mut |def_map, local_id| {
|
|
|
|
def_map[local_id].scope.get(&name).take_types().filter(|&id| id != *def_id)
|
|
|
|
})
|
|
|
|
.is_some();
|
|
|
|
return Some(ModPath::from_segments(
|
|
|
|
if name_already_occupied_in_type_ns { PathKind::Abs } else { PathKind::Plain },
|
|
|
|
vec![name],
|
|
|
|
));
|
2019-12-30 14:18:43 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// - if the item is in the prelude, return the name from there
|
2021-04-30 11:41:18 -05:00
|
|
|
if let Some(prelude_module) = root_def_map.prelude() {
|
|
|
|
// Preludes in block DefMaps are ignored, only the crate DefMap is searched
|
2021-01-22 09:31:40 -06:00
|
|
|
let prelude_def_map = prelude_module.def_map(db);
|
2019-12-30 15:51:37 -06:00
|
|
|
let prelude_scope: &crate::item_scope::ItemScope =
|
2021-01-20 08:41:18 -06:00
|
|
|
&prelude_def_map[prelude_module.local_id].scope;
|
2020-01-10 11:40:45 -06:00
|
|
|
if let Some((name, vis)) = prelude_scope.name_of(item) {
|
2019-12-30 14:33:25 -06:00
|
|
|
if vis.is_visible_from(db, from) {
|
2020-01-15 10:44:12 -06:00
|
|
|
return Some(ModPath::from_segments(PathKind::Plain, vec![name.clone()]));
|
2019-12-30 14:33:25 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-12-30 14:18:43 -06:00
|
|
|
|
2020-04-18 04:38:58 -05:00
|
|
|
// - if the item is a builtin, it's in scope
|
|
|
|
if let ItemInNs::Types(ModuleDefId::BuiltinType(builtin)) = item {
|
|
|
|
return Some(ModPath::from_segments(PathKind::Plain, vec![builtin.as_name()]));
|
|
|
|
}
|
|
|
|
|
2019-12-30 14:18:43 -06:00
|
|
|
// Recursive case:
|
|
|
|
// - if the item is an enum variant, refer to it via the enum
|
2019-12-30 14:33:25 -06:00
|
|
|
if let Some(ModuleDefId::EnumVariantId(variant)) = item.as_module_def_id() {
|
|
|
|
if let Some(mut path) = find_path(db, ItemInNs::Types(variant.parent.into()), from) {
|
|
|
|
let data = db.enum_data(variant.parent);
|
2021-02-04 13:49:24 -06:00
|
|
|
path.push_segment(data.variants[variant.local_id].name.clone());
|
2019-12-30 14:33:25 -06:00
|
|
|
return Some(path);
|
|
|
|
}
|
|
|
|
// If this doesn't work, it seems we have no way of referring to the
|
|
|
|
// enum; that's very weird, but there might still be a reexport of the
|
|
|
|
// variant somewhere
|
|
|
|
}
|
2019-12-30 14:18:43 -06:00
|
|
|
|
|
|
|
// - otherwise, look for modules containing (reexporting) it and import it from one of those
|
2020-06-04 12:30:29 -05:00
|
|
|
|
2021-03-01 12:36:34 -06:00
|
|
|
let crate_root = def_map.crate_root(db);
|
2020-04-25 09:24:44 -05:00
|
|
|
let crate_attrs = db.attrs(crate_root.into());
|
|
|
|
let prefer_no_std = crate_attrs.by_key("no_std").exists();
|
2019-12-30 16:07:47 -06:00
|
|
|
let mut best_path = None;
|
|
|
|
let mut best_path_len = max_len;
|
2020-01-27 16:09:56 -06:00
|
|
|
|
2020-06-05 06:05:19 -05:00
|
|
|
if item.krate(db) == Some(from.krate) {
|
2020-06-04 12:30:29 -05:00
|
|
|
// Item was defined in the same crate that wants to import it. It cannot be found in any
|
|
|
|
// dependency in this case.
|
2021-03-21 16:45:00 -05:00
|
|
|
for (module_id, name) in find_local_import_locations(db, item, from) {
|
|
|
|
if !visited_modules.insert(module_id) {
|
2021-03-22 08:15:53 -05:00
|
|
|
cov_mark::hit!(recursive_imports);
|
2021-03-21 16:45:00 -05:00
|
|
|
continue;
|
|
|
|
}
|
2020-06-04 12:30:29 -05:00
|
|
|
if let Some(mut path) = find_path_inner(
|
|
|
|
db,
|
|
|
|
ItemInNs::Types(ModuleDefId::ModuleId(module_id)),
|
|
|
|
from,
|
|
|
|
best_path_len - 1,
|
2020-09-25 08:19:22 -05:00
|
|
|
prefixed,
|
2021-03-21 16:45:00 -05:00
|
|
|
visited_modules,
|
2020-06-04 12:30:29 -05:00
|
|
|
) {
|
2021-02-04 13:49:24 -06:00
|
|
|
path.push_segment(name);
|
2020-06-04 12:30:29 -05:00
|
|
|
|
|
|
|
let new_path = if let Some(best_path) = best_path {
|
|
|
|
select_best_path(best_path, path, prefer_no_std)
|
|
|
|
} else {
|
|
|
|
path
|
|
|
|
};
|
|
|
|
best_path_len = new_path.len();
|
|
|
|
best_path = Some(new_path);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Item was defined in some upstream crate. This means that it must be exported from one,
|
|
|
|
// too (unless we can't name it at all). It could *also* be (re)exported by the same crate
|
|
|
|
// that wants to import it here, but we always prefer to use the external path here.
|
|
|
|
|
|
|
|
let crate_graph = db.crate_graph();
|
|
|
|
let extern_paths = crate_graph[from.krate].dependencies.iter().filter_map(|dep| {
|
|
|
|
let import_map = db.import_map(dep.crate_id);
|
2020-06-12 06:01:20 -05:00
|
|
|
import_map.import_info_for(item).and_then(|info| {
|
|
|
|
// Determine best path for containing module and append last segment from `info`.
|
|
|
|
let mut path = find_path_inner(
|
|
|
|
db,
|
|
|
|
ItemInNs::Types(ModuleDefId::ModuleId(info.container)),
|
|
|
|
from,
|
|
|
|
best_path_len - 1,
|
2020-09-25 08:19:22 -05:00
|
|
|
prefixed,
|
2021-03-21 16:45:00 -05:00
|
|
|
visited_modules,
|
2020-06-12 06:01:20 -05:00
|
|
|
)?;
|
2021-03-08 14:19:44 -06:00
|
|
|
cov_mark::hit!(partially_imported);
|
2021-02-04 13:49:24 -06:00
|
|
|
path.push_segment(info.path.segments.last().unwrap().clone());
|
2020-06-12 06:01:20 -05:00
|
|
|
Some(path)
|
2020-06-04 12:30:29 -05:00
|
|
|
})
|
|
|
|
});
|
|
|
|
|
|
|
|
for path in extern_paths {
|
|
|
|
let new_path = if let Some(best_path) = best_path {
|
|
|
|
select_best_path(best_path, path, prefer_no_std)
|
|
|
|
} else {
|
|
|
|
path
|
|
|
|
};
|
|
|
|
best_path = Some(new_path);
|
|
|
|
}
|
2019-12-30 16:07:47 -06:00
|
|
|
}
|
2020-06-04 12:30:29 -05:00
|
|
|
|
2021-03-01 12:36:34 -06:00
|
|
|
// If the item is declared inside a block expression, don't use a prefix, as we don't handle
|
|
|
|
// that correctly (FIXME).
|
|
|
|
if let Some(item_module) = item.as_module_def_id().and_then(|did| did.module(db)) {
|
|
|
|
if item_module.def_map(db).block_id().is_some() && prefixed.is_some() {
|
2021-03-08 14:19:44 -06:00
|
|
|
cov_mark::hit!(prefixed_in_block_expression);
|
2021-03-01 12:36:34 -06:00
|
|
|
prefixed = Some(PrefixKind::Plain);
|
2021-02-09 10:25:03 -06:00
|
|
|
}
|
2021-03-01 12:36:34 -06:00
|
|
|
}
|
2021-02-09 10:25:03 -06:00
|
|
|
|
2021-03-01 12:36:34 -06:00
|
|
|
if let Some(prefix) = prefixed.map(PrefixKind::prefix) {
|
2020-09-25 08:19:22 -05:00
|
|
|
best_path.or_else(|| {
|
|
|
|
scope_name.map(|scope_name| ModPath::from_segments(prefix, vec![scope_name]))
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
best_path
|
|
|
|
}
|
2019-12-30 16:07:47 -06:00
|
|
|
}
|
|
|
|
|
2020-04-25 09:24:44 -05:00
|
|
|
fn select_best_path(old_path: ModPath, new_path: ModPath, prefer_no_std: bool) -> ModPath {
|
|
|
|
if old_path.starts_with_std() && new_path.can_start_with_std() {
|
|
|
|
if prefer_no_std {
|
2021-03-08 14:19:44 -06:00
|
|
|
cov_mark::hit!(prefer_no_std_paths);
|
2020-04-25 09:24:44 -05:00
|
|
|
new_path
|
|
|
|
} else {
|
2021-03-08 14:19:44 -06:00
|
|
|
cov_mark::hit!(prefer_std_paths);
|
2020-04-25 09:24:44 -05:00
|
|
|
old_path
|
|
|
|
}
|
|
|
|
} else if new_path.starts_with_std() && old_path.can_start_with_std() {
|
|
|
|
if prefer_no_std {
|
2021-03-08 14:19:44 -06:00
|
|
|
cov_mark::hit!(prefer_no_std_paths);
|
2020-04-25 09:24:44 -05:00
|
|
|
old_path
|
|
|
|
} else {
|
2021-03-08 14:19:44 -06:00
|
|
|
cov_mark::hit!(prefer_std_paths);
|
2020-04-25 09:24:44 -05:00
|
|
|
new_path
|
|
|
|
}
|
2020-01-28 09:19:41 -06:00
|
|
|
} else if new_path.len() < old_path.len() {
|
|
|
|
new_path
|
|
|
|
} else {
|
|
|
|
old_path
|
2020-01-27 16:09:56 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-04 12:30:29 -05:00
|
|
|
/// Finds locations in `from.krate` from which `item` can be imported by `from`.
|
|
|
|
fn find_local_import_locations(
|
2020-03-13 10:05:46 -05:00
|
|
|
db: &dyn DefDatabase,
|
2019-12-30 15:51:37 -06:00
|
|
|
item: ItemInNs,
|
|
|
|
from: ModuleId,
|
|
|
|
) -> Vec<(ModuleId, Name)> {
|
2020-08-12 09:32:36 -05:00
|
|
|
let _p = profile::span("find_local_import_locations");
|
2020-06-04 12:30:29 -05:00
|
|
|
|
|
|
|
// `from` can import anything below `from` with visibility of at least `from`, and anything
|
|
|
|
// above `from` with any visibility. That means we do not need to descend into private siblings
|
|
|
|
// of `from` (and similar).
|
|
|
|
|
2021-01-22 09:31:40 -06:00
|
|
|
let def_map = from.def_map(db);
|
2020-06-04 12:30:29 -05:00
|
|
|
|
|
|
|
// Compute the initial worklist. We start with all direct child modules of `from` as well as all
|
|
|
|
// of its (recursive) parent modules.
|
2021-01-20 08:41:18 -06:00
|
|
|
let data = &def_map[from.local_id];
|
2021-01-25 08:21:33 -06:00
|
|
|
let mut worklist =
|
|
|
|
data.children.values().map(|child| def_map.module_id(*child)).collect::<Vec<_>>();
|
2021-04-19 12:50:11 -05:00
|
|
|
// FIXME: do we need to traverse out of block expressions here?
|
2021-03-01 12:36:34 -06:00
|
|
|
for ancestor in iter::successors(from.containing_module(db), |m| m.containing_module(db)) {
|
|
|
|
worklist.push(ancestor);
|
2019-12-30 15:51:37 -06:00
|
|
|
}
|
|
|
|
|
2021-03-01 12:36:34 -06:00
|
|
|
let def_map = def_map.crate_root(db).def_map(db);
|
|
|
|
|
2020-06-04 12:30:29 -05:00
|
|
|
let mut seen: FxHashSet<_> = FxHashSet::default();
|
|
|
|
|
|
|
|
let mut locations = Vec::new();
|
|
|
|
while let Some(module) = worklist.pop() {
|
|
|
|
if !seen.insert(module) {
|
|
|
|
continue; // already processed this module
|
|
|
|
}
|
|
|
|
|
|
|
|
let ext_def_map;
|
|
|
|
let data = if module.krate == from.krate {
|
2021-03-01 12:36:34 -06:00
|
|
|
if module.block.is_some() {
|
|
|
|
// Re-query the block's DefMap
|
|
|
|
ext_def_map = module.def_map(db);
|
|
|
|
&ext_def_map[module.local_id]
|
|
|
|
} else {
|
|
|
|
// Reuse the root DefMap
|
|
|
|
&def_map[module.local_id]
|
|
|
|
}
|
2020-06-04 12:30:29 -05:00
|
|
|
} else {
|
|
|
|
// The crate might reexport a module defined in another crate.
|
2021-01-22 09:31:40 -06:00
|
|
|
ext_def_map = module.def_map(db);
|
2020-06-04 12:30:29 -05:00
|
|
|
&ext_def_map[module.local_id]
|
|
|
|
};
|
|
|
|
|
2020-01-10 11:40:45 -06:00
|
|
|
if let Some((name, vis)) = data.scope.name_of(item) {
|
2020-06-04 12:30:29 -05:00
|
|
|
if vis.is_visible_from(db, from) {
|
|
|
|
let is_private = if let Visibility::Module(private_to) = vis {
|
|
|
|
private_to.local_id == module.local_id
|
|
|
|
} else {
|
|
|
|
false
|
|
|
|
};
|
|
|
|
let is_original_def = if let Some(module_def_id) = item.as_module_def_id() {
|
|
|
|
data.scope.declarations().any(|it| it == module_def_id)
|
|
|
|
} else {
|
|
|
|
false
|
|
|
|
};
|
|
|
|
|
2019-12-30 15:51:37 -06:00
|
|
|
// Ignore private imports. these could be used if we are
|
|
|
|
// in a submodule of this module, but that's usually not
|
|
|
|
// what the user wants; and if this module can import
|
|
|
|
// the item and we're a submodule of it, so can we.
|
|
|
|
// Also this keeps the cached data smaller.
|
2020-06-04 12:30:29 -05:00
|
|
|
if !is_private || is_original_def {
|
|
|
|
locations.push((module, name.clone()));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Descend into all modules visible from `from`.
|
|
|
|
for (_, per_ns) in data.scope.entries() {
|
|
|
|
if let Some((ModuleDefId::ModuleId(module), vis)) = per_ns.take_types_vis() {
|
|
|
|
if vis.is_visible_from(db, from) {
|
|
|
|
worklist.push(module);
|
|
|
|
}
|
2019-12-30 14:18:43 -06:00
|
|
|
}
|
|
|
|
}
|
2019-12-30 10:31:15 -06:00
|
|
|
}
|
2020-05-18 14:42:39 -05:00
|
|
|
|
2020-06-04 12:30:29 -05:00
|
|
|
locations
|
2019-12-30 07:25:19 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
2020-08-13 09:25:38 -05:00
|
|
|
use base_db::fixture::WithFixture;
|
2019-12-30 07:25:19 -06:00
|
|
|
use hir_expand::hygiene::Hygiene;
|
2020-08-12 11:26:51 -05:00
|
|
|
use syntax::ast::AstNode;
|
2020-05-20 05:59:20 -05:00
|
|
|
|
|
|
|
use crate::test_db::TestDB;
|
|
|
|
|
|
|
|
use super::*;
|
2019-12-30 07:25:19 -06:00
|
|
|
|
|
|
|
/// `code` needs to contain a cursor marker; checks that `find_path` for the
|
|
|
|
/// item the `path` refers to returns that same path when called from the
|
|
|
|
/// module the cursor is in.
|
2020-10-05 10:12:37 -05:00
|
|
|
fn check_found_path_(ra_fixture: &str, path: &str, prefix_kind: Option<PrefixKind>) {
|
2020-06-12 06:01:20 -05:00
|
|
|
let (db, pos) = TestDB::with_position(ra_fixture);
|
2021-02-09 10:25:03 -06:00
|
|
|
let module = db.module_at_position(pos);
|
2020-08-12 11:26:51 -05:00
|
|
|
let parsed_path_file = syntax::SourceFile::parse(&format!("use {};", path));
|
|
|
|
let ast_path =
|
|
|
|
parsed_path_file.syntax_node().descendants().find_map(syntax::ast::Path::cast).unwrap();
|
2021-05-06 12:59:54 -05:00
|
|
|
let mod_path = ModPath::from_src(&db, ast_path, &Hygiene::new_unhygienic()).unwrap();
|
2019-12-30 07:25:19 -06:00
|
|
|
|
2021-02-09 10:25:03 -06:00
|
|
|
let def_map = module.def_map(&db);
|
|
|
|
let resolved = def_map
|
2019-12-30 10:31:15 -06:00
|
|
|
.resolve_path(
|
|
|
|
&db,
|
|
|
|
module.local_id,
|
|
|
|
&mod_path,
|
|
|
|
crate::item_scope::BuiltinShadowMode::Module,
|
|
|
|
)
|
|
|
|
.0
|
|
|
|
.take_types()
|
|
|
|
.unwrap();
|
2019-12-30 07:25:19 -06:00
|
|
|
|
2021-03-21 16:45:00 -05:00
|
|
|
let mut visited_modules = FxHashSet::default();
|
|
|
|
let found_path = find_path_inner(
|
|
|
|
&db,
|
|
|
|
ItemInNs::Types(resolved),
|
|
|
|
module,
|
|
|
|
MAX_PATH_LEN,
|
|
|
|
prefix_kind,
|
|
|
|
&mut visited_modules,
|
|
|
|
);
|
2020-10-05 10:12:37 -05:00
|
|
|
assert_eq!(found_path, Some(mod_path), "{:?}", prefix_kind);
|
2020-09-25 08:19:22 -05:00
|
|
|
}
|
|
|
|
|
2020-10-05 10:12:37 -05:00
|
|
|
fn check_found_path(
|
|
|
|
ra_fixture: &str,
|
|
|
|
unprefixed: &str,
|
|
|
|
prefixed: &str,
|
|
|
|
absolute: &str,
|
|
|
|
self_prefixed: &str,
|
|
|
|
) {
|
|
|
|
check_found_path_(ra_fixture, unprefixed, None);
|
|
|
|
check_found_path_(ra_fixture, prefixed, Some(PrefixKind::Plain));
|
|
|
|
check_found_path_(ra_fixture, absolute, Some(PrefixKind::ByCrate));
|
|
|
|
check_found_path_(ra_fixture, self_prefixed, Some(PrefixKind::BySelf));
|
2019-12-30 07:25:19 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn same_module() {
|
2021-04-15 11:32:19 -05:00
|
|
|
check_found_path(
|
|
|
|
r#"
|
|
|
|
struct S;
|
|
|
|
$0
|
|
|
|
"#,
|
|
|
|
"S",
|
|
|
|
"S",
|
|
|
|
"crate::S",
|
|
|
|
"self::S",
|
|
|
|
);
|
2019-12-30 07:25:19 -06:00
|
|
|
}
|
2019-12-30 10:31:15 -06:00
|
|
|
|
2019-12-30 14:33:25 -06:00
|
|
|
#[test]
|
|
|
|
fn enum_variant() {
|
2021-04-15 11:32:19 -05:00
|
|
|
check_found_path(
|
|
|
|
r#"
|
|
|
|
enum E { A }
|
|
|
|
$0
|
|
|
|
"#,
|
|
|
|
"E::A",
|
|
|
|
"E::A",
|
|
|
|
"E::A",
|
|
|
|
"E::A",
|
|
|
|
);
|
2019-12-30 14:33:25 -06:00
|
|
|
}
|
|
|
|
|
2019-12-30 10:31:15 -06:00
|
|
|
#[test]
|
|
|
|
fn sub_module() {
|
2021-04-15 11:32:19 -05:00
|
|
|
check_found_path(
|
|
|
|
r#"
|
|
|
|
mod foo {
|
|
|
|
pub struct S;
|
|
|
|
}
|
|
|
|
$0
|
|
|
|
"#,
|
|
|
|
"foo::S",
|
|
|
|
"foo::S",
|
|
|
|
"crate::foo::S",
|
|
|
|
"self::foo::S",
|
|
|
|
);
|
2019-12-30 10:31:15 -06:00
|
|
|
}
|
|
|
|
|
2019-12-30 15:40:50 -06:00
|
|
|
#[test]
|
|
|
|
fn super_module() {
|
2021-04-15 11:32:19 -05:00
|
|
|
check_found_path(
|
|
|
|
r#"
|
|
|
|
//- /main.rs
|
|
|
|
mod foo;
|
|
|
|
//- /foo.rs
|
|
|
|
mod bar;
|
|
|
|
struct S;
|
|
|
|
//- /foo/bar.rs
|
|
|
|
$0
|
|
|
|
"#,
|
|
|
|
"super::S",
|
|
|
|
"super::S",
|
|
|
|
"crate::foo::S",
|
|
|
|
"super::S",
|
|
|
|
);
|
2019-12-30 15:40:50 -06:00
|
|
|
}
|
|
|
|
|
2019-12-31 06:01:00 -06:00
|
|
|
#[test]
|
|
|
|
fn self_module() {
|
2021-04-15 11:32:19 -05:00
|
|
|
check_found_path(
|
|
|
|
r#"
|
|
|
|
//- /main.rs
|
|
|
|
mod foo;
|
|
|
|
//- /foo.rs
|
|
|
|
$0
|
|
|
|
"#,
|
|
|
|
"self",
|
|
|
|
"self",
|
|
|
|
"crate::foo",
|
|
|
|
"self",
|
|
|
|
);
|
2019-12-31 06:01:00 -06:00
|
|
|
}
|
|
|
|
|
2019-12-30 14:18:43 -06:00
|
|
|
#[test]
|
|
|
|
fn crate_root() {
|
2021-04-15 11:32:19 -05:00
|
|
|
check_found_path(
|
|
|
|
r#"
|
|
|
|
//- /main.rs
|
|
|
|
mod foo;
|
|
|
|
//- /foo.rs
|
|
|
|
$0
|
|
|
|
"#,
|
|
|
|
"crate",
|
|
|
|
"crate",
|
|
|
|
"crate",
|
|
|
|
"crate",
|
|
|
|
);
|
2019-12-30 14:18:43 -06:00
|
|
|
}
|
|
|
|
|
2019-12-30 10:31:15 -06:00
|
|
|
#[test]
|
|
|
|
fn same_crate() {
|
2021-04-15 11:32:19 -05:00
|
|
|
check_found_path(
|
|
|
|
r#"
|
|
|
|
//- /main.rs
|
|
|
|
mod foo;
|
|
|
|
struct S;
|
|
|
|
//- /foo.rs
|
|
|
|
$0
|
|
|
|
"#,
|
|
|
|
"crate::S",
|
|
|
|
"crate::S",
|
|
|
|
"crate::S",
|
|
|
|
"crate::S",
|
|
|
|
);
|
2019-12-30 10:31:15 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn different_crate() {
|
2021-04-15 11:32:19 -05:00
|
|
|
check_found_path(
|
|
|
|
r#"
|
|
|
|
//- /main.rs crate:main deps:std
|
|
|
|
$0
|
|
|
|
//- /std.rs crate:std
|
|
|
|
pub struct S;
|
|
|
|
"#,
|
|
|
|
"std::S",
|
|
|
|
"std::S",
|
|
|
|
"std::S",
|
|
|
|
"std::S",
|
|
|
|
);
|
2019-12-30 10:31:15 -06:00
|
|
|
}
|
|
|
|
|
2019-12-30 14:18:43 -06:00
|
|
|
#[test]
|
|
|
|
fn different_crate_renamed() {
|
2020-10-05 10:12:37 -05:00
|
|
|
check_found_path(
|
2021-04-15 11:32:19 -05:00
|
|
|
r#"
|
|
|
|
//- /main.rs crate:main deps:std
|
|
|
|
extern crate std as std_renamed;
|
|
|
|
$0
|
|
|
|
//- /std.rs crate:std
|
|
|
|
pub struct S;
|
|
|
|
"#,
|
2020-10-05 10:12:37 -05:00
|
|
|
"std_renamed::S",
|
|
|
|
"std_renamed::S",
|
|
|
|
"std_renamed::S",
|
|
|
|
"std_renamed::S",
|
|
|
|
);
|
2020-06-12 06:01:20 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn partially_imported() {
|
2021-03-08 14:19:44 -06:00
|
|
|
cov_mark::check!(partially_imported);
|
2020-06-12 06:01:20 -05:00
|
|
|
// Tests that short paths are used even for external items, when parts of the path are
|
|
|
|
// already in scope.
|
2021-04-15 11:32:19 -05:00
|
|
|
check_found_path(
|
|
|
|
r#"
|
|
|
|
//- /main.rs crate:main deps:syntax
|
2020-06-12 06:01:20 -05:00
|
|
|
|
2021-04-15 11:32:19 -05:00
|
|
|
use syntax::ast;
|
|
|
|
$0
|
2020-06-12 06:01:20 -05:00
|
|
|
|
2021-04-15 11:32:19 -05:00
|
|
|
//- /lib.rs crate:syntax
|
|
|
|
pub mod ast {
|
|
|
|
pub enum ModuleItem {
|
|
|
|
A, B, C,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"#,
|
2020-10-05 10:12:37 -05:00
|
|
|
"ast::ModuleItem",
|
|
|
|
"syntax::ast::ModuleItem",
|
|
|
|
"syntax::ast::ModuleItem",
|
|
|
|
"syntax::ast::ModuleItem",
|
|
|
|
);
|
2020-06-12 06:01:20 -05:00
|
|
|
|
2020-10-05 10:12:37 -05:00
|
|
|
check_found_path(
|
2021-04-15 11:32:19 -05:00
|
|
|
r#"
|
|
|
|
//- /main.rs crate:main deps:syntax
|
|
|
|
$0
|
|
|
|
|
|
|
|
//- /lib.rs crate:syntax
|
|
|
|
pub mod ast {
|
|
|
|
pub enum ModuleItem {
|
|
|
|
A, B, C,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"#,
|
2020-10-05 10:12:37 -05:00
|
|
|
"syntax::ast::ModuleItem",
|
|
|
|
"syntax::ast::ModuleItem",
|
|
|
|
"syntax::ast::ModuleItem",
|
|
|
|
"syntax::ast::ModuleItem",
|
|
|
|
);
|
2019-12-30 14:18:43 -06:00
|
|
|
}
|
|
|
|
|
2019-12-30 10:31:15 -06:00
|
|
|
#[test]
|
|
|
|
fn same_crate_reexport() {
|
2021-04-15 11:32:19 -05:00
|
|
|
check_found_path(
|
|
|
|
r#"
|
|
|
|
mod bar {
|
|
|
|
mod foo { pub(super) struct S; }
|
|
|
|
pub(crate) use foo::*;
|
|
|
|
}
|
|
|
|
$0
|
|
|
|
"#,
|
|
|
|
"bar::S",
|
|
|
|
"bar::S",
|
|
|
|
"crate::bar::S",
|
|
|
|
"self::bar::S",
|
|
|
|
);
|
2019-12-30 10:31:15 -06:00
|
|
|
}
|
2019-12-30 14:18:43 -06:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn same_crate_reexport_rename() {
|
2021-04-15 11:32:19 -05:00
|
|
|
check_found_path(
|
|
|
|
r#"
|
|
|
|
mod bar {
|
|
|
|
mod foo { pub(super) struct S; }
|
|
|
|
pub(crate) use foo::S as U;
|
|
|
|
}
|
|
|
|
$0
|
|
|
|
"#,
|
|
|
|
"bar::U",
|
|
|
|
"bar::U",
|
|
|
|
"crate::bar::U",
|
|
|
|
"self::bar::U",
|
|
|
|
);
|
2019-12-30 14:18:43 -06:00
|
|
|
}
|
|
|
|
|
2019-12-30 14:33:25 -06:00
|
|
|
#[test]
|
|
|
|
fn different_crate_reexport() {
|
2021-04-15 11:32:19 -05:00
|
|
|
check_found_path(
|
|
|
|
r#"
|
|
|
|
//- /main.rs crate:main deps:std
|
|
|
|
$0
|
|
|
|
//- /std.rs crate:std deps:core
|
|
|
|
pub use core::S;
|
|
|
|
//- /core.rs crate:core
|
|
|
|
pub struct S;
|
|
|
|
"#,
|
|
|
|
"std::S",
|
|
|
|
"std::S",
|
|
|
|
"std::S",
|
|
|
|
"std::S",
|
|
|
|
);
|
2019-12-30 14:33:25 -06:00
|
|
|
}
|
|
|
|
|
2019-12-30 14:18:43 -06:00
|
|
|
#[test]
|
|
|
|
fn prelude() {
|
2021-04-15 11:32:19 -05:00
|
|
|
check_found_path(
|
|
|
|
r#"
|
|
|
|
//- /main.rs crate:main deps:std
|
|
|
|
$0
|
|
|
|
//- /std.rs crate:std
|
|
|
|
pub mod prelude { pub struct S; }
|
|
|
|
#[prelude_import]
|
|
|
|
pub use prelude::*;
|
|
|
|
"#,
|
|
|
|
"S",
|
|
|
|
"S",
|
|
|
|
"S",
|
|
|
|
"S",
|
|
|
|
);
|
2019-12-30 14:18:43 -06:00
|
|
|
}
|
2019-12-30 14:33:25 -06:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn enum_variant_from_prelude() {
|
|
|
|
let code = r#"
|
2021-04-15 11:32:19 -05:00
|
|
|
//- /main.rs crate:main deps:std
|
|
|
|
$0
|
|
|
|
//- /std.rs crate:std
|
|
|
|
pub mod prelude {
|
|
|
|
pub enum Option<T> { Some(T), None }
|
|
|
|
pub use Option::*;
|
|
|
|
}
|
|
|
|
#[prelude_import]
|
|
|
|
pub use prelude::*;
|
2019-12-30 14:33:25 -06:00
|
|
|
"#;
|
2020-10-05 10:12:37 -05:00
|
|
|
check_found_path(code, "None", "None", "None", "None");
|
|
|
|
check_found_path(code, "Some", "Some", "Some", "Some");
|
2019-12-30 14:33:25 -06:00
|
|
|
}
|
2019-12-30 15:22:12 -06:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn shortest_path() {
|
2021-04-15 11:32:19 -05:00
|
|
|
check_found_path(
|
|
|
|
r#"
|
|
|
|
//- /main.rs
|
|
|
|
pub mod foo;
|
|
|
|
pub mod baz;
|
|
|
|
struct S;
|
|
|
|
$0
|
|
|
|
//- /foo.rs
|
|
|
|
pub mod bar { pub struct S; }
|
|
|
|
//- /baz.rs
|
|
|
|
pub use crate::foo::bar::S;
|
|
|
|
"#,
|
|
|
|
"baz::S",
|
|
|
|
"baz::S",
|
|
|
|
"crate::baz::S",
|
|
|
|
"self::baz::S",
|
|
|
|
);
|
2019-12-30 15:22:12 -06:00
|
|
|
}
|
2019-12-30 15:40:50 -06:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn discount_private_imports() {
|
2021-04-15 11:32:19 -05:00
|
|
|
check_found_path(
|
|
|
|
r#"
|
|
|
|
//- /main.rs
|
|
|
|
mod foo;
|
|
|
|
pub mod bar { pub struct S; }
|
|
|
|
use bar::S;
|
|
|
|
//- /foo.rs
|
|
|
|
$0
|
|
|
|
"#,
|
|
|
|
// crate::S would be shorter, but using private imports seems wrong
|
|
|
|
"crate::bar::S",
|
|
|
|
"crate::bar::S",
|
|
|
|
"crate::bar::S",
|
|
|
|
"crate::bar::S",
|
|
|
|
);
|
2019-12-30 15:40:50 -06:00
|
|
|
}
|
2019-12-30 16:07:47 -06:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn import_cycle() {
|
2021-04-15 11:32:19 -05:00
|
|
|
check_found_path(
|
|
|
|
r#"
|
|
|
|
//- /main.rs
|
|
|
|
pub mod foo;
|
|
|
|
pub mod bar;
|
|
|
|
pub mod baz;
|
|
|
|
//- /bar.rs
|
|
|
|
$0
|
|
|
|
//- /foo.rs
|
|
|
|
pub use super::baz;
|
|
|
|
pub struct S;
|
|
|
|
//- /baz.rs
|
|
|
|
pub use super::foo;
|
|
|
|
"#,
|
|
|
|
"crate::foo::S",
|
|
|
|
"crate::foo::S",
|
|
|
|
"crate::foo::S",
|
|
|
|
"crate::foo::S",
|
|
|
|
);
|
2019-12-30 16:07:47 -06:00
|
|
|
}
|
2020-01-28 10:03:24 -06:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn prefer_std_paths_over_alloc() {
|
2021-03-08 14:19:44 -06:00
|
|
|
cov_mark::check!(prefer_std_paths);
|
2021-04-15 11:32:19 -05:00
|
|
|
check_found_path(
|
|
|
|
r#"
|
|
|
|
//- /main.rs crate:main deps:alloc,std
|
|
|
|
$0
|
2020-01-28 10:03:24 -06:00
|
|
|
|
2021-04-15 11:32:19 -05:00
|
|
|
//- /std.rs crate:std deps:alloc
|
|
|
|
pub mod sync {
|
|
|
|
pub use alloc::sync::Arc;
|
|
|
|
}
|
2020-01-28 10:03:24 -06:00
|
|
|
|
2021-04-15 11:32:19 -05:00
|
|
|
//- /zzz.rs crate:alloc
|
|
|
|
pub mod sync {
|
|
|
|
pub struct Arc;
|
|
|
|
}
|
|
|
|
"#,
|
2020-10-05 10:12:37 -05:00
|
|
|
"std::sync::Arc",
|
|
|
|
"std::sync::Arc",
|
|
|
|
"std::sync::Arc",
|
|
|
|
"std::sync::Arc",
|
|
|
|
);
|
2020-01-28 10:03:24 -06:00
|
|
|
}
|
|
|
|
|
2020-04-25 09:24:44 -05:00
|
|
|
#[test]
|
2020-05-20 05:59:20 -05:00
|
|
|
fn prefer_core_paths_over_std() {
|
2021-03-08 14:19:44 -06:00
|
|
|
cov_mark::check!(prefer_no_std_paths);
|
2021-04-15 11:32:19 -05:00
|
|
|
check_found_path(
|
|
|
|
r#"
|
|
|
|
//- /main.rs crate:main deps:core,std
|
|
|
|
#![no_std]
|
2020-04-25 09:24:44 -05:00
|
|
|
|
2021-04-15 11:32:19 -05:00
|
|
|
$0
|
2020-04-25 09:24:44 -05:00
|
|
|
|
2021-04-15 11:32:19 -05:00
|
|
|
//- /std.rs crate:std deps:core
|
2020-04-25 09:24:44 -05:00
|
|
|
|
2021-04-15 11:32:19 -05:00
|
|
|
pub mod fmt {
|
|
|
|
pub use core::fmt::Error;
|
|
|
|
}
|
2020-04-25 09:24:44 -05:00
|
|
|
|
2021-04-15 11:32:19 -05:00
|
|
|
//- /zzz.rs crate:core
|
2020-04-25 09:24:44 -05:00
|
|
|
|
2021-04-15 11:32:19 -05:00
|
|
|
pub mod fmt {
|
|
|
|
pub struct Error;
|
|
|
|
}
|
|
|
|
"#,
|
2020-10-05 10:12:37 -05:00
|
|
|
"core::fmt::Error",
|
|
|
|
"core::fmt::Error",
|
|
|
|
"core::fmt::Error",
|
|
|
|
"core::fmt::Error",
|
|
|
|
);
|
2020-04-25 09:24:44 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2020-05-20 05:59:20 -05:00
|
|
|
fn prefer_alloc_paths_over_std() {
|
2021-04-15 11:32:19 -05:00
|
|
|
check_found_path(
|
|
|
|
r#"
|
|
|
|
//- /main.rs crate:main deps:alloc,std
|
|
|
|
#![no_std]
|
2020-04-25 09:24:44 -05:00
|
|
|
|
2021-04-15 11:32:19 -05:00
|
|
|
$0
|
2020-04-25 09:24:44 -05:00
|
|
|
|
2021-04-15 11:32:19 -05:00
|
|
|
//- /std.rs crate:std deps:alloc
|
2020-04-25 09:24:44 -05:00
|
|
|
|
2021-04-15 11:32:19 -05:00
|
|
|
pub mod sync {
|
|
|
|
pub use alloc::sync::Arc;
|
|
|
|
}
|
2020-04-25 09:24:44 -05:00
|
|
|
|
2021-04-15 11:32:19 -05:00
|
|
|
//- /zzz.rs crate:alloc
|
2020-04-25 09:24:44 -05:00
|
|
|
|
2021-04-15 11:32:19 -05:00
|
|
|
pub mod sync {
|
|
|
|
pub struct Arc;
|
|
|
|
}
|
|
|
|
"#,
|
2020-10-05 10:12:37 -05:00
|
|
|
"alloc::sync::Arc",
|
|
|
|
"alloc::sync::Arc",
|
|
|
|
"alloc::sync::Arc",
|
|
|
|
"alloc::sync::Arc",
|
|
|
|
);
|
2020-04-25 09:24:44 -05:00
|
|
|
}
|
|
|
|
|
2020-01-28 10:03:24 -06:00
|
|
|
#[test]
|
|
|
|
fn prefer_shorter_paths_if_not_alloc() {
|
2021-04-15 11:32:19 -05:00
|
|
|
check_found_path(
|
|
|
|
r#"
|
|
|
|
//- /main.rs crate:main deps:megaalloc,std
|
|
|
|
$0
|
2020-01-28 10:03:24 -06:00
|
|
|
|
2021-04-15 11:32:19 -05:00
|
|
|
//- /std.rs crate:std deps:megaalloc
|
|
|
|
pub mod sync {
|
|
|
|
pub use megaalloc::sync::Arc;
|
|
|
|
}
|
2020-01-28 10:03:24 -06:00
|
|
|
|
2021-04-15 11:32:19 -05:00
|
|
|
//- /zzz.rs crate:megaalloc
|
|
|
|
pub struct Arc;
|
|
|
|
"#,
|
2020-10-05 10:12:37 -05:00
|
|
|
"megaalloc::Arc",
|
|
|
|
"megaalloc::Arc",
|
|
|
|
"megaalloc::Arc",
|
|
|
|
"megaalloc::Arc",
|
|
|
|
);
|
2020-01-28 10:03:24 -06:00
|
|
|
}
|
2020-04-18 04:38:58 -05:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn builtins_are_in_scope() {
|
|
|
|
let code = r#"
|
2021-04-15 11:32:19 -05:00
|
|
|
$0
|
2020-04-18 04:38:58 -05:00
|
|
|
|
2021-04-15 11:32:19 -05:00
|
|
|
pub mod primitive {
|
|
|
|
pub use u8;
|
|
|
|
}
|
2020-04-18 04:38:58 -05:00
|
|
|
"#;
|
2020-10-05 10:12:37 -05:00
|
|
|
check_found_path(code, "u8", "u8", "u8", "u8");
|
|
|
|
check_found_path(code, "u16", "u16", "u16", "u16");
|
2020-04-18 04:38:58 -05:00
|
|
|
}
|
2021-02-09 10:25:03 -06:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn inner_items() {
|
|
|
|
check_found_path(
|
|
|
|
r#"
|
2021-04-15 11:32:19 -05:00
|
|
|
fn main() {
|
|
|
|
struct Inner {}
|
|
|
|
$0
|
|
|
|
}
|
2021-02-09 10:25:03 -06:00
|
|
|
"#,
|
|
|
|
"Inner",
|
|
|
|
"Inner",
|
|
|
|
"Inner",
|
|
|
|
"Inner",
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn inner_items_from_outer_scope() {
|
|
|
|
check_found_path(
|
|
|
|
r#"
|
2021-04-15 11:32:19 -05:00
|
|
|
fn main() {
|
|
|
|
struct Struct {}
|
|
|
|
{
|
|
|
|
$0
|
|
|
|
}
|
|
|
|
}
|
2021-02-09 10:25:03 -06:00
|
|
|
"#,
|
|
|
|
"Struct",
|
|
|
|
"Struct",
|
|
|
|
"Struct",
|
|
|
|
"Struct",
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn inner_items_from_inner_module() {
|
2021-03-08 14:19:44 -06:00
|
|
|
cov_mark::check!(prefixed_in_block_expression);
|
2021-02-09 10:25:03 -06:00
|
|
|
check_found_path(
|
|
|
|
r#"
|
2021-04-15 11:32:19 -05:00
|
|
|
fn main() {
|
|
|
|
mod module {
|
|
|
|
struct Struct {}
|
|
|
|
}
|
|
|
|
{
|
|
|
|
$0
|
|
|
|
}
|
|
|
|
}
|
2021-02-09 10:25:03 -06:00
|
|
|
"#,
|
|
|
|
"module::Struct",
|
|
|
|
"module::Struct",
|
|
|
|
"module::Struct",
|
|
|
|
"module::Struct",
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-03-01 12:36:34 -06:00
|
|
|
#[test]
|
|
|
|
fn outer_items_with_inner_items_present() {
|
|
|
|
check_found_path(
|
|
|
|
r#"
|
2021-04-15 11:32:19 -05:00
|
|
|
mod module {
|
|
|
|
pub struct CompleteMe;
|
|
|
|
}
|
2021-03-01 12:36:34 -06:00
|
|
|
|
2021-04-15 11:32:19 -05:00
|
|
|
fn main() {
|
|
|
|
fn inner() {}
|
|
|
|
$0
|
|
|
|
}
|
2021-03-01 12:36:34 -06:00
|
|
|
"#,
|
2021-04-19 12:50:11 -05:00
|
|
|
// FIXME: these could use fewer/better prefixes
|
2021-03-01 12:36:34 -06:00
|
|
|
"module::CompleteMe",
|
|
|
|
"crate::module::CompleteMe",
|
2021-04-19 12:50:11 -05:00
|
|
|
"crate::module::CompleteMe",
|
|
|
|
"crate::module::CompleteMe",
|
2021-03-01 12:36:34 -06:00
|
|
|
)
|
|
|
|
}
|
2021-03-21 16:45:00 -05:00
|
|
|
|
2021-04-16 20:34:05 -05:00
|
|
|
#[test]
|
|
|
|
fn from_inside_module() {
|
|
|
|
// This worked correctly, but the test suite logic was broken.
|
|
|
|
cov_mark::check!(submodule_in_testdb);
|
|
|
|
check_found_path(
|
|
|
|
r#"
|
|
|
|
mod baz {
|
|
|
|
pub struct Foo {}
|
|
|
|
}
|
|
|
|
|
|
|
|
mod bar {
|
|
|
|
fn bar() {
|
|
|
|
$0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
"crate::baz::Foo",
|
|
|
|
"crate::baz::Foo",
|
|
|
|
"crate::baz::Foo",
|
|
|
|
"crate::baz::Foo",
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2021-04-19 12:50:11 -05:00
|
|
|
#[test]
|
|
|
|
fn from_inside_module_with_inner_items() {
|
|
|
|
check_found_path(
|
|
|
|
r#"
|
|
|
|
mod baz {
|
|
|
|
pub struct Foo {}
|
|
|
|
}
|
|
|
|
|
|
|
|
mod bar {
|
|
|
|
fn bar() {
|
|
|
|
fn inner() {}
|
|
|
|
$0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
"crate::baz::Foo",
|
|
|
|
"crate::baz::Foo",
|
|
|
|
"crate::baz::Foo",
|
|
|
|
"crate::baz::Foo",
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2021-03-21 16:45:00 -05:00
|
|
|
#[test]
|
|
|
|
fn recursive_pub_mod_reexport() {
|
2021-03-22 08:15:53 -05:00
|
|
|
cov_mark::check!(recursive_imports);
|
2021-03-21 16:45:00 -05:00
|
|
|
check_found_path(
|
|
|
|
r#"
|
|
|
|
fn main() {
|
|
|
|
let _ = 22_i32.as_name$0();
|
|
|
|
}
|
|
|
|
|
|
|
|
pub mod name {
|
|
|
|
pub trait AsName {
|
|
|
|
fn as_name(&self) -> String;
|
|
|
|
}
|
|
|
|
impl AsName for i32 {
|
|
|
|
fn as_name(&self) -> String {
|
|
|
|
format!("Name: {}", self)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
pub use crate::name;
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
"name::AsName",
|
|
|
|
"name::AsName",
|
|
|
|
"crate::name::AsName",
|
|
|
|
"self::name::AsName",
|
|
|
|
);
|
|
|
|
}
|
2021-04-19 12:50:11 -05:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn extern_crate() {
|
|
|
|
check_found_path(
|
|
|
|
r#"
|
|
|
|
//- /main.rs crate:main deps:dep
|
|
|
|
$0
|
|
|
|
//- /dep.rs crate:dep
|
|
|
|
"#,
|
|
|
|
"dep",
|
|
|
|
"dep",
|
|
|
|
"dep",
|
|
|
|
"dep",
|
|
|
|
);
|
|
|
|
|
|
|
|
check_found_path(
|
|
|
|
r#"
|
|
|
|
//- /main.rs crate:main deps:dep
|
|
|
|
fn f() {
|
|
|
|
fn inner() {}
|
|
|
|
$0
|
|
|
|
}
|
|
|
|
//- /dep.rs crate:dep
|
|
|
|
"#,
|
|
|
|
"dep",
|
|
|
|
"dep",
|
|
|
|
"dep",
|
|
|
|
"dep",
|
|
|
|
);
|
|
|
|
}
|
2021-04-30 11:41:18 -05:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn prelude_with_inner_items() {
|
|
|
|
check_found_path(
|
|
|
|
r#"
|
|
|
|
//- /main.rs crate:main deps:std
|
|
|
|
fn f() {
|
|
|
|
fn inner() {}
|
|
|
|
$0
|
|
|
|
}
|
|
|
|
//- /std.rs crate:std
|
|
|
|
pub mod prelude {
|
|
|
|
pub enum Option { None }
|
|
|
|
pub use Option::*;
|
|
|
|
}
|
|
|
|
#[prelude_import]
|
|
|
|
pub use prelude::*;
|
|
|
|
"#,
|
|
|
|
"None",
|
|
|
|
"None",
|
|
|
|
"None",
|
|
|
|
"None",
|
|
|
|
);
|
|
|
|
}
|
2019-12-30 07:25:19 -06:00
|
|
|
}
|