Skip to content

Commit 19149d1

Browse files
committed
Auto merge of rust-lang#116649 - nnethercote:improve-print_tts-precursors, r=petrochenkov
Token cleanups Some precursors to rust-lang#114571 that are worth merging even if the main part of rust-lang#114571 doesn't get merged. r? `@petrochenkov`
2 parents 3ff244b + 66c2b77 commit 19149d1

File tree

8 files changed

+433
-405
lines changed

8 files changed

+433
-405
lines changed

compiler/rustc_ast/src/attr/mod.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -197,10 +197,10 @@ impl Attribute {
197197
.unwrap_or_else(|| panic!("attribute is missing tokens: {self:?}"))
198198
.to_attr_token_stream()
199199
.to_tokenstream(),
200-
&AttrKind::DocComment(comment_kind, data) => TokenStream::new(vec![TokenTree::Token(
201-
Token::new(token::DocComment(comment_kind, self.style, data), self.span),
202-
Spacing::Alone,
203-
)]),
200+
&AttrKind::DocComment(comment_kind, data) => TokenStream::token_alone(
201+
token::DocComment(comment_kind, self.style, data),
202+
self.span,
203+
),
204204
}
205205
}
206206
}

compiler/rustc_ast/src/token.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -404,7 +404,7 @@ impl Token {
404404
[DotDot, DotDotDot, DotDotEq].contains(&self.kind)
405405
}
406406

407-
pub fn is_op(&self) -> bool {
407+
pub fn is_punct(&self) -> bool {
408408
match self.kind {
409409
Eq | Lt | Le | EqEq | Ne | Ge | Gt | AndAnd | OrOr | Not | Tilde | BinOp(_)
410410
| BinOpEq(_) | At | Dot | DotDot | DotDotDot | DotDotEq | Comma | Semi | Colon

compiler/rustc_ast_pretty/src/pprust/state.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ pub fn print_crate<'a>(
148148

149149
/// This makes printed token streams look slightly nicer,
150150
/// and also addresses some specific regressions described in #63896 and #73345.
151-
fn tt_prepend_space(tt: &TokenTree, prev: &TokenTree) -> bool {
151+
fn space_between(prev: &TokenTree, curr: &TokenTree) -> bool {
152152
if let TokenTree::Token(token, _) = prev {
153153
// No space after these tokens, e.g. `x.y`, `$e`
154154
// (The carets point to `prev`.) ^ ^
@@ -159,9 +159,9 @@ fn tt_prepend_space(tt: &TokenTree, prev: &TokenTree) -> bool {
159159
return comment_kind != CommentKind::Line;
160160
}
161161
}
162-
match tt {
162+
match curr {
163163
// No space before these tokens, e.g. `foo,`, `println!`, `x.y`
164-
// (The carets point to `token`.) ^ ^ ^
164+
// (The carets point to `curr`.) ^ ^ ^
165165
//
166166
// FIXME: having `Not` here works well for macro invocations like
167167
// `println!()`, but is bad when `!` means "logical not" or "the never
@@ -575,7 +575,7 @@ pub trait PrintState<'a>: std::ops::Deref<Target = pp::Printer> + std::ops::Dere
575575
while let Some(tt) = iter.next() {
576576
self.print_tt(tt, convert_dollar_crate);
577577
if let Some(next) = iter.peek() {
578-
if tt_prepend_space(next, tt) {
578+
if space_between(tt, next) {
579579
self.space();
580580
}
581581
}

compiler/rustc_parse/src/lexer/tokentrees.rs

+7-9
Original file line numberDiff line numberDiff line change
@@ -55,16 +55,14 @@ impl<'a> TokenTreesReader<'a> {
5555
let (this_spacing, next_tok) = loop {
5656
let (next_tok, is_next_tok_preceded_by_whitespace) =
5757
self.string_reader.next_token();
58-
if !is_next_tok_preceded_by_whitespace {
59-
if let Some(glued) = self.token.glue(&next_tok) {
60-
self.token = glued;
61-
} else {
62-
let this_spacing =
63-
if next_tok.is_op() { Spacing::Joint } else { Spacing::Alone };
64-
break (this_spacing, next_tok);
65-
}
66-
} else {
58+
if is_next_tok_preceded_by_whitespace {
6759
break (Spacing::Alone, next_tok);
60+
} else if let Some(glued) = self.token.glue(&next_tok) {
61+
self.token = glued;
62+
} else {
63+
let this_spacing =
64+
if next_tok.is_punct() { Spacing::Joint } else { Spacing::Alone };
65+
break (this_spacing, next_tok);
6866
}
6967
};
7068
let this_tok = std::mem::replace(&mut self.token, next_tok);

compiler/rustc_parse/src/parser/expr.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1592,7 +1592,7 @@ impl<'a> Parser<'a> {
15921592
} else if !ate_colon
15931593
&& self.may_recover()
15941594
&& (matches!(self.token.kind, token::CloseDelim(_) | token::Comma)
1595-
|| self.token.is_op())
1595+
|| self.token.is_punct())
15961596
{
15971597
let (lit, _) =
15981598
self.recover_unclosed_char(label_.ident, Parser::mk_token_lit_char, |self_| {

tests/ui/proc-macro/issue-75930-derive-cfg.rs

+30
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,36 @@ extern crate std;
1616
#[macro_use]
1717
extern crate test_macros;
1818

19+
// Note: the expected output contains this sequence:
20+
// ```
21+
// Punct {
22+
// ch: '<',
23+
// spacing: Joint,
24+
// span: $DIR/issue-75930-derive-cfg.rs:25:11: 25:12 (#0),
25+
// },
26+
// Ident {
27+
// ident: "B",
28+
// span: $DIR/issue-75930-derive-cfg.rs:25:29: 25:30 (#0),
29+
// },
30+
// ```
31+
// It's surprising to see a `Joint` token tree followed by an `Ident` token
32+
// tree, because `Joint` is supposed to only be used if the following token is
33+
// `Punct`.
34+
//
35+
// It is because of this code from below:
36+
// ```
37+
// struct Foo<#[cfg(FALSE)] A, B>
38+
// ```
39+
// When the token stream is formed during parsing, `<` is followed immediately
40+
// by `#`, which is punctuation, so it is marked `Joint`. But before being
41+
// passed to the proc macro it is rewritten to this:
42+
// ```
43+
// struct Foo<B>
44+
// ```
45+
// But the `Joint` marker on the `<` is not updated. Perhaps it should be
46+
// corrected before being passed to the proc macro? But a prior attempt to do
47+
// that kind of correction caused the problem seen in #76399, so maybe not.
48+
1949
#[print_helper(a)] //~ WARN derive helper attribute is used before it is introduced
2050
//~| WARN this was previously accepted
2151
#[cfg_attr(not(FALSE), allow(dead_code))]

tests/ui/proc-macro/issue-75930-derive-cfg.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
warning: derive helper attribute is used before it is introduced
2-
--> $DIR/issue-75930-derive-cfg.rs:19:3
2+
--> $DIR/issue-75930-derive-cfg.rs:49:3
33
|
44
LL | #[print_helper(a)]
55
| ^^^^^^^^^^^^
@@ -12,7 +12,7 @@ LL | #[derive(Print)]
1212
= note: `#[warn(legacy_derive_helpers)]` on by default
1313

1414
warning: derive helper attribute is used before it is introduced
15-
--> $DIR/issue-75930-derive-cfg.rs:19:3
15+
--> $DIR/issue-75930-derive-cfg.rs:49:3
1616
|
1717
LL | #[print_helper(a)]
1818
| ^^^^^^^^^^^^

0 commit comments

Comments
 (0)