Skip to content

Commit 6835748

Browse files
committed
Auto merge of #50838 - alexcrichton:token-impls, r=eddyb
rustc: Fix joint-ness of stringified token-streams This commit fixes `StringReader`'s parsing of tokens which have been stringified through procedural macros. Whether or not a token tree is joint is defined by span information, but when working with procedural macros these spans are often dummy and/or overridden which means that they end up considering all operators joint if they can! The fix here is to track the raw source span as opposed to the overridden span. With this information we can more accurately classify `Punct` structs as either joint or not. Closes #50700
2 parents 38fd7ea + 0ee031a commit 6835748

File tree

5 files changed

+117
-17
lines changed

5 files changed

+117
-17
lines changed

src/libsyntax/parse/lexer/mod.rs

+28-6
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,10 @@ pub struct TokenAndSpan {
3434

3535
impl Default for TokenAndSpan {
3636
fn default() -> Self {
37-
TokenAndSpan { tok: token::Whitespace, sp: syntax_pos::DUMMY_SP }
37+
TokenAndSpan {
38+
tok: token::Whitespace,
39+
sp: syntax_pos::DUMMY_SP,
40+
}
3841
}
3942
}
4043

@@ -54,22 +57,30 @@ pub struct StringReader<'a> {
5457
/// If part of a filemap is being re-lexed, this should be set to false.
5558
pub save_new_lines_and_multibyte: bool,
5659
// cached:
57-
pub peek_tok: token::Token,
58-
pub peek_span: Span,
60+
peek_tok: token::Token,
61+
peek_span: Span,
62+
peek_span_src_raw: Span,
5963
pub fatal_errs: Vec<DiagnosticBuilder<'a>>,
6064
// cache a direct reference to the source text, so that we don't have to
6165
// retrieve it via `self.filemap.src.as_ref().unwrap()` all the time.
6266
src: Lrc<String>,
6367
/// Stack of open delimiters and their spans. Used for error message.
6468
token: token::Token,
6569
span: Span,
70+
/// The raw source span which *does not* take `override_span` into account
71+
span_src_raw: Span,
6672
open_braces: Vec<(token::DelimToken, Span)>,
6773
pub override_span: Option<Span>,
6874
}
6975

