mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-10-01 14:21:44 +00:00
Support $crate
in item and expr place.
This commit is contained in:
parent
8cd23a4fb8
commit
2ecb126f5c
14 changed files with 268 additions and 37 deletions
|
@ -512,7 +512,7 @@ pub fn collect_hir_path_segments(path: &hir::Path) -> Option<Vec<SmolStr>> {
|
||||||
hir::PathKind::Plain => {}
|
hir::PathKind::Plain => {}
|
||||||
hir::PathKind::Self_ => ps.push("self".into()),
|
hir::PathKind::Self_ => ps.push("self".into()),
|
||||||
hir::PathKind::Super => ps.push("super".into()),
|
hir::PathKind::Super => ps.push("super".into()),
|
||||||
hir::PathKind::Type(_) => return None,
|
hir::PathKind::Type(_) | hir::PathKind::DollarCrate(_) => return None,
|
||||||
}
|
}
|
||||||
for s in path.segments.iter() {
|
for s in path.segments.iter() {
|
||||||
ps.push(s.name.to_string().into());
|
ps.push(s.name.to_string().into());
|
||||||
|
|
|
@ -272,8 +272,11 @@ where
|
||||||
self.alloc_expr(Expr::Match { expr, arms }, syntax_ptr)
|
self.alloc_expr(Expr::Match { expr, arms }, syntax_ptr)
|
||||||
}
|
}
|
||||||
ast::Expr::PathExpr(e) => {
|
ast::Expr::PathExpr(e) => {
|
||||||
let path =
|
let path = e
|
||||||
e.path().and_then(Path::from_ast).map(Expr::Path).unwrap_or(Expr::Missing);
|
.path()
|
||||||
|
.and_then(|path| self.parse_path(path))
|
||||||
|
.map(Expr::Path)
|
||||||
|
.unwrap_or(Expr::Missing);
|
||||||
self.alloc_expr(path, syntax_ptr)
|
self.alloc_expr(path, syntax_ptr)
|
||||||
}
|
}
|
||||||
ast::Expr::ContinueExpr(_e) => {
|
ast::Expr::ContinueExpr(_e) => {
|
||||||
|
@ -295,7 +298,7 @@ where
|
||||||
self.alloc_expr(Expr::Return { expr }, syntax_ptr)
|
self.alloc_expr(Expr::Return { expr }, syntax_ptr)
|
||||||
}
|
}
|
||||||
ast::Expr::RecordLit(e) => {
|
ast::Expr::RecordLit(e) => {
|
||||||
let path = e.path().and_then(Path::from_ast);
|
let path = e.path().and_then(|path| self.parse_path(path));
|
||||||
let mut field_ptrs = Vec::new();
|
let mut field_ptrs = Vec::new();
|
||||||
let record_lit = if let Some(nfl) = e.record_field_list() {
|
let record_lit = if let Some(nfl) = e.record_field_list() {
|
||||||
let fields = nfl
|
let fields = nfl
|
||||||
|
@ -459,7 +462,7 @@ where
|
||||||
.ast_id(&e)
|
.ast_id(&e)
|
||||||
.with_file_id(self.current_file_id);
|
.with_file_id(self.current_file_id);
|
||||||
|
|
||||||
if let Some(path) = e.path().and_then(Path::from_ast) {
|
if let Some(path) = e.path().and_then(|path| self.parse_path(path)) {
|
||||||
if let Some(def) = self.resolver.resolve_path_as_macro(self.db, &path) {
|
if let Some(def) = self.resolver.resolve_path_as_macro(self.db, &path) {
|
||||||
let call_id = MacroCallLoc { def: def.id, ast_id }.id(self.db);
|
let call_id = MacroCallLoc { def: def.id, ast_id }.id(self.db);
|
||||||
let file_id = call_id.as_file(MacroFileKind::Expr);
|
let file_id = call_id.as_file(MacroFileKind::Expr);
|
||||||
|
@ -529,7 +532,7 @@ where
|
||||||
Pat::Bind { name, mode: annotation, subpat }
|
Pat::Bind { name, mode: annotation, subpat }
|
||||||
}
|
}
|
||||||
ast::Pat::TupleStructPat(p) => {
|
ast::Pat::TupleStructPat(p) => {
|
||||||
let path = p.path().and_then(Path::from_ast);
|
let path = p.path().and_then(|path| self.parse_path(path));
|
||||||
let args = p.args().map(|p| self.collect_pat(p)).collect();
|
let args = p.args().map(|p| self.collect_pat(p)).collect();
|
||||||
Pat::TupleStruct { path, args }
|
Pat::TupleStruct { path, args }
|
||||||
}
|
}
|
||||||
|
@ -539,7 +542,7 @@ where
|
||||||
Pat::Ref { pat, mutability }
|
Pat::Ref { pat, mutability }
|
||||||
}
|
}
|
||||||
ast::Pat::PathPat(p) => {
|
ast::Pat::PathPat(p) => {
|
||||||
let path = p.path().and_then(Path::from_ast);
|
let path = p.path().and_then(|path| self.parse_path(path));
|
||||||
path.map(Pat::Path).unwrap_or(Pat::Missing)
|
path.map(Pat::Path).unwrap_or(Pat::Missing)
|
||||||
}
|
}
|
||||||
ast::Pat::TuplePat(p) => {
|
ast::Pat::TuplePat(p) => {
|
||||||
|
@ -548,7 +551,7 @@ where
|
||||||
}
|
}
|
||||||
ast::Pat::PlaceholderPat(_) => Pat::Wild,
|
ast::Pat::PlaceholderPat(_) => Pat::Wild,
|
||||||
ast::Pat::RecordPat(p) => {
|
ast::Pat::RecordPat(p) => {
|
||||||
let path = p.path().and_then(Path::from_ast);
|
let path = p.path().and_then(|path| self.parse_path(path));
|
||||||
let record_field_pat_list =
|
let record_field_pat_list =
|
||||||
p.record_field_pat_list().expect("every struct should have a field list");
|
p.record_field_pat_list().expect("every struct should have a field list");
|
||||||
let mut fields: Vec<_> = record_field_pat_list
|
let mut fields: Vec<_> = record_field_pat_list
|
||||||
|
@ -589,6 +592,10 @@ where
|
||||||
self.missing_pat()
|
self.missing_pat()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn parse_path(&mut self, path: ast::Path) -> Option<Path> {
|
||||||
|
Path::from_src(Source { ast: path, file_id: self.current_file_id }, self.db)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl From<ast::BinOp> for BinaryOp {
|
impl From<ast::BinOp> for BinaryOp {
|
||||||
|
|
|
@ -132,6 +132,7 @@ impl GenericParams {
|
||||||
fn fill_params(&mut self, params: ast::TypeParamList, start: u32) {
|
fn fill_params(&mut self, params: ast::TypeParamList, start: u32) {
|
||||||
for (idx, type_param) in params.type_params().enumerate() {
|
for (idx, type_param) in params.type_params().enumerate() {
|
||||||
let name = type_param.name().map_or_else(Name::missing, |it| it.as_name());
|
let name = type_param.name().map_or_else(Name::missing, |it| it.as_name());
|
||||||
|
// FIXME: Use `Path::from_src`
|
||||||
let default = type_param.default_type().and_then(|t| t.path()).and_then(Path::from_ast);
|
let default = type_param.default_type().and_then(|t| t.path()).and_then(Path::from_ast);
|
||||||
|
|
||||||
let param = GenericParam { idx: idx as u32 + start, name: name.clone(), default };
|
let param = GenericParam { idx: idx as u32 + start, name: name.clone(), default };
|
||||||
|
|
|
@ -58,6 +58,17 @@ impl HirFileId {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Get the crate which the macro lives in, if it is a macro file.
|
||||||
|
pub(crate) fn macro_crate(self, db: &impl AstDatabase) -> Option<Crate> {
|
||||||
|
match self.0 {
|
||||||
|
HirFileIdRepr::File(_) => None,
|
||||||
|
HirFileIdRepr::Macro(macro_file) => {
|
||||||
|
let loc = macro_file.macro_call_id.loc(db);
|
||||||
|
Some(loc.def.krate)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
pub(crate) fn parse_or_expand_query(
|
pub(crate) fn parse_or_expand_query(
|
||||||
db: &impl AstDatabase,
|
db: &impl AstDatabase,
|
||||||
file_id: HirFileId,
|
file_id: HirFileId,
|
||||||
|
|
|
@ -218,7 +218,10 @@ impl ModuleImplBlocks {
|
||||||
ast::ItemOrMacro::Macro(macro_call) => {
|
ast::ItemOrMacro::Macro(macro_call) => {
|
||||||
//FIXME: we should really cut down on the boilerplate required to process a macro
|
//FIXME: we should really cut down on the boilerplate required to process a macro
|
||||||
let ast_id = db.ast_id_map(file_id).ast_id(¯o_call).with_file_id(file_id);
|
let ast_id = db.ast_id_map(file_id).ast_id(¯o_call).with_file_id(file_id);
|
||||||
if let Some(path) = macro_call.path().and_then(Path::from_ast) {
|
if let Some(path) = macro_call
|
||||||
|
.path()
|
||||||
|
.and_then(|path| Path::from_src(Source { ast: path, file_id }, db))
|
||||||
|
{
|
||||||
if let Some(def) = self.module.resolver(db).resolve_path_as_macro(db, &path)
|
if let Some(def) = self.module.resolver(db).resolve_path_as_macro(db, &path)
|
||||||
{
|
{
|
||||||
let call_id = MacroCallLoc { def: def.id, ast_id }.id(db);
|
let call_id = MacroCallLoc { def: def.id, ast_id }.id(db);
|
||||||
|
|
|
@ -14,4 +14,6 @@ test_utils::marks!(
|
||||||
macro_rules_from_other_crates_are_visible_with_macro_use
|
macro_rules_from_other_crates_are_visible_with_macro_use
|
||||||
prelude_is_macro_use
|
prelude_is_macro_use
|
||||||
coerce_merge_fail_fallback
|
coerce_merge_fail_fallback
|
||||||
|
macro_dollar_crate_self
|
||||||
|
macro_dollar_crate_other
|
||||||
);
|
);
|
||||||
|
|
|
@ -332,6 +332,20 @@ impl CrateDefMap {
|
||||||
) -> ResolvePathResult {
|
) -> ResolvePathResult {
|
||||||
let mut segments = path.segments.iter().enumerate();
|
let mut segments = path.segments.iter().enumerate();
|
||||||
let mut curr_per_ns: PerNs = match path.kind {
|
let mut curr_per_ns: PerNs = match path.kind {
|
||||||
|
PathKind::DollarCrate(krate) => {
|
||||||
|
if krate == self.krate {
|
||||||
|
tested_by!(macro_dollar_crate_self);
|
||||||
|
PerNs::types(Module { krate: self.krate, module_id: self.root }.into())
|
||||||
|
} else {
|
||||||
|
match krate.root_module(db) {
|
||||||
|
Some(module) => {
|
||||||
|
tested_by!(macro_dollar_crate_other);
|
||||||
|
PerNs::types(module.into())
|
||||||
|
}
|
||||||
|
None => return ResolvePathResult::empty(ReachedFixedPoint::No),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
PathKind::Crate => {
|
PathKind::Crate => {
|
||||||
PerNs::types(Module { krate: self.krate, module_id: self.root }.into())
|
PerNs::types(Module { krate: self.krate, module_id: self.root }.into())
|
||||||
}
|
}
|
||||||
|
|
|
@ -9,7 +9,7 @@ use test_utils::tested_by;
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
db::{AstDatabase, DefDatabase},
|
db::{AstDatabase, DefDatabase},
|
||||||
AsName, AstIdMap, Either, FileAstId, HirFileId, ModuleSource, Name, Path,
|
AsName, AstIdMap, Either, FileAstId, HirFileId, ModuleSource, Name, Path, Source,
|
||||||
};
|
};
|
||||||
|
|
||||||
/// `RawItems` is a set of top-level items in a file (except for impls).
|
/// `RawItems` is a set of top-level items in a file (except for impls).
|
||||||
|
@ -71,6 +71,8 @@ impl RawItems {
|
||||||
raw_items: RawItems::default(),
|
raw_items: RawItems::default(),
|
||||||
source_ast_id_map: db.ast_id_map(file_id),
|
source_ast_id_map: db.ast_id_map(file_id),
|
||||||
source_map: ImportSourceMap::default(),
|
source_map: ImportSourceMap::default(),
|
||||||
|
file_id,
|
||||||
|
db,
|
||||||
};
|
};
|
||||||
if let Some(node) = db.parse_or_expand(file_id) {
|
if let Some(node) = db.parse_or_expand(file_id) {
|
||||||
if let Some(source_file) = ast::SourceFile::cast(node.clone()) {
|
if let Some(source_file) = ast::SourceFile::cast(node.clone()) {
|
||||||
|
@ -192,13 +194,15 @@ pub(super) struct MacroData {
|
||||||
pub(super) export: bool,
|
pub(super) export: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
struct RawItemsCollector {
|
struct RawItemsCollector<DB> {
|
||||||
raw_items: RawItems,
|
raw_items: RawItems,
|
||||||
source_ast_id_map: Arc<AstIdMap>,
|
source_ast_id_map: Arc<AstIdMap>,
|
||||||
source_map: ImportSourceMap,
|
source_map: ImportSourceMap,
|
||||||
|
file_id: HirFileId,
|
||||||
|
db: DB,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl RawItemsCollector {
|
impl<DB: AstDatabase> RawItemsCollector<&'_ DB> {
|
||||||
fn process_module(&mut self, current_module: Option<Module>, body: impl ast::ModuleItemOwner) {
|
fn process_module(&mut self, current_module: Option<Module>, body: impl ast::ModuleItemOwner) {
|
||||||
for item_or_macro in body.items_with_macros() {
|
for item_or_macro in body.items_with_macros() {
|
||||||
match item_or_macro {
|
match item_or_macro {
|
||||||
|
@ -300,17 +304,21 @@ impl RawItemsCollector {
|
||||||
fn add_use_item(&mut self, current_module: Option<Module>, use_item: ast::UseItem) {
|
fn add_use_item(&mut self, current_module: Option<Module>, use_item: ast::UseItem) {
|
||||||
let is_prelude = use_item.has_atom_attr("prelude_import");
|
let is_prelude = use_item.has_atom_attr("prelude_import");
|
||||||
|
|
||||||
Path::expand_use_item(&use_item, |path, use_tree, is_glob, alias| {
|
Path::expand_use_item(
|
||||||
let import_data = ImportData {
|
Source { ast: use_item, file_id: self.file_id },
|
||||||
path,
|
self.db,
|
||||||
alias,
|
|path, use_tree, is_glob, alias| {
|
||||||
is_glob,
|
let import_data = ImportData {
|
||||||
is_prelude,
|
path,
|
||||||
is_extern_crate: false,
|
alias,
|
||||||
is_macro_use: false,
|
is_glob,
|
||||||
};
|
is_prelude,
|
||||||
self.push_import(current_module, import_data, Either::A(AstPtr::new(use_tree)));
|
is_extern_crate: false,
|
||||||
})
|
is_macro_use: false,
|
||||||
|
};
|
||||||
|
self.push_import(current_module, import_data, Either::A(AstPtr::new(use_tree)));
|
||||||
|
},
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn add_extern_crate_item(
|
fn add_extern_crate_item(
|
||||||
|
@ -335,7 +343,10 @@ impl RawItemsCollector {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn add_macro(&mut self, current_module: Option<Module>, m: ast::MacroCall) {
|
fn add_macro(&mut self, current_module: Option<Module>, m: ast::MacroCall) {
|
||||||
let path = match m.path().and_then(Path::from_ast) {
|
let path = match m
|
||||||
|
.path()
|
||||||
|
.and_then(|path| Path::from_src(Source { ast: path, file_id: self.file_id }, self.db))
|
||||||
|
{
|
||||||
Some(it) => it,
|
Some(it) => it,
|
||||||
_ => return,
|
_ => return,
|
||||||
};
|
};
|
||||||
|
|
|
@ -515,3 +515,108 @@ fn path_qualified_macros() {
|
||||||
⋮not_found: _
|
⋮not_found: _
|
||||||
"###);
|
"###);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn macro_dollar_crate_is_correct_in_item() {
|
||||||
|
covers!(macro_dollar_crate_self);
|
||||||
|
covers!(macro_dollar_crate_other);
|
||||||
|
let map = def_map_with_crate_graph(
|
||||||
|
"
|
||||||
|
//- /main.rs
|
||||||
|
#[macro_use]
|
||||||
|
extern crate foo;
|
||||||
|
|
||||||
|
#[macro_use]
|
||||||
|
mod m {
|
||||||
|
macro_rules! current {
|
||||||
|
() => {
|
||||||
|
use $crate::Foo as FooSelf;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
struct Foo;
|
||||||
|
|
||||||
|
current!();
|
||||||
|
not_current1!();
|
||||||
|
foo::not_current2!();
|
||||||
|
|
||||||
|
//- /lib.rs
|
||||||
|
mod m {
|
||||||
|
#[macro_export]
|
||||||
|
macro_rules! not_current1 {
|
||||||
|
() => {
|
||||||
|
use $crate::Bar;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[macro_export]
|
||||||
|
macro_rules! not_current2 {
|
||||||
|
() => {
|
||||||
|
use $crate::Baz;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
struct Bar;
|
||||||
|
struct Baz;
|
||||||
|
",
|
||||||
|
crate_graph! {
|
||||||
|
"main": ("/main.rs", ["foo"]),
|
||||||
|
"foo": ("/lib.rs", []),
|
||||||
|
},
|
||||||
|
);
|
||||||
|
assert_snapshot!(map, @r###"
|
||||||
|
⋮crate
|
||||||
|
⋮Bar: t v
|
||||||
|
⋮Baz: t v
|
||||||
|
⋮Foo: t v
|
||||||
|
⋮FooSelf: t v
|
||||||
|
⋮foo: t
|
||||||
|
⋮m: t
|
||||||
|
⋮
|
||||||
|
⋮crate::m
|
||||||
|
"###);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn macro_dollar_crate_is_correct_in_indirect_deps() {
|
||||||
|
covers!(macro_dollar_crate_other);
|
||||||
|
// From std
|
||||||
|
let map = def_map_with_crate_graph(
|
||||||
|
r#"
|
||||||
|
//- /main.rs
|
||||||
|
foo!();
|
||||||
|
|
||||||
|
//- /std.rs
|
||||||
|
#[prelude_import]
|
||||||
|
use self::prelude::*;
|
||||||
|
|
||||||
|
pub use core::foo;
|
||||||
|
|
||||||
|
mod prelude {}
|
||||||
|
|
||||||
|
#[macro_use]
|
||||||
|
mod std_macros;
|
||||||
|
|
||||||
|
//- /core.rs
|
||||||
|
#[macro_export]
|
||||||
|
macro_rules! foo {
|
||||||
|
() => {
|
||||||
|
use $crate::bar;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct bar;
|
||||||
|
"#,
|
||||||
|
crate_graph! {
|
||||||
|
"main": ("/main.rs", ["std"]),
|
||||||
|
"std": ("/std.rs", ["core"]),
|
||||||
|
"core": ("/core.rs", []),
|
||||||
|
},
|
||||||
|
);
|
||||||
|
assert_snapshot!(map, @r###"
|
||||||
|
⋮crate
|
||||||
|
⋮bar: t v
|
||||||
|
"###);
|
||||||
|
}
|
||||||
|
|
|
@ -5,7 +5,7 @@ use ra_syntax::{
|
||||||
AstNode,
|
AstNode,
|
||||||
};
|
};
|
||||||
|
|
||||||
use crate::{name, type_ref::TypeRef, AsName, Name};
|
use crate::{db::AstDatabase, name, type_ref::TypeRef, AsName, Crate, Name, Source};
|
||||||
|
|
||||||
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
|
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
|
||||||
pub struct Path {
|
pub struct Path {
|
||||||
|
@ -52,16 +52,19 @@ pub enum PathKind {
|
||||||
Abs,
|
Abs,
|
||||||
// Type based path like `<T>::foo`
|
// Type based path like `<T>::foo`
|
||||||
Type(Box<TypeRef>),
|
Type(Box<TypeRef>),
|
||||||
|
// `$crate` from macro expansion
|
||||||
|
DollarCrate(Crate),
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Path {
|
impl Path {
|
||||||
/// Calls `cb` with all paths, represented by this use item.
|
/// Calls `cb` with all paths, represented by this use item.
|
||||||
pub fn expand_use_item(
|
pub fn expand_use_item(
|
||||||
item: &ast::UseItem,
|
item_src: Source<ast::UseItem>,
|
||||||
|
db: &impl AstDatabase,
|
||||||
mut cb: impl FnMut(Path, &ast::UseTree, bool, Option<Name>),
|
mut cb: impl FnMut(Path, &ast::UseTree, bool, Option<Name>),
|
||||||
) {
|
) {
|
||||||
if let Some(tree) = item.use_tree() {
|
if let Some(tree) = item_src.ast.use_tree() {
|
||||||
expand_use_tree(None, tree, &mut cb);
|
expand_use_tree(None, tree, &|| item_src.file_id.macro_crate(db), &mut cb);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -76,7 +79,19 @@ impl Path {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Converts an `ast::Path` to `Path`. Works with use trees.
|
/// Converts an `ast::Path` to `Path`. Works with use trees.
|
||||||
pub fn from_ast(mut path: ast::Path) -> Option<Path> {
|
/// DEPRECATED: It does not handle `$crate` from macro call.
|
||||||
|
pub fn from_ast(path: ast::Path) -> Option<Path> {
|
||||||
|
Path::parse(path, &|| None)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// 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<ast::Path>, db: &impl AstDatabase) -> Option<Path> {
|
||||||
|
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<Crate>) -> Option<Path> {
|
||||||
let mut kind = PathKind::Plain;
|
let mut kind = PathKind::Plain;
|
||||||
let mut segments = Vec::new();
|
let mut segments = Vec::new();
|
||||||
loop {
|
loop {
|
||||||
|
@ -88,6 +103,13 @@ impl Path {
|
||||||
|
|
||||||
match segment.kind()? {
|
match segment.kind()? {
|
||||||
ast::PathSegmentKind::Name(name) => {
|
ast::PathSegmentKind::Name(name) => {
|
||||||
|
if name.text() == "$crate" {
|
||||||
|
if let Some(macro_crate) = macro_crate() {
|
||||||
|
kind = PathKind::DollarCrate(macro_crate);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
let args = segment
|
let args = segment
|
||||||
.type_arg_list()
|
.type_arg_list()
|
||||||
.and_then(GenericArgs::from_ast)
|
.and_then(GenericArgs::from_ast)
|
||||||
|
@ -113,7 +135,7 @@ impl Path {
|
||||||
}
|
}
|
||||||
// <T as Trait<A>>::Foo desugars to Trait<Self=T, A>::Foo
|
// <T as Trait<A>>::Foo desugars to Trait<Self=T, A>::Foo
|
||||||
Some(trait_ref) => {
|
Some(trait_ref) => {
|
||||||
let path = Path::from_ast(trait_ref.path()?)?;
|
let path = Path::parse(trait_ref.path()?, macro_crate)?;
|
||||||
kind = path.kind;
|
kind = path.kind;
|
||||||
let mut prefix_segments = path.segments;
|
let mut prefix_segments = path.segments;
|
||||||
prefix_segments.reverse();
|
prefix_segments.reverse();
|
||||||
|
@ -264,6 +286,7 @@ impl From<Name> for Path {
|
||||||
fn expand_use_tree(
|
fn expand_use_tree(
|
||||||
prefix: Option<Path>,
|
prefix: Option<Path>,
|
||||||
tree: ast::UseTree,
|
tree: ast::UseTree,
|
||||||
|
macro_crate: &impl Fn() -> Option<Crate>,
|
||||||
cb: &mut impl FnMut(Path, &ast::UseTree, bool, Option<Name>),
|
cb: &mut impl FnMut(Path, &ast::UseTree, bool, Option<Name>),
|
||||||
) {
|
) {
|
||||||
if let Some(use_tree_list) = tree.use_tree_list() {
|
if let Some(use_tree_list) = tree.use_tree_list() {
|
||||||
|
@ -272,13 +295,13 @@ fn expand_use_tree(
|
||||||
None => prefix,
|
None => prefix,
|
||||||
// E.g. `use something::{inner}` (prefix is `None`, path is `something`)
|
// E.g. `use something::{inner}` (prefix is `None`, path is `something`)
|
||||||
// or `use something::{path::{inner::{innerer}}}` (prefix is `something::path`, path is `inner`)
|
// or `use something::{path::{inner::{innerer}}}` (prefix is `something::path`, path is `inner`)
|
||||||
Some(path) => match convert_path(prefix, path) {
|
Some(path) => match convert_path(prefix, path, macro_crate) {
|
||||||
Some(it) => Some(it),
|
Some(it) => Some(it),
|
||||||
None => return, // FIXME: report errors somewhere
|
None => return, // FIXME: report errors somewhere
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
for child_tree in use_tree_list.use_trees() {
|
for child_tree in use_tree_list.use_trees() {
|
||||||
expand_use_tree(prefix.clone(), child_tree, cb);
|
expand_use_tree(prefix.clone(), child_tree, macro_crate, cb);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
let alias = tree.alias().and_then(|a| a.name()).map(|a| a.as_name());
|
let alias = tree.alias().and_then(|a| a.name()).map(|a| a.as_name());
|
||||||
|
@ -295,7 +318,7 @@ fn expand_use_tree(
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if let Some(path) = convert_path(prefix, ast_path) {
|
if let Some(path) = convert_path(prefix, ast_path, macro_crate) {
|
||||||
let is_glob = tree.has_star();
|
let is_glob = tree.has_star();
|
||||||
cb(path, &tree, is_glob, alias)
|
cb(path, &tree, is_glob, alias)
|
||||||
}
|
}
|
||||||
|
@ -305,12 +328,29 @@ fn expand_use_tree(
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn convert_path(prefix: Option<Path>, path: ast::Path) -> Option<Path> {
|
fn convert_path(
|
||||||
let prefix =
|
prefix: Option<Path>,
|
||||||
if let Some(qual) = path.qualifier() { Some(convert_path(prefix, qual)?) } else { prefix };
|
path: ast::Path,
|
||||||
|
macro_crate: &impl Fn() -> Option<Crate>,
|
||||||
|
) -> Option<Path> {
|
||||||
|
let prefix = if let Some(qual) = path.qualifier() {
|
||||||
|
Some(convert_path(prefix, qual, macro_crate)?)
|
||||||
|
} else {
|
||||||
|
prefix
|
||||||
|
};
|
||||||
|
|
||||||
let segment = path.segment()?;
|
let segment = path.segment()?;
|
||||||
let res = match segment.kind()? {
|
let res = match segment.kind()? {
|
||||||
ast::PathSegmentKind::Name(name) => {
|
ast::PathSegmentKind::Name(name) => {
|
||||||
|
if name.text() == "$crate" {
|
||||||
|
if let Some(krate) = macro_crate() {
|
||||||
|
return Some(Path::from_simple_segments(
|
||||||
|
PathKind::DollarCrate(krate),
|
||||||
|
iter::empty(),
|
||||||
|
));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// no type args in use
|
// no type args in use
|
||||||
let mut res = prefix
|
let mut res = prefix
|
||||||
.unwrap_or_else(|| Path { kind: PathKind::Plain, segments: Vec::with_capacity(1) });
|
.unwrap_or_else(|| Path { kind: PathKind::Plain, segments: Vec::with_capacity(1) });
|
||||||
|
|
|
@ -203,6 +203,7 @@ impl SourceAnalyzer {
|
||||||
db: &impl HirDatabase,
|
db: &impl HirDatabase,
|
||||||
macro_call: &ast::MacroCall,
|
macro_call: &ast::MacroCall,
|
||||||
) -> Option<MacroDef> {
|
) -> Option<MacroDef> {
|
||||||
|
// This must be a normal source file rather than macro file.
|
||||||
let path = macro_call.path().and_then(Path::from_ast)?;
|
let path = macro_call.path().and_then(Path::from_ast)?;
|
||||||
self.resolver.resolve_path_as_macro(db, &path)
|
self.resolver.resolve_path_as_macro(db, &path)
|
||||||
}
|
}
|
||||||
|
@ -261,6 +262,7 @@ impl SourceAnalyzer {
|
||||||
return Some(PathResolution::AssocItem(assoc));
|
return Some(PathResolution::AssocItem(assoc));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
// This must be a normal source file rather than macro file.
|
||||||
let hir_path = crate::Path::from_ast(path.clone())?;
|
let hir_path = crate::Path::from_ast(path.clone())?;
|
||||||
self.resolve_hir_path(db, &hir_path)
|
self.resolve_hir_path(db, &hir_path)
|
||||||
}
|
}
|
||||||
|
|
|
@ -3130,6 +3130,39 @@ fn test() { S.foo()<|>; }
|
||||||
assert_eq!(t, "u128");
|
assert_eq!(t, "u128");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn infer_macro_with_dollar_crate_is_correct_in_expr() {
|
||||||
|
covers!(macro_dollar_crate_other);
|
||||||
|
let (mut db, pos) = MockDatabase::with_position(
|
||||||
|
r#"
|
||||||
|
//- /main.rs
|
||||||
|
fn test() {
|
||||||
|
let x = (foo::foo!(1), foo::foo!(2));
|
||||||
|
x<|>;
|
||||||
|
}
|
||||||
|
|
||||||
|
//- /lib.rs
|
||||||
|
#[macro_export]
|
||||||
|
macro_rules! foo {
|
||||||
|
(1) => { $crate::bar!() };
|
||||||
|
(2) => { 1 + $crate::baz() };
|
||||||
|
}
|
||||||
|
|
||||||
|
#[macro_export]
|
||||||
|
macro_rules! bar {
|
||||||
|
() => { 42 }
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn baz() -> usize { 31usize }
|
||||||
|
"#,
|
||||||
|
);
|
||||||
|
db.set_crate_graph_from_fixture(crate_graph! {
|
||||||
|
"main": ("/main.rs", ["foo"]),
|
||||||
|
"foo": ("/lib.rs", []),
|
||||||
|
});
|
||||||
|
assert_eq!("(i32, usize)", type_at_pos(&db, pos));
|
||||||
|
}
|
||||||
|
|
||||||
#[ignore]
|
#[ignore]
|
||||||
#[test]
|
#[test]
|
||||||
fn method_resolution_trait_before_autoref() {
|
fn method_resolution_trait_before_autoref() {
|
||||||
|
|
|
@ -72,6 +72,7 @@ impl TypeRef {
|
||||||
}
|
}
|
||||||
ast::TypeRef::NeverType(..) => TypeRef::Never,
|
ast::TypeRef::NeverType(..) => TypeRef::Never,
|
||||||
ast::TypeRef::PathType(inner) => {
|
ast::TypeRef::PathType(inner) => {
|
||||||
|
// FIXME: Use `Path::from_src`
|
||||||
inner.path().and_then(Path::from_ast).map(TypeRef::Path).unwrap_or(TypeRef::Error)
|
inner.path().and_then(Path::from_ast).map(TypeRef::Path).unwrap_or(TypeRef::Error)
|
||||||
}
|
}
|
||||||
ast::TypeRef::PointerType(inner) => {
|
ast::TypeRef::PointerType(inner) => {
|
||||||
|
@ -141,6 +142,7 @@ impl TypeBound {
|
||||||
Some(p) => p,
|
Some(p) => p,
|
||||||
None => return TypeBound::Error,
|
None => return TypeBound::Error,
|
||||||
};
|
};
|
||||||
|
// FIXME: Use `Path::from_src`
|
||||||
let path = match Path::from_ast(path) {
|
let path = match Path::from_ast(path) {
|
||||||
Some(p) => p,
|
Some(p) => p,
|
||||||
None => return TypeBound::Error,
|
None => return TypeBound::Error,
|
||||||
|
|
|
@ -86,7 +86,7 @@ fn expand_subtree(ctx: &mut ExpandCtx, template: &tt::Subtree) -> Result<tt::Sub
|
||||||
|
|
||||||
fn expand_var(ctx: &mut ExpandCtx, v: &SmolStr) -> Result<Fragment, ExpandError> {
|
fn expand_var(ctx: &mut ExpandCtx, v: &SmolStr) -> Result<Fragment, ExpandError> {
|
||||||
let res = if v == "crate" {
|
let res = if v == "crate" {
|
||||||
// FIXME: Properly handle $crate token
|
// We simply produce identifier `$crate` here. And it will be resolved when lowering ast to Path.
|
||||||
let tt =
|
let tt =
|
||||||
tt::Leaf::from(tt::Ident { text: "$crate".into(), id: tt::TokenId::unspecified() })
|
tt::Leaf::from(tt::Ident { text: "$crate".into(), id: tt::TokenId::unspecified() })
|
||||||
.into();
|
.into();
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue