Auto merge of #76291 - matklad:spacing, r=petrochenkov
Rename IsJoint -> Spacing Builds on #76286 and might conflict with #76285 r? `@petrochenkov`
This commit is contained in:
commit
a18b34d979
11 changed files with 86 additions and 97 deletions
|
@ -8,7 +8,7 @@ use crate::ast::{Path, PathSegment};
|
|||
use crate::mut_visit::visit_clobber;
|
||||
use crate::ptr::P;
|
||||
use crate::token::{self, CommentKind, Token};
|
||||
use crate::tokenstream::{DelimSpan, TokenStream, TokenTree, TreeAndJoint};
|
||||
use crate::tokenstream::{DelimSpan, TokenStream, TokenTree, TreeAndSpacing};
|
||||
|
||||
use rustc_index::bit_set::GrowableBitSet;
|
||||
use rustc_span::source_map::{BytePos, Spanned};
|
||||
|
@ -361,7 +361,7 @@ pub fn list_contains_name(items: &[NestedMetaItem], name: Symbol) -> bool {
|
|||
}
|
||||
|
||||
impl MetaItem {
|
||||
fn token_trees_and_joints(&self) -> Vec<TreeAndJoint> {
|
||||
fn token_trees_and_spacings(&self) -> Vec<TreeAndSpacing> {
|
||||
let mut idents = vec![];
|
||||
let mut last_pos = BytePos(0 as u32);
|
||||
for (i, segment) in self.path.segments.iter().enumerate() {
|
||||
|
@ -374,7 +374,7 @@ impl MetaItem {
|
|||
idents.push(TokenTree::Token(Token::from_ast_ident(segment.ident)).into());
|
||||
last_pos = segment.ident.span.hi();
|
||||
}
|
||||
idents.extend(self.kind.token_trees_and_joints(self.span));
|
||||
idents.extend(self.kind.token_trees_and_spacings(self.span));
|
||||
idents
|
||||
}
|
||||
|
||||
|
@ -447,7 +447,7 @@ impl MetaItemKind {
|
|||
if i > 0 {
|
||||
tts.push(TokenTree::token(token::Comma, span).into());
|
||||
}
|
||||
tts.extend(item.token_trees_and_joints())
|
||||
tts.extend(item.token_trees_and_spacings())
|
||||
}
|
||||
MacArgs::Delimited(
|
||||
DelimSpan::from_single(span),
|
||||
|
@ -458,7 +458,7 @@ impl MetaItemKind {
|
|||
}
|
||||
}
|
||||
|
||||
fn token_trees_and_joints(&self, span: Span) -> Vec<TreeAndJoint> {
|
||||
fn token_trees_and_spacings(&self, span: Span) -> Vec<TreeAndSpacing> {
|
||||
match *self {
|
||||
MetaItemKind::Word => vec![],
|
||||
MetaItemKind::NameValue(ref lit) => {
|
||||
|
@ -470,7 +470,7 @@ impl MetaItemKind {
|
|||
if i > 0 {
|
||||
tokens.push(TokenTree::token(token::Comma, span).into());
|
||||
}
|
||||
tokens.extend(item.token_trees_and_joints())
|
||||
tokens.extend(item.token_trees_and_spacings())
|
||||
}
|
||||
vec![
|
||||
TokenTree::Delimited(
|
||||
|
@ -553,9 +553,9 @@ impl NestedMetaItem {
|
|||
}
|
||||
}
|
||||
|
||||
fn token_trees_and_joints(&self) -> Vec<TreeAndJoint> {
|
||||
fn token_trees_and_spacings(&self) -> Vec<TreeAndSpacing> {
|
||||
match *self {
|
||||
NestedMetaItem::MetaItem(ref item) => item.token_trees_and_joints(),
|
||||
NestedMetaItem::MetaItem(ref item) => item.token_trees_and_spacings(),
|
||||
NestedMetaItem::Literal(ref lit) => vec![lit.token_tree().into()],
|
||||
}
|
||||
}
|
||||
|
|
|
@ -83,7 +83,7 @@ impl TokenTree {
|
|||
}
|
||||
|
||||
pub fn joint(self) -> TokenStream {
|
||||
TokenStream::new(vec![(self, Joint)])
|
||||
TokenStream::new(vec![(self, Spacing::Joint)])
|
||||
}
|
||||
|
||||
pub fn token(kind: TokenKind, span: Span) -> TokenTree {
|
||||
|
@ -125,22 +125,20 @@ where
|
|||
/// instead of a representation of the abstract syntax tree.
|
||||
/// Today's `TokenTree`s can still contain AST via `token::Interpolated` for back-compat.
|
||||
#[derive(Clone, Debug, Default, Encodable, Decodable)]
|
||||
pub struct TokenStream(pub Lrc<Vec<TreeAndJoint>>);
|
||||
pub struct TokenStream(pub Lrc<Vec<TreeAndSpacing>>);
|
||||
|
||||
pub type TreeAndJoint = (TokenTree, IsJoint);
|
||||
pub type TreeAndSpacing = (TokenTree, Spacing);
|
||||
|
||||
// `TokenStream` is used a lot. Make sure it doesn't unintentionally get bigger.
|
||||
#[cfg(target_arch = "x86_64")]
|
||||
rustc_data_structures::static_assert_size!(TokenStream, 8);
|
||||
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Encodable, Decodable)]
|
||||
pub enum IsJoint {
|
||||
pub enum Spacing {
|
||||
Alone,
|
||||
Joint,
|
||||
NonJoint,
|
||||
}
|
||||
|
||||
use IsJoint::*;
|
||||
|
||||
impl TokenStream {
|
||||
/// Given a `TokenStream` with a `Stream` of only two arguments, return a new `TokenStream`
|
||||
/// separating the two arguments with a comma for diagnostic suggestions.
|
||||
|
@ -153,7 +151,7 @@ impl TokenStream {
|
|||
let sp = match (&ts, &next) {
|
||||
(_, (TokenTree::Token(Token { kind: token::Comma, .. }), _)) => continue,
|
||||
(
|
||||
(TokenTree::Token(token_left), NonJoint),
|
||||
(TokenTree::Token(token_left), Spacing::Alone),
|
||||
(TokenTree::Token(token_right), _),
|
||||
) if ((token_left.is_ident() && !token_left.is_reserved_ident())
|
||||
|| token_left.is_lit())
|
||||
|
@ -162,11 +160,11 @@ impl TokenStream {
|
|||
{
|
||||
token_left.span
|
||||
}
|
||||
((TokenTree::Delimited(sp, ..), NonJoint), _) => sp.entire(),
|
||||
((TokenTree::Delimited(sp, ..), Spacing::Alone), _) => sp.entire(),
|
||||
_ => continue,
|
||||
};
|
||||
let sp = sp.shrink_to_hi();
|
||||
let comma = (TokenTree::token(token::Comma, sp), NonJoint);
|
||||
let comma = (TokenTree::token(token::Comma, sp), Spacing::Alone);
|
||||
suggestion = Some((pos, comma, sp));
|
||||
}
|
||||
}
|
||||
|
@ -184,19 +182,19 @@ impl TokenStream {
|
|||
|
||||
impl From<TokenTree> for TokenStream {
|
||||
fn from(tree: TokenTree) -> TokenStream {
|
||||
TokenStream::new(vec![(tree, NonJoint)])
|
||||
TokenStream::new(vec![(tree, Spacing::Alone)])
|
||||
}
|
||||
}
|
||||
|
||||
impl From<TokenTree> for TreeAndJoint {
|
||||
fn from(tree: TokenTree) -> TreeAndJoint {
|
||||
(tree, NonJoint)
|
||||
impl From<TokenTree> for TreeAndSpacing {
|
||||
fn from(tree: TokenTree) -> TreeAndSpacing {
|
||||
(tree, Spacing::Alone)
|
||||
}
|
||||
}
|
||||
|
||||
impl iter::FromIterator<TokenTree> for TokenStream {
|
||||
fn from_iter<I: IntoIterator<Item = TokenTree>>(iter: I) -> Self {
|
||||
TokenStream::new(iter.into_iter().map(Into::into).collect::<Vec<TreeAndJoint>>())
|
||||
TokenStream::new(iter.into_iter().map(Into::into).collect::<Vec<TreeAndSpacing>>())
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -209,7 +207,7 @@ impl PartialEq<TokenStream> for TokenStream {
|
|||
}
|
||||
|
||||
impl TokenStream {
|
||||
pub fn new(streams: Vec<TreeAndJoint>) -> TokenStream {
|
||||
pub fn new(streams: Vec<TreeAndSpacing>) -> TokenStream {
|
||||
TokenStream(Lrc::new(streams))
|
||||
}
|
||||
|
||||
|
@ -320,11 +318,11 @@ impl TokenStreamBuilder {
|
|||
// If `self` is not empty and the last tree within the last stream is a
|
||||
// token tree marked with `Joint`...
|
||||
if let Some(TokenStream(ref mut last_stream_lrc)) = self.0.last_mut() {
|
||||
if let Some((TokenTree::Token(last_token), Joint)) = last_stream_lrc.last() {
|
||||
if let Some((TokenTree::Token(last_token), Spacing::Joint)) = last_stream_lrc.last() {
|
||||
// ...and `stream` is not empty and the first tree within it is
|
||||
// a token tree...
|
||||
let TokenStream(ref mut stream_lrc) = stream;
|
||||
if let Some((TokenTree::Token(token), is_joint)) = stream_lrc.first() {
|
||||
if let Some((TokenTree::Token(token), spacing)) = stream_lrc.first() {
|
||||
// ...and the two tokens can be glued together...
|
||||
if let Some(glued_tok) = last_token.glue(&token) {
|
||||
// ...then do so, by overwriting the last token
|
||||
|
@ -337,8 +335,7 @@ impl TokenStreamBuilder {
|
|||
// Overwrite the last token tree with the merged
|
||||
// token.
|
||||
let last_vec_mut = Lrc::make_mut(last_stream_lrc);
|
||||
*last_vec_mut.last_mut().unwrap() =
|
||||
(TokenTree::Token(glued_tok), *is_joint);
|
||||
*last_vec_mut.last_mut().unwrap() = (TokenTree::Token(glued_tok), *spacing);
|
||||
|
||||
// Remove the first token tree from `stream`. (This
|
||||
// is almost always the only tree in `stream`.)
|
||||
|
@ -375,7 +372,7 @@ impl Iterator for Cursor {
|
|||
type Item = TokenTree;
|
||||
|
||||
fn next(&mut self) -> Option<TokenTree> {
|
||||
self.next_with_joint().map(|(tree, _)| tree)
|
||||
self.next_with_spacing().map(|(tree, _)| tree)
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -384,7 +381,7 @@ impl Cursor {
|
|||
Cursor { stream, index: 0 }
|
||||
}
|
||||
|
||||
pub fn next_with_joint(&mut self) -> Option<TreeAndJoint> {
|
||||
pub fn next_with_spacing(&mut self) -> Option<TreeAndSpacing> {
|
||||
if self.index < self.stream.len() {
|
||||
self.index += 1;
|
||||
Some(self.stream.0[self.index - 1].clone())
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue