Skip to content

Commit 4d513cb

Browse files
committed
Add some comments.
1 parent a1b6d46 commit 4d513cb

File tree

3 files changed

+46
-11
lines changed

3 files changed

+46
-11
lines changed

compiler/rustc_ast_pretty/src/pprust/state.rs

+29-11
Original file line numberDiff line numberDiff line change
@@ -681,22 +681,40 @@ pub trait PrintState<'a>: std::ops::Deref<Target = pp::Printer> + std::ops::Dere
681681
}
682682
}
683683

684+
// The easiest way to implement token stream pretty printing would be to
685+
// print each token followed by a single space. But that would produce ugly
686+
// output, so we go to some effort to do better.
687+
//
688+
// First, we track whether each token that appears in source code is
689+
// followed by a space, with `Spacing`, and reproduce that in the output.
690+
// This works well in a lot of cases. E.g. `stringify!(x + y)` produces
691+
// "x + y" and `stringify!(x+y)` produces "x+y".
692+
//
693+
// But this doesn't work for code produced by proc macros (which have no
694+
// original source text representation) nor for code produced by decl
695+
// macros (which are tricky because the whitespace after tokens appearing
696+
// in macro rules isn't always what you want in the produced output). For
697+
// these we mostly use `Spacing::Alone`, which is the conservative choice.
698+
//
699+
// So we have a backup mechanism for when `Spacing::Alone` occurs between a
700+
// pair of tokens: we check if that pair of tokens can obviously go
701+
// together without a space between them. E.g. token `x` followed by token
702+
// `,` is better printed as `x,` than `x ,`. (Even if the original source
703+
// code was `x ,`.)
704+
//
705+
// Finally, we must be careful about changing the output. Token pretty
706+
// printing is used by `stringify!` and `impl Display for
707+
// proc_macro::TokenStream`, and some programs rely on the output having a
708+
// particular form, even though they shouldn't. In particular, some proc
709+
// macros do `format!({stream})` on a token stream and then "parse" the
710+
// output with simple string matching that can't handle whitespace changes.
711+
// E.g. we have seen cases where a proc macro can handle `a :: b` but not
712+
// `a::b`. See #117433 for some examples.
684713
fn print_tts(&mut self, tts: &TokenStream, convert_dollar_crate: bool) {
685714
let mut iter = tts.trees().peekable();
686715
while let Some(tt) = iter.next() {
687716
let spacing = self.print_tt(tt, convert_dollar_crate);
688717
if let Some(next) = iter.peek() {
689-
// Should we print a space after `tt`? There are two guiding
690-
// factors.
691-
// - `spacing` is the more important and accurate one. Most
692-
// tokens have good spacing information, and
693-
// `Joint`/`JointHidden` get used a lot.
694-
// - `space_between` is the backup. Code produced by proc
695-
// macros has worse spacing information, with no
696-
// `JointHidden` usage and too much `Alone` usage, which
697-
// would result in over-spaced output such as
698-
// `( x () , y . z )`. `space_between` avoids some of the
699-
// excess whitespace.
700718
if spacing == Spacing::Alone && space_between(tt, next) {
701719
self.space();
702720
}

compiler/rustc_expand/src/mbe.rs

+2
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,8 @@ pub(crate) enum KleeneOp {
6868
/// `MetaVarExpr` are "first-class" token trees. Useful for parsing macros.
6969
#[derive(Debug, PartialEq, Encodable, Decodable)]
7070
enum TokenTree {
71+
/// A token. Unlike `tokenstream::TokenTree::Token` this lacks a `Spacing`.
72+
/// See the comments about `Spacing` in the `transcribe` function.
7173
Token(Token),
7274
/// A delimited sequence, e.g. `($e:expr)` (RHS) or `{ $e }` (LHS).
7375
Delimited(DelimSpan, DelimSpacing, Delimited),

compiler/rustc_expand/src/mbe/transcribe.rs

+15
Original file line numberDiff line numberDiff line change
@@ -253,8 +253,23 @@ pub(super) fn transcribe<'a>(
253253
mbe::TokenTree::MetaVar(mut sp, mut original_ident) => {
254254
// Find the matched nonterminal from the macro invocation, and use it to replace
255255
// the meta-var.
256+
//
257+
// We use `Spacing::Alone` everywhere here, because that's the conservative choice
258+
// and spacing of declarative macros is tricky. E.g. in this macro:
259+
// ```
260+
// macro_rules! idents {
261+
// ($($a:ident,)*) => { stringify!($($a)*) }
262+
// }
263+
// ```
264+
// `$a` has no whitespace after it and will be marked `JointHidden`. If you then
265+
// call `idents!(x,y,z,)`, each of `x`, `y`, and `z` will be marked as `Joint`. So
266+
// if you choose to use `$x`'s spacing or the identifier's spacing, you'll end up
267+
// producing "xyz", which is bad because it effectively merges tokens.
268+
// `Spacing::Alone` is the safer option. Fortunately, `space_between` will avoid
269+
// some of the unnecessary whitespace.
256270
let ident = MacroRulesNormalizedIdent::new(original_ident);
257271
if let Some(cur_matched) = lookup_cur_matched(ident, interp, &repeats) {
272+
// njn: explain the use of alone here
258273
let tt = match cur_matched {
259274
MatchedSingle(ParseNtResult::Tt(tt)) => {
260275
// `tt`s are emitted into the output stream directly as "raw tokens",

0 commit comments

Comments
 (0)