Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 0 additions & 8 deletions crates/hir_def/src/item_tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -646,12 +646,6 @@ pub struct MacroCall {
pub struct MacroRules {
/// The name of the declared macro.
pub name: Name,
/// Has `#[macro_export]`.
pub is_export: bool,
/// Has `#[macro_export(local_inner_macros)]`.
pub is_local_inner: bool,
/// Has `#[rustc_builtin_macro]`.
pub is_builtin: bool,
pub ast_id: FileAstId<ast::MacroRules>,
}

Expand All @@ -660,8 +654,6 @@ pub struct MacroRules {
pub struct MacroDef {
pub name: Name,
pub visibility: RawVisibilityId,
/// Has `#[rustc_builtin_macro]`.
pub is_builtin: bool,
pub ast_id: FileAstId<ast::MacroDef>,
}

Expand Down
24 changes: 2 additions & 22 deletions crates/hir_def/src/item_tree/lower.rs
Original file line number Diff line number Diff line change
Expand Up @@ -539,39 +539,19 @@ impl Ctx {

fn lower_macro_rules(&mut self, m: &ast::MacroRules) -> Option<FileItemTreeId<MacroRules>> {
let name = m.name().map(|it| it.as_name())?;
let attrs = Attrs::new(m, &self.hygiene);

let ast_id = self.source_ast_id_map.ast_id(m);

// FIXME: cfg_attr
let export_attr = attrs.by_key("macro_export");

let is_export = export_attr.exists();
let is_local_inner = if is_export {
export_attr.tt_values().map(|it| &it.token_trees).flatten().any(|it| match it {
tt::TokenTree::Leaf(tt::Leaf::Ident(ident)) => {
ident.text.contains("local_inner_macros")
}
_ => false,
})
} else {
false
};

let is_builtin = attrs.by_key("rustc_builtin_macro").exists();
let res = MacroRules { name, is_export, is_builtin, is_local_inner, ast_id };
let res = MacroRules { name, ast_id };
Some(id(self.data().macro_rules.alloc(res)))
}

fn lower_macro_def(&mut self, m: &ast::MacroDef) -> Option<FileItemTreeId<MacroDef>> {
let name = m.name().map(|it| it.as_name())?;
let attrs = Attrs::new(m, &self.hygiene);

let ast_id = self.source_ast_id_map.ast_id(m);
let visibility = self.lower_visibility(m);

let is_builtin = attrs.by_key("rustc_builtin_macro").exists();
let res = MacroDef { name, is_builtin, ast_id, visibility };
let res = MacroDef { name, ast_id, visibility };
Some(id(self.data().macro_defs.alloc(res)))
}

Expand Down
34 changes: 26 additions & 8 deletions crates/hir_def/src/nameres/collector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ use crate::{
db::DefDatabase,
item_scope::{ImportType, PerNsGlobImports},
item_tree::{
self, ItemTree, ItemTreeId, MacroCall, MacroRules, Mod, ModItem, ModKind, StructDefKind,
self, FileItemTreeId, ItemTree, ItemTreeId, MacroCall, MacroRules, Mod, ModItem, ModKind,
StructDefKind,
},
nameres::{
diagnostics::DefDiagnostic, mod_resolution::ModDir, path_resolution::ReachedFixedPoint,
Expand Down Expand Up @@ -967,14 +968,15 @@ impl ModCollector<'_, '_> {
})
}
ModItem::MacroCall(mac) => self.collect_macro_call(&self.item_tree[mac]),
ModItem::MacroRules(mac) => self.collect_macro_rules(&self.item_tree[mac]),
ModItem::MacroRules(id) => self.collect_macro_rules(id),
ModItem::MacroDef(id) => {
let mac = &self.item_tree[id];
let ast_id = InFile::new(self.file_id, mac.ast_id.upcast());

// "Macro 2.0" is not currently supported by rust-analyzer, but libcore uses it
// to define builtin macros, so we support at least that part.
if mac.is_builtin {
let attrs = self.item_tree.attrs(ModItem::from(id).into());
if attrs.by_key("rustc_builtin_macro").exists() {
let krate = self.def_collector.def_map.krate;
let macro_id = find_builtin_macro(&mac.name, krate, ast_id)
.or_else(|| find_builtin_derive(&mac.name, krate, ast_id));
Expand Down Expand Up @@ -1300,18 +1302,34 @@ impl ModCollector<'_, '_> {
self.def_collector.resolve_proc_macro(&macro_name);
}

fn collect_macro_rules(&mut self, mac: &MacroRules) {
fn collect_macro_rules(&mut self, id: FileItemTreeId<MacroRules>) {
let mac = &self.item_tree[id];
let attrs = self.item_tree.attrs(ModItem::from(id).into());
let ast_id = InFile::new(self.file_id, mac.ast_id.upcast());

let export_attr = attrs.by_key("macro_export");

let is_export = export_attr.exists();
let is_local_inner = if is_export {
export_attr.tt_values().map(|it| &it.token_trees).flatten().any(|it| match it {
tt::TokenTree::Leaf(tt::Leaf::Ident(ident)) => {
ident.text.contains("local_inner_macros")
}
_ => false,
})
} else {
false
};

// Case 1: builtin macros
if mac.is_builtin {
if attrs.by_key("rustc_builtin_macro").exists() {
let krate = self.def_collector.def_map.krate;
if let Some(macro_id) = find_builtin_macro(&mac.name, krate, ast_id) {
self.def_collector.define_macro(
self.module_id,
mac.name.clone(),
macro_id,
mac.is_export,
is_export,
);
return;
}
Expand All @@ -1322,9 +1340,9 @@ impl ModCollector<'_, '_> {
ast_id: Some(ast_id),
krate: self.def_collector.def_map.krate,
kind: MacroDefKind::Declarative,
local_inner: mac.is_local_inner,
local_inner: is_local_inner,
};
self.def_collector.define_macro(self.module_id, mac.name.clone(), macro_id, mac.is_export);
self.def_collector.define_macro(self.module_id, mac.name.clone(), macro_id, is_export);
}

fn collect_macro_call(&mut self, mac: &MacroCall) {
Expand Down