Skip to content

Commit 85cee57

Browse files
authored
Rollup merge of #76285 - matklad:censor-spacing, r=petrochenkov
Move jointness censoring to proc_macro Proc-macro API currently exposes jointness in `Punct` tokens. That is, `+` in `+one` is **non** joint. Our lexer produces jointness info for all tokens, so we need to censor it *somewhere* Previously we did this in a lexer, but it makes more sense to do this in a proc-macro server. r? @petrochenkov
2 parents b4d3873 + 09d3db2 commit 85cee57

File tree

4 files changed

+25
-16
lines changed

4 files changed

+25
-16
lines changed

compiler/rustc_ast/src/tokenstream.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -403,8 +403,8 @@ impl Cursor {
403403
self.index = index;
404404
}
405405

406-
pub fn look_ahead(&self, n: usize) -> Option<TokenTree> {
407-
self.stream.0[self.index..].get(n).map(|(tree, _)| tree.clone())
406+
pub fn look_ahead(&self, n: usize) -> Option<&TokenTree> {
407+
self.stream.0[self.index..].get(n).map(|(tree, _)| tree)
408408
}
409409
}
410410

compiler/rustc_expand/src/proc_macro_server.rs

+17-5
Original file line numberDiff line numberDiff line change
@@ -47,15 +47,26 @@ impl ToInternal<token::DelimToken> for Delimiter {
4747
}
4848
}
4949

50-
impl FromInternal<(TreeAndJoint, &'_ ParseSess, &'_ mut Vec<Self>)>
51-
for TokenTree<Group, Punct, Ident, Literal>
50+
impl
51+
FromInternal<(
52+
TreeAndJoint,
53+
Option<&'_ tokenstream::TokenTree>,
54+
&'_ ParseSess,
55+
&'_ mut Vec<Self>,
56+
)> for TokenTree<Group, Punct, Ident, Literal>
5257
{
5358
fn from_internal(
54-
((tree, is_joint), sess, stack): (TreeAndJoint, &ParseSess, &mut Vec<Self>),
59+
((tree, is_joint), look_ahead, sess, stack): (
60+
TreeAndJoint,
61+
Option<&tokenstream::TokenTree>,
62+
&ParseSess,
63+
&mut Vec<Self>,
64+
),
5565
) -> Self {
5666
use rustc_ast::token::*;
5767

58-
let joint = is_joint == Joint;
68+
let joint = is_joint == Joint
69+
&& matches!(look_ahead, Some(tokenstream::TokenTree::Token(t)) if t.is_op());
5970
let Token { kind, span } = match tree {
6071
tokenstream::TokenTree::Delimited(span, delim, tts) => {
6172
let delimiter = Delimiter::from_internal(delim);
@@ -445,7 +456,8 @@ impl server::TokenStreamIter for Rustc<'_> {
445456
loop {
446457
let tree = iter.stack.pop().or_else(|| {
447458
let next = iter.cursor.next_with_joint()?;
448-
Some(TokenTree::from_internal((next, self.sess, &mut iter.stack)))
459+
let lookahead = iter.cursor.look_ahead(0);
460+
Some(TokenTree::from_internal((next, lookahead, self.sess, &mut iter.stack)))
449461
})?;
450462
// A hack used to pass AST fragments to attribute and derive macros
451463
// as a single nonterminal token instead of a token stream.

compiler/rustc_parse/src/lexer/tokentrees.rs

+1-4
Original file line numberDiff line numberDiff line change
@@ -262,10 +262,7 @@ impl<'a> TokenTreesReader<'a> {
262262
}
263263
_ => {
264264
let tt = TokenTree::Token(self.token.take());
265-
let mut is_joint = self.bump();
266-
if !self.token.is_op() {
267-
is_joint = NonJoint;
268-
}
265+
let is_joint = self.bump();
269266
Ok((tt, is_joint))
270267
}
271268
}

compiler/rustc_parse/src/parser/mod.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -822,15 +822,15 @@ impl<'a> Parser<'a> {
822822
}
823823

824824
let frame = &self.token_cursor.frame;
825-
looker(&match frame.tree_cursor.look_ahead(dist - 1) {
825+
match frame.tree_cursor.look_ahead(dist - 1) {
826826
Some(tree) => match tree {
827-
TokenTree::Token(token) => token,
827+
TokenTree::Token(token) => looker(token),
828828
TokenTree::Delimited(dspan, delim, _) => {
829-
Token::new(token::OpenDelim(delim), dspan.open)
829+
looker(&Token::new(token::OpenDelim(delim.clone()), dspan.open))
830830
}
831831
},
832-
None => Token::new(token::CloseDelim(frame.delim), frame.span.close),
833-
})
832+
None => looker(&Token::new(token::CloseDelim(frame.delim), frame.span.close)),
833+
}
834834
}
835835

836836
/// Returns whether any of the given keywords are `dist` tokens ahead of the current one.

0 commit comments

Comments
 (0)