Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit cff209f

Browse files
committed
WIP: Actually fix up syntax errors in attribute macro input
1 parent 212e82f commit cff209f

File tree

6 files changed

+112
-34
lines changed

6 files changed

+112
-34
lines changed

crates/hir_def/src/macro_expansion_tests.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -345,6 +345,7 @@ impl base_db::ProcMacroExpander for IdentityWhenValidProcMacroExpander {
345345
if parse.errors().is_empty() {
346346
Ok(subtree.clone())
347347
} else {
348+
eprintln!("parse errors: {:?}", parse.errors());
348349
use tt::{Delimiter, DelimiterKind, Ident, Leaf, Literal, Punct, TokenTree};
349350
let mut subtree = Subtree::default();
350351
subtree.token_trees.push(TokenTree::Leaf(

crates/hir_def/src/macro_expansion_tests/proc_macros.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,10 @@ fn foo() { bar.; blub }
8686
expect![[r##"
8787
#[proc_macros::identity_when_valid]
8888
fn foo() { bar.; blub }
89-
"##]],
89+
90+
fn foo() {
91+
bar.;
92+
blub
93+
}"##]],
9094
);
9195
}

crates/hir_expand/src/db.rs

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,16 @@ use std::sync::Arc;
55
use base_db::{salsa, SourceDatabase};
66
use either::Either;
77
use limit::Limit;
8-
use mbe::{syntax_node_to_token_tree, ExpandError, ExpandResult};
9-
use rustc_hash::FxHashSet;
8+
use mbe::{syntax_node_to_token_tree, ExpandError, ExpandResult, SyntheticToken};
9+
use rustc_hash::{FxHashMap, FxHashSet};
1010
use syntax::{
1111
algo::diff,
1212
ast::{self, HasAttrs, HasDocComments},
1313
AstNode, GreenNode, Parse, SyntaxNode, SyntaxToken, T,
1414
};
1515

1616
use crate::{
17-
ast_id_map::AstIdMap, hygiene::HygieneFrame, BuiltinAttrExpander, BuiltinDeriveExpander,
17+
ast_id_map::AstIdMap, fixup, hygiene::HygieneFrame, BuiltinAttrExpander, BuiltinDeriveExpander,
1818
BuiltinFnLikeExpander, ExpandTo, HirFileId, HirFileIdRepr, MacroCallId, MacroCallKind,
1919
MacroCallLoc, MacroDefId, MacroDefKind, MacroFile, ProcMacroExpander,
2020
};
@@ -146,8 +146,10 @@ pub fn expand_speculative(
146146

147147
// Build the subtree and token mapping for the speculative args
148148
let censor = censor_for_macro_input(&loc, &speculative_args);
149+
let mut fixups = fixup::fixup_syntax(&speculative_args);
150+
fixups.replace.extend(censor.into_iter().map(|node| (node, Vec::new())));
149151
let (mut tt, spec_args_tmap) =
150-
mbe::syntax_node_to_token_tree_censored(&speculative_args, &censor);
152+
mbe::syntax_node_to_token_tree_censored(&speculative_args, fixups.replace, fixups.append);
151153

152154
let (attr_arg, token_id) = match loc.kind {
153155
MacroCallKind::Attr { invoc_attr_index, .. } => {
@@ -294,8 +296,17 @@ fn macro_arg(db: &dyn AstDatabase, id: MacroCallId) -> Option<Arc<(tt::Subtree,
294296
let loc = db.lookup_intern_macro_call(id);
295297

296298
let node = SyntaxNode::new_root(arg);
299+
eprintln!("input text:\n{node}");
300+
eprintln!("input syntax:\n{node:#?}");
297301
let censor = censor_for_macro_input(&loc, &node);
298-
let (mut tt, tmap) = mbe::syntax_node_to_token_tree_censored(&node, &censor);
302+
// TODO only fixup for attribute macro input
303+
let mut fixups = fixup::fixup_syntax(&node);
304+
fixups.replace.extend(censor.into_iter().map(|node| (node, Vec::new())));
305+
eprintln!("fixups: {fixups:?}");
306+
let (mut tt, tmap) =
307+
mbe::syntax_node_to_token_tree_censored(&node, fixups.replace, fixups.append);
308+
309+
eprintln!("fixed-up input: {}", tt);
299310

300311
if loc.def.is_proc_macro() {
301312
// proc macros expect their inputs without parentheses, MBEs expect it with them included

crates/hir_expand/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ pub mod proc_macro;
1515
pub mod quote;
1616
pub mod eager;
1717
pub mod mod_path;
18+
mod fixup;
1819

1920
pub use mbe::{ExpandError, ExpandResult, Origin};
2021

crates/mbe/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ pub use tt::{Delimiter, DelimiterKind, Punct};
3030
pub use crate::{
3131
syntax_bridge::{
3232
parse_exprs_with_sep, parse_to_token_tree, syntax_node_to_token_tree,
33-
syntax_node_to_token_tree_censored, token_tree_to_syntax_node,
33+
syntax_node_to_token_tree_censored, token_tree_to_syntax_node, SyntheticToken,
3434
},
3535
token_map::TokenMap,
3636
};

crates/mbe/src/syntax_bridge.rs

Lines changed: 88 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -15,22 +15,26 @@ use crate::{to_parser_input::to_parser_input, tt_iter::TtIter, TokenMap};
1515
/// Convert the syntax node to a `TokenTree` (what macro
1616
/// will consume).
1717
pub fn syntax_node_to_token_tree(node: &SyntaxNode) -> (tt::Subtree, TokenMap) {
18-
syntax_node_to_token_tree_censored(node, &Default::default())
18+
syntax_node_to_token_tree_censored(node, Default::default(), Default::default())
1919
}
2020

21+
// TODO rename
2122
/// Convert the syntax node to a `TokenTree` (what macro will consume)
2223
/// with the censored range excluded.
2324
pub fn syntax_node_to_token_tree_censored(
2425
node: &SyntaxNode,
25-
censor: &FxHashSet<SyntaxNode>,
26+
replace: FxHashMap<SyntaxNode, Vec<SyntheticToken>>,
27+
append: FxHashMap<SyntaxNode, Vec<SyntheticToken>>,
2628
) -> (tt::Subtree, TokenMap) {
2729
let global_offset = node.text_range().start();
28-
let mut c = Convertor::new(node, global_offset, censor);
30+
let mut c = Convertor::new(node, global_offset, replace, append);
2931
let subtree = convert_tokens(&mut c);
3032
c.id_alloc.map.shrink_to_fit();
3133
(subtree, c.id_alloc.map)
3234
}
3335

36+
pub type SyntheticToken = (SyntaxKind, SmolStr);
37+
3438
// The following items are what `rustc` macro can be parsed into :
3539
// link: https://github.com/rust-lang/rust/blob/9ebf47851a357faa4cd97f4b1dc7835f6376e639/src/libsyntax/ext/expand.rs#L141
3640
// * Expr(P<ast::Expr>) -> token_tree_to_expr
@@ -465,86 +469,124 @@ impl<'a> TokenConvertor for RawConvertor<'a> {
465469
}
466470
}
467471

468-
struct Convertor<'c> {
472+
struct Convertor {
469473
id_alloc: TokenIdAlloc,
470474
current: Option<SyntaxToken>,
475+
current_synthetic: Vec<SyntheticToken>,
471476
preorder: PreorderWithTokens,
472-
censor: &'c FxHashSet<SyntaxNode>,
477+
replace: FxHashMap<SyntaxNode, Vec<SyntheticToken>>,
478+
append: FxHashMap<SyntaxNode, Vec<SyntheticToken>>,
473479
range: TextRange,
474480
punct_offset: Option<(SyntaxToken, TextSize)>,
475481
}
476482

477-
impl<'c> Convertor<'c> {
483+
impl Convertor {
478484
fn new(
479485
node: &SyntaxNode,
480486
global_offset: TextSize,
481-
censor: &'c FxHashSet<SyntaxNode>,
482-
) -> Convertor<'c> {
487+
replace: FxHashMap<SyntaxNode, Vec<SyntheticToken>>,
488+
append: FxHashMap<SyntaxNode, Vec<SyntheticToken>>,
489+
) -> Convertor {
483490
let range = node.text_range();
484491
let mut preorder = node.preorder_with_tokens();
485-
let first = Self::next_token(&mut preorder, censor);
492+
let (first, synthetic) = Self::next_token(&mut preorder, &replace, &append);
486493
Convertor {
487494
id_alloc: { TokenIdAlloc { map: TokenMap::default(), global_offset, next_id: 0 } },
488495
current: first,
496+
current_synthetic: synthetic,
489497
preorder,
490498
range,
491-
censor,
499+
replace,
500+
append,
492501
punct_offset: None,
493502
}
494503
}
495504

496505
fn next_token(
497506
preorder: &mut PreorderWithTokens,
498-
censor: &FxHashSet<SyntaxNode>,
499-
) -> Option<SyntaxToken> {
507+
replace: &FxHashMap<SyntaxNode, Vec<SyntheticToken>>,
508+
append: &FxHashMap<SyntaxNode, Vec<SyntheticToken>>,
509+
) -> (Option<SyntaxToken>, Vec<SyntheticToken>) {
500510
while let Some(ev) = preorder.next() {
501511
let ele = match ev {
502512
WalkEvent::Enter(ele) => ele,
513+
WalkEvent::Leave(SyntaxElement::Node(node)) => {
514+
if let Some(v) = append.get(&node) {
515+
eprintln!("after {:?}, appending {:?}", node, v);
516+
if !v.is_empty() {
517+
let mut reversed = v.clone();
518+
reversed.reverse();
519+
return (None, reversed);
520+
}
521+
}
522+
continue;
523+
}
503524
_ => continue,
504525
};
505526
match ele {
506-
SyntaxElement::Token(t) => return Some(t),
507-
SyntaxElement::Node(node) if censor.contains(&node) => preorder.skip_subtree(),
508-
SyntaxElement::Node(_) => (),
527+
SyntaxElement::Token(t) => return (Some(t), Vec::new()),
528+
SyntaxElement::Node(node) => {
529+
if let Some(v) = replace.get(&node) {
530+
preorder.skip_subtree();
531+
eprintln!("replacing {:?} by {:?}", node, v);
532+
if !v.is_empty() {
533+
let mut reversed = v.clone();
534+
reversed.reverse();
535+
return (None, reversed);
536+
}
537+
}
538+
}
509539
}
510540
}
511-
None
541+
(None, Vec::new())
512542
}
513543
}
514544

515545
#[derive(Debug)]
516546
enum SynToken {
517547
Ordinary(SyntaxToken),
548+
// FIXME is this supposed to be `Punct`?
518549
Punch(SyntaxToken, TextSize),
550+
Synthetic(SyntheticToken),
519551
}
520552

521553
impl SynToken {
522-
fn token(&self) -> &SyntaxToken {
554+
fn token(&self) -> Option<&SyntaxToken> {
523555
match self {
524-
SynToken::Ordinary(it) | SynToken::Punch(it, _) => it,
556+
SynToken::Ordinary(it) | SynToken::Punch(it, _) => Some(it),
557+
SynToken::Synthetic(_) => None,
525558
}
526559
}
527560
}
528561

529-
impl<'a> SrcToken<Convertor<'a>> for SynToken {
530-
fn kind(&self, _ctx: &Convertor<'a>) -> SyntaxKind {
531-
self.token().kind()
562+
impl SrcToken<Convertor> for SynToken {
563+
fn kind(&self, _ctx: &Convertor) -> SyntaxKind {
564+
match self {
565+
SynToken::Ordinary(token) => token.kind(),
566+
SynToken::Punch(token, _) => token.kind(),
567+
SynToken::Synthetic((kind, _)) => *kind,
568+
}
532569
}
533-
fn to_char(&self, _ctx: &Convertor<'a>) -> Option<char> {
570+
fn to_char(&self, _ctx: &Convertor) -> Option<char> {
534571
match self {
535572
SynToken::Ordinary(_) => None,
536573
SynToken::Punch(it, i) => it.text().chars().nth((*i).into()),
574+
SynToken::Synthetic(_) => None,
537575
}
538576
}
539-
fn to_text(&self, _ctx: &Convertor<'a>) -> SmolStr {
540-
self.token().text().into()
577+
fn to_text(&self, _ctx: &Convertor) -> SmolStr {
578+
match self {
579+
SynToken::Ordinary(token) => token.text().into(),
580+
SynToken::Punch(token, _) => token.text().into(),
581+
SynToken::Synthetic((_, text)) => text.clone(),
582+
}
541583
}
542584
}
543585

544-
impl TokenConvertor for Convertor<'_> {
586+
impl TokenConvertor for Convertor {
545587
type Token = SynToken;
546588
fn convert_doc_comment(&self, token: &Self::Token) -> Option<Vec<tt::TokenTree>> {
547-
convert_doc_comment(token.token())
589+
convert_doc_comment(token.token()?)
548590
}
549591

550592
fn bump(&mut self) -> Option<(Self::Token, TextRange)> {
@@ -558,11 +600,25 @@ impl TokenConvertor for Convertor<'_> {
558600
}
559601
}
560602

603+
if let Some(synth_token) = self.current_synthetic.pop() {
604+
if self.current_synthetic.is_empty() {
605+
let (new_current, new_synth) =
606+
Self::next_token(&mut self.preorder, &self.replace, &self.append);
607+
self.current = new_current;
608+
self.current_synthetic = new_synth;
609+
}
610+
// TODO fix range?
611+
return Some((SynToken::Synthetic(synth_token), self.range));
612+
}
613+
561614
let curr = self.current.clone()?;
562615
if !&self.range.contains_range(curr.text_range()) {
563616
return None;
564617
}
565-
self.current = Self::next_token(&mut self.preorder, self.censor);
618+
let (new_current, new_synth) =
619+
Self::next_token(&mut self.preorder, &self.replace, &self.append);
620+
self.current = new_current;
621+
self.current_synthetic = new_synth;
566622
let token = if curr.kind().is_punct() {
567623
self.punct_offset = Some((curr.clone(), 0.into()));
568624
let range = curr.text_range();
@@ -585,6 +641,11 @@ impl TokenConvertor for Convertor<'_> {
585641
}
586642
}
587643

644+
if let Some(synth_token) = self.current_synthetic.last() {
645+
// TODO fix range?
646+
return Some(SynToken::Synthetic(synth_token.clone()));
647+
}
648+
588649
let curr = self.current.clone()?;
589650
if !self.range.contains_range(curr.text_range()) {
590651
return None;

0 commit comments

Comments
 (0)