Skip to content

Commit de01889

Browse files
committed
Change MetaItem::tokens() to MetaItem::token_trees_and_joints().
Likewise for `MetaItemKind::tokens()` and `NestedMetaItem::tokens()`. This avoids some unnecessary `TokenTree` to `TokenStream` conversions, and removes the need for the clumsy `TokenStream::append_to_tree_and_joint_vec()`.
1 parent dbf2523 commit de01889

File tree

3 files changed

+40
-26
lines changed

3 files changed

+40
-26
lines changed

src/libsyntax/attr/mod.rs

+36-21
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ use crate::ptr::P;
2222
use crate::sess::ParseSess;
2323
use crate::symbol::{sym, Symbol};
2424
use crate::ThinVec;
25-
use crate::tokenstream::{TokenStream, TokenTree, DelimSpan};
25+
use crate::tokenstream::{DelimSpan, TokenStream, TokenTree, TreeAndJoint};
2626
use crate::GLOBALS;
2727

2828
use log::debug;
@@ -340,7 +340,10 @@ impl Attribute {
340340
DUMMY_SP,
341341
);
342342
f(&Attribute {
343-
item: AttrItem { path: meta.path, tokens: meta.kind.tokens(meta.span) },
343+
item: AttrItem {
344+
path: meta.path,
345+
tokens: TokenStream::new(meta.kind.token_trees_and_joints(meta.span)),
346+
},
344347
id: self.id,
345348
style: self.style,
346349
is_sugared_doc: true,
@@ -400,12 +403,22 @@ pub fn mk_attr(style: AttrStyle, path: Path, tokens: TokenStream, span: Span) ->
400403

401404
/// Returns an inner attribute with the given value and span.
402405
pub fn mk_attr_inner(item: MetaItem) -> Attribute {
403-
mk_attr(AttrStyle::Inner, item.path, item.kind.tokens(item.span), item.span)
406+
mk_attr(
407+
AttrStyle::Inner,
408+
item.path,
409+
TokenStream::new(item.kind.token_trees_and_joints(item.span)),
410+
item.span,
411+
)
404412
}
405413

406414
/// Returns an outer attribute with the given value and span.
407415
pub fn mk_attr_outer(item: MetaItem) -> Attribute {
408-
mk_attr(AttrStyle::Outer, item.path, item.kind.tokens(item.span), item.span)
416+
mk_attr(
417+
AttrStyle::Outer,
418+
item.path,
419+
TokenStream::new(item.kind.token_trees_and_joints(item.span)),
420+
item.span,
421+
)
409422
}
410423

411424
pub fn mk_sugared_doc_attr(text: Symbol, span: Span) -> Attribute {
@@ -415,7 +428,7 @@ pub fn mk_sugared_doc_attr(text: Symbol, span: Span) -> Attribute {
415428
Attribute {
416429
item: AttrItem {
417430
path: Path::from_ident(Ident::with_dummy_span(sym::doc).with_span_pos(span)),
418-
tokens: MetaItemKind::NameValue(lit).tokens(span),
431+
tokens: TokenStream::new(MetaItemKind::NameValue(lit).token_trees_and_joints(span)),
419432
},
420433
id: mk_attr_id(),
421434
style,
@@ -476,7 +489,7 @@ pub fn first_attr_value_str_by_name(attrs: &[Attribute], name: Symbol) -> Option
476489
}
477490

478491
impl MetaItem {
479-
fn tokens(&self) -> TokenStream {
492+
fn token_trees_and_joints(&self) -> Vec<TreeAndJoint> {
480493
let mut idents = vec![];
481494
let mut last_pos = BytePos(0 as u32);
482495
for (i, segment) in self.path.segments.iter().enumerate() {
@@ -490,8 +503,8 @@ impl MetaItem {
490503
idents.push(TokenTree::Token(Token::from_ast_ident(segment.ident)).into());
491504
last_pos = segment.ident.span.hi();
492505
}
493-
self.kind.tokens(self.span).append_to_tree_and_joint_vec(&mut idents);
494-
TokenStream::new(idents)
506+
idents.extend(self.kind.token_trees_and_joints(self.span));
507+
idents
495508
}
496509

497510
fn from_tokens<I>(tokens: &mut iter::Peekable<I>) -> Option<MetaItem>
@@ -550,28 +563,30 @@ impl MetaItem {
550563
}
551564

552565
impl MetaItemKind {
553-
pub fn tokens(&self, span: Span) -> TokenStream {
566+
pub fn token_trees_and_joints(&self, span: Span) -> Vec<TreeAndJoint> {
554567
match *self {
555-
MetaItemKind::Word => TokenStream::default(),
568+
MetaItemKind::Word => vec![],
556569
MetaItemKind::NameValue(ref lit) => {
557-
TokenStream::new(vec![
570+
vec![
558571
TokenTree::token(token::Eq, span).into(),
559572
lit.token_tree().into(),
560-
])
573+
]
561574
}
562575
MetaItemKind::List(ref list) => {
563576
let mut tokens = Vec::new();
564577
for (i, item) in list.iter().enumerate() {
565578
if i > 0 {
566579
tokens.push(TokenTree::token(token::Comma, span).into());
567580
}
568-
item.tokens().append_to_tree_and_joint_vec(&mut tokens);
581+
tokens.extend(item.token_trees_and_joints())
569582
}
570-
TokenTree::Delimited(
571-
DelimSpan::from_single(span),
572-
token::Paren,
573-
TokenStream::new(tokens).into(),
574-
).into()
583+
vec![
584+
TokenTree::Delimited(
585+
DelimSpan::from_single(span),
586+
token::Paren,
587+
TokenStream::new(tokens).into(),
588+
).into()
589+
]
575590
}
576591
}
577592
}
@@ -617,10 +632,10 @@ impl NestedMetaItem {
617632
}
618633
}
619634

620-
fn tokens(&self) -> TokenStream {
635+
fn token_trees_and_joints(&self) -> Vec<TreeAndJoint> {
621636
match *self {
622-
NestedMetaItem::MetaItem(ref item) => item.tokens(),
623-
NestedMetaItem::Literal(ref lit) => lit.token_tree().into(),
637+
NestedMetaItem::MetaItem(ref item) => item.token_trees_and_joints(),
638+
NestedMetaItem::Literal(ref lit) => vec![lit.token_tree().into()],
624639
}
625640
}
626641

src/libsyntax/ext/expand.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -1487,7 +1487,10 @@ impl<'a, 'b> MutVisitor for InvocationCollector<'a, 'b> {
14871487

14881488
let meta = attr::mk_list_item(Ident::with_dummy_span(sym::doc), items);
14891489
*at = attr::Attribute {
1490-
item: AttrItem { path: meta.path, tokens: meta.kind.tokens(meta.span) },
1490+
item: AttrItem {
1491+
path: meta.path,
1492+
tokens: TokenStream::new(meta.kind.token_trees_and_joints(meta.span)),
1493+
},
14911494
span: at.span,
14921495
id: at.id,
14931496
style: at.style,

src/libsyntax/tokenstream.rs

-4
Original file line numberDiff line numberDiff line change
@@ -271,10 +271,6 @@ impl TokenStream {
271271
}
272272
}
273273

274-
pub fn append_to_tree_and_joint_vec(self, vec: &mut Vec<TreeAndJoint>) {
275-
vec.extend(self.0.iter().cloned());
276-
}
277-
278274
pub fn trees(&self) -> Cursor {
279275
self.clone().into_trees()
280276
}

0 commit comments

Comments
 (0)