Skip to content

Commit af72f7a

Browse files
committed
Remove TreeAndSpacing.
A `TokenStream` contains a `Lrc<Vec<(TokenTree, Spacing)>>`. But this is not quite right. `Spacing` makes sense for `TokenTree::Token`, but does not make sense for `TokenTree::Delimited`, because a `TokenTree::Delimited` cannot be joined with another `TokenTree`. This commit fixes this problem, by adding `Spacing` to `TokenTree::Token`, changing `TokenStream` to contain a `Lrc<Vec<TokenTree>>`, and removing the `TreeAndSpacing` typedef. The commit removes these two impls: - `impl From<TokenTree> for TokenStream` - `impl From<TokenTree> for TreeAndSpacing` These were useful, but also resulted in code with many `.into()` calls that was hard to read, particularly for anyone not highly familiar with the relevant types. This commit makes some other changes to compensate: - `TokenTree::token()` becomes `TokenTree::token_{alone,joint}()`. - `TokenStream::token_{alone,joint}()` are added. - `TokenStream::delimited` is added. This results in things like this: ```rust TokenTree::token(token::Semi, stmt.span).into() ``` changing to this: ```rust TokenStream::token_alone(token::Semi, stmt.span) ``` This makes the type of the result, and its spacing, clearer. These changes also simplifies `Cursor` and `CursorRef`, because they no longer need to distinguish between `next` and `next_with_spacing`.
1 parent f026688 commit af72f7a

File tree

1 file changed

+61
-37
lines changed

1 file changed

+61
-37
lines changed

src/macros.rs

Lines changed: 61 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use std::collections::HashMap;
1313
use std::panic::{catch_unwind, AssertUnwindSafe};
1414

1515
use rustc_ast::token::{BinOpToken, Delimiter, Token, TokenKind};
16-
use rustc_ast::tokenstream::{Cursor, Spacing, TokenStream, TokenTree};
16+
use rustc_ast::tokenstream::{Cursor, TokenStream, TokenTree};
1717
use rustc_ast::{ast, ptr};
1818
use rustc_ast_pretty::pprust;
1919
use rustc_span::{
@@ -682,7 +682,7 @@ struct MacroArgParser {
682682

683683
fn last_tok(tt: &TokenTree) -> Token {
684684
match *tt {
685-
TokenTree::Token(ref t) => t.clone(),
685+
TokenTree::Token(ref t, _) => t.clone(),
686686
TokenTree::Delimited(delim_span, delim, _) => Token {
687687
kind: TokenKind::CloseDelim(delim),
688688
span: delim_span.close,
@@ -737,10 +737,13 @@ impl MacroArgParser {
737737

738738
fn add_meta_variable(&mut self, iter: &mut Cursor) -> Option<()> {
739739
match iter.next() {
740-
Some(TokenTree::Token(Token {
741-
kind: TokenKind::Ident(name, _),
742-
..
743-
})) => {
740+
Some(TokenTree::Token(
741+
Token {
742+
kind: TokenKind::Ident(name, _),
743+
..
744+
},
745+
_,
746+
)) => {
744747
self.result.push(ParsedMacroArg {
745748
kind: MacroArgKind::MetaVariable(name, self.buf.clone()),
746749
});
@@ -777,21 +780,30 @@ impl MacroArgParser {
777780
}
778781

779782
match tok {
780-
TokenTree::Token(Token {
781-
kind: TokenKind::BinOp(BinOpToken::Plus),
782-
..
783-
})
784-
| TokenTree::Token(Token {
785-
kind: TokenKind::Question,
786-
..
787-
})
788-
| TokenTree::Token(Token {
789-
kind: TokenKind::BinOp(BinOpToken::Star),
790-
..
791-
}) => {
783+
TokenTree::Token(
784+
Token {
785+
kind: TokenKind::BinOp(BinOpToken::Plus),
786+
..
787+
},
788+
_,
789+
)
790+
| TokenTree::Token(
791+
Token {
792+
kind: TokenKind::Question,
793+
..
794+
},
795+
_,
796+
)
797+
| TokenTree::Token(
798+
Token {
799+
kind: TokenKind::BinOp(BinOpToken::Star),
800+
..
801+
},
802+
_,
803+
) => {
792804
break;
793805
}
794-
TokenTree::Token(ref t) => {
806+
TokenTree::Token(ref t, _) => {
795807
buffer.push_str(&pprust::token_to_string(t));
796808
}
797809
_ => return None,
@@ -859,10 +871,13 @@ impl MacroArgParser {
859871

860872
while let Some(tok) = iter.next() {
861873
match tok {
862-
TokenTree::Token(Token {
863-
kind: TokenKind::Dollar,
864-
span,
865-
}) => {
874+
TokenTree::Token(
875+
Token {
876+
kind: TokenKind::Dollar,
877+
span,
878+
},
879+
_,
880+
) => {
866881
// We always want to add a separator before meta variables.
867882
if !self.buf.is_empty() {
868883
self.add_separator();
@@ -875,13 +890,16 @@ impl MacroArgParser {
875890
span,
876891
};
877892
}
878-
TokenTree::Token(Token {
879-
kind: TokenKind::Colon,
880-
..
881-
}) if self.is_meta_var => {
893+
TokenTree::Token(
894+
Token {
895+
kind: TokenKind::Colon,
896+
..
897+
},
898+
_,
899+
) if self.is_meta_var => {
882900
self.add_meta_variable(&mut iter)?;
883901
}
884-
TokenTree::Token(ref t) => self.update_buffer(t),
902+
TokenTree::Token(ref t, _) => self.update_buffer(t),
885903
TokenTree::Delimited(_delimited_span, delimited, ref tts) => {
886904
if !self.buf.is_empty() {
887905
if next_space(&self.last_tok.kind) == SpaceState::Always {
@@ -1123,12 +1141,15 @@ impl MacroParser {
11231141
TokenTree::Token(..) => return None,
11241142
TokenTree::Delimited(delimited_span, d, _) => (delimited_span.open.lo(), d),
11251143
};
1126-
let args = TokenStream::new(vec![(tok, Spacing::Joint)]);
1144+
let args = TokenStream::new(vec![tok]);
11271145
match self.toks.next()? {
1128-
TokenTree::Token(Token {
1129-
kind: TokenKind::FatArrow,
1130-
..
1131-
}) => {}
1146+
TokenTree::Token(
1147+
Token {
1148+
kind: TokenKind::FatArrow,
1149+
..
1150+
},
1151+
_,
1152+
) => {}
11321153
_ => return None,
11331154
}
11341155
let (mut hi, body, whole_body) = match self.toks.next()? {
@@ -1147,10 +1168,13 @@ impl MacroParser {
11471168
)
11481169
}
11491170
};
1150-
if let Some(TokenTree::Token(Token {
1151-
kind: TokenKind::Semi,
1152-
span,
1153-
})) = self.toks.look_ahead(0)
1171+
if let Some(TokenTree::Token(
1172+
Token {
1173+
kind: TokenKind::Semi,
1174+
span,
1175+
},
1176+
_,
1177+
)) = self.toks.look_ahead(0)
11541178
{
11551179
hi = span.hi();
11561180
self.toks.next();

0 commit comments

Comments
 (0)