Skip to content

Commit 97b1128

Browse files
committed
Auto merge of #62574 - petrochenkov:dcrate-40000, r=Mark-Simulacrum
pretty-print: Do not lose the `$crate` printing flag in `print_tt` #62393 had this accidental mistake. Fixes #62562 r? @Mark-Simulacrum
2 parents 6907005 + e381065 commit 97b1128

File tree

6 files changed

+45
-30
lines changed

6 files changed

+45
-30
lines changed

src/libsyntax/print/pprust.rs

+17-20
Original file line numberDiff line numberDiff line change
@@ -187,8 +187,9 @@ pub fn literal_to_string(lit: token::Lit) -> String {
187187
out
188188
}
189189

190-
fn ident_to_string(ident: ast::Ident, is_raw: bool) -> String {
191-
ident_to_string_ext(ident.name, is_raw, Some(ident.span))
190+
/// Print an ident from AST, `$crate` is converted into its respective crate name.
191+
fn ast_ident_to_string(ident: ast::Ident, is_raw: bool) -> String {
192+
ident_to_string(ident.name, is_raw, Some(ident.span))
192193
}
193194

194195
// AST pretty-printer is used as a fallback for turning AST structures into token streams for
@@ -202,9 +203,7 @@ fn ident_to_string(ident: ast::Ident, is_raw: bool) -> String {
202203
// but not otherwise. Pretty-printing is the only way for proc macros to discover token contents,
203204
// so we should not perform this lossy conversion if the top level call to the pretty-printer was
204205
// done for a token stream or a single token.
205-
fn ident_to_string_ext(
206-
name: ast::Name, is_raw: bool, convert_dollar_crate: Option<Span>
207-
) -> String {
206+
fn ident_to_string(name: ast::Name, is_raw: bool, convert_dollar_crate: Option<Span>) -> String {
208207
if is_raw {
209208
format!("r#{}", name)
210209
} else {
@@ -222,6 +221,7 @@ fn ident_to_string_ext(
222221
}
223222
}
224223

224+
/// Print the token kind precisely, without converting `$crate` into its respective crate name.
225225
pub fn token_kind_to_string(tok: &TokenKind) -> String {
226226
token_kind_to_string_ext(tok, None)
227227
}
@@ -272,7 +272,7 @@ fn token_kind_to_string_ext(tok: &TokenKind, convert_dollar_crate: Option<Span>)
272272
token::Literal(lit) => literal_to_string(lit),
273273

274274
/* Name components */
275-
token::Ident(s, is_raw) => ident_to_string_ext(s, is_raw, convert_dollar_crate),
275+
token::Ident(s, is_raw) => ident_to_string(s, is_raw, convert_dollar_crate),
276276
token::Lifetime(s) => s.to_string(),
277277

278278
/* Other */
@@ -286,6 +286,7 @@ fn token_kind_to_string_ext(tok: &TokenKind, convert_dollar_crate: Option<Span>)
286286
}
287287
}
288288

289+
/// Print the token precisely, without converting `$crate` into its respective crate name.
289290
pub fn token_to_string(token: &Token) -> String {
290291
token_to_string_ext(token, false)
291292
}
@@ -305,7 +306,7 @@ crate fn nonterminal_to_string(nt: &Nonterminal) -> String {
305306
token::NtBlock(ref e) => block_to_string(e),
306307
token::NtStmt(ref e) => stmt_to_string(e),
307308
token::NtPat(ref e) => pat_to_string(e),
308-
token::NtIdent(e, is_raw) => ident_to_string(e, is_raw),
309+
token::NtIdent(e, is_raw) => ast_ident_to_string(e, is_raw),
309310
token::NtLifetime(e) => e.to_string(),
310311
token::NtLiteral(ref e) => expr_to_string(e),
311312
token::NtTT(ref tree) => tt_to_string(tree.clone()),
@@ -341,7 +342,7 @@ pub fn tts_to_string(tts: &[tokenstream::TokenTree]) -> String {
341342
}
342343

343344
pub fn tokens_to_string(tokens: TokenStream) -> String {
344-
to_string(|s| s.print_tts_ext(tokens, false))
345+
to_string(|s| s.print_tts(tokens, false))
345346
}
346347

347348
pub fn stmt_to_string(stmt: &ast::Stmt) -> String {
@@ -601,7 +602,7 @@ pub trait PrintState<'a>: std::ops::Deref<Target=pp::Printer> + std::ops::DerefM
601602
self.word("::");
602603
}
603604
if segment.ident.name != kw::PathRoot {
604-
self.word(ident_to_string(segment.ident, segment.ident.is_raw_guess()));
605+
self.word(ast_ident_to_string(segment.ident, segment.ident.is_raw_guess()));
605606
}
606607
}
607608
}
@@ -629,7 +630,7 @@ pub trait PrintState<'a>: std::ops::Deref<Target=pp::Printer> + std::ops::DerefM
629630
} else {
630631
self.print_attribute_path(&attr.path);
631632
self.space();
632-
self.print_tts(attr.tokens.clone());
633+
self.print_tts(attr.tokens.clone(), true);
633634
}
634635
self.word("]");
635636
}
@@ -689,18 +690,14 @@ pub trait PrintState<'a>: std::ops::Deref<Target=pp::Printer> + std::ops::DerefM
689690
TokenTree::Delimited(_, delim, tts) => {
690691
self.word(token_kind_to_string(&token::OpenDelim(delim)));
691692
self.space();
692-
self.print_tts(tts);
693+
self.print_tts(tts, convert_dollar_crate);
693694
self.space();
694695
self.word(token_kind_to_string(&token::CloseDelim(delim)))
695696
},
696697
}
697698
}
698699

