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

Commit 83f91f6

Browse files
committed
Infect mbe crate with generic span type parameter
1 parent 2ee17bc commit 83f91f6

File tree

10 files changed

+360
-325
lines changed

10 files changed

+360
-325
lines changed

crates/mbe/src/benchmark.rs

Lines changed: 21 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,11 @@ use syntax::{
66
AstNode, SmolStr,
77
};
88
use test_utils::{bench, bench_fixture, skip_slow_tests};
9+
use tt::{Span, TokenId};
910

1011
use crate::{
1112
parser::{MetaVarKind, Op, RepeatKind, Separator},
12-
syntax_node_to_token_tree, tt, DeclarativeMacro,
13+
syntax_node_to_token_tree, DeclarativeMacro,
1314
};
1415

1516
#[test]
@@ -54,7 +55,7 @@ fn macro_rules_fixtures() -> FxHashMap<String, DeclarativeMacro> {
5455
.collect()
5556
}
5657

57-
fn macro_rules_fixtures_tt() -> FxHashMap<String, tt::Subtree> {
58+
fn macro_rules_fixtures_tt() -> FxHashMap<String, tt::Subtree<TokenId>> {
5859
let fixture = bench_fixture::numerous_macro_rules();
5960
let source_file = ast::SourceFile::parse(&fixture).ok().unwrap();
6061

@@ -71,7 +72,9 @@ fn macro_rules_fixtures_tt() -> FxHashMap<String, tt::Subtree> {
7172
}
7273

7374
/// Generate random invocation fixtures from rules
74-
fn invocation_fixtures(rules: &FxHashMap<String, DeclarativeMacro>) -> Vec<(String, tt::Subtree)> {
75+
fn invocation_fixtures(
76+
rules: &FxHashMap<String, DeclarativeMacro>,
77+
) -> Vec<(String, tt::Subtree<TokenId>)> {
7578
let mut seed = 123456789;
7679
let mut res = Vec::new();
7780

@@ -93,8 +96,8 @@ fn invocation_fixtures(rules: &FxHashMap<String, DeclarativeMacro>) -> Vec<(Stri
9396
loop {
9497
let mut subtree = tt::Subtree {
9598
delimiter: tt::Delimiter {
96-
open: tt::TokenId::UNSPECIFIED,
97-
close: tt::TokenId::UNSPECIFIED,
99+
open: tt::TokenId::DUMMY,
100+
close: tt::TokenId::DUMMY,
98101
kind: tt::DelimiterKind::Invisible,
99102
},
100103
token_trees: vec![],
@@ -116,7 +119,7 @@ fn invocation_fixtures(rules: &FxHashMap<String, DeclarativeMacro>) -> Vec<(Stri
116119
}
117120
return res;
118121

119-
fn collect_from_op(op: &Op, parent: &mut tt::Subtree, seed: &mut usize) {
122+
fn collect_from_op(op: &Op<TokenId>, parent: &mut tt::Subtree<TokenId>, seed: &mut usize) {
120123
return match op {
121124
Op::Var { kind, .. } => match kind.as_ref() {
122125
Some(MetaVarKind::Ident) => parent.token_trees.push(make_ident("foo")),
@@ -202,36 +205,30 @@ fn invocation_fixtures(rules: &FxHashMap<String, DeclarativeMacro>) -> Vec<(Stri
202205
*seed = usize::wrapping_add(usize::wrapping_mul(*seed, a), c);
203206
*seed
204207
}
205-
fn make_ident(ident: &str) -> tt::TokenTree {
206-
tt::Leaf::Ident(tt::Ident {
207-
span: tt::TokenId::unspecified(),
208-
text: SmolStr::new(ident),
209-
})
210-
.into()
208+
fn make_ident(ident: &str) -> tt::TokenTree<TokenId> {
209+
tt::Leaf::Ident(tt::Ident { span: tt::TokenId::DUMMY, text: SmolStr::new(ident) })
210+
.into()
211211
}
212-
fn make_punct(char: char) -> tt::TokenTree {
212+
fn make_punct(char: char) -> tt::TokenTree<TokenId> {
213213
tt::Leaf::Punct(tt::Punct {
214-
span: tt::TokenId::unspecified(),
214+
span: tt::TokenId::DUMMY,
215215
char,
216216
spacing: tt::Spacing::Alone,
217217
})
218218
.into()
219219
}
220-
fn make_literal(lit: &str) -> tt::TokenTree {
221-
tt::Leaf::Literal(tt::Literal {
222-
span: tt::TokenId::unspecified(),
223-
text: SmolStr::new(lit),
224-
})
225-
.into()
220+
fn make_literal(lit: &str) -> tt::TokenTree<TokenId> {
221+
tt::Leaf::Literal(tt::Literal { span: tt::TokenId::DUMMY, text: SmolStr::new(lit) })
222+
.into()
226223
}
227224
fn make_subtree(
228225
kind: tt::DelimiterKind,
229-
token_trees: Option<Vec<tt::TokenTree>>,
230-
) -> tt::TokenTree {
226+
token_trees: Option<Vec<tt::TokenTree<TokenId>>>,
227+
) -> tt::TokenTree<TokenId> {
231228
tt::Subtree {
232229
delimiter: tt::Delimiter {
233-
open: tt::TokenId::unspecified(),
234-
close: tt::TokenId::unspecified(),
230+
open: tt::TokenId::DUMMY,
231+
close: tt::TokenId::DUMMY,
235232
kind,
236233
},
237234
token_trees: token_trees.unwrap_or_default(),

crates/mbe/src/expander.rs

Lines changed: 24 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,16 @@ mod transcriber;
77

88
use rustc_hash::FxHashMap;
99
use syntax::SmolStr;
10+
use tt::Span;
1011

11-
use crate::{parser::MetaVarKind, tt, ExpandError, ExpandResult};
12+
use crate::{parser::MetaVarKind, ExpandError, ExpandResult};
1213

13-
pub(crate) fn expand_rules(
14-
rules: &[crate::Rule],
15-
input: &tt::Subtree,
14+
pub(crate) fn expand_rules<S: Span>(
15+
rules: &[crate::Rule<S>],
16+
input: &tt::Subtree<S>,
1617
is_2021: bool,
17-
) -> ExpandResult<tt::Subtree> {
18-
let mut match_: Option<(matcher::Match, &crate::Rule)> = None;
18+
) -> ExpandResult<tt::Subtree<S>> {
19+
let mut match_: Option<(matcher::Match<S>, &crate::Rule<S>)> = None;
1920
for rule in rules {
2021
let new_match = matcher::match_(&rule.lhs, input, is_2021);
2122

@@ -47,7 +48,7 @@ pub(crate) fn expand_rules(
4748
ExpandResult { value, err: match_.err.or(transcribe_err) }
4849
} else {
4950
ExpandResult::new(
50-
tt::Subtree { delimiter: tt::Delimiter::unspecified(), token_trees: vec![] },
51+
tt::Subtree { delimiter: tt::Delimiter::UNSPECIFIED, token_trees: vec![] },
5152
ExpandError::NoMatchingRule,
5253
)
5354
}
@@ -98,31 +99,37 @@ pub(crate) fn expand_rules(
9899
/// In other words, `Bindings` is a *multi* mapping from `SmolStr` to
99100
/// `tt::TokenTree`, where the index to select a particular `TokenTree` among
100101
/// many is not a plain `usize`, but a `&[usize]`.
101-
#[derive(Debug, Default, Clone, PartialEq, Eq)]
102-
struct Bindings {
103-
inner: FxHashMap<SmolStr, Binding>,
102+
#[derive(Debug, Clone, PartialEq, Eq)]
103+
struct Bindings<S> {
104+
inner: FxHashMap<SmolStr, Binding<S>>,
105+
}
106+
107+
impl<S> Default for Bindings<S> {
108+
fn default() -> Self {
109+
Self { inner: Default::default() }
110+
}
104111
}
105112

106113
#[derive(Debug, Clone, PartialEq, Eq)]
107-
enum Binding {
108-
Fragment(Fragment),
109-
Nested(Vec<Binding>),
114+
enum Binding<S> {
115+
Fragment(Fragment<S>),
116+
Nested(Vec<Binding<S>>),
110117
Empty,
111118
Missing(MetaVarKind),
112119
}
113120

114121
#[derive(Debug, Clone, PartialEq, Eq)]
115-
enum Fragment {
122+
enum Fragment<S> {
116123
/// token fragments are just copy-pasted into the output
117-
Tokens(tt::TokenTree),
124+
Tokens(tt::TokenTree<S>),
118125
/// Expr ast fragments are surrounded with `()` on insertion to preserve
119126
/// precedence. Note that this impl is different from the one currently in
120127
/// `rustc` -- `rustc` doesn't translate fragments into token trees at all.
121128
///
122129
/// At one point in time, we tried to use "fake" delimiters here à la
123130
/// proc-macro delimiter=none. As we later discovered, "none" delimiters are
124131
/// tricky to handle in the parser, and rustc doesn't handle those either.
125-
Expr(tt::TokenTree),
132+
Expr(tt::TokenTree<S>),
126133
/// There are roughly two types of paths: paths in expression context, where a
127134
/// separator `::` between an identifier and its following generic argument list
128135
/// is mandatory, and paths in type context, where `::` can be omitted.
@@ -132,5 +139,5 @@ enum Fragment {
132139
/// and is trasncribed as an expression-context path, verbatim transcription
133140
/// would cause a syntax error. We need to fix it up just before transcribing;
134141
/// see `transcriber::fix_up_and_push_path_tt()`.
135-
Path(tt::TokenTree),
142+
Path(tt::TokenTree<S>),
136143
}

0 commit comments

Comments
 (0)