Skip to content

Commit 92b0f52

Browse files
committed
Auto merge of #62667 - petrochenkov:printattr2, r=Mark-Simulacrum
pprust: Improve pretty-printing of delimited token groups The commit "Do not convert attributes into `MetaItem`s for printing" fixes #62628. Other commits fix regressions from abandoning `MetaItem`s, and make formatting for attributes, macro calls, macro definitions and other delimited token groups better and more consistent. r? @Mark-Simulacrum
2 parents 5480b47 + 0cdd18d commit 92b0f52

26 files changed

+350
-254
lines changed

src/librustc/hir/print.rs

+10-10
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use syntax::source_map::{SourceMap, Spanned};
44
use syntax::parse::ParseSess;
55
use syntax::print::pp::{self, Breaks};
66
use syntax::print::pp::Breaks::{Consistent, Inconsistent};
7-
use syntax::print::pprust::{Comments, PrintState};
7+
use syntax::print::pprust::{self, Comments, PrintState};
88
use syntax::symbol::kw;
99
use syntax::util::parser::{self, AssocOp, Fixity};
1010
use syntax_pos::{self, BytePos, FileName};
@@ -90,6 +90,15 @@ impl<'a> PrintState<'a> for State<'a> {
9090
fn comments(&mut self) -> &mut Option<Comments<'a>> {
9191
&mut self.comments
9292
}
93+
94+
fn print_ident(&mut self, ident: ast::Ident) {
95+
self.s.word(pprust::ast_ident_to_string(ident, ident.is_raw_guess()));
96+
self.ann.post(self, AnnNode::Name(&ident.name))
97+
}
98+
99+
fn print_generic_args(&mut self, args: &ast::GenericArgs, _colons_before_params: bool) {
100+
span_bug!(args.span(), "AST generic args printed by HIR pretty-printer");
101+
}
93102
}
94103

95104
pub const INDENT_UNIT: usize = 4;
@@ -1442,15 +1451,6 @@ impl<'a> State<'a> {
14421451
self.s.word(i.to_string())
14431452
}
14441453

1445-
pub fn print_ident(&mut self, ident: ast::Ident) {
1446-
if ident.is_raw_guess() {
1447-
self.s.word(format!("r#{}", ident.name));
1448-
} else {
1449-
self.s.word(ident.as_str().to_string());
1450-
}
1451-
self.ann.post(self, AnnNode::Name(&ident.name))
1452-
}
1453-
14541454
pub fn print_name(&mut self, name: ast::Name) {
14551455
self.print_ident(ast::Ident::with_empty_ctxt(name))
14561456
}

src/libsyntax/ast.rs

+11-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ pub use crate::symbol::{Ident, Symbol as Name};
66
pub use crate::util::parser::ExprPrecedence;
77

88
use crate::ext::hygiene::{Mark, SyntaxContext};
9-
use crate::parse::token;
9+
use crate::parse::token::{self, DelimToken};
1010
use crate::print::pprust;
1111
use crate::ptr::P;
1212
use crate::source_map::{dummy_spanned, respan, Spanned};
@@ -1298,6 +1298,16 @@ impl Mac_ {
12981298
}
12991299
}
13001300

1301+
impl MacDelimiter {
1302+
crate fn to_token(self) -> DelimToken {
1303+
match self {
1304+
MacDelimiter::Parenthesis => DelimToken::Paren,
1305+
MacDelimiter::Bracket => DelimToken::Bracket,
1306+
MacDelimiter::Brace => DelimToken::Brace,
1307+
}
1308+
}
1309+
}
1310+
13011311
#[derive(Clone, RustcEncodable, RustcDecodable, Debug)]
13021312
pub struct MacroDef {
13031313
pub tokens: TokenStream,

src/libsyntax/mut_visit.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1328,7 +1328,7 @@ mod tests {
13281328
matches_codepattern,
13291329
"matches_codepattern",
13301330
pprust::to_string(|s| fake_print_crate(s, &krate)),
1331-
"macro_rules! zz((zz$zz:zz$(zz $zz:zz)zz+=>(zz$(zz$zz$zz)+)));".to_string());
1331+
"macro_rules! zz{(zz$zz:zz$(zz $zz:zz)zz+=>(zz$(zz$zz$zz)+))}".to_string());
13321332
})
13331333
}
13341334
}

0 commit comments

Comments
 (0)