This commit is contained in:
Aleksey Kladov 2020-01-15 17:44:12 +01:00
parent c0661ce744
commit 263401bf75
3 changed files with 14 additions and 17 deletions

View file

@ -35,7 +35,7 @@ fn find_path_inner(
let def_map = db.crate_def_map(from.krate); let def_map = db.crate_def_map(from.krate);
let from_scope: &crate::item_scope::ItemScope = &def_map.modules[from.local_id].scope; let from_scope: &crate::item_scope::ItemScope = &def_map.modules[from.local_id].scope;
if let Some((name, _)) = from_scope.name_of(item) { if let Some((name, _)) = from_scope.name_of(item) {
return Some(ModPath::from_simple_segments(PathKind::Plain, vec![name.clone()])); return Some(ModPath::from_segments(PathKind::Plain, vec![name.clone()]));
} }
// - if the item is the crate root, return `crate` // - if the item is the crate root, return `crate`
@ -45,12 +45,12 @@ fn find_path_inner(
local_id: def_map.root, local_id: def_map.root,
})) }))
{ {
return Some(ModPath::from_simple_segments(PathKind::Crate, Vec::new())); return Some(ModPath::from_segments(PathKind::Crate, Vec::new()));
} }
// - if the item is the module we're in, use `self` // - if the item is the module we're in, use `self`
if item == ItemInNs::Types(from.into()) { if item == ItemInNs::Types(from.into()) {
return Some(ModPath::from_simple_segments(PathKind::Super(0), Vec::new())); return Some(ModPath::from_segments(PathKind::Super(0), Vec::new()));
} }
// - if the item is the parent module, use `super` (this is not used recursively, since `super::super` is ugly) // - if the item is the parent module, use `super` (this is not used recursively, since `super::super` is ugly)
@ -61,14 +61,14 @@ fn find_path_inner(
local_id: parent_id, local_id: parent_id,
})) }))
{ {
return Some(ModPath::from_simple_segments(PathKind::Super(1), Vec::new())); return Some(ModPath::from_segments(PathKind::Super(1), Vec::new()));
} }
} }
// - if the item is the crate root of a dependency crate, return the name from the extern prelude // - if the item is the crate root of a dependency crate, return the name from the extern prelude
for (name, def_id) in &def_map.extern_prelude { for (name, def_id) in &def_map.extern_prelude {
if item == ItemInNs::Types(*def_id) { if item == ItemInNs::Types(*def_id) {
return Some(ModPath::from_simple_segments(PathKind::Plain, vec![name.clone()])); return Some(ModPath::from_segments(PathKind::Plain, vec![name.clone()]));
} }
} }
@ -79,7 +79,7 @@ fn find_path_inner(
&prelude_def_map.modules[prelude_module.local_id].scope; &prelude_def_map.modules[prelude_module.local_id].scope;
if let Some((name, vis)) = prelude_scope.name_of(item) { if let Some((name, vis)) = prelude_scope.name_of(item) {
if vis.is_visible_from(db, from) { if vis.is_visible_from(db, from) {
return Some(ModPath::from_simple_segments(PathKind::Plain, vec![name.clone()])); return Some(ModPath::from_segments(PathKind::Plain, vec![name.clone()]));
} }
} }
} }

View file

@ -39,10 +39,7 @@ impl ModPath {
lower::lower_path(path, hygiene).map(|it| it.mod_path) lower::lower_path(path, hygiene).map(|it| it.mod_path)
} }
pub fn from_simple_segments( pub fn from_segments(kind: PathKind, segments: impl IntoIterator<Item = Name>) -> ModPath {
kind: PathKind,
segments: impl IntoIterator<Item = Name>,
) -> ModPath {
let segments = segments.into_iter().collect::<Vec<_>>(); let segments = segments.into_iter().collect::<Vec<_>>();
ModPath { kind, segments } ModPath { kind, segments }
} }
@ -240,7 +237,7 @@ impl From<Name> for Path {
fn from(name: Name) -> Path { fn from(name: Name) -> Path {
Path { Path {
type_anchor: None, type_anchor: None,
mod_path: ModPath::from_simple_segments(PathKind::Plain, iter::once(name)), mod_path: ModPath::from_segments(PathKind::Plain, iter::once(name)),
generic_args: vec![None], generic_args: vec![None],
} }
} }
@ -248,7 +245,7 @@ impl From<Name> for Path {
impl From<Name> for ModPath { impl From<Name> for ModPath {
fn from(name: Name) -> ModPath { fn from(name: Name) -> ModPath {
ModPath::from_simple_segments(PathKind::Plain, iter::once(name)) ModPath::from_segments(PathKind::Plain, iter::once(name))
} }
} }
@ -311,7 +308,7 @@ macro_rules! __known_path {
macro_rules! __path { macro_rules! __path {
($start:ident $(:: $seg:ident)*) => ({ ($start:ident $(:: $seg:ident)*) => ({
$crate::__known_path!($start $(:: $seg)*); $crate::__known_path!($start $(:: $seg)*);
$crate::path::ModPath::from_simple_segments($crate::path::PathKind::Abs, vec![ $crate::path::ModPath::from_segments($crate::path::PathKind::Abs, vec![
$crate::path::__name![$start], $($crate::path::__name![$seg],)* $crate::path::__name![$start], $($crate::path::__name![$seg],)*
]) ])
}); });

View file

@ -84,7 +84,7 @@ fn convert_path(prefix: Option<ModPath>, path: ast::Path, hygiene: &Hygiene) ->
res res
} }
Either::Right(crate_id) => { Either::Right(crate_id) => {
return Some(ModPath::from_simple_segments( return Some(ModPath::from_segments(
PathKind::DollarCrate(crate_id), PathKind::DollarCrate(crate_id),
iter::empty(), iter::empty(),
)) ))
@ -95,19 +95,19 @@ fn convert_path(prefix: Option<ModPath>, path: ast::Path, hygiene: &Hygiene) ->
if prefix.is_some() { if prefix.is_some() {
return None; return None;
} }
ModPath::from_simple_segments(PathKind::Crate, iter::empty()) ModPath::from_segments(PathKind::Crate, iter::empty())
} }
ast::PathSegmentKind::SelfKw => { ast::PathSegmentKind::SelfKw => {
if prefix.is_some() { if prefix.is_some() {
return None; return None;
} }
ModPath::from_simple_segments(PathKind::Super(0), iter::empty()) ModPath::from_segments(PathKind::Super(0), iter::empty())
} }
ast::PathSegmentKind::SuperKw => { ast::PathSegmentKind::SuperKw => {
if prefix.is_some() { if prefix.is_some() {
return None; return None;
} }
ModPath::from_simple_segments(PathKind::Super(1), iter::empty()) ModPath::from_segments(PathKind::Super(1), iter::empty())
} }
ast::PathSegmentKind::Type { .. } => { ast::PathSegmentKind::Type { .. } => {
// not allowed in imports // not allowed in imports