From 0bc7d285189caaffc13e4d6856baf895f72ed80c Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Wed, 30 Oct 2019 18:41:50 +0300 Subject: refactor $crate handling Introduce proper hygiene module, which should grow quite a bit eventually. --- crates/ra_hir_def/src/path.rs | 101 ++++++++++++++++++++---------------------- 1 file changed, 49 insertions(+), 52 deletions(-) (limited to 'crates/ra_hir_def/src/path.rs') diff --git a/crates/ra_hir_def/src/path.rs b/crates/ra_hir_def/src/path.rs index fe060437d..39f394c3f 100644 --- a/crates/ra_hir_def/src/path.rs +++ b/crates/ra_hir_def/src/path.rs @@ -2,7 +2,6 @@ use std::{iter, sync::Arc}; -use hir_expand::db::AstDatabase; use ra_db::CrateId; use ra_syntax::{ ast::{self, NameOwner, TypeAscriptionOwner}, @@ -10,6 +9,8 @@ use ra_syntax::{ }; use crate::{ + either::Either, + hygiene::Hygiene, name::{self, AsName, Name}, type_ref::TypeRef, Source, @@ -68,11 +69,11 @@ impl Path { /// Calls `cb` with all paths, represented by this use item. pub fn expand_use_item( item_src: Source, - db: &impl AstDatabase, + hygiene: &Hygiene, mut cb: impl FnMut(Path, &ast::UseTree, bool, Option), ) { if let Some(tree) = item_src.ast.use_tree() { - expand_use_tree(None, tree, &|| item_src.file_id.macro_crate(db), &mut cb); + expand_use_tree(None, tree, hygiene, &mut cb); } } @@ -89,17 +90,12 @@ impl Path { /// Converts an `ast::Path` to `Path`. Works with use trees. /// DEPRECATED: It does not handle `$crate` from macro call. pub fn from_ast(path: ast::Path) -> Option { - Path::parse(path, &|| None) + Path::from_src(path, &Hygiene::new_unhygienic()) } /// Converts an `ast::Path` to `Path`. Works with use trees. /// It correctly handles `$crate` based path from macro call. - pub fn from_src(source: Source, db: &impl AstDatabase) -> Option { - let file_id = source.file_id; - Path::parse(source.ast, &|| file_id.macro_crate(db)) - } - - fn parse(mut path: ast::Path, macro_crate: &impl Fn() -> Option) -> Option { + pub fn from_src(mut path: ast::Path, hygiene: &Hygiene) -> Option { let mut kind = PathKind::Plain; let mut segments = Vec::new(); loop { @@ -110,26 +106,28 @@ impl Path { } match segment.kind()? { - ast::PathSegmentKind::Name(name) => { - if name.text() == "$crate" { - if let Some(macro_crate) = macro_crate() { - kind = PathKind::DollarCrate(macro_crate); + ast::PathSegmentKind::Name(name_ref) => { + // FIXME: this should just return name + match hygiene.name_ref_to_name(name_ref) { + Either::A(name) => { + let args = segment + .type_arg_list() + .and_then(GenericArgs::from_ast) + .or_else(|| { + GenericArgs::from_fn_like_path_ast( + segment.param_list(), + segment.ret_type(), + ) + }) + .map(Arc::new); + let segment = PathSegment { name, args_and_bindings: args }; + segments.push(segment); + } + Either::B(crate_id) => { + kind = PathKind::DollarCrate(crate_id); break; } } - - let args = segment - .type_arg_list() - .and_then(GenericArgs::from_ast) - .or_else(|| { - GenericArgs::from_fn_like_path_ast( - segment.param_list(), - segment.ret_type(), - ) - }) - .map(Arc::new); - let segment = PathSegment { name: name.as_name(), args_and_bindings: args }; - segments.push(segment); } ast::PathSegmentKind::Type { type_ref, trait_ref } => { assert!(path.qualifier().is_none()); // this can only occur at the first segment @@ -143,7 +141,7 @@ impl Path { } // >::Foo desugars to Trait::Foo Some(trait_ref) => { - let path = Path::parse(trait_ref.path()?, macro_crate)?; + let path = Path::from_src(trait_ref.path()?, hygiene)?; kind = path.kind; let mut prefix_segments = path.segments; prefix_segments.reverse(); @@ -294,7 +292,7 @@ impl From for Path { fn expand_use_tree( prefix: Option, tree: ast::UseTree, - macro_crate: &impl Fn() -> Option, + hygiene: &Hygiene, cb: &mut impl FnMut(Path, &ast::UseTree, bool, Option), ) { if let Some(use_tree_list) = tree.use_tree_list() { @@ -303,13 +301,13 @@ fn expand_use_tree( None => prefix, // E.g. `use something::{inner}` (prefix is `None`, path is `something`) // or `use something::{path::{inner::{innerer}}}` (prefix is `something::path`, path is `inner`) - Some(path) => match convert_path(prefix, path, macro_crate) { + Some(path) => match convert_path(prefix, path, hygiene) { Some(it) => Some(it), None => return, // FIXME: report errors somewhere }, }; for child_tree in use_tree_list.use_trees() { - expand_use_tree(prefix.clone(), child_tree, macro_crate, cb); + expand_use_tree(prefix.clone(), child_tree, hygiene, cb); } } else { let alias = tree.alias().and_then(|a| a.name()).map(|a| a.as_name()); @@ -326,7 +324,7 @@ fn expand_use_tree( } } } - if let Some(path) = convert_path(prefix, ast_path, macro_crate) { + if let Some(path) = convert_path(prefix, ast_path, hygiene) { let is_glob = tree.has_star(); cb(path, &tree, is_glob, alias) } @@ -336,37 +334,36 @@ fn expand_use_tree( } } -fn convert_path( - prefix: Option, - path: ast::Path, - macro_crate: &impl Fn() -> Option, -) -> Option { +fn convert_path(prefix: Option, path: ast::Path, hygiene: &Hygiene) -> Option { let prefix = if let Some(qual) = path.qualifier() { - Some(convert_path(prefix, qual, macro_crate)?) + Some(convert_path(prefix, qual, hygiene)?) } else { prefix }; let segment = path.segment()?; let res = match segment.kind()? { - ast::PathSegmentKind::Name(name) => { - if name.text() == "$crate" { - if let Some(krate) = macro_crate() { + ast::PathSegmentKind::Name(name_ref) => { + match hygiene.name_ref_to_name(name_ref) { + Either::A(name) => { + // no type args in use + let mut res = prefix.unwrap_or_else(|| Path { + kind: PathKind::Plain, + segments: Vec::with_capacity(1), + }); + res.segments.push(PathSegment { + name, + args_and_bindings: None, // no type args in use + }); + res + } + Either::B(crate_id) => { return Some(Path::from_simple_segments( - PathKind::DollarCrate(krate), + PathKind::DollarCrate(crate_id), iter::empty(), - )); + )) } } - - // no type args in use - let mut res = prefix - .unwrap_or_else(|| Path { kind: PathKind::Plain, segments: Vec::with_capacity(1) }); - res.segments.push(PathSegment { - name: name.as_name(), - args_and_bindings: None, // no type args in use - }); - res } ast::PathSegmentKind::CrateKw => { if prefix.is_some() { -- cgit v1.2.3 From b05d6e53fb0e9a008dc2e1220b1201818e63ed2d Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Wed, 30 Oct 2019 18:50:10 +0300 Subject: push either to hir_expand --- crates/ra_hir_def/src/path.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'crates/ra_hir_def/src/path.rs') diff --git a/crates/ra_hir_def/src/path.rs b/crates/ra_hir_def/src/path.rs index 39f394c3f..8d57e7761 100644 --- a/crates/ra_hir_def/src/path.rs +++ b/crates/ra_hir_def/src/path.rs @@ -2,6 +2,7 @@ use std::{iter, sync::Arc}; +use hir_expand::either::Either; use ra_db::CrateId; use ra_syntax::{ ast::{self, NameOwner, TypeAscriptionOwner}, @@ -9,7 +10,6 @@ use ra_syntax::{ }; use crate::{ - either::Either, hygiene::Hygiene, name::{self, AsName, Name}, type_ref::TypeRef, -- cgit v1.2.3 From 872ac566bfc6cf43ac55354cf5223b962dbc1d92 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Wed, 30 Oct 2019 18:56:20 +0300 Subject: push name down to hir_expand --- crates/ra_hir_def/src/path.rs | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) (limited to 'crates/ra_hir_def/src/path.rs') diff --git a/crates/ra_hir_def/src/path.rs b/crates/ra_hir_def/src/path.rs index 8d57e7761..d0b842a6b 100644 --- a/crates/ra_hir_def/src/path.rs +++ b/crates/ra_hir_def/src/path.rs @@ -2,19 +2,17 @@ use std::{iter, sync::Arc}; -use hir_expand::either::Either; +use hir_expand::{ + either::Either, + name::{self, AsName, Name}, +}; use ra_db::CrateId; use ra_syntax::{ ast::{self, NameOwner, TypeAscriptionOwner}, AstNode, }; -use crate::{ - hygiene::Hygiene, - name::{self, AsName, Name}, - type_ref::TypeRef, - Source, -}; +use crate::{hygiene::Hygiene, type_ref::TypeRef, Source}; #[derive(Debug, Clone, PartialEq, Eq, Hash)] pub struct Path { @@ -392,8 +390,9 @@ fn convert_path(prefix: Option, path: ast::Path, hygiene: &Hygiene) -> Opt } pub mod known { + use hir_expand::name; + use super::{Path, PathKind}; - use crate::name; pub fn std_iter_into_iterator() -> Path { Path::from_simple_segments( -- cgit v1.2.3 From ab559f170ee02e3bdd9aeeb55933bb143b520c34 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Wed, 30 Oct 2019 19:10:53 +0300 Subject: move hygiene to hir_expand --- crates/ra_hir_def/src/path.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'crates/ra_hir_def/src/path.rs') diff --git a/crates/ra_hir_def/src/path.rs b/crates/ra_hir_def/src/path.rs index d0b842a6b..ddabc7ca6 100644 --- a/crates/ra_hir_def/src/path.rs +++ b/crates/ra_hir_def/src/path.rs @@ -4,6 +4,7 @@ use std::{iter, sync::Arc}; use hir_expand::{ either::Either, + hygiene::Hygiene, name::{self, AsName, Name}, }; use ra_db::CrateId; @@ -12,7 +13,7 @@ use ra_syntax::{ AstNode, }; -use crate::{hygiene::Hygiene, type_ref::TypeRef, Source}; +use crate::{type_ref::TypeRef, Source}; #[derive(Debug, Clone, PartialEq, Eq, Hash)] pub struct Path { -- cgit v1.2.3 From cf4f7eb56660cfff355cb6bd41d5c17f7d19571b Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Wed, 30 Oct 2019 19:15:41 +0300 Subject: improve compile time a bit --- crates/ra_hir_def/src/path.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'crates/ra_hir_def/src/path.rs') diff --git a/crates/ra_hir_def/src/path.rs b/crates/ra_hir_def/src/path.rs index ddabc7ca6..04039376f 100644 --- a/crates/ra_hir_def/src/path.rs +++ b/crates/ra_hir_def/src/path.rs @@ -292,7 +292,7 @@ fn expand_use_tree( prefix: Option, tree: ast::UseTree, hygiene: &Hygiene, - cb: &mut impl FnMut(Path, &ast::UseTree, bool, Option), + cb: &mut dyn FnMut(Path, &ast::UseTree, bool, Option), ) { if let Some(use_tree_list) = tree.use_tree_list() { let prefix = match tree.path() { -- cgit v1.2.3