Skip to content

Commit

Permalink
Auto merge of #62667 - petrochenkov:printattr2, r=Mark-Simulacrum
Browse files Browse the repository at this point in the history
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
  • Loading branch information
bors committed Jul 15, 2019
2 parents 5480b47 + 0cdd18d commit 92b0f52
Show file tree
Hide file tree
Showing 26 changed files with 350 additions and 254 deletions.
20 changes: 10 additions & 10 deletions src/librustc/hir/print.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use syntax::source_map::{SourceMap, Spanned};
use syntax::parse::ParseSess;
use syntax::print::pp::{self, Breaks};
use syntax::print::pp::Breaks::{Consistent, Inconsistent};
use syntax::print::pprust::{Comments, PrintState};
use syntax::print::pprust::{self, Comments, PrintState};
use syntax::symbol::kw;
use syntax::util::parser::{self, AssocOp, Fixity};
use syntax_pos::{self, BytePos, FileName};
Expand Down Expand Up @@ -90,6 +90,15 @@ impl<'a> PrintState<'a> for State<'a> {
fn comments(&mut self) -> &mut Option<Comments<'a>> {
&mut self.comments
}

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

fn print_generic_args(&mut self, args: &ast::GenericArgs, _colons_before_params: bool) {
span_bug!(args.span(), "AST generic args printed by HIR pretty-printer");
}
}

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

pub fn print_ident(&mut self, ident: ast::Ident) {
if ident.is_raw_guess() {
self.s.word(format!("r#{}", ident.name));
} else {
self.s.word(ident.as_str().to_string());
}
self.ann.post(self, AnnNode::Name(&ident.name))
}

pub fn print_name(&mut self, name: ast::Name) {
self.print_ident(ast::Ident::with_empty_ctxt(name))
}
Expand Down
12 changes: 11 additions & 1 deletion src/libsyntax/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ pub use crate::symbol::{Ident, Symbol as Name};
pub use crate::util::parser::ExprPrecedence;

use crate::ext::hygiene::{Mark, SyntaxContext};
use crate::parse::token;
use crate::parse::token::{self, DelimToken};
use crate::print::pprust;
use crate::ptr::P;
use crate::source_map::{dummy_spanned, respan, Spanned};
Expand Down Expand Up @@ -1298,6 +1298,16 @@ impl Mac_ {
}
}

impl MacDelimiter {
crate fn to_token(self) -> DelimToken {
match self {
MacDelimiter::Parenthesis => DelimToken::Paren,
MacDelimiter::Bracket => DelimToken::Bracket,
MacDelimiter::Brace => DelimToken::Brace,
}
}
}

#[derive(Clone, RustcEncodable, RustcDecodable, Debug)]
pub struct MacroDef {
pub tokens: TokenStream,
Expand Down
2 changes: 1 addition & 1 deletion src/libsyntax/mut_visit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1328,7 +1328,7 @@ mod tests {
matches_codepattern,
"matches_codepattern",
pprust::to_string(|s| fake_print_crate(s, &krate)),
"macro_rules! zz((zz$zz:zz$(zz $zz:zz)zz+=>(zz$(zz$zz$zz)+)));".to_string());
"macro_rules! zz{(zz$zz:zz$(zz $zz:zz)zz+=>(zz$(zz$zz$zz)+))}".to_string());
})
}
}
Loading

0 comments on commit 92b0f52

Please sign in to comment.