Skip to content

Commit b98597f

Browse files
committed
Re-enable proc-macros
1 parent 98cfdde commit b98597f

File tree

24 files changed

+787
-493
lines changed

24 files changed

+787
-493
lines changed

Cargo.lock

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/base-db/src/fixture.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -543,6 +543,9 @@ impl ProcMacroExpander for IdentityProcMacroExpander {
543543
subtree: &Subtree<SpanData>,
544544
_: Option<&Subtree<SpanData>>,
545545
_: &Env,
546+
_: SpanData,
547+
_: SpanData,
548+
_: SpanData,
546549
) -> Result<Subtree<SpanData>, ProcMacroExpansionError> {
547550
Ok(subtree.clone())
548551
}
@@ -557,6 +560,9 @@ impl ProcMacroExpander for AttributeInputReplaceProcMacroExpander {
557560
_: &Subtree<SpanData>,
558561
attrs: Option<&Subtree<SpanData>>,
559562
_: &Env,
563+
_: SpanData,
564+
_: SpanData,
565+
_: SpanData,
560566
) -> Result<Subtree<SpanData>, ProcMacroExpansionError> {
561567
attrs
562568
.cloned()
@@ -572,6 +578,9 @@ impl ProcMacroExpander for MirrorProcMacroExpander {
572578
input: &Subtree<SpanData>,
573579
_: Option<&Subtree<SpanData>>,
574580
_: &Env,
581+
_: SpanData,
582+
_: SpanData,
583+
_: SpanData,
575584
) -> Result<Subtree<SpanData>, ProcMacroExpansionError> {
576585
fn traverse(input: &Subtree<SpanData>) -> Subtree<SpanData> {
577586
let mut token_trees = vec![];
@@ -599,6 +608,9 @@ impl ProcMacroExpander for ShortenProcMacroExpander {
599608
input: &Subtree<SpanData>,
600609
_: Option<&Subtree<SpanData>>,
601610
_: &Env,
611+
_: SpanData,
612+
_: SpanData,
613+
_: SpanData,
602614
) -> Result<Subtree<SpanData>, ProcMacroExpansionError> {
603615
return Ok(traverse(input));
604616

crates/base-db/src/input.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,9 @@ pub trait ProcMacroExpander: fmt::Debug + Send + Sync + RefUnwindSafe {
262262
subtree: &tt::Subtree<SpanData>,
263263
attrs: Option<&tt::Subtree<SpanData>>,
264264
env: &Env,
265+
def_site: SpanData,
266+
call_site: SpanData,
267+
mixed_site: SpanData,
265268
) -> Result<tt::Subtree<SpanData>, ProcMacroExpansionError>;
266269
}
267270

crates/base-db/src/span.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ impl SyntaxContextId {
3838
// currently (which kind of makes sense but we need it here!)
3939
pub const SELF_REF: Self =
4040
SyntaxContextId(unsafe { InternId::new_unchecked(InternId::MAX - 1) });
41+
/// Used syntax fixups
42+
pub const FAKE: Self = SyntaxContextId(unsafe { InternId::new_unchecked(InternId::MAX - 2) });
4143

4244
pub fn is_root(self) -> bool {
4345
self == Self::ROOT

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ mod proc_macros;
1616

1717
use std::{iter, ops::Range, sync};
1818

19-
use base_db::{fixture::WithFixture, ProcMacro, SourceDatabase};
19+
use base_db::{fixture::WithFixture, span::SpanData, ProcMacro, SourceDatabase};
2020
use expect_test::Expect;
2121
use hir_expand::{db::ExpandDatabase, span::SpanMapRef, HirFileIdExt, InFile, MacroFileId};
2222
use stdx::format_to;
@@ -307,6 +307,9 @@ impl base_db::ProcMacroExpander for IdentityWhenValidProcMacroExpander {
307307
subtree: &Subtree,
308308
_: Option<&Subtree>,
309309
_: &base_db::Env,
310+
_: SpanData,
311+
_: SpanData,
312+
_: SpanData,
310313
) -> Result<Subtree, base_db::ProcMacroExpansionError> {
311314
let (parse, _) =
312315
::mbe::token_tree_to_syntax_node(subtree, ::mbe::TopEntryPoint::MacroItems);

crates/hir-expand/src/db.rs

Lines changed: 41 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,17 @@ pub fn expand_speculative(
233233
let speculative_expansion = match loc.def.kind {
234234
MacroDefKind::ProcMacro(expander, ..) => {
235235
tt.delimiter = tt::Delimiter::UNSPECIFIED;
236-
expander.expand(db, loc.def.krate, loc.krate, &tt, attr_arg.as_ref())
236+
let call_site = loc.span(db);
237+
expander.expand(
238+
db,
239+
loc.def.krate,
240+
loc.krate,
241+
&tt,
242+
attr_arg.as_ref(),
243+
call_site,
244+
call_site,
245+
call_site,
246+
)
237247
}
238248
MacroDefKind::BuiltInAttr(BuiltinAttrExpander::Derive, _) => {
239249
pseudo_derive_attr_expansion(&tt, attr_arg.as_ref()?)
@@ -398,17 +408,23 @@ fn macro_arg(
398408
MacroCallKind::Attr { ast_id, .. } => ast_id.to_ptr(db).to_node(&root).syntax().clone(),
399409
};
400410
let censor = censor_for_macro_input(&loc, &syntax);
401-
// let mut fixups = fixup::fixup_syntax(&node);
402-
// fixups.replace.extend(censor.into_iter().map(|node| (node.into(), Vec::new())));
403-
// let (mut tt, tmap, _) = mbe::syntax_node_to_token_tree_with_modifications(
404-
// &node,
405-
// fixups.token_map,
406-
// fixups.next_id,
407-
// fixups.replace,
408-
// fixups.append,
409-
// );
410-
411-
let mut tt = mbe::syntax_node_to_token_tree_censored(&syntax, map.as_ref(), censor);
411+
let mut tt = match loc.kind {
412+
MacroCallKind::FnLike { .. } => {
413+
mbe::syntax_node_to_token_tree_censored(&syntax, map.as_ref(), censor)
414+
}
415+
MacroCallKind::Derive { .. } | MacroCallKind::Attr { .. } => {
416+
// let mut fixups = crate::fixup::fixup_syntax(&syntax);
417+
// fixups.replace.extend(censor.into_iter().map(|node| (node.into(), Vec::new())));
418+
// let (mut tt, tmap, _) = mbe::syntax_node_to_token_tree_with_modifications(
419+
// &node,
420+
// fixups.token_map,
421+
// fixups.next_id,
422+
// fixups.replace,
423+
// fixups.append,
424+
// );
425+
mbe::syntax_node_to_token_tree_censored(&syntax, map.as_ref(), censor)
426+
}
427+
};
412428

413429
if loc.def.is_proc_macro() {
414430
// proc macros expect their inputs without parentheses, MBEs expect it with them included
@@ -658,8 +674,19 @@ fn expand_proc_macro(db: &dyn ExpandDatabase, id: MacroCallId) -> ExpandResult<A
658674
_ => None,
659675
};
660676

661-
let ExpandResult { value: tt, err } =
662-
expander.expand(db, loc.def.krate, loc.krate, &macro_arg, attr_arg);
677+
let call_site = loc.span(db);
678+
let ExpandResult { value: tt, err } = expander.expand(
679+
db,
680+
loc.def.krate,
681+
loc.krate,
682+
&macro_arg,
683+
attr_arg,
684+
// FIXME
685+
call_site,
686+
call_site,
687+
// FIXME
688+
call_site,
689+
);
663690

664691
// Set a hard limit for the expanded tt
665692
if let Err(value) = check_tt_count(&tt) {

0 commit comments

Comments
 (0)