Skip to content

Commit

Permalink
Auto merge of #62574 - petrochenkov:dcrate-40000, r=Mark-Simulacrum
Browse files Browse the repository at this point in the history
pretty-print: Do not lose the `$crate` printing flag in `print_tt`

#62393 had this accidental mistake.

Fixes #62562
r? @Mark-Simulacrum
  • Loading branch information
bors committed Jul 11, 2019
2 parents 6907005 + e381065 commit 97b1128
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 30 deletions.
37 changes: 17 additions & 20 deletions src/libsyntax/print/pprust.rs
Original file line number Diff line number Diff line change
Expand Up @@ -187,8 +187,9 @@ pub fn literal_to_string(lit: token::Lit) -> String {
out
}

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

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

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

/* Name components */
token::Ident(s, is_raw) => ident_to_string_ext(s, is_raw, convert_dollar_crate),
token::Ident(s, is_raw) => ident_to_string(s, is_raw, convert_dollar_crate),
token::Lifetime(s) => s.to_string(),

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

/// Print the token precisely, without converting `$crate` into its respective crate name.
pub fn token_to_string(token: &Token) -> String {
token_to_string_ext(token, false)
}
Expand All @@ -305,7 +306,7 @@ crate fn nonterminal_to_string(nt: &Nonterminal) -> String {
token::NtBlock(ref e) => block_to_string(e),
token::NtStmt(ref e) => stmt_to_string(e),
token::NtPat(ref e) => pat_to_string(e),
token::NtIdent(e, is_raw) => ident_to_string(e, is_raw),
token::NtIdent(e, is_raw) => ast_ident_to_string(e, is_raw),
token::NtLifetime(e) => e.to_string(),
token::NtLiteral(ref e) => expr_to_string(e),
token::NtTT(ref tree) => tt_to_string(tree.clone()),
Expand Down Expand Up @@ -341,7 +342,7 @@ pub fn tts_to_string(tts: &[tokenstream::TokenTree]) -> String {
}

pub fn tokens_to_string(tokens: TokenStream) -> String {
to_string(|s| s.print_tts_ext(tokens, false))
to_string(|s| s.print_tts(tokens, false))
}

pub fn stmt_to_string(stmt: &ast::Stmt) -> String {
Expand Down Expand Up @@ -601,7 +602,7 @@ pub trait PrintState<'a>: std::ops::Deref<Target=pp::Printer> + std::ops::DerefM
self.word("::");
}
if segment.ident.name != kw::PathRoot {
self.word(ident_to_string(segment.ident, segment.ident.is_raw_guess()));
self.word(ast_ident_to_string(segment.ident, segment.ident.is_raw_guess()));
}
}
}
Expand Down Expand Up @@ -629,7 +630,7 @@ pub trait PrintState<'a>: std::ops::Deref<Target=pp::Printer> + std::ops::DerefM
} else {
self.print_attribute_path(&attr.path);
self.space();
self.print_tts(attr.tokens.clone());
self.print_tts(attr.tokens.clone(), true);
}
self.word("]");
}
Expand Down Expand Up @@ -689,18 +690,14 @@ pub trait PrintState<'a>: std::ops::Deref<Target=pp::Printer> + std::ops::DerefM
TokenTree::Delimited(_, delim, tts) => {
self.word(token_kind_to_string(&token::OpenDelim(delim)));
self.space();
self.print_tts(tts);
self.print_tts(tts, convert_dollar_crate);
self.space();
self.word(token_kind_to_string(&token::CloseDelim(delim)))
},
}
}

fn print_tts(&mut self, tts: tokenstream::TokenStream) {
self.print_tts_ext(tts, true)
}

