Skip to content

Commit 4e94fb7

Browse files
committed
Fix ProcMacroData recording wrong name for derives
1 parent c37fe77 commit 4e94fb7

File tree

6 files changed

+43
-30
lines changed

6 files changed

+43
-30
lines changed

crates/hir/src/lib.rs

Lines changed: 13 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1383,27 +1383,19 @@ impl Function {
13831383
db.function_data(self.id).has_body()
13841384
}
13851385

1386-
pub fn as_proc_macro(self, _db: &dyn HirDatabase) -> Option<Macro> {
1387-
// let function_data = db.function_data(self.id);
1388-
// let attrs = &function_data.attrs;
1389-
// if !(attrs.is_proc_macro()
1390-
// || attrs.is_proc_macro_attribute()
1391-
// || attrs.is_proc_macro_derive())
1392-
// {
1393-
// return None;
1394-
// }
1395-
// let loc = self.id.lookup(db.upcast());
1396-
// let krate = loc.krate(db);
1397-
// let def_map = db.crate_def_map(krate.into());
1398-
// let ast_id =
1399-
// InFile::new(loc.id.file_id(), loc.id.item_tree(db.upcast())[loc.id.value].ast_id);
1400-
1401-
// let mut exported_proc_macros = def_map.exported_proc_macros();
1402-
// exported_proc_macros
1403-
// .find(|&(id, _)| matches!(id.kind, MacroDefKind::ProcMacro(_, _, id) if id == ast_id))
1404-
// .map(|(id, _)| Macro { id })
1405-
// FIXME
1406-
None
1386+
pub fn as_proc_macro(self, db: &dyn HirDatabase) -> Option<Macro> {
1387+
let function_data = db.function_data(self.id);
1388+
let attrs = &function_data.attrs;
1389+
// FIXME: Store this in FunctionData flags?
1390+
if !(attrs.is_proc_macro()
1391+
|| attrs.is_proc_macro_attribute()
1392+
|| attrs.is_proc_macro_derive())
1393+
{
1394+
return None;
1395+
}
1396+
let loc = self.id.lookup(db.upcast());
1397+
let def_map = db.crate_def_map(loc.krate(db).into());
1398+
def_map.fn_as_proc_macro(loc.id).map(|id| Macro { id: id.into() })
14071399
}
14081400

14091401
/// A textual representation of the HIR of this function for debugging purposes.

crates/hir_def/src/data.rs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -332,6 +332,7 @@ impl MacroRulesData {
332332
#[derive(Debug, Clone, PartialEq, Eq)]
333333
pub struct ProcMacroData {
334334
pub name: Name,
335+
// FIXME: Record deriver helper here?
335336
}
336337

337338
impl ProcMacroData {
@@ -343,7 +344,17 @@ impl ProcMacroData {
343344
let item_tree = loc.id.item_tree(db);
344345
let makro = &item_tree[loc.id.value];
345346

346-
Arc::new(ProcMacroData { name: makro.name.clone() })
347+
let name = if let Some(def) = item_tree
348+
.attrs(db, loc.container.krate(), ModItem::from(loc.id.value).into())
349+
.parse_proc_macro_decl(&makro.name)
350+
{
351+
def.name
352+
} else {
353+
// eeeh...
354+
stdx::never!("proc macro declaration is not a proc macro");
355+
makro.name.clone()
356+
};
357+
Arc::new(ProcMacroData { name })
347358
}
348359
}
349360

crates/hir_def/src/nameres.rs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,12 +70,12 @@ use syntax::{ast, SmolStr};
7070
use crate::{
7171
db::DefDatabase,
7272
item_scope::{BuiltinShadowMode, ItemScope},
73-
item_tree::TreeId,
73+
item_tree::{self, ItemTreeId, TreeId},
7474
nameres::{diagnostics::DefDiagnostic, path_resolution::ResolveMode},
7575
path::ModPath,
7676
per_ns::PerNs,
7777
visibility::Visibility,
78-
AstId, BlockId, BlockLoc, LocalModuleId, ModuleDefId, ModuleId,
78+
AstId, BlockId, BlockLoc, LocalModuleId, ModuleDefId, ModuleId, ProcMacroId,
7979
};
8080

8181
/// Contains the results of (early) name resolution.
@@ -102,6 +102,7 @@ pub struct DefMap {
102102

103103
/// Side table for resolving derive helpers.
104104
exported_derives: FxHashMap<MacroDefId, Box<[Name]>>,
105+
fn_proc_macro_mapping: FxHashMap<ItemTreeId<item_tree::Function>, ProcMacroId>,
105106

106107
/// Custom attributes registered with `#![register_attr]`.
107108
registered_attrs: Vec<SmolStr>,
@@ -271,6 +272,7 @@ impl DefMap {
271272
recursion_limit: None,
272273
extern_prelude: FxHashMap::default(),
273274
exported_derives: FxHashMap::default(),
275+
fn_proc_macro_mapping: FxHashMap::default(),
274276
prelude: None,
275277
root,
276278
modules,
@@ -300,6 +302,11 @@ impl DefMap {
300302
self.root
301303
}
302304

305+
// FIXME: This is an odd interface....
306+
pub fn fn_as_proc_macro(&self, id: ItemTreeId<item_tree::Function>) -> Option<ProcMacroId> {
307+
self.fn_proc_macro_mapping.get(&id).copied()
308+
}
309+
303310
pub(crate) fn krate(&self) -> CrateId {
304311
self.krate
305312
}
@@ -453,6 +460,7 @@ impl DefMap {
453460
modules,
454461
registered_attrs,
455462
registered_tools,
463+
fn_proc_macro_mapping,
456464
block: _,
457465
edition: _,
458466
recursion_limit: _,
@@ -467,6 +475,7 @@ impl DefMap {
467475
modules.shrink_to_fit();
468476
registered_attrs.shrink_to_fit();
469477
registered_tools.shrink_to_fit();
478+
fn_proc_macro_mapping.shrink_to_fit();
470479
for (_, module) in modules.iter_mut() {
471480
module.children.shrink_to_fit();
472481
module.scope.shrink_to_fit();

crates/hir_def/src/nameres/collector.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -570,6 +570,7 @@ impl DefCollector<'_> {
570570
.exported_derives
571571
.insert(macro_id_to_def_id(self.db, proc_macro_id.into()), helpers);
572572
}
573+
self.def_map.fn_proc_macro_mapping.insert(id, proc_macro_id);
573574
}
574575

575576
/// Define a macro with `macro_rules`.

crates/hir_def/src/nameres/proc_macro.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,13 @@ use tt::{Leaf, TokenTree};
66
use crate::attr::Attrs;
77

88
#[derive(Debug, PartialEq, Eq)]
9-
pub(super) struct ProcMacroDef {
10-
pub(super) name: Name,
11-
pub(super) kind: ProcMacroKind,
9+
pub struct ProcMacroDef {
10+
pub name: Name,
11+
pub kind: ProcMacroKind,
1212
}
1313

1414
#[derive(Debug, PartialEq, Eq)]
15-
pub(super) enum ProcMacroKind {
15+
pub enum ProcMacroKind {
1616
CustomDerive { helpers: Box<[Name]> },
1717
FnLike,
1818
Attr,
@@ -30,7 +30,7 @@ impl ProcMacroKind {
3030

3131
impl Attrs {
3232
#[rustfmt::skip]
33-
pub(super) fn parse_proc_macro_decl(&self, func_name: &Name) -> Option<ProcMacroDef> {
33+
pub fn parse_proc_macro_decl(&self, func_name: &Name) -> Option<ProcMacroDef> {
3434
if self.is_proc_macro() {
3535
Some(ProcMacroDef { name: func_name.clone(), kind: ProcMacroKind::FnLike })
3636
} else if self.is_proc_macro_attribute() {

crates/ide/src/hover/tests.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4151,7 +4151,7 @@ struct Foo;
41514151
*Copy*
41524152
41534153
```rust
4154-
test
4154+
test::foo
41554155
```
41564156
41574157
```rust

0 commit comments

Comments
 (0)