Skip to content

Commit a56d345

Browse files
committed
Rename AttrAnnotatedToken{Stream,Tree}.
These two type names are long and have long matching prefixes. I find them hard to read, especially in combinations like `AttrAnnotatedTokenStream::new(vec![AttrAnnotatedTokenTree::Token(..)])`. This commit renames them as `AttrToken{Stream,Tree}`.
1 parent 890e759 commit a56d345

File tree

8 files changed

+80
-84
lines changed

8 files changed

+80
-84
lines changed

compiler/rustc_ast/src/attr/mod.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use crate::ast::{MacArgs, MacArgsEq, MacDelimiter, MetaItem, MetaItemKind, Neste
77
use crate::ast::{Path, PathSegment};
88
use crate::ptr::P;
99
use crate::token::{self, CommentKind, Delimiter, Token};
10-
use crate::tokenstream::{AttrAnnotatedTokenStream, AttrAnnotatedTokenTree};
10+
use crate::tokenstream::{AttrTokenStream, AttrTokenTree};
1111
use crate::tokenstream::{DelimSpan, Spacing, TokenTree};
1212
use crate::tokenstream::{LazyTokenStream, TokenStream};
1313
use crate::util::comments;
@@ -296,15 +296,15 @@ impl Attribute {
296296
}
297297
}
298298