fn print_tts_ext(&mut self, tts: tokenstream::TokenStream, convert_dollar_crate: bool) {
fn print_tts(&mut self, tts: tokenstream::TokenStream, convert_dollar_crate: bool) {
self.ibox(0);
for (i, tt) in tts.into_trees().enumerate() {
if i != 0 {
Expand Down Expand Up @@ -1247,7 +1244,7 @@ impl<'a> State<'a> {
self.print_ident(item.ident);
self.cbox(INDENT_UNIT);
self.popen();
self.print_tts(mac.node.stream());
self.print_tts(mac.node.stream(), true);
self.pclose();
self.s.word(";");
self.end();
Expand All @@ -1258,7 +1255,7 @@ impl<'a> State<'a> {
self.print_ident(item.ident);
self.cbox(INDENT_UNIT);
self.popen();
self.print_tts(tts.stream());
self.print_tts(tts.stream(), true);
self.pclose();
self.s.word(";");
self.end();
Expand Down Expand Up @@ -1659,7 +1656,7 @@ impl<'a> State<'a> {
self.bopen();
}
}
self.print_tts(m.node.stream());
self.print_tts(m.node.stream(), true);
match m.node.delim {
MacDelimiter::Parenthesis => self.pclose(),
MacDelimiter::Bracket => self.s.word("]"),
Expand Down Expand Up @@ -2209,7 +2206,7 @@ impl<'a> State<'a> {
}

crate fn print_ident(&mut self, ident: ast::Ident) {
self.s.word(ident_to_string(ident, ident.is_raw_guess()));
self.s.word(ast_ident_to_string(ident, ident.is_raw_guess()));
self.ann.post(self, AnnNode::Ident(&ident))
}

Expand Down
10 changes: 10 additions & 0 deletions src/test/ui/macros/auxiliary/dollar-crate-nested-encoding.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
pub type S = u8;

macro_rules! generate_exported { () => {
#[macro_export]
macro_rules! exported {
() => ($crate::S)
}
}}

generate_exported!();
8 changes: 8 additions & 0 deletions src/test/ui/macros/dollar-crate-nested-encoding.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// check-pass
// aux-build:dollar-crate-nested-encoding.rs

extern crate dollar_crate_nested_encoding;

type A = dollar_crate_nested_encoding::exported!();

fn main() {}
4 changes: 2 additions & 2 deletions src/test/ui/proc-macro/dollar-crate-issue-57089.stdout
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
PRINT-BANG INPUT (DISPLAY): struct M ( crate :: S ) ;
PRINT-BANG INPUT (DISPLAY): struct M ( $crate :: S ) ;
PRINT-BANG INPUT (DEBUG): TokenStream [
Ident {
ident: "struct",
Expand Down Expand Up @@ -39,7 +39,7 @@ PRINT-BANG INPUT (DEBUG): TokenStream [
},
]
PRINT-ATTR INPUT (DISPLAY): struct A(crate::S);
PRINT-ATTR RE-COLLECTED (DISPLAY): struct A ( crate :: S ) ;
PRINT-ATTR RE-COLLECTED (DISPLAY): struct A ( $crate :: S ) ;
PRINT-ATTR INPUT (DEBUG): TokenStream [
Ident {
ident: "struct",
Expand Down
4 changes: 2 additions & 2 deletions src/test/ui/proc-macro/dollar-crate-issue-62325.stdout
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
PRINT-ATTR INPUT (DISPLAY): struct A(identity!(crate :: S));
PRINT-ATTR RE-COLLECTED (DISPLAY): struct A ( identity ! ( crate :: S ) ) ;
PRINT-ATTR RE-COLLECTED (DISPLAY): struct A ( identity ! ( $crate :: S ) ) ;
PRINT-ATTR INPUT (DEBUG): TokenStream [
Ident {
ident: "struct",
Expand Down Expand Up @@ -55,7 +55,7 @@ PRINT-ATTR INPUT (DEBUG): TokenStream [
},
]
PRINT-ATTR INPUT (DISPLAY): struct B(identity!(::dollar_crate_external :: S));
PRINT-ATTR RE-COLLECTED (DISPLAY): struct B ( identity ! ( ::dollar_crate_external :: S ) ) ;
PRINT-ATTR RE-COLLECTED (DISPLAY): struct B ( identity ! ( $crate :: S ) ) ;
PRINT-ATTR INPUT (DEBUG): TokenStream [
Ident {
ident: "struct",
Expand Down
12 changes: 6 additions & 6 deletions src/test/ui/proc-macro/dollar-crate.stdout
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
PRINT-BANG INPUT (DISPLAY): struct M ( crate :: S ) ;
PRINT-BANG INPUT (DISPLAY): struct M ( $crate :: S ) ;
PRINT-BANG INPUT (DEBUG): TokenStream [
Ident {
ident: "struct",
Expand Down Expand Up @@ -39,7 +39,7 @@ PRINT-BANG INPUT (DEBUG): TokenStream [
},
]
PRINT-ATTR INPUT (DISPLAY): struct A(crate::S);
PRINT-ATTR RE-COLLECTED (DISPLAY): struct A ( crate :: S ) ;
PRINT-ATTR RE-COLLECTED (DISPLAY): struct A ( $crate :: S ) ;
PRINT-ATTR INPUT (DEBUG): TokenStream [
Ident {
ident: "struct",
Expand Down Expand Up @@ -80,7 +80,7 @@ PRINT-ATTR INPUT (DEBUG): TokenStream [
},
]
PRINT-DERIVE INPUT (DISPLAY): struct D(crate::S);
PRINT-DERIVE RE-COLLECTED (DISPLAY): struct D ( crate :: S ) ;
PRINT-DERIVE RE-COLLECTED (DISPLAY): struct D ( $crate :: S ) ;
PRINT-DERIVE INPUT (DEBUG): TokenStream [
Ident {
ident: "struct",
Expand Down Expand Up @@ -120,7 +120,7 @@ PRINT-DERIVE INPUT (DEBUG): TokenStream [
span: #2 bytes(LO..HI),
},
]
PRINT-BANG INPUT (DISPLAY): struct M ( ::dollar_crate_external :: S ) ;
PRINT-BANG INPUT (DISPLAY): struct M ( $crate :: S ) ;
PRINT-BANG INPUT (DEBUG): TokenStream [
Ident {
ident: "struct",
Expand Down Expand Up @@ -161,7 +161,7 @@ PRINT-BANG INPUT (DEBUG): TokenStream [
},
]
PRINT-ATTR INPUT (DISPLAY): struct A(::dollar_crate_external::S);
PRINT-ATTR RE-COLLECTED (DISPLAY): struct A ( ::dollar_crate_external :: S ) ;
PRINT-ATTR RE-COLLECTED (DISPLAY): struct A ( $crate :: S ) ;
PRINT-ATTR INPUT (DEBUG): TokenStream [
Ident {
ident: "struct",
Expand Down Expand Up @@ -202,7 +202,7 @@ PRINT-ATTR INPUT (DEBUG): TokenStream [
},
]
PRINT-DERIVE INPUT (DISPLAY): struct D(::dollar_crate_external::S);
PRINT-DERIVE RE-COLLECTED (DISPLAY): struct D ( ::dollar_crate_external :: S ) ;
PRINT-DERIVE RE-COLLECTED (DISPLAY): struct D ( $crate :: S ) ;
PRINT-DERIVE INPUT (DEBUG): TokenStream [
Ident {
ident: "struct",
Expand Down

0 comments on commit 97b1128

Please sign in to comment.