7076
impl<'a> StringReader<'a> {
7177
fn mk_sp(&self, lo: BytePos, hi: BytePos) -> Span {
72-
unwrap_or!(self.override_span, Span::new(lo, hi, NO_EXPANSION))
78+
self.mk_sp_and_raw(lo, hi).0
79+
}
80+
fn mk_sp_and_raw(&self, lo: BytePos, hi: BytePos) -> (Span, Span) {
81+
let raw = Span::new(lo, hi, NO_EXPANSION);
82+
let real = unwrap_or!(self.override_span, raw);
83+
(real, raw)
7384
}
7485
fn mk_ident(&self, string: &str) -> Ident {
7586
let mut ident = Ident::from_str(string);
@@ -121,6 +132,7 @@ impl<'a> StringReader<'a> {
121132
sp: self.peek_span,
122133
};
123134
self.advance_token()?;
135+
self.span_src_raw = self.peek_span_src_raw;
124136
Ok(ret_val)
125137
}
126138

@@ -182,10 +194,12 @@ impl<'a> StringReader<'a> {
182194
// dummy values; not read
183195
peek_tok: token::Eof,
184196
peek_span: syntax_pos::DUMMY_SP,
197+
peek_span_src_raw: syntax_pos::DUMMY_SP,
185198
src,
186199
fatal_errs: Vec::new(),
187200
token: token::Eof,
188201
span: syntax_pos::DUMMY_SP,
202+
span_src_raw: syntax_pos::DUMMY_SP,
189203
open_braces: Vec::new(),
190204
override_span,
191205
}
@@ -328,17 +342,25 @@ impl<'a> StringReader<'a> {
328342
fn advance_token(&mut self) -> Result<(), ()> {
329343
match self.scan_whitespace_or_comment() {
330344
Some(comment) => {
345+
self.peek_span_src_raw = comment.sp;
331346
self.peek_span = comment.sp;
332347
self.peek_tok = comment.tok;
333348
}
334349
None => {
335350
if self.is_eof() {
336351
self.peek_tok = token::Eof;
337-
self.peek_span = self.mk_sp(self.filemap.end_pos, self.filemap.end_pos);
352+
let (real, raw) = self.mk_sp_and_raw(
353+
self.filemap.end_pos,
354+
self.filemap.end_pos,
355+
);
356+
self.peek_span = real;
357+
self.peek_span_src_raw = raw;
338358
} else {
339359
let start_bytepos = self.pos;
340360
self.peek_tok = self.next_token_inner()?;
341-
self.peek_span = self.mk_sp(start_bytepos, self.pos);
361+
let (real, raw) = self.mk_sp_and_raw(start_bytepos, self.pos);
362+
self.peek_span = real;
363+
self.peek_span_src_raw = raw;
342364
};
343365
}
344366
}

src/libsyntax/parse/lexer/tokentrees.rs

+12-11
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,7 @@ impl<'a> StringReader<'a> {
1818
pub fn parse_all_token_trees(&mut self) -> PResult<'a, TokenStream> {
1919
let mut tts = Vec::new();
2020
while self.token != token::Eof {
21-
let tree = self.parse_token_tree()?;
22-
let is_joint = tree.span().hi() == self.span.lo() && token::is_op(&self.token);
23-
tts.push(if is_joint { tree.joint() } else { tree.into() });
21+
tts.push(self.parse_token_tree()?);
2422
}
2523
Ok(TokenStream::concat(tts))
2624
}
@@ -32,19 +30,17 @@ impl<'a> StringReader<'a> {
3230
if let token::CloseDelim(..) = self.token {
3331
return TokenStream::concat(tts);
3432
}
35-
let tree = match self.parse_token_tree() {
36-
Ok(tree) => tree,
33+
match self.parse_token_tree() {
34+
Ok(tree) => tts.push(tree),
3735
Err(mut e) => {
3836
e.emit();
3937
return TokenStream::concat(tts);
4038
}
41-
};
42-
let is_joint = tree.span().hi() == self.span.lo() && token::is_op(&self.token);
43-
tts.push(if is_joint { tree.joint() } else { tree.into() });
39+
}
4440
}
4541
}
4642

47-
fn parse_token_tree(&mut self) -> PResult<'a, TokenTree> {
43+
fn parse_token_tree(&mut self) -> PResult<'a, TokenStream> {
4844
match self.token {
4945
token::Eof => {
5046
let msg = "this file contains an un-closed delimiter";
@@ -115,7 +111,7 @@ impl<'a> StringReader<'a> {
115111
Ok(TokenTree::Delimited(span, Delimited {
116112
delim,
117113
tts: tts.into(),
118-
}))
114+
}).into())
119115
},
120116
token::CloseDelim(_) => {
121117
// An unexpected closing delimiter (i.e., there is no
@@ -127,8 +123,13 @@ impl<'a> StringReader<'a> {
127123
},
128124
_ => {
129125
let tt = TokenTree::Token(self.span, self.token.clone());
126+
// Note that testing for joint-ness here is done via the raw
127+
// source span as the joint-ness is a property of the raw source
128+
// rather than wanting to take `override_span` into account.
129+
let raw = self.span_src_raw;
130130
self.real_token();
131-
Ok(tt)
131+
let is_joint = raw.hi() == self.span_src_raw.lo() && token::is_op(&self.token);
132+
Ok(if is_joint { tt.joint() } else { tt.into() })
132133
}
133134
}
134135
}

src/libsyntax/parse/token.rs

+2
Original file line numberDiff line numberDiff line change
@@ -581,6 +581,8 @@ impl Token {
581581
if tokens.probably_equal_for_proc_macro(&tokens_for_real) {
582582
return tokens
583583
}
584+
info!("cached tokens found, but they're not \"probably equal\", \
585+
going with stringified version");
584586
}
585587
return tokens_for_real
586588
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
// Copyright 2018 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
// no-prefer-dynamic
12+
13+
#![crate_type = "proc-macro"]
14+
#![feature(proc_macro)]
15+
16+
extern crate proc_macro;
17+
18+
use proc_macro::*;
19+
20+
#[proc_macro]
21+
pub fn tokens(input: TokenStream) -> TokenStream {
22+
assert_nothing_joint(input);
23+
TokenStream::empty()
24+
}
25+
26+
#[proc_macro_attribute]
27+
pub fn nothing(_: TokenStream, input: TokenStream) -> TokenStream {
28+
assert_nothing_joint(input);
29+
TokenStream::empty()
30+
}
31+
32+
fn assert_nothing_joint(s: TokenStream) {
33+
for tt in s {
34+
match tt {
35+
TokenTree::Group(g) => assert_nothing_joint(g.stream()),
36+
TokenTree::Punct(p) => assert_eq!(p.spacing(), Spacing::Alone),
37+
_ => {}
38+
}
39+
}
40+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// Copyright 2018 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
// aux-build:not-joint.rs
12+
13+
#![feature(proc_macro)]
14+
15+
extern crate not_joint as bar;
16+
use bar::{tokens, nothing};
17+
18+
tokens![< -];
19+
20+
#[nothing]
21+
a![< -];
22+
23+
#[nothing]
24+
b!{< -}
25+
26+
#[nothing]
27+
c!(< -);
28+
29+
#[nothing]
30+
fn foo() {
31+
//! dox
32+
let x = 2 < - 3;
33+
}
34+
35+
fn main() {}

0 commit comments

Comments
 (0)