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

Commit c479804

Browse files
committed
Auto merge of rust-lang#14572 - Veykril:proc-macro-close-span, r=Veykril
Encode closing delimiter span in FlatTrees Mainly serves as a test for the api versioning, as I don't think we make use of the closing span yet.
2 parents 10e0aaf + 9fb1b04 commit c479804

File tree

5 files changed

+68
-26
lines changed

5 files changed

+68
-26
lines changed

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

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -146,15 +146,16 @@ impl ProcMacro {
146146
attr: Option<&tt::Subtree>,
147147
env: Vec<(String, String)>,
148148
) -> Result<Result<tt::Subtree, PanicMessage>, ServerError> {
149+
let version = self.process.lock().unwrap_or_else(|e| e.into_inner()).version();
149150
let current_dir = env
150151
.iter()
151152
.find(|(name, _)| name == "CARGO_MANIFEST_DIR")
152153
.map(|(_, value)| value.clone());
153154

154155
let task = ExpandMacro {
155-
macro_body: FlatTree::new(subtree),
156+
macro_body: FlatTree::new(subtree, version),
156157
macro_name: self.name.to_string(),
157-
attributes: attr.map(FlatTree::new),
158+
attributes: attr.map(|subtree| FlatTree::new(subtree, version)),
158159
lib: self.dylib_path.to_path_buf().into(),
159160
env,
160161
current_dir,
@@ -163,7 +164,9 @@ impl ProcMacro {
163164
let request = msg::Request::ExpandMacro(task);
164165
let response = self.process.lock().unwrap_or_else(|e| e.into_inner()).send_task(request)?;
165166
match response {
166-
msg::Response::ExpandMacro(it) => Ok(it.map(FlatTree::to_subtree)),
167+
msg::Response::ExpandMacro(it) => {
168+
Ok(it.map(|tree| FlatTree::to_subtree(tree, version)))
169+
}
167170
msg::Response::ListMacros(..) | msg::Response::ApiVersionCheck(..) => {
168171
Err(ServerError { message: "unexpected response".to_string(), io: None })
169172
}

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

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,12 @@ use crate::ProcMacroKind;
1212

1313
pub use crate::msg::flat::FlatTree;
1414

15+
// The versions of the server protocol
1516
pub const NO_VERSION_CHECK_VERSION: u32 = 0;
16-
pub const CURRENT_API_VERSION: u32 = 1;
17+
pub const VERSION_CHECK_VERSION: u32 = 1;
18+
pub const ENCODE_CLOSE_SPAN_VERSION: u32 = 2;
19+
20+
pub const CURRENT_API_VERSION: u32 = ENCODE_CLOSE_SPAN_VERSION;
1721

1822
#[derive(Debug, Serialize, Deserialize)]
1923
pub enum Request {
@@ -146,7 +150,7 @@ mod tests {
146150
fn test_proc_macro_rpc_works() {
147151
let tt = fixture_token_tree();
148152
let task = ExpandMacro {
149-
macro_body: FlatTree::new(&tt),
153+
macro_body: FlatTree::new(&tt, CURRENT_API_VERSION),
150154
macro_name: Default::default(),
151155
attributes: None,
152156
lib: std::env::current_dir().unwrap(),
@@ -158,6 +162,6 @@ mod tests {
158162
// println!("{}", json);
159163
let back: ExpandMacro = serde_json::from_str(&json).unwrap();
160164

161-
assert_eq!(tt, back.macro_body.to_subtree());
165+
assert_eq!(tt, back.macro_body.to_subtree(CURRENT_API_VERSION));
162166
}
163167
}

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

Lines changed: 44 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,10 @@ use std::collections::{HashMap, VecDeque};
3939

4040
use serde::{Deserialize, Serialize};
4141

42-
use crate::tt::{self, TokenId};
42+
use crate::{
43+
msg::ENCODE_CLOSE_SPAN_VERSION,
44+
tt::{self, TokenId},
45+
};
4346

4447
#[derive(Serialize, Deserialize, Debug)]
4548
pub struct FlatTree {
@@ -52,7 +55,8 @@ pub struct FlatTree {
5255
}
5356

5457
struct SubtreeRepr {
55-
id: tt::TokenId,
58+
open: tt::TokenId,
59+
close: tt::TokenId,
5660
kind: tt::DelimiterKind,
5761
tt: [u32; 2],
5862
}
@@ -74,7 +78,7 @@ struct IdentRepr {
7478
}
7579

7680
impl FlatTree {
77-
pub fn new(subtree: &tt::Subtree) -> FlatTree {
81+
pub fn new(subtree: &tt::Subtree, version: u32) -> FlatTree {
7882
let mut w = Writer {
7983
string_table: HashMap::new(),
8084
work: VecDeque::new(),
@@ -89,7 +93,11 @@ impl FlatTree {
8993
w.write(subtree);
9094

9195
return FlatTree {
92-
subtree: write_vec(w.subtree, SubtreeRepr::write),
96+
subtree: if version >= ENCODE_CLOSE_SPAN_VERSION {
97+
write_vec(w.subtree, SubtreeRepr::write_with_close_span)
98+
} else {
99+
write_vec(w.subtree, SubtreeRepr::write)
100+
},
93101
literal: write_vec(w.literal, LiteralRepr::write),
94102
punct: write_vec(w.punct, PunctRepr::write),
95103
ident: write_vec(w.ident, IdentRepr::write),
@@ -102,9 +110,13 @@ impl FlatTree {
102110
}
103111
}
104112

105-
pub fn to_subtree(self) -> tt::Subtree {
113+
pub fn to_subtree(self, version: u32) -> tt::Subtree {
106114
return Reader {
107-
subtree: read_vec(self.subtree, SubtreeRepr::read),
115+
subtree: if version >= ENCODE_CLOSE_SPAN_VERSION {
116+
read_vec(self.subtree, SubtreeRepr::read_with_close_span)
117+
} else {
118+
read_vec(self.subtree, SubtreeRepr::read)
119+
},
108120
literal: read_vec(self.literal, LiteralRepr::read),
109121
punct: read_vec(self.punct, PunctRepr::read),
110122
ident: read_vec(self.ident, IdentRepr::read),
@@ -130,17 +142,36 @@ impl SubtreeRepr {
130142
tt::DelimiterKind::Brace => 2,
131143
tt::DelimiterKind::Bracket => 3,
132144
};
133-
[self.id.0, kind, self.tt[0], self.tt[1]]
145+
[self.open.0, kind, self.tt[0], self.tt[1]]
134146
}
135-
fn read([id, kind, lo, len]: [u32; 4]) -> SubtreeRepr {
147+
fn read([open, kind, lo, len]: [u32; 4]) -> SubtreeRepr {
136148
let kind = match kind {
137149
0 => tt::DelimiterKind::Invisible,
138150
1 => tt::DelimiterKind::Parenthesis,
139151
2 => tt::DelimiterKind::Brace,
140152
3 => tt::DelimiterKind::Bracket,
141153
other => panic!("bad kind {other}"),
142154
};
143-
SubtreeRepr { id: TokenId(id), kind, tt: [lo, len] }
155+
SubtreeRepr { open: TokenId(open), close: TokenId::UNSPECIFIED, kind, tt: [lo, len] }
156+
}
157+
fn write_with_close_span(self) -> [u32; 5] {
158+
let kind = match self.kind {
159+
tt::DelimiterKind::Invisible => 0,
160+
tt::DelimiterKind::Parenthesis => 1,
161+
tt::DelimiterKind::Brace => 2,
162+
tt::DelimiterKind::Bracket => 3,
163+
};
164+
[self.open.0, self.close.0, kind, self.tt[0], self.tt[1]]
165+
}
166+
fn read_with_close_span([open, close, kind, lo, len]: [u32; 5]) -> SubtreeRepr {
167+
let kind = match kind {
168+
0 => tt::DelimiterKind::Invisible,
169+
1 => tt::DelimiterKind::Parenthesis,
170+
2 => tt::DelimiterKind::Brace,
171+
3 => tt::DelimiterKind::Bracket,
172+
other => panic!("bad kind {other}"),
173+
};
174+
SubtreeRepr { open: TokenId(open), close: TokenId(close), kind, tt: [lo, len] }
144175
}
145176
}
146177

@@ -244,9 +275,10 @@ impl<'a> Writer<'a> {
244275

245276
fn enqueue(&mut self, subtree: &'a tt::Subtree) -> u32 {
246277
let idx = self.subtree.len();
247-
let delimiter_id = subtree.delimiter.open;
278+
let open = subtree.delimiter.open;
279+
let close = subtree.delimiter.close;
248280
let delimiter_kind = subtree.delimiter.kind;
249-
self.subtree.push(SubtreeRepr { id: delimiter_id, kind: delimiter_kind, tt: [!0, !0] });
281+
self.subtree.push(SubtreeRepr { open, close, kind: delimiter_kind, tt: [!0, !0] });
250282
self.work.push_back((idx, subtree));
251283
idx as u32
252284
}
@@ -277,11 +309,7 @@ impl Reader {
277309
let repr = &self.subtree[i];
278310
let token_trees = &self.token_tree[repr.tt[0] as usize..repr.tt[1] as usize];
279311
let s = tt::Subtree {
280-
delimiter: tt::Delimiter {
281-
open: repr.id,
282-
close: TokenId::UNSPECIFIED,
283-
kind: repr.kind,
284-
},
312+
delimiter: tt::Delimiter { open: repr.open, close: repr.close, kind: repr.kind },
285313
token_trees: token_trees
286314
.iter()
287315
.copied()

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,10 @@ impl ProcMacroProcessSrv {
5656
}
5757
}
5858

59+
pub(crate) fn version(&self) -> u32 {
60+
self.version
61+
}
62+
5963
pub(crate) fn version_check(&mut self) -> Result<u32, ServerError> {
6064
let request = Request::ApiVersionCheck {};
6165
let response = self.send_task(request)?;

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

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,10 @@ use std::{
3131
time::SystemTime,
3232
};
3333

34-
use proc_macro_api::{msg, ProcMacroKind};
34+
use proc_macro_api::{
35+
msg::{self, CURRENT_API_VERSION},
36+
ProcMacroKind,
37+
};
3538

3639
use ::tt::token_id as tt;
3740

@@ -67,16 +70,16 @@ impl ProcMacroSrv {
6770
None => None,
6871
};
6972

70-
let macro_body = task.macro_body.to_subtree();
71-
let attributes = task.attributes.map(|it| it.to_subtree());
73+
let macro_body = task.macro_body.to_subtree(CURRENT_API_VERSION);
74+
let attributes = task.attributes.map(|it| it.to_subtree(CURRENT_API_VERSION));
7275
let result = thread::scope(|s| {
7376
let thread = thread::Builder::new()
7477
.stack_size(EXPANDER_STACK_SIZE)
7578
.name(task.macro_name.clone())
7679
.spawn_scoped(s, || {
7780
expander
7881
.expand(&task.macro_name, &macro_body, attributes.as_ref())
79-
.map(|it| msg::FlatTree::new(&it))
82+
.map(|it| msg::FlatTree::new(&it, CURRENT_API_VERSION))
8083
});
8184
let res = match thread {
8285
Ok(handle) => handle.join(),

0 commit comments

Comments
 (0)