Skip to content

Commit 94012e2

Browse files
Merge #7884
7884: Simplify TokenStream FromStr r=edwin0cheng a=edwin0cheng Make sure `FromStr` ignore all `TokenMap` in all cases. bors r+ Co-authored-by: Edwin Cheng <edwin0cheng@gmail.com>
2 parents 7199d5b + 83230b2 commit 94012e2

File tree

2 files changed

+43
-39
lines changed

2 files changed

+43
-39
lines changed

crates/proc_macro_srv/src/rustc_server.rs

Lines changed: 42 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,7 @@ pub mod token_stream {
184184
let (subtree, _token_map) =
185185
mbe::parse_to_token_tree(src).ok_or("Failed to parse from mbe")?;
186186

187+
let subtree = subtree_replace_token_ids_with_unspecified(subtree);
187188
Ok(TokenStream { subtree })
188189
}
189190
}
@@ -226,6 +227,44 @@ pub mod token_stream {
226227
}
227228
}
228229
}
230+
231+
fn subtree_replace_token_ids_with_unspecified(subtree: tt::Subtree) -> tt::Subtree {
232+
tt::Subtree {
233+
delimiter: subtree
234+
.delimiter
235+
.map(|d| tt::Delimiter { id: tt::TokenId::unspecified(), ..d }),
236+
token_trees: subtree
237+
.token_trees
238+
.into_iter()
239+
.map(|t| token_tree_replace_token_ids_with_unspecified(t))
240+
.collect(),
241+
}
242+
}
243+
244+
fn token_tree_replace_token_ids_with_unspecified(tt: tt::TokenTree) -> tt::TokenTree {
245+
match tt {
246+
tt::TokenTree::Leaf(leaf) => {
247+
tt::TokenTree::Leaf(leaf_replace_token_ids_with_unspecified(leaf))
248+
}
249+
tt::TokenTree::Subtree(subtree) => {
250+
tt::TokenTree::Subtree(subtree_replace_token_ids_with_unspecified(subtree))
251+
}
252+
}
253+
}
254+
255+
fn leaf_replace_token_ids_with_unspecified(leaf: tt::Leaf) -> tt::Leaf {
256+
match leaf {
257+
tt::Leaf::Literal(lit) => {
258+
tt::Leaf::Literal(tt::Literal { id: tt::TokenId::unspecified(), ..lit })
259+
}
260+
tt::Leaf::Punct(punct) => {
261+
tt::Leaf::Punct(tt::Punct { id: tt::TokenId::unspecified(), ..punct })
262+
}
263+
tt::Leaf::Ident(ident) => {
264+
tt::Leaf::Ident(tt::Ident { id: tt::TokenId::unspecified(), ..ident })
265+
}
266+
}
267+
}
229268
}
230269

231270
impl TokenStreamBuilder {
@@ -277,42 +316,6 @@ impl server::FreeFunctions for Rustc {
277316
}
278317
}
279318

280-
fn subtree_replace_token_ids_with_unspecified(subtree: tt::Subtree) -> tt::Subtree {
281-
tt::Subtree {
282-
delimiter: subtree.delimiter.map(|d| tt::Delimiter { id: tt::TokenId::unspecified(), ..d }),
283-
token_trees: subtree
284-
.token_trees
285-
.into_iter()
286-
.map(|t| token_tree_replace_token_ids_with_unspecified(t))
287-
.collect(),
288-
}
289-
}
290-
291-
fn token_tree_replace_token_ids_with_unspecified(tt: tt::TokenTree) -> tt::TokenTree {
292-
match tt {
293-
tt::TokenTree::Leaf(leaf) => {
294-
tt::TokenTree::Leaf(leaf_replace_token_ids_with_unspecified(leaf))
295-
}
296-
tt::TokenTree::Subtree(subtree) => {
297-
tt::TokenTree::Subtree(subtree_replace_token_ids_with_unspecified(subtree))
298-
}
299-
}
300-
}
301-
302-
fn leaf_replace_token_ids_with_unspecified(leaf: tt::Leaf) -> tt::Leaf {
303-
match leaf {
304-
tt::Leaf::Literal(lit) => {
305-
tt::Leaf::Literal(tt::Literal { id: tt::TokenId::unspecified(), ..lit })
306-
}
307-
tt::Leaf::Punct(punct) => {
308-
tt::Leaf::Punct(tt::Punct { id: tt::TokenId::unspecified(), ..punct })
309-
}
310-
tt::Leaf::Ident(ident) => {
311-
tt::Leaf::Ident(tt::Ident { id: tt::TokenId::unspecified(), ..ident })
312-
}
313-
}
314-
}
315-
316319
impl server::TokenStream for Rustc {
317320
fn new(&mut self) -> Self::TokenStream {
318321
Self::TokenStream::new()
@@ -322,8 +325,9 @@ impl server::TokenStream for Rustc {
322325
stream.is_empty()
323326
}
324327
fn from_str(&mut self, src: &str) -> Self::TokenStream {
325-
let (subtree, _) = mbe::parse_to_token_tree(src).expect("cannot parse string");
326-
TokenStream::with_subtree(subtree_replace_token_ids_with_unspecified(subtree))
328+
use std::str::FromStr;
329+
330+
Self::TokenStream::from_str(src).expect("cannot parse string")
327331
}
328332
fn to_string(&mut self, stream: &Self::TokenStream) -> String {
329333
stream.to_string()

crates/proc_macro_srv/src/tests/fixtures/test_serialize_proc_macro.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ SUBTREE $
101101
PUNCH : [alone] 4294967295
102102
IDENT Serialize 4294967295
103103
IDENT for 4294967295
104-
IDENT Foo 1
104+
IDENT Foo 4294967295
105105
SUBTREE {} 4294967295
106106
IDENT fn 4294967295
107107
IDENT serialize 4294967295

0 commit comments

Comments
 (0)