299-
pub fn tokens(&self) -> AttrAnnotatedTokenStream {
299+
pub fn tokens(&self) -> AttrTokenStream {
300300
match self.kind {
301301
AttrKind::Normal(ref normal) => normal
302302
.tokens
303303
.as_ref()
304304
.unwrap_or_else(|| panic!("attribute is missing tokens: {:?}", self))
305305
.create_token_stream(),
306306
AttrKind::DocComment(comment_kind, data) => {
307-
AttrAnnotatedTokenStream::new(vec![AttrAnnotatedTokenTree::Token(
307+
AttrTokenStream::new(vec![AttrTokenTree::Token(
308308
Token::new(token::DocComment(comment_kind, self.style, data), self.span),
309309
Spacing::Alone,
310310
)])

compiler/rustc_ast/src/mut_visit.rs

+8-11
Original file line numberDiff line numberDiff line change
@@ -642,17 +642,17 @@ pub fn noop_flat_map_param<T: MutVisitor>(mut param: Param, vis: &mut T) -> Smal
642642
}
643643

644644
// No `noop_` prefix because there isn't a corresponding method in `MutVisitor`.
645-
pub fn visit_attr_annotated_tt<T: MutVisitor>(tt: &mut AttrAnnotatedTokenTree, vis: &mut T) {
645+
pub fn visit_attr_tt<T: MutVisitor>(tt: &mut AttrTokenTree, vis: &mut T) {
646646
match tt {
647-
AttrAnnotatedTokenTree::Token(token, _) => {
647+
AttrTokenTree::Token(token, _) => {
648648
visit_token(token, vis);
649649
}
650-
AttrAnnotatedTokenTree::Delimited(DelimSpan { open, close }, _delim, tts) => {
650+
AttrTokenTree::Delimited(DelimSpan { open, close }, _delim, tts) => {
651651
vis.visit_span(open);
652652
vis.visit_span(close);
653-
visit_attr_annotated_tts(tts, vis);
653+
visit_attr_tts(tts, vis);
654654
}
655-
AttrAnnotatedTokenTree::Attributes(data) => {
655+
AttrTokenTree::Attributes(data) => {
656656
for attr in &mut *data.attrs {
657657
match &mut attr.kind {
658658
AttrKind::Normal(normal) => {
@@ -690,21 +690,18 @@ pub fn visit_tts<T: MutVisitor>(TokenStream(tts): &mut TokenStream, vis: &mut T)
690690
}
691691
}
692692

693-
pub fn visit_attr_annotated_tts<T: MutVisitor>(
694-
AttrAnnotatedTokenStream(tts): &mut AttrAnnotatedTokenStream,
695-
vis: &mut T,
696-
) {
693+
pub fn visit_attr_tts<T: MutVisitor>(AttrTokenStream(tts): &mut AttrTokenStream, vis: &mut T) {
697694
if T::VISIT_TOKENS && !tts.is_empty() {
698695
let tts = Lrc::make_mut(tts);
699-
visit_vec(tts, |tree| visit_attr_annotated_tt(tree, vis));
696+
visit_vec(tts, |tree| visit_attr_tt(tree, vis));
700697
}
701698
}
702699

703700
pub fn visit_lazy_tts_opt_mut<T: MutVisitor>(lazy_tts: Option<&mut LazyTokenStream>, vis: &mut T) {
704701
if T::VISIT_TOKENS {
705702
if let Some(lazy_tts) = lazy_tts {
706703
let mut tts = lazy_tts.create_token_stream();
707-
visit_attr_annotated_tts(&mut tts, vis);
704+
visit_attr_tts(&mut tts, vis);
708705
*lazy_tts = LazyTokenStream::new(tts);
709706
}
710707
}

compiler/rustc_ast/src/tokenstream.rs

+20-20
Original file line numberDiff line numberDiff line change
@@ -122,11 +122,11 @@ where
122122
}
123123

124124
pub trait CreateTokenStream: sync::Send + sync::Sync {
125-
fn create_token_stream(&self) -> AttrAnnotatedTokenStream;
125+
fn create_token_stream(&self) -> AttrTokenStream;
126126
}
127127

128-
impl CreateTokenStream for AttrAnnotatedTokenStream {
129-
fn create_token_stream(&self) -> AttrAnnotatedTokenStream {
128+
impl CreateTokenStream for AttrTokenStream {
129+
fn create_token_stream(&self) -> AttrTokenStream {
130130
self.clone()
131131
}
132132
}
@@ -142,7 +142,7 @@ impl LazyTokenStream {
142142
LazyTokenStream(Lrc::new(Box::new(inner)))
143143
}
144144

145-
pub fn create_token_stream(&self) -> AttrAnnotatedTokenStream {
145+
pub fn create_token_stream(&self) -> AttrTokenStream {
146146
self.0.create_token_stream()
147147
}
148148
}
@@ -172,31 +172,31 @@ impl<CTX> HashStable<CTX> for LazyTokenStream {
172172
}
173173
}
174174

175-
/// A `AttrAnnotatedTokenStream` is similar to a `TokenStream`, but with extra
175+
/// An `AttrTokenStream` is similar to a `TokenStream`, but with extra
176176
/// information about the tokens for attribute targets. This is used
177177
/// during expansion to perform early cfg-expansion, and to process attributes
178178
/// during proc-macro invocations.
179179
#[derive(Clone, Debug, Default, Encodable, Decodable)]
180-
pub struct AttrAnnotatedTokenStream(pub Lrc<Vec<AttrAnnotatedTokenTree>>);
180+
pub struct AttrTokenStream(pub Lrc<Vec<AttrTokenTree>>);
181181

182-
/// Like `TokenTree`, but for `AttrAnnotatedTokenStream`
182+
/// Like `TokenTree`, but for `AttrTokenStream`.
183183
#[derive(Clone, Debug, Encodable, Decodable)]
184-
pub enum AttrAnnotatedTokenTree {
184+
pub enum AttrTokenTree {
185185
Token(Token, Spacing),
186-
Delimited(DelimSpan, Delimiter, AttrAnnotatedTokenStream),
186+
Delimited(DelimSpan, Delimiter, AttrTokenStream),
187187
/// Stores the attributes for an attribute target,
188188
/// along with the tokens for that attribute target.
189189
/// See `AttributesData` for more information
190190
Attributes(AttributesData),
191191
}
192192

193-
impl AttrAnnotatedTokenStream {
194-
pub fn new(tokens: Vec<AttrAnnotatedTokenTree>) -> AttrAnnotatedTokenStream {
195-
AttrAnnotatedTokenStream(Lrc::new(tokens))
193+
impl AttrTokenStream {
194+
pub fn new(tokens: Vec<AttrTokenTree>) -> AttrTokenStream {
195+
AttrTokenStream(Lrc::new(tokens))
196196
}
197197

198-
/// Converts this `AttrAnnotatedTokenStream` to a plain `TokenStream
199-
/// During conversion, `AttrAnnotatedTokenTree::Attributes` get 'flattened'
198+
/// Converts this `AttrTokenStream` to a plain `TokenStream`.
199+
/// During conversion, `AttrTokenTree::Attributes` get 'flattened'
200200
/// back to a `TokenStream` of the form `outer_attr attr_target`.
201201
/// If there are inner attributes, they are inserted into the proper
202202
/// place in the attribute target tokens.
@@ -205,14 +205,14 @@ impl AttrAnnotatedTokenStream {
205205
.0
206206
.iter()
207207
.flat_map(|tree| match &tree {
208-
AttrAnnotatedTokenTree::Token(inner, spacing) => {
208+
AttrTokenTree::Token(inner, spacing) => {
209209
smallvec![TokenTree::Token(inner.clone(), *spacing)].into_iter()
210210
}
211-
AttrAnnotatedTokenTree::Delimited(span, delim, stream) => {
211+
AttrTokenTree::Delimited(span, delim, stream) => {
212212
smallvec![TokenTree::Delimited(*span, *delim, stream.to_tokenstream()),]
213213
.into_iter()
214214
}
215-
AttrAnnotatedTokenTree::Attributes(data) => {
215+
AttrTokenTree::Attributes(data) => {
216216
let mut outer_attrs = Vec::new();
217217
let mut inner_attrs = Vec::new();
218218
for attr in &data.attrs {
@@ -417,14 +417,14 @@ impl TokenStream {
417417
fn opt_from_ast(node: &(impl HasAttrs + HasTokens)) -> Option<TokenStream> {
418418
let tokens = node.tokens()?;
419419
let attrs = node.attrs();
420-
let attr_annotated = if attrs.is_empty() {
420+
let attr_stream = if attrs.is_empty() {
421421
tokens.create_token_stream()
422422
} else {
423423
let attr_data =
424424
AttributesData { attrs: attrs.iter().cloned().collect(), tokens: tokens.clone() };
425-
AttrAnnotatedTokenStream::new(vec![AttrAnnotatedTokenTree::Attributes(attr_data)])
425+
AttrTokenStream::new(vec![AttrTokenTree::Attributes(attr_data)])
426426
};
427-
Some(attr_annotated.to_tokenstream())
427+
Some(attr_stream.to_tokenstream())
428428
}
429429

430430
// Create a token stream containing a single token with alone spacing.

compiler/rustc_builtin_macros/src/cfg_eval.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -188,14 +188,14 @@ impl CfgEval<'_, '_> {
188188
let orig_tokens = annotatable.to_tokens().flattened();
189189

190190
// Re-parse the tokens, setting the `capture_cfg` flag to save extra information
191-
// to the captured `AttrAnnotatedTokenStream` (specifically, we capture
192-
// `AttrAnnotatedTokenTree::AttributesData` for all occurrences of `#[cfg]` and `#[cfg_attr]`)
191+
// to the captured `AttrTokenStream` (specifically, we capture
192+
// `AttrTokenTree::AttributesData` for all occurrences of `#[cfg]` and `#[cfg_attr]`)
193193
let mut parser =
194194
rustc_parse::stream_to_parser(&self.cfg.sess.parse_sess, orig_tokens, None);
195195
parser.capture_cfg = true;
196196
annotatable = parse_annotatable_with(&mut parser);
197197

198-
// Now that we have our re-parsed `AttrAnnotatedTokenStream`, recursively configuring
198+
// Now that we have our re-parsed `AttrTokenStream`, recursively configuring
199199
// our attribute target will correctly the tokens as well.
200200
flat_map_annotatable(self, annotatable)
201201
}

compiler/rustc_expand/src/config.rs

+21-21
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
33
use rustc_ast::ptr::P;
44
use rustc_ast::token::{Delimiter, Token, TokenKind};
5-
use rustc_ast::tokenstream::{AttrAnnotatedTokenStream, AttrAnnotatedTokenTree};
5+
use rustc_ast::tokenstream::{AttrTokenStream, AttrTokenTree};
66
use rustc_ast::tokenstream::{DelimSpan, Spacing};
77
use rustc_ast::tokenstream::{LazyTokenStream, TokenTree};
88
use rustc_ast::NodeId;
@@ -259,8 +259,8 @@ impl<'a> StripUnconfigured<'a> {
259259
fn try_configure_tokens<T: HasTokens>(&self, node: &mut T) {
260260
if self.config_tokens {
261261
if let Some(Some(tokens)) = node.tokens_mut() {
262-
let attr_annotated_tokens = tokens.create_token_stream();
263-
*tokens = LazyTokenStream::new(self.configure_tokens(&attr_annotated_tokens));
262+
let attr_stream = tokens.create_token_stream();
263+
*tokens = LazyTokenStream::new(self.configure_tokens(&attr_stream));
264264
}
265265
}
266266
}
@@ -270,16 +270,16 @@ impl<'a> StripUnconfigured<'a> {
270270
if self.in_cfg(&attrs) { Some(attrs) } else { None }
271271
}
272272

273-
/// Performs cfg-expansion on `stream`, producing a new `AttrAnnotatedTokenStream`.
273+
/// Performs cfg-expansion on `stream`, producing a new `AttrTokenStream`.
274274
/// This is only used during the invocation of `derive` proc-macros,
275275
/// which require that we cfg-expand their entire input.
276276
/// Normal cfg-expansion operates on parsed AST nodes via the `configure` method
277-
fn configure_tokens(&self, stream: &AttrAnnotatedTokenStream) -> AttrAnnotatedTokenStream {
278-
fn can_skip(stream: &AttrAnnotatedTokenStream) -> bool {
277+
fn configure_tokens(&self, stream: &AttrTokenStream) -> AttrTokenStream {
278+
fn can_skip(stream: &AttrTokenStream) -> bool {
279279
stream.0.iter().all(|tree| match tree {
280-
AttrAnnotatedTokenTree::Attributes(_) => false,
281-
AttrAnnotatedTokenTree::Token(..) => true,
282-
AttrAnnotatedTokenTree::Delimited(_, _, inner) => can_skip(inner),
280+
AttrTokenTree::Attributes(_) => false,
281+
AttrTokenTree::Token(..) => true,
282+
AttrTokenTree::Delimited(_, _, inner) => can_skip(inner),
283283
})
284284
}
285285

@@ -291,35 +291,35 @@ impl<'a> StripUnconfigured<'a> {
291291
.0
292292
.iter()
293293
.flat_map(|tree| match tree.clone() {
294-
AttrAnnotatedTokenTree::Attributes(mut data) => {
294+
AttrTokenTree::Attributes(mut data) => {
295295
data.attrs.flat_map_in_place(|attr| self.process_cfg_attr(attr));
296296

297297
if self.in_cfg(&data.attrs) {
298298
data.tokens = LazyTokenStream::new(
299299
self.configure_tokens(&data.tokens.create_token_stream()),
300300
);
301-
Some(AttrAnnotatedTokenTree::Attributes(data)).into_iter()
301+
Some(AttrTokenTree::Attributes(data)).into_iter()
302302
} else {
303303
None.into_iter()
304304
}
305305
}
306-
AttrAnnotatedTokenTree::Delimited(sp, delim, mut inner) => {
306+
AttrTokenTree::Delimited(sp, delim, mut inner) => {
307307
inner = self.configure_tokens(&inner);
308-
Some(AttrAnnotatedTokenTree::Delimited(sp, delim, inner))
308+
Some(AttrTokenTree::Delimited(sp, delim, inner))
309309
.into_iter()
310310
}
311-
AttrAnnotatedTokenTree::Token(ref token, _) if let TokenKind::Interpolated(ref nt) = token.kind => {
311+
AttrTokenTree::Token(ref token, _) if let TokenKind::Interpolated(ref nt) = token.kind => {
312312
panic!(
313313
"Nonterminal should have been flattened at {:?}: {:?}",
314314
token.span, nt
315315
);
316316
}
317-
AttrAnnotatedTokenTree::Token(token, spacing) => {
318-
Some(AttrAnnotatedTokenTree::Token(token, spacing)).into_iter()
317+
AttrTokenTree::Token(token, spacing) => {
318+
Some(AttrTokenTree::Token(token, spacing)).into_iter()
319319
}
320320
})
321321
.collect();
322-
AttrAnnotatedTokenStream::new(trees)
322+
AttrTokenStream::new(trees)
323323
}
324324

325325
/// Parse and expand all `cfg_attr` attributes into a list of attributes
@@ -404,17 +404,17 @@ impl<'a> StripUnconfigured<'a> {
404404
};
405405
let pound_span = pound_token.span;
406406

407-
let mut trees = vec![AttrAnnotatedTokenTree::Token(pound_token, Spacing::Alone)];
407+
let mut trees = vec![AttrTokenTree::Token(pound_token, Spacing::Alone)];
408408
if attr.style == AttrStyle::Inner {
409409
// For inner attributes, we do the same thing for the `!` in `#![some_attr]`
410410
let TokenTree::Token(bang_token @ Token { kind: TokenKind::Not, .. }, _) = orig_trees.next().unwrap() else {
411411
panic!("Bad tokens for attribute {:?}", attr);
412412
};
413-
trees.push(AttrAnnotatedTokenTree::Token(bang_token, Spacing::Alone));
413+
trees.push(AttrTokenTree::Token(bang_token, Spacing::Alone));
414414
}
415415
// We don't really have a good span to use for the synthesized `[]`
416416
// in `#[attr]`, so just use the span of the `#` token.
417-
let bracket_group = AttrAnnotatedTokenTree::Delimited(
417+
let bracket_group = AttrTokenTree::Delimited(
418418
DelimSpan::from_single(pound_span),
419419
Delimiter::Bracket,
420420
item.tokens
@@ -423,7 +423,7 @@ impl<'a> StripUnconfigured<'a> {
423423
.create_token_stream(),
424424
);
425425
trees.push(bracket_group);
426-
let tokens = Some(LazyTokenStream::new(AttrAnnotatedTokenStream::new(trees)));
426+
let tokens = Some(LazyTokenStream::new(AttrTokenStream::new(trees)));
427427
let attr = attr::mk_attr_from_item(item, tokens, attr.style, item_span);
428428
if attr.has_name(sym::crate_type) {
429429
self.sess.parse_sess.buffer_lint(

compiler/rustc_parse/src/parser/attr.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -303,7 +303,7 @@ impl<'a> Parser<'a> {
303303
// If we are currently capturing tokens, mark the location of this inner attribute.
304304
// If capturing ends up creating a `LazyTokenStream`, we will include
305305
// this replace range with it, removing the inner attribute from the final
306-
// `AttrAnnotatedTokenStream`. Inner attributes are stored in the parsed AST note.
306+
// `AttrTokenStream`. Inner attributes are stored in the parsed AST note.
307307
// During macro expansion, they are selectively inserted back into the
308308
// token stream (the first inner attribute is removed each time we invoke the
309309
// corresponding macro).

0 commit comments

Comments
 (0)