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

Commit f79439c

Browse files
committed
Infect proc-macro-api crate with generic span type parameter
1 parent 83f91f6 commit f79439c

File tree

3 files changed

+180
-85
lines changed

3 files changed

+180
-85
lines changed

crates/proc-macro-api/src/lib.rs

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,8 @@ use triomphe::Arc;
1717

1818
use serde::{Deserialize, Serialize};
1919

20-
use ::tt::token_id as tt;
21-
2220
use crate::{
23-
msg::{ExpandMacro, FlatTree, PanicMessage},
21+
msg::{flat::SerializableSpan, ExpandMacro, FlatTree, PanicMessage},
2422
process::ProcMacroProcessSrv,
2523
};
2624

@@ -134,12 +132,12 @@ impl ProcMacro {
134132
self.kind
135133
}
136134

137-
pub fn expand(
135+
pub fn expand<const L: usize, S: SerializableSpan<L>>(
138136
&self,
139-
subtree: &tt::Subtree,
140-
attr: Option<&tt::Subtree>,
137+
subtree: &tt::Subtree<S>,
138+
attr: Option<&tt::Subtree<S>>,
141139
env: Vec<(String, String)>,
142-
) -> Result<Result<tt::Subtree, PanicMessage>, ServerError> {
140+
) -> Result<Result<tt::Subtree<S>, PanicMessage>, ServerError> {
143141
let version = self.process.lock().unwrap_or_else(|e| e.into_inner()).version();
144142
let current_dir = env
145143
.iter()

crates/proc-macro-api/src/msg.rs

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,15 @@ pub use crate::msg::flat::FlatTree;
1616
pub const NO_VERSION_CHECK_VERSION: u32 = 0;
1717
pub const VERSION_CHECK_VERSION: u32 = 1;
1818
pub const ENCODE_CLOSE_SPAN_VERSION: u32 = 2;
19+
/// This version changes how spans are encoded, kind of. Prior to this version,
20+
/// spans were represented as a single u32 which effectively forced spans to be
21+
/// token ids. Starting with this version, the span fields are still u32,
22+
/// but if the size of the span is greater than 1 then the span data is encoded in
23+
/// an additional vector where the span represents the offset into that vector.
24+
/// This allows encoding bigger spans while supporting the previous versions.
25+
pub const VARIABLE_SIZED_SPANS: u32 = 2;
1926

20-
pub const CURRENT_API_VERSION: u32 = ENCODE_CLOSE_SPAN_VERSION;
27+
pub const CURRENT_API_VERSION: u32 = VARIABLE_SIZED_SPANS;
2128

2229
#[derive(Debug, Serialize, Deserialize)]
2330
pub enum Request {
@@ -115,10 +122,14 @@ fn write_json(out: &mut impl Write, msg: &str) -> io::Result<()> {
115122

116123
#[cfg(test)]
117124
mod tests {
125+
use tt::{
126+
Delimiter, DelimiterKind, Ident, Leaf, Literal, Punct, Spacing, Span, Subtree, TokenId,
127+
TokenTree,
128+
};
129+
118130
use super::*;
119-
use crate::tt::*;
120131

121-
fn fixture_token_tree() -> Subtree {
132+
fn fixture_token_tree() -> Subtree<TokenId> {
122133
let mut subtree = Subtree { delimiter: Delimiter::unspecified(), token_trees: Vec::new() };
123134
subtree
124135
.token_trees
@@ -128,17 +139,17 @@ mod tests {
128139
.push(TokenTree::Leaf(Ident { text: "Foo".into(), span: TokenId(1) }.into()));
129140
subtree.token_trees.push(TokenTree::Leaf(Leaf::Literal(Literal {
130141
text: "Foo".into(),
131-
span: TokenId::unspecified(),
142+
span: TokenId::DUMMY,
132143
})));
133144
subtree.token_trees.push(TokenTree::Leaf(Leaf::Punct(Punct {
134145
char: '@',
135-
span: TokenId::unspecified(),
146+
span: TokenId::DUMMY,
136147
spacing: Spacing::Joint,
137148
})));
138149
subtree.token_trees.push(TokenTree::Subtree(Subtree {
139150
delimiter: Delimiter {
140151
open: TokenId(2),
141-
close: TokenId::UNSPECIFIED,
152+
close: TokenId::DUMMY,
142153
kind: DelimiterKind::Brace,
143154
},
144155
token_trees: vec![],

0 commit comments

Comments
 (0)