Skip to content

Commit 5494f08

Browse files
committed
Remove TokenStream::flattened.
It's no longer needed. This does slightly worsen the error message for a single test, but that test contains code that is so badly broken that I'm not worried about it.
1 parent eccc0d3 commit 5494f08

File tree

6 files changed

+10
-62
lines changed

6 files changed

+10
-62
lines changed

compiler/rustc_ast/src/tokenstream.rs

+1-43
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ use rustc_span::{DUMMY_SP, Span, SpanDecoder, SpanEncoder, Symbol, sym};
2424

2525
use crate::ast::AttrStyle;
2626
use crate::ast_traits::{HasAttrs, HasTokens};
27-
use crate::token::{self, Delimiter, InvisibleOrigin, Token, TokenKind};
27+
use crate::token::{self, Delimiter, Token, TokenKind};
2828
use crate::{AttrVec, Attribute};
2929

3030
/// Part of a `TokenStream`.
@@ -471,48 +471,6 @@ impl TokenStream {
471471
TokenStream::new(tts)
472472
}
473473

474-
fn flatten_token(token: &Token, spacing: Spacing) -> TokenTree {
475-
match token.kind {
476-
token::NtIdent(ident, is_raw) => {
477-
TokenTree::Token(Token::new(token::Ident(ident.name, is_raw), ident.span), spacing)
478-
}
479-
token::NtLifetime(ident, is_raw) => TokenTree::Delimited(
480-
DelimSpan::from_single(token.span),
481-
DelimSpacing::new(Spacing::JointHidden, spacing),
482-
Delimiter::Invisible(InvisibleOrigin::FlattenToken),
483-
TokenStream::token_alone(token::Lifetime(ident.name, is_raw), ident.span),
484-
),
485-
_ => TokenTree::Token(*token, spacing),
486-
}
487-
}
488-
489-
fn flatten_token_tree(tree: &TokenTree) -> TokenTree {
490-
match tree {
491-
TokenTree::Token(token, spacing) => TokenStream::flatten_token(token, *spacing),
492-
TokenTree::Delimited(span, spacing, delim, tts) => {
493-
TokenTree::Delimited(*span, *spacing, *delim, tts.flattened())
494-
}
495-
}
496-
}
497-
498-
#[must_use]
499-
pub fn flattened(&self) -> TokenStream {
500-
fn can_skip(stream: &TokenStream) -> bool {
501-
stream.trees().all(|tree| match tree {
502-
TokenTree::Token(token, _) => {
503-
!matches!(token.kind, token::NtIdent(..) | token::NtLifetime(..))
504-
}
505-
TokenTree::Delimited(.., inner) => can_skip(inner),
506-
})
507-
}
508-
509-
if can_skip(self) {
510-
return self.clone();
511-
}
512-
513-
self.trees().map(|tree| TokenStream::flatten_token_tree(tree)).collect()
514-
}
515-
516474
// If `vec` is not empty, try to glue `tt` onto its last token. The return
517475
// value indicates if gluing took place.
518476
fn try_glue_to_last(vec: &mut Vec<TokenTree>, tt: &TokenTree) -> bool {

compiler/rustc_ast_lowering/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1001,7 +1001,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
10011001
}
10021002

10031003
fn lower_delim_args(&self, args: &DelimArgs) -> DelimArgs {
1004-
DelimArgs { dspan: args.dspan, delim: args.delim, tokens: args.tokens.flattened() }
1004+
DelimArgs { dspan: args.dspan, delim: args.delim, tokens: args.tokens.clone() }
10051005
}
10061006

10071007
/// Lower an associated item constraint.

compiler/rustc_builtin_macros/src/cfg_eval.rs

+3-7
Original file line numberDiff line numberDiff line change
@@ -173,20 +173,16 @@ impl CfgEval<'_> {
173173
_ => unreachable!(),
174174
};
175175

176-
// 'Flatten' all nonterminals (i.e. `TokenKind::Nt{Ident,Lifetime}`)
177-
// to `None`-delimited groups containing the corresponding tokens. This
178-
// is normally delayed until the proc-macro server actually needs to
179-
// provide tokens to a proc-macro. We do this earlier, so that we can
180-
// handle cases like:
176+
// Interesting cases:
181177
//
182178
// ```rust
183179
// #[cfg_eval] #[cfg] $item
184180
//```
185181
//
186182
// where `$item` is `#[cfg_attr] struct Foo {}`. We want to make
187183
// sure to evaluate *all* `#[cfg]` and `#[cfg_attr]` attributes - the simplest
188-
// way to do this is to do a single parse of a stream without any nonterminals.
189-
let orig_tokens = annotatable.to_tokens().flattened();
184+
// way to do this is to do a single parse of the token stream.
185+
let orig_tokens = annotatable.to_tokens();
190186

191187
// Re-parse the tokens, setting the `capture_cfg` flag to save extra information
192188
// to the captured `AttrTokenStream` (specifically, we capture

compiler/rustc_expand/src/config.rs

-6
Original file line numberDiff line numberDiff line change
@@ -213,12 +213,6 @@ impl<'a> StripUnconfigured<'a> {
213213
inner = self.configure_tokens(&inner);
214214
Some(AttrTokenTree::Delimited(sp, spacing, delim, inner))
215215
}
216-
AttrTokenTree::Token(
217-
Token { kind: TokenKind::NtIdent(..) | TokenKind::NtLifetime(..), .. },
218-
_,
219-
) => {
220-
panic!("Nonterminal should have been flattened: {:?}", tree);
221-
}
222216
AttrTokenTree::Token(
223217
Token { kind: TokenKind::OpenDelim(_) | TokenKind::CloseDelim(_), .. },
224218
_,

tests/ui/macros/syntax-error-recovery.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,14 @@ macro_rules! values {
55
$(
66
#[$attr]
77
$token $($inner)? = $value,
8+
//~^ ERROR expected one of `!` or `::`, found `<eof>`
89
)*
910
}
1011
};
1112
}
12-
//~^^^^^ ERROR expected one of `(`, `,`, `=`, `{`, or `}`, found `ty` metavariable
13+
//~^^^^^^ ERROR expected one of `(`, `,`, `=`, `{`, or `}`, found `ty` metavariable
1314
//~| ERROR macro expansion ignores `ty` metavariable and any tokens following
1415

1516
values!(STRING(1) as (String) => cfg(test),);
16-
//~^ ERROR expected one of `!` or `::`, found `<eof>`
1717

1818
fn main() {}

tests/ui/macros/syntax-error-recovery.stderr

+3-3
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,10 @@ LL | values!(STRING(1) as (String) => cfg(test),);
2222
= note: the usage of `values!` is likely invalid in item context
2323

2424
error: expected one of `!` or `::`, found `<eof>`
25-
--> $DIR/syntax-error-recovery.rs:15:9
25+
--> $DIR/syntax-error-recovery.rs:7:17
2626
|
27-
LL | values!(STRING(1) as (String) => cfg(test),);
28-
| ^^^^^^ expected one of `!` or `::`
27+
LL | $token $($inner)? = $value,
28+
| ^^^^^^ expected one of `!` or `::`
2929

3030
error: aborting due to 3 previous errors
3131

0 commit comments

Comments
 (0)