Skip to content

Commit 8bea334

Browse files
committed
don't rely on spans when checking tokens for jointness
1 parent 1c6eb19 commit 8bea334

File tree

2 files changed

+29
-47
lines changed

2 files changed

+29
-47
lines changed

src/libsyntax/parse/lexer/mod.rs

Lines changed: 11 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -123,54 +123,30 @@ impl<'a> StringReader<'a> {
123123
/// `Err(())` means that some errors were encountered, which can be
124124
/// retrieved using `buffer_fatal_errors`.
125125
pub fn try_next_token(&mut self) -> Result<Token, ()> {
126-
let (token, _raw_span) = self.try_next_token_with_raw_span()?;
127-
Ok(token)
128-
}
129-
130-
/// Returns the next token, including trivia like whitespace or comments.
131-
///
132-
/// Aborts in case of an error.
133-
pub fn next_token(&mut self) -> Token {
134-
let res = self.try_next_token();
135-
self.unwrap_or_abort(res)
136-
}
137-
138-
/// Returns the next token, skipping over trivia.
139-
/// Also returns an unoverriden span which can be used to check tokens
140-
fn real_token(&mut self) -> (Token, Span) {
141-
let res = try {
142-
loop {
143-
let t = self.try_next_token_with_raw_span()?;
144-
match t.0.kind {
145-
token::Whitespace | token::Comment | token::Shebang(_) => continue,
146-
_ => break t,
147-
}
148-
}
149-
};
150-
151-
self.unwrap_or_abort(res)
152-
}
153-
154-
fn try_next_token_with_raw_span(&mut self) -> Result<(Token, Span), ()> {
155126
assert!(self.fatal_errs.is_empty());
156127
match self.scan_whitespace_or_comment() {
157-
Some(comment) => {
158-
let raw_span = comment.span;
159-
Ok((comment, raw_span))
160-
}
128+
Some(comment) => Ok(comment),
161129
None => {
162130
let (kind, start_pos, end_pos) = if self.is_eof() {
163131
(token::Eof, self.source_file.end_pos, self.source_file.end_pos)
164132
} else {
165133
let start_pos = self.pos;
166134
(self.next_token_inner()?, start_pos, self.pos)
167135
};
168-
let (real, raw) = self.mk_sp_and_raw(start_pos, end_pos);
169-
Ok((Token::new(kind, real), raw))
136+
let (real, _raw) = self.mk_sp_and_raw(start_pos, end_pos);
137+
Ok(Token::new(kind, real))
170138
}
171139
}
172140
}
173141

142+
/// Returns the next token, including trivia like whitespace or comments.
143+
///
144+
/// Aborts in case of an error.
145+
pub fn next_token(&mut self) -> Token {
146+
let res = self.try_next_token();
147+
self.unwrap_or_abort(res)
148+
}
149+
174150
#[inline]
175151
fn is_eof(&self) -> bool {
176152
self.ch.is_none()

src/libsyntax/parse/lexer/tokentrees.rs

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
1-
use syntax_pos::{Span, DUMMY_SP};
1+
use syntax_pos::Span;
22

33
use crate::print::pprust::token_to_string;
44
use crate::parse::lexer::{StringReader, UnmatchedBrace};
55
use crate::parse::token::{self, Token};
66
use crate::parse::PResult;
7-
use crate::tokenstream::{DelimSpan, IsJoint::*, TokenStream, TokenTree, TreeAndJoint};
7+
use crate::tokenstream::{DelimSpan, IsJoint::{self, *}, TokenStream, TokenTree, TreeAndJoint};
88

99
impl<'a> StringReader<'a> {
1010
crate fn into_token_trees(self) -> (PResult<'a, TokenStream>, Vec<UnmatchedBrace>) {
1111
let mut tt_reader = TokenTreesReader {
1212
string_reader: self,
1313
token: Token::dummy(),
14-
raw_span: DUMMY_SP,
14+
joint_to_prev: Joint,
1515
open_braces: Vec::new(),
1616
unmatched_braces: Vec::new(),
1717
matching_delim_spans: Vec::new(),
@@ -25,7 +25,7 @@ impl<'a> StringReader<'a> {
2525
struct TokenTreesReader<'a> {
2626
string_reader: StringReader<'a>,
2727
token: Token,
28-
raw_span: Span,
28+
joint_to_prev: IsJoint,
2929
/// Stack of open delimiters and their spans. Used for error message.
3030
open_braces: Vec<(token::DelimToken, Span)>,
3131
unmatched_braces: Vec<UnmatchedBrace>,
@@ -205,20 +205,26 @@ impl<'a> TokenTreesReader<'a> {
205205
},
206206
_ => {
207207
let tt = TokenTree::Token(self.token.take());
208-
// Note that testing for joint-ness here is done via the raw
209-
// source span as the joint-ness is a property of the raw source
210-
// rather than wanting to take `override_span` into account.
211-
let raw_span = self.raw_span;
212208
self.real_token();
213-
let is_joint = raw_span.hi() == self.raw_span.lo() && self.token.is_op();
209+
let is_joint = self.joint_to_prev == Joint && self.token.is_op();
214210
Ok((tt, if is_joint { Joint } else { NonJoint }))
215211
}
216212
}
217213
}
218214

219215
fn real_token(&mut self) {
220-
let (token, raw_span) = self.string_reader.real_token();
221-
self.token = token;
222-
self.raw_span = raw_span;
216+
self.joint_to_prev = Joint;
217+
loop {
218+
let token = self.string_reader.next_token();
219+
match token.kind {
220+
token::Whitespace | token::Comment | token::Shebang(_) => {
221+
self.joint_to_prev = NonJoint;
222+
}
223+
_ => {
224+
self.token = token;
225+
return;
226+
},
227+
}
228+
}
223229
}
224230
}

0 commit comments

Comments
 (0)