Skip to content

Commit a18b34d

Browse files
committed
Auto merge of rust-lang#76291 - matklad:spacing, r=petrochenkov
Rename IsJoint -> Spacing Builds on rust-lang#76286 and might conflict with rust-lang#76285 r? `@petrochenkov`
2 parents 8819721 + ccf41dd commit a18b34d

File tree

11 files changed

+86
-97
lines changed

11 files changed

+86
-97
lines changed

compiler/rustc_ast/src/attr/mod.rs

+8-8
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use crate::ast::{Path, PathSegment};
88
use crate::mut_visit::visit_clobber;
99
use crate::ptr::P;
1010
use crate::token::{self, CommentKind, Token};
11-
use crate::tokenstream::{DelimSpan, TokenStream, TokenTree, TreeAndJoint};
11+
use crate::tokenstream::{DelimSpan, TokenStream, TokenTree, TreeAndSpacing};
1212

1313
use rustc_index::bit_set::GrowableBitSet;
1414
use rustc_span::source_map::{BytePos, Spanned};
@@ -361,7 +361,7 @@ pub fn list_contains_name(items: &[NestedMetaItem], name: Symbol) -> bool {
361361
}
362362

363363
impl MetaItem {
364-
fn token_trees_and_joints(&self) -> Vec<TreeAndJoint> {
364+
fn token_trees_and_spacings(&self) -> Vec<TreeAndSpacing> {
365365
let mut idents = vec![];
366366
let mut last_pos = BytePos(0 as u32);
367367
for (i, segment) in self.path.segments.iter().enumerate() {
@@ -374,7 +374,7 @@ impl MetaItem {
374374
idents.push(TokenTree::Token(Token::from_ast_ident(segment.ident)).into());
375375
last_pos = segment.ident.span.hi();
376376
}
377-
idents.extend(self.kind.token_trees_and_joints(self.span));
377+
idents.extend(self.kind.token_trees_and_spacings(self.span));
378378
idents
379379
}
380380

@@ -447,7 +447,7 @@ impl MetaItemKind {
447447
if i > 0 {
448448
tts.push(TokenTree::token(token::Comma, span).into());
449449
}
450-
tts.extend(item.token_trees_and_joints())
450+
tts.extend(item.token_trees_and_spacings())
451451
}
452452
MacArgs::Delimited(
453453
DelimSpan::from_single(span),
@@ -458,7 +458,7 @@ impl MetaItemKind {
458458
}
459459
}
460460

461-
fn token_trees_and_joints(&self, span: Span) -> Vec<TreeAndJoint> {
461+
fn token_trees_and_spacings(&self, span: Span) -> Vec<TreeAndSpacing> {
462462
match *self {
463463
MetaItemKind::Word => vec![],
464464
MetaItemKind::NameValue(ref lit) => {
@@ -470,7 +470,7 @@ impl MetaItemKind {
470470
if i > 0 {
471471
tokens.push(TokenTree::token(token::Comma, span).into());
472472
}
473-
tokens.extend(item.token_trees_and_joints())
473+
tokens.extend(item.token_trees_and_spacings())
474474
}
475475
vec![
476476
TokenTree::Delimited(
@@ -553,9 +553,9 @@ impl NestedMetaItem {
553553
}
554554
}
555555

556-
fn token_trees_and_joints(&self) -> Vec<TreeAndJoint> {
556+
fn token_trees_and_spacings(&self) -> Vec<TreeAndSpacing> {
557557
match *self {
558-
NestedMetaItem::MetaItem(ref item) => item.token_trees_and_joints(),
558+
NestedMetaItem::MetaItem(ref item) => item.token_trees_and_spacings(),
559559
NestedMetaItem::Literal(ref lit) => vec![lit.token_tree().into()],
560560
}
561561
}

compiler/rustc_ast/src/tokenstream.rs

+19-22
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ impl TokenTree {
8383
}
8484

8585
pub fn joint(self) -> TokenStream {
86-
TokenStream::new(vec![(self, Joint)])
86+
TokenStream::new(vec![(self, Spacing::Joint)])
8787
}
8888

8989
pub fn token(kind: TokenKind, span: Span) -> TokenTree {
@@ -125,22 +125,20 @@ where
125125
/// instead of a representation of the abstract syntax tree.
126126
/// Today's `TokenTree`s can still contain AST via `token::Interpolated` for back-compat.
127127
#[derive(Clone, Debug, Default, Encodable, Decodable)]
128-
pub struct TokenStream(pub Lrc<Vec<TreeAndJoint>>);
128+
pub struct TokenStream(pub Lrc<Vec<TreeAndSpacing>>);
129129

130-
pub type TreeAndJoint = (TokenTree, IsJoint);
130+
pub type TreeAndSpacing = (TokenTree, Spacing);
131131

132132
// `TokenStream` is used a lot. Make sure it doesn't unintentionally get bigger.
133133
#[cfg(target_arch = "x86_64")]
134134
rustc_data_structures::static_assert_size!(TokenStream, 8);
135135

136136
#[derive(Clone, Copy, Debug, PartialEq, Encodable, Decodable)]
137-
pub enum IsJoint {
137+
pub enum Spacing {
138+
Alone,
138139
Joint,
139-
NonJoint,
140140
}
141141

142-
use IsJoint::*;
143-
144142
impl TokenStream {
145143
/// Given a `TokenStream` with a `Stream` of only two arguments, return a new `TokenStream`
146144
/// separating the two arguments with a comma for diagnostic suggestions.
@@ -153,7 +151,7 @@ impl TokenStream {
153151
let sp = match (&ts, &next) {
154152
(_, (TokenTree::Token(Token { kind: token::Comma, .. }), _)) => continue,
155153
(
156-
(TokenTree::Token(token_left), NonJoint),
154+
(TokenTree::Token(token_left), Spacing::Alone),
157155
(TokenTree::Token(token_right), _),
158156
) if ((token_left.is_ident() && !token_left.is_reserved_ident())
159157
|| token_left.is_lit())
@@ -162,11 +160,11 @@ impl TokenStream {
162160
{
163161
token_left.span
164162
}
165-
((TokenTree::Delimited(sp, ..), NonJoint), _) => sp.entire(),
163+
((TokenTree::Delimited(sp, ..), Spacing::Alone), _) => sp.entire(),
166164
_ => continue,
167165
};
168166
let sp = sp.shrink_to_hi();
169-
let comma = (TokenTree::token(token::Comma, sp), NonJoint);
167+
let comma = (TokenTree::token(token::Comma, sp), Spacing::Alone);
170168
suggestion = Some((pos, comma, sp));
171169
}
172170
}
@@ -184,19 +182,19 @@ impl TokenStream {
184182

185183
impl From<TokenTree> for TokenStream {
186184
fn from(tree: TokenTree) -> TokenStream {
187-
TokenStream::new(vec![(tree, NonJoint)])
185+
TokenStream::new(vec![(tree, Spacing::Alone)])
188186
}
189187
}
190188

191-
impl From<TokenTree> for TreeAndJoint {
192-
fn from(tree: TokenTree) -> TreeAndJoint {
193-
(tree, NonJoint)
189+
impl From<TokenTree> for TreeAndSpacing {
190+
fn from(tree: TokenTree) -> TreeAndSpacing {
191+
(tree, Spacing::Alone)
194192
}
195193
}
196194

197195
impl iter::FromIterator<TokenTree> for TokenStream {
198196
fn from_iter<I: IntoIterator<Item = TokenTree>>(iter: I) -> Self {
199-
TokenStream::new(iter.into_iter().map(Into::into).collect::<Vec<TreeAndJoint>>())
197+
TokenStream::new(iter.into_iter().map(Into::into).collect::<Vec<TreeAndSpacing>>())
200198
}
201199
}
202200

@@ -209,7 +207,7 @@ impl PartialEq<TokenStream> for TokenStream {
209207
}
210208

211209
impl TokenStream {
212-
pub fn new(streams: Vec<TreeAndJoint>) -> TokenStream {
210+
pub fn new(streams: Vec<TreeAndSpacing>) -> TokenStream {
213211
TokenStream(Lrc::new(streams))
214212
}
215213

@@ -320,11 +318,11 @@ impl TokenStreamBuilder {
320318
// If `self` is not empty and the last tree within the last stream is a
321319
// token tree marked with `Joint`...
322320
if let Some(TokenStream(ref mut last_stream_lrc)) = self.0.last_mut() {
323-
if let Some((TokenTree::Token(last_token), Joint)) = last_stream_lrc.last() {
321+
if let Some((TokenTree::Token(last_token), Spacing::Joint)) = last_stream_lrc.last() {
324322
// ...and `stream` is not empty and the first tree within it is
325323
// a token tree...
326324
let TokenStream(ref mut stream_lrc) = stream;
327-
if let Some((TokenTree::Token(token), is_joint)) = stream_lrc.first() {
325+
if let Some((TokenTree::Token(token), spacing)) = stream_lrc.first() {
328326
// ...and the two tokens can be glued together...
329327
if let Some(glued_tok) = last_token.glue(&token) {
330328
// ...then do so, by overwriting the last token
@@ -337,8 +335,7 @@ impl TokenStreamBuilder {
337335
// Overwrite the last token tree with the merged
338336
// token.
339337
let last_vec_mut = Lrc::make_mut(last_stream_lrc);
340-
*last_vec_mut.last_mut().unwrap() =
341-
(TokenTree::Token(glued_tok), *is_joint);
338+
*last_vec_mut.last_mut().unwrap() = (TokenTree::Token(glued_tok), *spacing);
342339

343340
// Remove the first token tree from `stream`. (This
344341
// is almost always the only tree in `stream`.)
@@ -375,7 +372,7 @@ impl Iterator for Cursor {
375372
type Item = TokenTree;
376373

377374
fn next(&mut self) -> Option<TokenTree> {
378-
self.next_with_joint().map(|(tree, _)| tree)
375+
self.next_with_spacing().map(|(tree, _)| tree)
379376
}
380377
}
381378

@@ -384,7 +381,7 @@ impl Cursor {
384381
Cursor { stream, index: 0 }
385382
}
386383

387-
pub fn next_with_joint(&mut self) -> Option<TreeAndJoint> {
384+
pub fn next_with_spacing(&mut self) -> Option<TreeAndSpacing> {
388385
if self.index < self.stream.len() {
389386
self.index += 1;
390387
Some(self.stream.0[self.index - 1].clone())

compiler/rustc_expand/src/mbe/transcribe.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use crate::mbe::macro_parser::{MatchedNonterminal, MatchedSeq, NamedMatch};
44

55
use rustc_ast::mut_visit::{self, MutVisitor};
66
use rustc_ast::token::{self, NtTT, Token};
7-
use rustc_ast::tokenstream::{DelimSpan, TokenStream, TokenTree, TreeAndJoint};
7+
use rustc_ast::tokenstream::{DelimSpan, TokenStream, TokenTree, TreeAndSpacing};
88
use rustc_ast::MacCall;
99
use rustc_data_structures::fx::FxHashMap;
1010
use rustc_data_structures::sync::Lrc;
@@ -111,7 +111,7 @@ pub(super) fn transcribe<'a>(
111111
//
112112
// Thus, if we try to pop the `result_stack` and it is empty, we have reached the top-level
113113
// again, and we are done transcribing.
114-
let mut result: Vec<TreeAndJoint> = Vec::new();
114+
let mut result: Vec<TreeAndSpacing> = Vec::new();
115115
let mut result_stack = Vec::new();
116116
let mut marker = Marker(cx.current_expansion.id, transparency);
117117

compiler/rustc_expand/src/proc_macro_server.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use crate::base::ExtCtxt;
22

33
use rustc_ast as ast;
44
use rustc_ast::token;
5-
use rustc_ast::tokenstream::{self, DelimSpan, IsJoint::*, TokenStream, TreeAndJoint};
5+
use rustc_ast::tokenstream::{self, DelimSpan, Spacing::*, TokenStream, TreeAndSpacing};
66
use rustc_ast_pretty::pprust;
77
use rustc_data_structures::sync::Lrc;
88
use rustc_errors::Diagnostic;
@@ -47,15 +47,15 @@ impl ToInternal<token::DelimToken> for Delimiter {
4747
}
4848
}
4949

50-
impl FromInternal<(TreeAndJoint, &'_ ParseSess, &'_ mut Vec<Self>)>
50+
impl FromInternal<(TreeAndSpacing, &'_ ParseSess, &'_ mut Vec<Self>)>
5151
for TokenTree<Group, Punct, Ident, Literal>
5252
{
5353
fn from_internal(
54-
((tree, is_joint), sess, stack): (TreeAndJoint, &ParseSess, &mut Vec<Self>),
54+
((tree, spacing), sess, stack): (TreeAndSpacing, &ParseSess, &mut Vec<Self>),
5555
) -> Self {
5656
use rustc_ast::token::*;
5757

58-
let joint = is_joint == Joint;
58+
let joint = spacing == Joint;
5959
let Token { kind, span } = match tree {
6060
tokenstream::TokenTree::Delimited(span, delim, tts) => {
6161
let delimiter = Delimiter::from_internal(delim);
@@ -261,7 +261,7 @@ impl ToInternal<TokenStream> for TokenTree<Group, Punct, Ident, Literal> {
261261
};
262262

263263
let tree = tokenstream::TokenTree::token(kind, span);
264-
TokenStream::new(vec![(tree, if joint { Joint } else { NonJoint })])
264+
TokenStream::new(vec![(tree, if joint { Joint } else { Alone })])
265265
}
266266
}
267267

@@ -444,7 +444,7 @@ impl server::TokenStreamIter for Rustc<'_> {
444444
) -> Option<TokenTree<Self::Group, Self::Punct, Self::Ident, Self::Literal>> {
445445
loop {
446446
let tree = iter.stack.pop().or_else(|| {
447-
let next = iter.cursor.next_with_joint()?;
447+
let next = iter.cursor.next_with_spacing()?;
448448
Some(TokenTree::from_internal((next, self.sess, &mut iter.stack)))
449449
})?;
450450
// A hack used to pass AST fragments to attribute and derive macros

compiler/rustc_parse/src/lexer/mod.rs

+22-35
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,19 @@
11
use rustc_ast::ast::AttrStyle;
22
use rustc_ast::token::{self, CommentKind, Token, TokenKind};
3-
use rustc_ast::tokenstream::IsJoint;
4-
use rustc_data_structures::sync::Lrc;
5-
use rustc_errors::{error_code, Applicability, DiagnosticBuilder, FatalError};
6-
use rustc_lexer::Base;
7-
use rustc_lexer::{unescape, RawStrError};
3+
use rustc_ast::tokenstream::{Spacing, TokenStream};
4+
use rustc_errors::{error_code, Applicability, DiagnosticBuilder, FatalError, PResult};
5+
use rustc_lexer::unescape::{self, Mode};
6+
use rustc_lexer::{Base, DocStyle, RawStrError};
87
use rustc_session::parse::ParseSess;
98
use rustc_span::symbol::{sym, Symbol};
109
use rustc_span::{BytePos, Pos, Span};
1110

12-
use std::char;
1311
use tracing::debug;
1412

1513
mod tokentrees;
1614
mod unescape_error_reporting;
1715
mod unicode_chars;
1816

19-
use rustc_lexer::{unescape::Mode, DocStyle};
2017
use unescape_error_reporting::{emit_unescape_error, push_escaped_char};
2118

2219
#[derive(Clone, Debug)]
@@ -28,7 +25,17 @@ pub struct UnmatchedBrace {
2825
pub candidate_span: Option<Span>,
2926
}
3027

31-
crate struct StringReader<'a> {
28+
crate fn parse_token_trees<'a>(
29+
sess: &'a ParseSess,
30+
src: &'a str,
31+
start_pos: BytePos,
32+
override_span: Option<Span>,
33+
) -> (PResult<'a, TokenStream>, Vec<UnmatchedBrace>) {
34+
StringReader { sess, start_pos, pos: start_pos, end_src_index: src.len(), src, override_span }
35+
.into_token_trees()
36+
}
37+
38+
struct StringReader<'a> {
3239
sess: &'a ParseSess,
3340
/// Initial position, read-only.
3441
start_pos: BytePos,
@@ -37,38 +44,18 @@ crate struct StringReader<'a> {
3744
/// Stop reading src at this index.
3845
end_src_index: usize,
3946
/// Source text to tokenize.
40-
src: Lrc<String>,
47+
src: &'a str,
4148
override_span: Option<Span>,
4249
}
4350

4451
impl<'a> StringReader<'a> {
45-
crate fn new(
46-
sess: &'a ParseSess,
47-
source_file: Lrc<rustc_span::SourceFile>,
48-
override_span: Option<Span>,
49-
) -> Self {
50-
let src = source_file.src.clone().unwrap_or_else(|| {
51-
sess.span_diagnostic
52-
.bug(&format!("cannot lex `source_file` without source: {}", source_file.name));
53-
});
54-
55-
StringReader {
56-
sess,
57-
start_pos: source_file.start_pos,
58-
pos: source_file.start_pos,
59-
end_src_index: src.len(),
60-
src,
61-
override_span,
62-
}
63-
}
64-
6552
fn mk_sp(&self, lo: BytePos, hi: BytePos) -> Span {
6653
self.override_span.unwrap_or_else(|| Span::with_root_ctxt(lo, hi))
6754
}
6855

6956
/// Returns the next token, and info about preceding whitespace, if any.
70-
fn next_token(&mut self) -> (IsJoint, Token) {
71-
let mut is_joint = IsJoint::Joint;
57+
fn next_token(&mut self) -> (Spacing, Token) {
58+
let mut spacing = Spacing::Joint;
7259

7360
// Skip `#!` at the start of the file
7461
let start_src_index = self.src_index(self.pos);
@@ -77,7 +64,7 @@ impl<'a> StringReader<'a> {
7764
if is_beginning_of_file {
7865
if let Some(shebang_len) = rustc_lexer::strip_shebang(text) {
7966
self.pos = self.pos + BytePos::from_usize(shebang_len);
80-
is_joint = IsJoint::NonJoint;
67+
spacing = Spacing::Alone;
8168
}
8269
}
8370

@@ -88,7 +75,7 @@ impl<'a> StringReader<'a> {
8875

8976
if text.is_empty() {
9077
let span = self.mk_sp(self.pos, self.pos);
91-
return (is_joint, Token::new(token::Eof, span));
78+
return (spacing, Token::new(token::Eof, span));
9279
}
9380

9481
let token = rustc_lexer::first_token(text);
@@ -101,9 +88,9 @@ impl<'a> StringReader<'a> {
10188
match self.cook_lexer_token(token.kind, start) {
10289
Some(kind) => {
10390
let span = self.mk_sp(start, self.pos);
104-
return (is_joint, Token::new(kind, span));
91+
return (spacing, Token::new(kind, span));
10592
}
106-
None => is_joint = IsJoint::NonJoint,
93+
None => spacing = Spacing::Alone,
10794
}
10895
}
10996
}

0 commit comments

Comments
 (0)