Skip to content

Commit 9ca83b8

Browse files
committed
Pass calling span through to builtin macro expansions
1 parent f48fa0c commit 9ca83b8

File tree

26 files changed

+573
-446
lines changed

26 files changed

+573
-446
lines changed

crates/hir-def/src/data.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -663,7 +663,7 @@ impl<'a> AssocItemCollector<'a> {
663663
self.module_id.local_id,
664664
MacroCallKind::Attr {
665665
ast_id,
666-
attr_args: Arc::new(tt::Subtree::empty()),
666+
attr_args: None,
667667
invoc_attr_index: attr.id,
668668
},
669669
attr.path().clone(),

crates/hir-def/src/lib.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1351,19 +1351,19 @@ fn attr_macro_as_call_id(
13511351
let arg = match macro_attr.input.as_deref() {
13521352
Some(AttrInput::TokenTree(tt)) => {
13531353
let mut tt = tt.as_ref().clone();
1354-
tt.delimiter = tt::Delimiter::UNSPECIFIED;
1355-
tt
1354+
tt.delimiter = tt::Delimiter::DUMMY_INVISIBLE;
1355+
Some(tt)
13561356
}
13571357

1358-
_ => tt::Subtree::empty(),
1358+
_ => None,
13591359
};
13601360

13611361
def.as_lazy_macro(
13621362
db.upcast(),
13631363
krate,
13641364
MacroCallKind::Attr {
13651365
ast_id: item_attr.ast_id,
1366-
attr_args: Arc::new(arg),
1366+
attr_args: arg.map(Arc::new),
13671367
invoc_attr_index: macro_attr.id,
13681368
},
13691369
macro_attr.ctxt,

crates/hir-def/src/macro_expansion_tests/mbe.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ macro_rules! identity {
138138
}
139139
140140
fn main(foo: ()) {
141-
builtin#FileId(0):[email protected]\0# ##FileId(0):[email protected]\0#format_args#FileId(0):[email protected]\0# (#FileId(0):[email protected]\0#"{} {} {}"#FileId(0):[email protected]\0#,#FileId(0):[email protected]\0# format_args#FileId(0):[email protected]\0#!#FileId(0):[email protected]\0#(#FileId(0):[email protected]\0#"{}"#FileId(0):[email protected]\0#,#FileId(0):[email protected]\0# 0#FileId(0):[email protected]\0#)#FileId(0):[email protected]\0#,#FileId(0):[email protected]\0# foo#FileId(0):[email protected]\0#,#FileId(0):[email protected]\0# identity#FileId(0):[email protected]\0#!#FileId(0):[email protected]\0#(#FileId(0):[email protected]\0#10#FileId(0):[email protected]\0#)#FileId(0):[email protected]\0#,#FileId(0):[email protected]\0# "bar"#FileId(0):[email protected]\0#)#FileId(0):[email protected]\0#
141+
builtin#FileId(0):[email protected]\3# ##FileId(0):[email protected]\3#format_args#FileId(0):[email protected]\3# (#FileId(0):[email protected]\0#"{} {} {}"#FileId(0):[email protected]\0#,#FileId(0):[email protected]\0# format_args#FileId(0):[email protected]\0#!#FileId(0):[email protected]\0#(#FileId(0):[email protected]\0#"{}"#FileId(0):[email protected]\0#,#FileId(0):[email protected]\0# 0#FileId(0):[email protected]\0#)#FileId(0):[email protected]\0#,#FileId(0):[email protected]\0# foo#FileId(0):[email protected]\0#,#FileId(0):[email protected]\0# identity#FileId(0):[email protected]\0#!#FileId(0):[email protected]\0#(#FileId(0):[email protected]\0#10#FileId(0):[email protected]\0#)#FileId(0):[email protected]\0#,#FileId(0):[email protected]\0# "bar"#FileId(0):[email protected]\0#)#FileId(0):[email protected]\0#
142142
}
143143
144144
"##]],

crates/hir-def/src/nameres/collector.rs

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
66
use std::{cmp::Ordering, iter, mem};
77

8-
use ::tt::Span;
98
use base_db::{span::SyntaxContextId, CrateId, Dependency, Edition, FileId};
109
use cfg::{CfgExpr, CfgOptions};
1110
use either::Either;
@@ -85,7 +84,17 @@ pub(super) fn collect_defs(db: &dyn DefDatabase, def_map: DefMap, tree_id: TreeI
8584
.enumerate()
8685
.map(|(idx, it)| {
8786
// FIXME: a hacky way to create a Name from string.
88-
let name = tt::Ident { text: it.name.clone(), span: tt::SpanData::DUMMY };
87+
let name = tt::Ident {
88+
text: it.name.clone(),
89+
span: tt::SpanData {
90+
range: syntax::TextRange::empty(syntax::TextSize::new(0)),
91+
anchor: base_db::span::SpanAnchor {
92+
file_id: FileId::BOGUS,
93+
ast_id: base_db::span::ROOT_ERASED_FILE_AST_ID,
94+
},
95+
ctx: SyntaxContextId::ROOT,
96+
},
97+
};
8998
(name.as_name(), ProcMacroExpander::new(base_db::ProcMacroId(idx as u32)))
9099
})
91100
.collect())
@@ -476,7 +485,7 @@ impl DefCollector<'_> {
476485
directive.module_id,
477486
MacroCallKind::Attr {
478487
ast_id: ast_id.ast_id,
479-
attr_args: Arc::new(tt::Subtree::empty()),
488+
attr_args: None,
480489
invoc_attr_index: attr.id,
481490
},
482491
attr.path().clone(),
@@ -2079,7 +2088,18 @@ impl ModCollector<'_, '_> {
20792088
let name = match attrs.by_key("rustc_builtin_macro").string_value() {
20802089
Some(it) => {
20812090
// FIXME: a hacky way to create a Name from string.
2082-
name = tt::Ident { text: it.clone(), span: tt::SpanData::DUMMY }.as_name();
2091+
name = tt::Ident {
2092+
text: it.clone(),
2093+
span: tt::SpanData {
2094+
range: syntax::TextRange::empty(syntax::TextSize::new(0)),
2095+
anchor: base_db::span::SpanAnchor {
2096+
file_id: FileId::BOGUS,
2097+
ast_id: base_db::span::ROOT_ERASED_FILE_AST_ID,
2098+
},
2099+
ctx: SyntaxContextId::ROOT,
2100+
},
2101+
}
2102+
.as_name();
20832103
&name
20842104
}
20852105
None => {

crates/hir-expand/src/attrs.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ impl RawAttrs {
130130
let attrs = parts.enumerate().take(1 << AttrId::CFG_ATTR_BITS).filter_map(
131131
|(idx, attr)| {
132132
let tree = Subtree {
133-
delimiter: tt::Delimiter::unspecified(),
133+
delimiter: tt::Delimiter::dummy_invisible(),
134134
token_trees: attr.to_vec(),
135135
};
136136
Attr::from_tt(db, &tree, index.with_cfg_attr(idx))
@@ -296,7 +296,7 @@ impl Attr {
296296
// FIXME: This is necessarily a hack. It'd be nice if we could avoid allocation
297297
// here or maybe just parse a mod path from a token tree directly
298298
let subtree = tt::Subtree {
299-
delimiter: tt::Delimiter::unspecified(),
299+
delimiter: tt::Delimiter::dummy_invisible(),
300300
token_trees: tts.to_vec(),
301301
};
302302
let (parse, span_map) =

crates/hir-expand/src/builtin_attr_macro.rs

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,22 @@
11
//! Builtin attributes.
22
3-
use ::tt::Span;
3+
use base_db::{
4+
span::{SyntaxContextId, ROOT_ERASED_FILE_AST_ID},
5+
FileId,
6+
};
7+
use syntax::{TextRange, TextSize};
48

59
use crate::{db::ExpandDatabase, name, tt, ExpandResult, MacroCallId, MacroCallKind};
610

711
macro_rules! register_builtin {
8-
( $(($name:ident, $variant:ident) => $expand:ident),* ) => {
12+
($expand_fn:ident: $(($name:ident, $variant:ident) => $expand:ident),* ) => {
913
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
1014
pub enum BuiltinAttrExpander {
1115
$($variant),*
1216
}
1317

1418
impl BuiltinAttrExpander {
15-
pub fn expand(
19+
pub fn $expand_fn(
1620
&self,
1721
db: &dyn ExpandDatabase,
1822
id: MacroCallId,
@@ -47,7 +51,7 @@ impl BuiltinAttrExpander {
4751
}
4852
}
4953

50-
register_builtin! {
54+
register_builtin! { expand:
5155
(bench, Bench) => dummy_attr_expand,
5256
(cfg_accessible, CfgAccessible) => dummy_attr_expand,
5357
(cfg_eval, CfgEval) => dummy_attr_expand,
@@ -99,21 +103,31 @@ fn derive_attr_expand(
99103
) -> ExpandResult<tt::Subtree> {
100104
let loc = db.lookup_intern_macro_call(id);
101105
let derives = match &loc.kind {
102-
MacroCallKind::Attr { attr_args, .. } if loc.def.is_attribute_derive() => attr_args,
103-
_ => return ExpandResult::ok(tt::Subtree::empty()),
106+
MacroCallKind::Attr { attr_args: Some(attr_args), .. } if loc.def.is_attribute_derive() => {
107+
attr_args
108+
}
109+
_ => return ExpandResult::ok(tt::Subtree::empty(tt::DelimSpan::DUMMY)),
104110
};
105-
pseudo_derive_attr_expansion(tt, derives)
111+
pseudo_derive_attr_expansion(tt, derives, loc.call_site)
106112
}
107113

108114
pub fn pseudo_derive_attr_expansion(
109115
tt: &tt::Subtree,
110116
args: &tt::Subtree,
117+
call_site: SyntaxContextId,
111118
) -> ExpandResult<tt::Subtree> {
112119
let mk_leaf = |char| {
113120
tt::TokenTree::Leaf(tt::Leaf::Punct(tt::Punct {
114121
char,
115122
spacing: tt::Spacing::Alone,
116-
span: tt::SpanData::DUMMY,
123+
span: tt::SpanData {
124+
range: TextRange::empty(TextSize::new(0)),
125+
anchor: base_db::span::SpanAnchor {
126+
file_id: FileId::BOGUS,
127+
ast_id: ROOT_ERASED_FILE_AST_ID,
128+
},
129+
ctx: call_site,
130+
},
117131
}))
118132
};
119133

0 commit comments

Comments
 (0)