699-
fn print_tts(&mut self, tts: tokenstream::TokenStream) {
700-
self.print_tts_ext(tts, true)
701-
}
702-
703-
fn print_tts_ext(&mut self, tts: tokenstream::TokenStream, convert_dollar_crate: bool) {
700+
fn print_tts(&mut self, tts: tokenstream::TokenStream, convert_dollar_crate: bool) {
704701
self.ibox(0);
705702
for (i, tt) in tts.into_trees().enumerate() {
706703
if i != 0 {
@@ -1247,7 +1244,7 @@ impl<'a> State<'a> {
12471244
self.print_ident(item.ident);
12481245
self.cbox(INDENT_UNIT);
12491246
self.popen();
1250-
self.print_tts(mac.node.stream());
1247+
self.print_tts(mac.node.stream(), true);
12511248
self.pclose();
12521249
self.s.word(";");
12531250
self.end();
@@ -1258,7 +1255,7 @@ impl<'a> State<'a> {
12581255
self.print_ident(item.ident);
12591256
self.cbox(INDENT_UNIT);
12601257
self.popen();
1261-
self.print_tts(tts.stream());
1258+
self.print_tts(tts.stream(), true);
12621259
self.pclose();
12631260
self.s.word(";");
12641261
self.end();
@@ -1659,7 +1656,7 @@ impl<'a> State<'a> {
16591656
self.bopen();
16601657
}
16611658
}
1662-
self.print_tts(m.node.stream());
1659+
self.print_tts(m.node.stream(), true);
16631660
match m.node.delim {
16641661
MacDelimiter::Parenthesis => self.pclose(),
16651662
MacDelimiter::Bracket => self.s.word("]"),
@@ -2209,7 +2206,7 @@ impl<'a> State<'a> {
22092206
}
22102207

22112208
crate fn print_ident(&mut self, ident: ast::Ident) {
2212-
self.s.word(ident_to_string(ident, ident.is_raw_guess()));
2209+
self.s.word(ast_ident_to_string(ident, ident.is_raw_guess()));
22132210
self.ann.post(self, AnnNode::Ident(&ident))
22142211
}
22152212

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
pub type S = u8;
2+
3+
macro_rules! generate_exported { () => {
4+
#[macro_export]
5+
macro_rules! exported {
6+
() => ($crate::S)
7+
}
8+
}}
9+
10+
generate_exported!();
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// check-pass
2+
// aux-build:dollar-crate-nested-encoding.rs
3+
4+
extern crate dollar_crate_nested_encoding;
5+
6+
type A = dollar_crate_nested_encoding::exported!();
7+
8+
fn main() {}

src/test/ui/proc-macro/dollar-crate-issue-57089.stdout

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
PRINT-BANG INPUT (DISPLAY): struct M ( crate :: S ) ;
1+
PRINT-BANG INPUT (DISPLAY): struct M ( $crate :: S ) ;
22
PRINT-BANG INPUT (DEBUG): TokenStream [
33
Ident {
44
ident: "struct",
@@ -39,7 +39,7 @@ PRINT-BANG INPUT (DEBUG): TokenStream [
3939
},
4040
]
4141
PRINT-ATTR INPUT (DISPLAY): struct A(crate::S);
42-
PRINT-ATTR RE-COLLECTED (DISPLAY): struct A ( crate :: S ) ;
42+
PRINT-ATTR RE-COLLECTED (DISPLAY): struct A ( $crate :: S ) ;
4343
PRINT-ATTR INPUT (DEBUG): TokenStream [
4444
Ident {
4545
ident: "struct",

src/test/ui/proc-macro/dollar-crate-issue-62325.stdout

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
PRINT-ATTR INPUT (DISPLAY): struct A(identity!(crate :: S));
2-
PRINT-ATTR RE-COLLECTED (DISPLAY): struct A ( identity ! ( crate :: S ) ) ;
2+
PRINT-ATTR RE-COLLECTED (DISPLAY): struct A ( identity ! ( $crate :: S ) ) ;
33
PRINT-ATTR INPUT (DEBUG): TokenStream [
44
Ident {
55
ident: "struct",
@@ -55,7 +55,7 @@ PRINT-ATTR INPUT (DEBUG): TokenStream [
5555
},
5656
]
5757
PRINT-ATTR INPUT (DISPLAY): struct B(identity!(::dollar_crate_external :: S));
58-
PRINT-ATTR RE-COLLECTED (DISPLAY): struct B ( identity ! ( ::dollar_crate_external :: S ) ) ;
58+
PRINT-ATTR RE-COLLECTED (DISPLAY): struct B ( identity ! ( $crate :: S ) ) ;
5959
PRINT-ATTR INPUT (DEBUG): TokenStream [
6060
Ident {
6161
ident: "struct",

src/test/ui/proc-macro/dollar-crate.stdout

+6-6
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
PRINT-BANG INPUT (DISPLAY): struct M ( crate :: S ) ;
1+
PRINT-BANG INPUT (DISPLAY): struct M ( $crate :: S ) ;
22
PRINT-BANG INPUT (DEBUG): TokenStream [
33
Ident {
44
ident: "struct",
@@ -39,7 +39,7 @@ PRINT-BANG INPUT (DEBUG): TokenStream [
3939
},
4040
]
4141
PRINT-ATTR INPUT (DISPLAY): struct A(crate::S);
42-
PRINT-ATTR RE-COLLECTED (DISPLAY): struct A ( crate :: S ) ;
42+
PRINT-ATTR RE-COLLECTED (DISPLAY): struct A ( $crate :: S ) ;
4343
PRINT-ATTR INPUT (DEBUG): TokenStream [
4444
Ident {
4545
ident: "struct",
@@ -80,7 +80,7 @@ PRINT-ATTR INPUT (DEBUG): TokenStream [
8080
},
8181
]
8282
PRINT-DERIVE INPUT (DISPLAY): struct D(crate::S);
83-
PRINT-DERIVE RE-COLLECTED (DISPLAY): struct D ( crate :: S ) ;
83+
PRINT-DERIVE RE-COLLECTED (DISPLAY): struct D ( $crate :: S ) ;
8484
PRINT-DERIVE INPUT (DEBUG): TokenStream [
8585
Ident {
8686
ident: "struct",
@@ -120,7 +120,7 @@ PRINT-DERIVE INPUT (DEBUG): TokenStream [
120120
span: #2 bytes(LO..HI),
121121
},
122122
]
123-
PRINT-BANG INPUT (DISPLAY): struct M ( ::dollar_crate_external :: S ) ;
123+
PRINT-BANG INPUT (DISPLAY): struct M ( $crate :: S ) ;
124124
PRINT-BANG INPUT (DEBUG): TokenStream [
125125
Ident {
126126
ident: "struct",
@@ -161,7 +161,7 @@ PRINT-BANG INPUT (DEBUG): TokenStream [
161161
},
162162
]
163163
PRINT-ATTR INPUT (DISPLAY): struct A(::dollar_crate_external::S);
164-
PRINT-ATTR RE-COLLECTED (DISPLAY): struct A ( ::dollar_crate_external :: S ) ;
164+
PRINT-ATTR RE-COLLECTED (DISPLAY): struct A ( $crate :: S ) ;
165165
PRINT-ATTR INPUT (DEBUG): TokenStream [
166166
Ident {
167167
ident: "struct",
@@ -202,7 +202,7 @@ PRINT-ATTR INPUT (DEBUG): TokenStream [
202202
},
203203
]
204204
PRINT-DERIVE INPUT (DISPLAY): struct D(::dollar_crate_external::S);
205-
PRINT-DERIVE RE-COLLECTED (DISPLAY): struct D ( ::dollar_crate_external :: S ) ;
205+
PRINT-DERIVE RE-COLLECTED (DISPLAY): struct D ( $crate :: S ) ;
206206
PRINT-DERIVE INPUT (DEBUG): TokenStream [
207207
Ident {
208208
ident: "struct",

0 commit comments

Comments
 (0)