mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-09-28 12:54:58 +00:00
Fix ProcMacroData recording wrong name for derives
This commit is contained in:
parent
c37fe779c6
commit
4e94fb7028
6 changed files with 43 additions and 30 deletions
|
@ -1383,27 +1383,19 @@ impl Function {
|
||||||
db.function_data(self.id).has_body()
|
db.function_data(self.id).has_body()
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn as_proc_macro(self, _db: &dyn HirDatabase) -> Option<Macro> {
|
pub fn as_proc_macro(self, db: &dyn HirDatabase) -> Option<Macro> {
|
||||||
// let function_data = db.function_data(self.id);
|
let function_data = db.function_data(self.id);
|
||||||
// let attrs = &function_data.attrs;
|
let attrs = &function_data.attrs;
|
||||||
// if !(attrs.is_proc_macro()
|
// FIXME: Store this in FunctionData flags?
|
||||||
// || attrs.is_proc_macro_attribute()
|
if !(attrs.is_proc_macro()
|
||||||
// || attrs.is_proc_macro_derive())
|
|| attrs.is_proc_macro_attribute()
|
||||||
// {
|
|| attrs.is_proc_macro_derive())
|
||||||
// return None;
|
{
|
||||||
// }
|
return None;
|
||||||
// let loc = self.id.lookup(db.upcast());
|
}
|
||||||
// let krate = loc.krate(db);
|
let loc = self.id.lookup(db.upcast());
|
||||||
// let def_map = db.crate_def_map(krate.into());
|
let def_map = db.crate_def_map(loc.krate(db).into());
|
||||||
// let ast_id =
|
def_map.fn_as_proc_macro(loc.id).map(|id| Macro { id: id.into() })
|
||||||
// InFile::new(loc.id.file_id(), loc.id.item_tree(db.upcast())[loc.id.value].ast_id);
|
|
||||||
|
|
||||||
// let mut exported_proc_macros = def_map.exported_proc_macros();
|
|
||||||
// exported_proc_macros
|
|
||||||
// .find(|&(id, _)| matches!(id.kind, MacroDefKind::ProcMacro(_, _, id) if id == ast_id))
|
|
||||||
// .map(|(id, _)| Macro { id })
|
|
||||||
// FIXME
|
|
||||||
None
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// A textual representation of the HIR of this function for debugging purposes.
|
/// A textual representation of the HIR of this function for debugging purposes.
|
||||||
|
|
|
@ -332,6 +332,7 @@ impl MacroRulesData {
|
||||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||||
pub struct ProcMacroData {
|
pub struct ProcMacroData {
|
||||||
pub name: Name,
|
pub name: Name,
|
||||||
|
// FIXME: Record deriver helper here?
|
||||||
}
|
}
|
||||||
|
|
||||||
impl ProcMacroData {
|
impl ProcMacroData {
|
||||||
|
@ -343,7 +344,17 @@ impl ProcMacroData {
|
||||||
let item_tree = loc.id.item_tree(db);
|
let item_tree = loc.id.item_tree(db);
|
||||||
let makro = &item_tree[loc.id.value];
|
let makro = &item_tree[loc.id.value];
|
||||||
|
|
||||||
Arc::new(ProcMacroData { name: makro.name.clone() })
|
let name = if let Some(def) = item_tree
|
||||||
|
.attrs(db, loc.container.krate(), ModItem::from(loc.id.value).into())
|
||||||
|
.parse_proc_macro_decl(&makro.name)
|
||||||
|
{
|
||||||
|
def.name
|
||||||
|
} else {
|
||||||
|
// eeeh...
|
||||||
|
stdx::never!("proc macro declaration is not a proc macro");
|
||||||
|
makro.name.clone()
|
||||||
|
};
|
||||||
|
Arc::new(ProcMacroData { name })
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -70,12 +70,12 @@ use syntax::{ast, SmolStr};
|
||||||
use crate::{
|
use crate::{
|
||||||
db::DefDatabase,
|
db::DefDatabase,
|
||||||
item_scope::{BuiltinShadowMode, ItemScope},
|
item_scope::{BuiltinShadowMode, ItemScope},
|
||||||
item_tree::TreeId,
|
item_tree::{self, ItemTreeId, TreeId},
|
||||||
nameres::{diagnostics::DefDiagnostic, path_resolution::ResolveMode},
|
nameres::{diagnostics::DefDiagnostic, path_resolution::ResolveMode},
|
||||||
path::ModPath,
|
path::ModPath,
|
||||||
per_ns::PerNs,
|
per_ns::PerNs,
|
||||||
visibility::Visibility,
|
visibility::Visibility,
|
||||||
AstId, BlockId, BlockLoc, LocalModuleId, ModuleDefId, ModuleId,
|
AstId, BlockId, BlockLoc, LocalModuleId, ModuleDefId, ModuleId, ProcMacroId,
|
||||||
};
|
};
|
||||||
|
|
||||||
/// Contains the results of (early) name resolution.
|
/// Contains the results of (early) name resolution.
|
||||||
|
@ -102,6 +102,7 @@ pub struct DefMap {
|
||||||
|
|
||||||
/// Side table for resolving derive helpers.
|
/// Side table for resolving derive helpers.
|
||||||
exported_derives: FxHashMap<MacroDefId, Box<[Name]>>,
|
exported_derives: FxHashMap<MacroDefId, Box<[Name]>>,
|
||||||
|
fn_proc_macro_mapping: FxHashMap<ItemTreeId<item_tree::Function>, ProcMacroId>,
|
||||||
|
|
||||||
/// Custom attributes registered with `#![register_attr]`.
|
/// Custom attributes registered with `#![register_attr]`.
|
||||||
registered_attrs: Vec<SmolStr>,
|
registered_attrs: Vec<SmolStr>,
|
||||||
|
@ -271,6 +272,7 @@ impl DefMap {
|
||||||
recursion_limit: None,
|
recursion_limit: None,
|
||||||
extern_prelude: FxHashMap::default(),
|
extern_prelude: FxHashMap::default(),
|
||||||
exported_derives: FxHashMap::default(),
|
exported_derives: FxHashMap::default(),
|
||||||
|
fn_proc_macro_mapping: FxHashMap::default(),
|
||||||
prelude: None,
|
prelude: None,
|
||||||
root,
|
root,
|
||||||
modules,
|
modules,
|
||||||
|
@ -300,6 +302,11 @@ impl DefMap {
|
||||||
self.root
|
self.root
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// FIXME: This is an odd interface....
|
||||||
|
pub fn fn_as_proc_macro(&self, id: ItemTreeId<item_tree::Function>) -> Option<ProcMacroId> {
|
||||||
|
self.fn_proc_macro_mapping.get(&id).copied()
|
||||||
|
}
|
||||||
|
|
||||||
pub(crate) fn krate(&self) -> CrateId {
|
pub(crate) fn krate(&self) -> CrateId {
|
||||||
self.krate
|
self.krate
|
||||||
}
|
}
|
||||||
|
@ -453,6 +460,7 @@ impl DefMap {
|
||||||
modules,
|
modules,
|
||||||
registered_attrs,
|
registered_attrs,
|
||||||
registered_tools,
|
registered_tools,
|
||||||
|
fn_proc_macro_mapping,
|
||||||
block: _,
|
block: _,
|
||||||
edition: _,
|
edition: _,
|
||||||
recursion_limit: _,
|
recursion_limit: _,
|
||||||
|
@ -467,6 +475,7 @@ impl DefMap {
|
||||||
modules.shrink_to_fit();
|
modules.shrink_to_fit();
|
||||||
registered_attrs.shrink_to_fit();
|
registered_attrs.shrink_to_fit();
|
||||||
registered_tools.shrink_to_fit();
|
registered_tools.shrink_to_fit();
|
||||||
|
fn_proc_macro_mapping.shrink_to_fit();
|
||||||
for (_, module) in modules.iter_mut() {
|
for (_, module) in modules.iter_mut() {
|
||||||
module.children.shrink_to_fit();
|
module.children.shrink_to_fit();
|
||||||
module.scope.shrink_to_fit();
|
module.scope.shrink_to_fit();
|
||||||
|
|
|
@ -570,6 +570,7 @@ impl DefCollector<'_> {
|
||||||
.exported_derives
|
.exported_derives
|
||||||
.insert(macro_id_to_def_id(self.db, proc_macro_id.into()), helpers);
|
.insert(macro_id_to_def_id(self.db, proc_macro_id.into()), helpers);
|
||||||
}
|
}
|
||||||
|
self.def_map.fn_proc_macro_mapping.insert(id, proc_macro_id);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Define a macro with `macro_rules`.
|
/// Define a macro with `macro_rules`.
|
||||||
|
|
|
@ -6,13 +6,13 @@ use tt::{Leaf, TokenTree};
|
||||||
use crate::attr::Attrs;
|
use crate::attr::Attrs;
|
||||||
|
|
||||||
#[derive(Debug, PartialEq, Eq)]
|
#[derive(Debug, PartialEq, Eq)]
|
||||||
pub(super) struct ProcMacroDef {
|
pub struct ProcMacroDef {
|
||||||
pub(super) name: Name,
|
pub name: Name,
|
||||||
pub(super) kind: ProcMacroKind,
|
pub kind: ProcMacroKind,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, PartialEq, Eq)]
|
#[derive(Debug, PartialEq, Eq)]
|
||||||
pub(super) enum ProcMacroKind {
|
pub enum ProcMacroKind {
|
||||||
CustomDerive { helpers: Box<[Name]> },
|
CustomDerive { helpers: Box<[Name]> },
|
||||||
FnLike,
|
FnLike,
|
||||||
Attr,
|
Attr,
|
||||||
|
@ -30,7 +30,7 @@ impl ProcMacroKind {
|
||||||
|
|
||||||
impl Attrs {
|
impl Attrs {
|
||||||
#[rustfmt::skip]
|
#[rustfmt::skip]
|
||||||
pub(super) fn parse_proc_macro_decl(&self, func_name: &Name) -> Option<ProcMacroDef> {
|
pub fn parse_proc_macro_decl(&self, func_name: &Name) -> Option<ProcMacroDef> {
|
||||||
if self.is_proc_macro() {
|
if self.is_proc_macro() {
|
||||||
Some(ProcMacroDef { name: func_name.clone(), kind: ProcMacroKind::FnLike })
|
Some(ProcMacroDef { name: func_name.clone(), kind: ProcMacroKind::FnLike })
|
||||||
} else if self.is_proc_macro_attribute() {
|
} else if self.is_proc_macro_attribute() {
|
||||||
|
|
|
@ -4151,7 +4151,7 @@ struct Foo;
|
||||||
*Copy*
|
*Copy*
|
||||||
|
|
||||||
```rust
|
```rust
|
||||||
test
|
test::foo
|
||||||
```
|
```
|
||||||
|
|
||||||
```rust
|
```rust
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue