From 103bd4a8208d813a958f3d3653c687be577ec3c2 Mon Sep 17 00:00:00 2001 From: Nicholas Nethercote Date: Thu, 27 Jul 2023 10:00:04 +1000 Subject: [PATCH] Use `TokenStream::trees` instead of `into_trees` for attributes. This avoids cloning some token trees. A couple of `clone` calls were inserted, but only on some paths, and the next commit will remove them. --- compiler/rustc_ast/src/attr/mod.rs | 27 +++++++++++++-------------- 1 file changed, 13 insertions(+), 14 deletions(-) diff --git a/compiler/rustc_ast/src/attr/mod.rs b/compiler/rustc_ast/src/attr/mod.rs index 15fe29580c2..466765f2dbf 100644 --- a/compiler/rustc_ast/src/attr/mod.rs +++ b/compiler/rustc_ast/src/attr/mod.rs @@ -285,12 +285,12 @@ impl MetaItem { self.kind.value_str() } - fn from_tokens(tokens: &mut iter::Peekable) -> Option + fn from_tokens<'a, I>(tokens: &mut iter::Peekable) -> Option where - I: Iterator, + I: Iterator, { // FIXME: Share code with `parse_path`. - let path = match tokens.next().map(TokenTree::uninterpolate) { + let path = match tokens.next().map(|tt| TokenTree::uninterpolate(tt.clone())) { Some(TokenTree::Token( Token { kind: kind @ (token::Ident(..) | token::ModSep), span }, _, @@ -309,7 +309,7 @@ impl MetaItem { }; loop { if let Some(TokenTree::Token(Token { kind: token::Ident(name, _), span }, _)) = - tokens.next().map(TokenTree::uninterpolate) + tokens.next().map(|tt| TokenTree::uninterpolate(tt.clone())) { segments.push(PathSegment::from_ident(Ident::new(name, span))); } else { @@ -354,7 +354,7 @@ impl MetaItemKind { } fn list_from_tokens(tokens: TokenStream) -> Option> { - let mut tokens = tokens.into_trees().peekable(); + let mut tokens = tokens.trees().peekable(); let mut result = ThinVec::new(); while tokens.peek().is_some() { let item = NestedMetaItem::from_tokens(&mut tokens)?; @@ -367,12 +367,12 @@ impl MetaItemKind { Some(result) } - fn name_value_from_tokens( - tokens: &mut impl Iterator, + fn name_value_from_tokens<'a>( + tokens: &mut impl Iterator, ) -> Option { match tokens.next() { Some(TokenTree::Delimited(_, Delimiter::Invisible, inner_tokens)) => { - MetaItemKind::name_value_from_tokens(&mut inner_tokens.into_trees()) + MetaItemKind::name_value_from_tokens(&mut inner_tokens.trees()) } Some(TokenTree::Token(token, _)) => { MetaItemLit::from_token(&token).map(MetaItemKind::NameValue) @@ -381,8 +381,8 @@ impl MetaItemKind { } } - fn from_tokens( - tokens: &mut iter::Peekable>, + fn from_tokens<'a>( + tokens: &mut iter::Peekable>, ) -> Option { match tokens.peek() { Some(TokenTree::Delimited(_, Delimiter::Parenthesis, inner_tokens)) => { @@ -501,9 +501,9 @@ impl NestedMetaItem { self.meta_item().is_some() } - fn from_tokens(tokens: &mut iter::Peekable) -> Option + fn from_tokens<'a, I>(tokens: &mut iter::Peekable) -> Option where - I: Iterator, + I: Iterator, { match tokens.peek() { Some(TokenTree::Token(token, _)) @@ -513,9 +513,8 @@ impl NestedMetaItem { return Some(NestedMetaItem::Lit(lit)); } Some(TokenTree::Delimited(_, Delimiter::Invisible, inner_tokens)) => { - let inner_tokens = inner_tokens.clone(); tokens.next(); - return NestedMetaItem::from_tokens(&mut inner_tokens.into_trees().peekable()); + return NestedMetaItem::from_tokens(&mut inner_tokens.trees().peekable()); } _ => {} }