From 65a714a6a0848d5604e1d5f847a06e196951e669 Mon Sep 17 00:00:00 2001 From: Vadim Petrochenkov Date: Sat, 13 Jul 2019 19:11:07 +0300 Subject: [PATCH 1/8] pprust: Move some methods to the `PrintState` trait So that path and macro argument printing code can be shared --- src/librustc/hir/print.rs | 20 +-- src/libsyntax/print/pprust.rs | 267 +++++++++++++++++----------------- src/test/pretty/issue-4264.pp | 4 +- 3 files changed, 144 insertions(+), 147 deletions(-) diff --git a/src/librustc/hir/print.rs b/src/librustc/hir/print.rs index 5a14b30e699be..3e571baaa4e51 100644 --- a/src/librustc/hir/print.rs +++ b/src/librustc/hir/print.rs @@ -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}; @@ -90,6 +90,15 @@ impl<'a> PrintState<'a> for State<'a> { fn comments(&mut self) -> &mut Option> { &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; @@ -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)) } diff --git a/src/libsyntax/print/pprust.rs b/src/libsyntax/print/pprust.rs index 0e3ce2787f3ed..5f3d691400bf2 100644 --- a/src/libsyntax/print/pprust.rs +++ b/src/libsyntax/print/pprust.rs @@ -188,7 +188,7 @@ pub fn literal_to_string(lit: token::Lit) -> String { } /// 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 { +pub fn ast_ident_to_string(ident: ast::Ident, is_raw: bool) -> String { ident_to_string(ident.name, is_raw, Some(ident.span)) } @@ -446,6 +446,8 @@ impl std::ops::DerefMut for State<'_> { pub trait PrintState<'a>: std::ops::Deref + std::ops::DerefMut { fn comments(&mut self) -> &mut Option>; + fn print_ident(&mut self, ident: ast::Ident); + fn print_generic_args(&mut self, args: &ast::GenericArgs, colons_before_params: bool); fn commasep(&mut self, b: Breaks, elts: &[T], mut op: F) where F: FnMut(&mut Self, &T), @@ -596,17 +598,6 @@ pub trait PrintState<'a>: std::ops::Deref + std::ops::DerefM } } - fn print_attribute_path(&mut self, path: &ast::Path) { - for (i, segment) in path.segments.iter().enumerate() { - if i > 0 { - self.word("::"); - } - if segment.ident.name != kw::PathRoot { - self.word(ast_ident_to_string(segment.ident, segment.ident.is_raw_guess())); - } - } - } - fn print_attribute(&mut self, attr: &ast::Attribute) { self.print_attribute_inline(attr, false) } @@ -628,7 +619,7 @@ pub trait PrintState<'a>: std::ops::Deref + std::ops::DerefM if let Some(mi) = attr.meta() { self.print_meta_item(&mi); } else { - self.print_attribute_path(&attr.path); + self.print_path(&attr.path, false, 0); self.space(); self.print_tts(attr.tokens.clone(), true); } @@ -650,15 +641,15 @@ pub trait PrintState<'a>: std::ops::Deref + std::ops::DerefM fn print_meta_item(&mut self, item: &ast::MetaItem) { self.ibox(INDENT_UNIT); match item.node { - ast::MetaItemKind::Word => self.print_attribute_path(&item.path), + ast::MetaItemKind::Word => self.print_path(&item.path, false, 0), ast::MetaItemKind::NameValue(ref value) => { - self.print_attribute_path(&item.path); + self.print_path(&item.path, false, 0); self.space(); self.word_space("="); self.print_literal(value); } ast::MetaItemKind::List(ref items) => { - self.print_attribute_path(&item.path); + self.print_path(&item.path, false, 0); self.popen(); self.commasep(Consistent, &items[..], @@ -707,16 +698,56 @@ pub trait PrintState<'a>: std::ops::Deref + std::ops::DerefM } self.end(); } -} -impl<'a> PrintState<'a> for State<'a> { - fn comments(&mut self) -> &mut Option> { - &mut self.comments + fn print_mac_common( + &mut self, + path: &ast::Path, + has_bang: bool, + tts: TokenStream, + delim: MacDelimiter, + span: Span, + ) { + self.print_path(path, false, 0); + if has_bang { + self.word("!"); + } + match delim { + MacDelimiter::Parenthesis => self.popen(), + MacDelimiter::Bracket => self.word("["), + MacDelimiter::Brace => { + self.head(""); + self.bopen(); + } + } + self.print_tts(tts, true); + match delim { + MacDelimiter::Parenthesis => self.pclose(), + MacDelimiter::Bracket => self.word("]"), + MacDelimiter::Brace => self.bclose(span), + } } -} -impl<'a> State<'a> { - crate fn head>>(&mut self, w: S) { + fn print_path(&mut self, path: &ast::Path, colons_before_params: bool, depth: usize) { + self.maybe_print_comment(path.span.lo()); + + for (i, segment) in path.segments[..path.segments.len() - depth].iter().enumerate() { + if i > 0 { + self.word("::") + } + self.print_path_segment(segment, colons_before_params); + } + } + + fn print_path_segment(&mut self, segment: &ast::PathSegment, colons_before_params: bool) { + if segment.ident.name != kw::PathRoot { + self.print_ident(segment.ident); + if let Some(ref args) = segment.args { + self.print_generic_args(args, colons_before_params); + } + } + } + + fn head>>(&mut self, w: S) { let w = w.into(); // outer-box is consistent self.cbox(INDENT_UNIT); @@ -728,36 +759,103 @@ impl<'a> State<'a> { } } - crate fn bopen(&mut self) { - self.s.word("{"); + fn bopen(&mut self) { + self.word("{"); self.end(); // close the head-box } - crate fn bclose_maybe_open(&mut self, span: syntax_pos::Span, close_box: bool) { + fn bclose_maybe_open(&mut self, span: syntax_pos::Span, close_box: bool) { self.maybe_print_comment(span.hi()); self.break_offset_if_not_bol(1, -(INDENT_UNIT as isize)); - self.s.word("}"); + self.word("}"); if close_box { self.end(); // close the outer-box } } - crate fn bclose(&mut self, span: syntax_pos::Span) { + + fn bclose(&mut self, span: syntax_pos::Span) { self.bclose_maybe_open(span, true) } - crate fn break_offset_if_not_bol(&mut self, n: usize, off: isize) { - if !self.s.is_beginning_of_line() { - self.s.break_offset(n, off) + fn break_offset_if_not_bol(&mut self, n: usize, off: isize) { + if !self.is_beginning_of_line() { + self.break_offset(n, off) } else { - if off != 0 && self.s.last_token().is_hardbreak_tok() { + if off != 0 && self.last_token().is_hardbreak_tok() { // We do something pretty sketchy here: tuck the nonzero // offset-adjustment we were going to deposit along with the // break into the previous hardbreak. - self.s.replace_last_token(pp::Printer::hardbreak_tok_offset(off)); + self.replace_last_token(pp::Printer::hardbreak_tok_offset(off)); } } } +} +impl<'a> PrintState<'a> for State<'a> { + fn comments(&mut self) -> &mut Option> { + &mut self.comments + } + + fn print_ident(&mut self, ident: ast::Ident) { + self.s.word(ast_ident_to_string(ident, ident.is_raw_guess())); + self.ann.post(self, AnnNode::Ident(&ident)) + } + + fn print_generic_args(&mut self, args: &ast::GenericArgs, colons_before_params: bool) { + if colons_before_params { + self.s.word("::") + } + + match *args { + ast::GenericArgs::AngleBracketed(ref data) => { + self.s.word("<"); + + self.commasep(Inconsistent, &data.args, |s, generic_arg| { + s.print_generic_arg(generic_arg) + }); + + let mut comma = data.args.len() != 0; + + for constraint in data.constraints.iter() { + if comma { + self.word_space(",") + } + self.print_ident(constraint.ident); + self.s.space(); + match constraint.kind { + ast::AssocTyConstraintKind::Equality { ref ty } => { + self.word_space("="); + self.print_type(ty); + } + ast::AssocTyConstraintKind::Bound { ref bounds } => { + self.print_type_bounds(":", &*bounds); + } + } + comma = true; + } + + self.s.word(">") + } + + ast::GenericArgs::Parenthesized(ref data) => { + self.s.word("("); + self.commasep( + Inconsistent, + &data.inputs, + |s, ty| s.print_type(ty)); + self.s.word(")"); + + if let Some(ref ty) = data.output { + self.space_if_not_bol(); + self.word_space("->"); + self.print_type(ty); + } + } + } + } +} + +impl<'a> State<'a> { // Synthesizes a comment that was not textually present in the original source // file. pub fn synth_comment(&mut self, text: String) { @@ -1645,22 +1743,7 @@ impl<'a> State<'a> { } crate fn print_mac(&mut self, m: &ast::Mac) { - self.print_path(&m.node.path, false, 0); - self.s.word("!"); - match m.node.delim { - MacDelimiter::Parenthesis => self.popen(), - MacDelimiter::Bracket => self.s.word("["), - MacDelimiter::Brace => { - self.head(""); - self.bopen(); - } - } - self.print_tts(m.node.stream(), true); - match m.node.delim { - MacDelimiter::Parenthesis => self.pclose(), - MacDelimiter::Bracket => self.s.word("]"), - MacDelimiter::Brace => self.bclose(m.span), - } + self.print_mac_common(&m.node.path, true, m.node.stream(), m.node.delim, m.span); } @@ -2204,11 +2287,6 @@ impl<'a> State<'a> { } } - crate fn print_ident(&mut self, ident: ast::Ident) { - self.s.word(ast_ident_to_string(ident, ident.is_raw_guess())); - self.ann.post(self, AnnNode::Ident(&ident)) - } - crate fn print_usize(&mut self, i: usize) { self.s.word(i.to_string()) } @@ -2218,31 +2296,6 @@ impl<'a> State<'a> { self.ann.post(self, AnnNode::Name(&name)) } - fn print_path(&mut self, - path: &ast::Path, - colons_before_params: bool, - depth: usize) { - self.maybe_print_comment(path.span.lo()); - - for (i, segment) in path.segments[..path.segments.len() - depth].iter().enumerate() { - if i > 0 { - self.s.word("::") - } - self.print_path_segment(segment, colons_before_params); - } - } - - fn print_path_segment(&mut self, - segment: &ast::PathSegment, - colons_before_params: bool) { - if segment.ident.name != kw::PathRoot { - self.print_ident(segment.ident); - if let Some(ref args) = segment.args { - self.print_generic_args(args, colons_before_params); - } - } - } - fn print_qpath(&mut self, path: &ast::Path, qself: &ast::QSelf, @@ -2266,62 +2319,6 @@ impl<'a> State<'a> { } } - fn print_generic_args(&mut self, - args: &ast::GenericArgs, - colons_before_params: bool) - { - if colons_before_params { - self.s.word("::") - } - - match *args { - ast::GenericArgs::AngleBracketed(ref data) => { - self.s.word("<"); - - self.commasep(Inconsistent, &data.args, |s, generic_arg| { - s.print_generic_arg(generic_arg) - }); - - let mut comma = data.args.len() != 0; - - for constraint in data.constraints.iter() { - if comma { - self.word_space(",") - } - self.print_ident(constraint.ident); - self.s.space(); - match constraint.kind { - ast::AssocTyConstraintKind::Equality { ref ty } => { - self.word_space("="); - self.print_type(ty); - } - ast::AssocTyConstraintKind::Bound { ref bounds } => { - self.print_type_bounds(":", &*bounds); - } - } - comma = true; - } - - self.s.word(">") - } - - ast::GenericArgs::Parenthesized(ref data) => { - self.s.word("("); - self.commasep( - Inconsistent, - &data.inputs, - |s, ty| s.print_type(ty)); - self.s.word(")"); - - if let Some(ref ty) = data.output { - self.space_if_not_bol(); - self.word_space("->"); - self.print_type(ty); - } - } - } - } - crate fn print_pat(&mut self, pat: &ast::Pat) { self.maybe_print_comment(pat.span.lo()); self.ann.pre(self, AnnNode::Pat(pat)); diff --git a/src/test/pretty/issue-4264.pp b/src/test/pretty/issue-4264.pp index ad663412e7776..bd839d3542199 100644 --- a/src/test/pretty/issue-4264.pp +++ b/src/test/pretty/issue-4264.pp @@ -29,8 +29,8 @@ - (($crate::fmt::format as - for<'r> fn(std::fmt::Arguments<'r>) -> std::string::String {std::fmt::format})(((<$crate::fmt::Arguments>::new_v1 + ((::alloc::fmt::format as + for<'r> fn(std::fmt::Arguments<'r>) -> std::string::String {std::fmt::format})(((<::std::fmt::Arguments>::new_v1 as fn(&[&str], &[std::fmt::ArgumentV1<'_>]) -> std::fmt::Arguments<'_> {std::fmt::Arguments::<'_>::new_v1})((&([("test" as From 500b00127b1b5f0b3891c73327e20a972940a715 Mon Sep 17 00:00:00 2001 From: Vadim Petrochenkov Date: Sat, 13 Jul 2019 20:35:46 +0300 Subject: [PATCH 2/8] pprust: Use `print_mac_common` for `macro_rules` definitions --- src/libsyntax/print/pprust.rs | 42 ++++++++++--------------- src/test/pretty/cast-lt.pp | 2 +- src/test/pretty/stmt_expr_attributes.rs | 6 ++-- 3 files changed, 21 insertions(+), 29 deletions(-) diff --git a/src/libsyntax/print/pprust.rs b/src/libsyntax/print/pprust.rs index 5f3d691400bf2..8542be8ea6085 100644 --- a/src/libsyntax/print/pprust.rs +++ b/src/libsyntax/print/pprust.rs @@ -703,6 +703,7 @@ pub trait PrintState<'a>: std::ops::Deref + std::ops::DerefM &mut self, path: &ast::Path, has_bang: bool, + ident: Option, tts: TokenStream, delim: MacDelimiter, span: Span, @@ -711,6 +712,11 @@ pub trait PrintState<'a>: std::ops::Deref + std::ops::DerefM if has_bang { self.word("!"); } + if let Some(ident) = ident { + self.space(); + self.print_ident(ident); + self.space(); + } match delim { MacDelimiter::Parenthesis => self.popen(), MacDelimiter::Bracket => self.word("["), @@ -1329,33 +1335,17 @@ impl<'a> State<'a> { self.s.word(";"); } ast::ItemKind::Mac(ref mac) => { - if item.ident.name == kw::Invalid { - self.print_mac(mac); - match mac.node.delim { - MacDelimiter::Brace => {} - _ => self.s.word(";"), - } - } else { - self.print_path(&mac.node.path, false, 0); - self.s.word("! "); - self.print_ident(item.ident); - self.cbox(INDENT_UNIT); - self.popen(); - self.print_tts(mac.node.stream(), true); - self.pclose(); - self.s.word(";"); - self.end(); + self.print_mac(mac); + match mac.node.delim { + MacDelimiter::Brace => {} + _ => self.s.word(";"), } } - ast::ItemKind::MacroDef(ref tts) => { - self.s.word("macro_rules! "); - self.print_ident(item.ident); - self.cbox(INDENT_UNIT); - self.popen(); - self.print_tts(tts.stream(), true); - self.pclose(); - self.s.word(";"); - self.end(); + ast::ItemKind::MacroDef(ref macro_def) => { + let path = &ast::Path::from_ident(ast::Ident::with_empty_ctxt(sym::macro_rules)); + self.print_mac_common( + path, true, Some(item.ident), macro_def.stream(), MacDelimiter::Brace, item.span + ); } } self.ann.post(self, AnnNode::Item(item)) @@ -1743,7 +1733,7 @@ impl<'a> State<'a> { } crate fn print_mac(&mut self, m: &ast::Mac) { - self.print_mac_common(&m.node.path, true, m.node.stream(), m.node.delim, m.span); + self.print_mac_common(&m.node.path, true, None, m.node.stream(), m.node.delim, m.span); } diff --git a/src/test/pretty/cast-lt.pp b/src/test/pretty/cast-lt.pp index 351889da2453d..c9fd781636033 100644 --- a/src/test/pretty/cast-lt.pp +++ b/src/test/pretty/cast-lt.pp @@ -8,6 +8,6 @@ // pretty-mode:expanded // pp-exact:cast-lt.pp -macro_rules! negative(( $ e : expr ) => { $ e < 0 }); +macro_rules! negative {( $ e : expr ) => { $ e < 0 } } fn main() { (1 as i32) < 0; } diff --git a/src/test/pretty/stmt_expr_attributes.rs b/src/test/pretty/stmt_expr_attributes.rs index d81485b555fa6..0d438d457bf30 100644 --- a/src/test/pretty/stmt_expr_attributes.rs +++ b/src/test/pretty/stmt_expr_attributes.rs @@ -111,7 +111,9 @@ fn _8() { } fn _9() { - macro_rules! stmt_mac(( ) => { let _ = ( ) ; }); + macro_rules! + stmt_mac + {( ) => { let _ = ( ) ; } } #[rustc_dummy] stmt_mac!(); @@ -128,7 +130,7 @@ fn _9() { let _ = (); } -macro_rules! expr_mac(( ) => { ( ) }); +macro_rules! expr_mac {( ) => { ( ) } } fn _10() { let _ = #[rustc_dummy] expr_mac!(); From 23c5c1bfe7e3785fa1dcf1d3ff6e6bbaa855f66f Mon Sep 17 00:00:00 2001 From: Vadim Petrochenkov Date: Sat, 13 Jul 2019 21:01:04 +0300 Subject: [PATCH 3/8] pprust: Use `print_mac_common` for attributes --- src/libsyntax/print/pprust.rs | 22 +++++++++++++++---- .../run-pass/proc-macro/auxiliary/derive-b.rs | 2 +- 2 files changed, 19 insertions(+), 5 deletions(-) diff --git a/src/libsyntax/print/pprust.rs b/src/libsyntax/print/pprust.rs index 8542be8ea6085..af9f6bb442dd1 100644 --- a/src/libsyntax/print/pprust.rs +++ b/src/libsyntax/print/pprust.rs @@ -4,7 +4,7 @@ use crate::ast::{Attribute, MacDelimiter, GenericArg}; use crate::util::parser::{self, AssocOp, Fixity}; use crate::attr; use crate::source_map::{self, SourceMap, Spanned}; -use crate::parse::token::{self, BinOpToken, Nonterminal, Token, TokenKind}; +use crate::parse::token::{self, BinOpToken, DelimToken, Nonterminal, Token, TokenKind}; use crate::parse::lexer::comments; use crate::parse::{self, ParseSess}; use crate::print::pp::{self, Breaks}; @@ -619,9 +619,23 @@ pub trait PrintState<'a>: std::ops::Deref + std::ops::DerefM if let Some(mi) = attr.meta() { self.print_meta_item(&mi); } else { - self.print_path(&attr.path, false, 0); - self.space(); - self.print_tts(attr.tokens.clone(), true); + match attr.tokens.trees().next() { + Some(TokenTree::Delimited(_, delim, tts)) => { + let delim = match delim { + DelimToken::Brace => MacDelimiter::Brace, + DelimToken::Bracket => MacDelimiter::Bracket, + DelimToken::Paren | DelimToken::NoDelim => MacDelimiter::Parenthesis, + }; + self.print_mac_common(&attr.path, false, None, tts, delim, attr.span); + } + tree => { + self.print_path(&attr.path, false, 0); + if tree.is_some() { + self.space(); + self.print_tts(attr.tokens.clone(), true); + } + } + } } self.word("]"); } diff --git a/src/test/run-pass/proc-macro/auxiliary/derive-b.rs b/src/test/run-pass/proc-macro/auxiliary/derive-b.rs index fd056d605a0e6..3e6af67a9f412 100644 --- a/src/test/run-pass/proc-macro/auxiliary/derive-b.rs +++ b/src/test/run-pass/proc-macro/auxiliary/derive-b.rs @@ -10,7 +10,7 @@ use proc_macro::TokenStream; #[proc_macro_derive(B, attributes(B, C))] pub fn derive(input: TokenStream) -> TokenStream { let input = input.to_string(); - assert!(input.contains("#[B [ arbitrary tokens ]]")); + assert!(input.contains("#[B[arbitrary tokens]]")); assert!(input.contains("struct B {")); assert!(input.contains("#[C]")); "".parse().unwrap() From 11585b598ccf5942df405320bead98c096e93259 Mon Sep 17 00:00:00 2001 From: Vadim Petrochenkov Date: Sat, 13 Jul 2019 23:08:29 +0300 Subject: [PATCH 4/8] pprust: Use `print_mac_common` for delimited token groups --- src/libsyntax/ast.rs | 12 +++- src/libsyntax/print/pprust.rs | 60 ++++++++++--------- src/test/pretty/cast-lt.pp | 2 +- src/test/pretty/stmt_expr_attributes.rs | 4 +- ...dition-keywords-2015-2018-expansion.stderr | 4 +- .../edition-keywords-2018-2015-parsing.stderr | 6 +- ...dition-keywords-2018-2018-expansion.stderr | 4 +- .../edition-keywords-2018-2018-parsing.stderr | 6 +- src/test/ui/macro_backtrace/main.stderr | 40 ++++++------- src/test/ui/macros/trace-macro.stderr | 2 +- src/test/ui/macros/trace_faulty_macros.stderr | 10 ++-- .../attribute-spans-preserved.stdout | 2 +- .../dollar-crate-issue-57089.stdout | 4 +- .../dollar-crate-issue-62325.stdout | 4 +- src/test/ui/proc-macro/dollar-crate.stdout | 12 ++-- 15 files changed, 94 insertions(+), 78 deletions(-) diff --git a/src/libsyntax/ast.rs b/src/libsyntax/ast.rs index 8801e89a0cfc4..6cfc1b77e03fe 100644 --- a/src/libsyntax/ast.rs +++ b/src/libsyntax/ast.rs @@ -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}; @@ -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, diff --git a/src/libsyntax/print/pprust.rs b/src/libsyntax/print/pprust.rs index af9f6bb442dd1..77f2dff0d8eba 100644 --- a/src/libsyntax/print/pprust.rs +++ b/src/libsyntax/print/pprust.rs @@ -621,12 +621,9 @@ pub trait PrintState<'a>: std::ops::Deref + std::ops::DerefM } else { match attr.tokens.trees().next() { Some(TokenTree::Delimited(_, delim, tts)) => { - let delim = match delim { - DelimToken::Brace => MacDelimiter::Brace, - DelimToken::Bracket => MacDelimiter::Bracket, - DelimToken::Paren | DelimToken::NoDelim => MacDelimiter::Parenthesis, - }; - self.print_mac_common(&attr.path, false, None, tts, delim, attr.span); + self.print_mac_common( + Some(&attr.path), false, None, delim, tts, true, attr.span + ); } tree => { self.print_path(&attr.path, false, 0); @@ -692,13 +689,11 @@ pub trait PrintState<'a>: std::ops::Deref + std::ops::DerefM _ => {} } } - TokenTree::Delimited(_, delim, tts) => { - self.word(token_kind_to_string(&token::OpenDelim(delim))); - self.space(); - self.print_tts(tts, convert_dollar_crate); - self.space(); - self.word(token_kind_to_string(&token::CloseDelim(delim))) - }, + TokenTree::Delimited(dspan, delim, tts) => { + self.print_mac_common( + None, false, None, delim, tts, convert_dollar_crate, dspan.entire() + ); + } } } @@ -715,14 +710,17 @@ pub trait PrintState<'a>: std::ops::Deref + std::ops::DerefM fn print_mac_common( &mut self, - path: &ast::Path, + path: Option<&ast::Path>, has_bang: bool, ident: Option, + delim: DelimToken, tts: TokenStream, - delim: MacDelimiter, + convert_dollar_crate: bool, span: Span, ) { - self.print_path(path, false, 0); + if let Some(path) = path { + self.print_path(path, false, 0); + } if has_bang { self.word("!"); } @@ -732,18 +730,20 @@ pub trait PrintState<'a>: std::ops::Deref + std::ops::DerefM self.space(); } match delim { - MacDelimiter::Parenthesis => self.popen(), - MacDelimiter::Bracket => self.word("["), - MacDelimiter::Brace => { + DelimToken::Paren => self.popen(), + DelimToken::Bracket => self.word("["), + DelimToken::NoDelim => self.word(" "), + DelimToken::Brace => { self.head(""); self.bopen(); } } - self.print_tts(tts, true); + self.print_tts(tts, convert_dollar_crate); match delim { - MacDelimiter::Parenthesis => self.pclose(), - MacDelimiter::Bracket => self.word("]"), - MacDelimiter::Brace => self.bclose(span), + DelimToken::Paren => self.pclose(), + DelimToken::Bracket => self.word("]"), + DelimToken::NoDelim => self.word(" "), + DelimToken::Brace => self.bclose(span), } } @@ -1356,9 +1356,14 @@ impl<'a> State<'a> { } } ast::ItemKind::MacroDef(ref macro_def) => { - let path = &ast::Path::from_ident(ast::Ident::with_empty_ctxt(sym::macro_rules)); self.print_mac_common( - path, true, Some(item.ident), macro_def.stream(), MacDelimiter::Brace, item.span + Some(&ast::Path::from_ident(ast::Ident::with_empty_ctxt(sym::macro_rules))), + true, + Some(item.ident), + DelimToken::Brace, + macro_def.stream(), + true, + item.span, ); } } @@ -1747,10 +1752,11 @@ impl<'a> State<'a> { } crate fn print_mac(&mut self, m: &ast::Mac) { - self.print_mac_common(&m.node.path, true, None, m.node.stream(), m.node.delim, m.span); + self.print_mac_common( + Some(&m.node.path), true, None, m.node.delim.to_token(), m.node.stream(), true, m.span + ); } - fn print_call_post(&mut self, args: &[P]) { self.popen(); self.commasep_exprs(Inconsistent, args); diff --git a/src/test/pretty/cast-lt.pp b/src/test/pretty/cast-lt.pp index c9fd781636033..1ae30983d202a 100644 --- a/src/test/pretty/cast-lt.pp +++ b/src/test/pretty/cast-lt.pp @@ -8,6 +8,6 @@ // pretty-mode:expanded // pp-exact:cast-lt.pp -macro_rules! negative {( $ e : expr ) => { $ e < 0 } } +macro_rules! negative {($ e : expr) => {$ e < 0 } } fn main() { (1 as i32) < 0; } diff --git a/src/test/pretty/stmt_expr_attributes.rs b/src/test/pretty/stmt_expr_attributes.rs index 0d438d457bf30..acc11f3f84aba 100644 --- a/src/test/pretty/stmt_expr_attributes.rs +++ b/src/test/pretty/stmt_expr_attributes.rs @@ -113,7 +113,7 @@ fn _8() { fn _9() { macro_rules! stmt_mac - {( ) => { let _ = ( ) ; } } + {() => {let _ = () ; } } #[rustc_dummy] stmt_mac!(); @@ -130,7 +130,7 @@ fn _9() { let _ = (); } -macro_rules! expr_mac {( ) => { ( ) } } +macro_rules! expr_mac {() => {() } } fn _10() { let _ = #[rustc_dummy] expr_mac!(); diff --git a/src/test/ui/editions/edition-keywords-2015-2018-expansion.stderr b/src/test/ui/editions/edition-keywords-2015-2018-expansion.stderr index 9724f78db6618..321545740cf48 100644 --- a/src/test/ui/editions/edition-keywords-2015-2018-expansion.stderr +++ b/src/test/ui/editions/edition-keywords-2015-2018-expansion.stderr @@ -7,8 +7,8 @@ LL | produces_async! {} = note: this error originates in a macro outside of the current crate (in Nightly builds, run with -Z external-macro-backtrace for more info) help: you can escape reserved keywords to use them as identifiers | -LL | ( ) => ( pub fn r#async ( ) { } ) - | ^^^^^^^ +LL | () => (pub fn r#async () { }) + | ^^^^^^^ error: aborting due to previous error diff --git a/src/test/ui/editions/edition-keywords-2018-2015-parsing.stderr b/src/test/ui/editions/edition-keywords-2018-2015-parsing.stderr index 0d8850c2397c6..3c4a153353447 100644 --- a/src/test/ui/editions/edition-keywords-2018-2015-parsing.stderr +++ b/src/test/ui/editions/edition-keywords-2018-2015-parsing.stderr @@ -31,10 +31,10 @@ LL | r#async = consumes_async_raw!(async); | ^^^^^ no rules expected this token in macro call error: macro expansion ends with an incomplete expression: expected one of `move`, `|`, or `||` - --> <::edition_kw_macro_2015::passes_ident macros>:1:25 + --> <::edition_kw_macro_2015::passes_ident macros>:1:22 | -LL | ( $ i : ident ) => ( $ i ) - | ^ expected one of `move`, `|`, or `||` here +LL | ($ i : ident) => ($ i) + | ^ expected one of `move`, `|`, or `||` here | ::: $DIR/edition-keywords-2018-2015-parsing.rs:16:8 | diff --git a/src/test/ui/editions/edition-keywords-2018-2018-expansion.stderr b/src/test/ui/editions/edition-keywords-2018-2018-expansion.stderr index ab601c8d8a707..8942e3ce430a8 100644 --- a/src/test/ui/editions/edition-keywords-2018-2018-expansion.stderr +++ b/src/test/ui/editions/edition-keywords-2018-2018-expansion.stderr @@ -7,8 +7,8 @@ LL | produces_async! {} = note: this error originates in a macro outside of the current crate (in Nightly builds, run with -Z external-macro-backtrace for more info) help: you can escape reserved keywords to use them as identifiers | -LL | ( ) => ( pub fn r#async ( ) { } ) - | ^^^^^^^ +LL | () => (pub fn r#async () { }) + | ^^^^^^^ error: aborting due to previous error diff --git a/src/test/ui/editions/edition-keywords-2018-2018-parsing.stderr b/src/test/ui/editions/edition-keywords-2018-2018-parsing.stderr index 0604b600d23d0..46aa9ca34e17c 100644 --- a/src/test/ui/editions/edition-keywords-2018-2018-parsing.stderr +++ b/src/test/ui/editions/edition-keywords-2018-2018-parsing.stderr @@ -31,10 +31,10 @@ LL | r#async = consumes_async_raw!(async); | ^^^^^ no rules expected this token in macro call error: macro expansion ends with an incomplete expression: expected one of `move`, `|`, or `||` - --> <::edition_kw_macro_2018::passes_ident macros>:1:25 + --> <::edition_kw_macro_2018::passes_ident macros>:1:22 | -LL | ( $ i : ident ) => ( $ i ) - | ^ expected one of `move`, `|`, or `||` here +LL | ($ i : ident) => ($ i) + | ^ expected one of `move`, `|`, or `||` here | ::: $DIR/edition-keywords-2018-2018-parsing.rs:16:8 | diff --git a/src/test/ui/macro_backtrace/main.stderr b/src/test/ui/macro_backtrace/main.stderr index 239b53f2338c4..6f82d4040bce5 100644 --- a/src/test/ui/macro_backtrace/main.stderr +++ b/src/test/ui/macro_backtrace/main.stderr @@ -24,10 +24,10 @@ LL | ping!(); | ::: <::ping::ping macros>:1:1 | -LL | ( ) => { pong ! ( ) ; } - | ------------------------- - | | | - | | in this macro invocation +LL | () => {pong ! () ; } + | -------------------- + | | | + | | in this macro invocation | in this expansion of `ping!` error: expected one of `!`, `.`, `::`, `;`, `?`, `{`, `}`, or an operator, found `error` @@ -44,34 +44,34 @@ LL | deep!(); | ::: <::ping::deep macros>:1:1 | -LL | ( ) => { foo ! ( ) ; } - | ------------------------ - | | | - | | in this macro invocation (#2) +LL | () => {foo ! () ; } + | ------------------- + | | | + | | in this macro invocation (#2) | in this expansion of `deep!` (#1) | ::: <::ping::foo macros>:1:1 | -LL | ( ) => { bar ! ( ) ; } - | ------------------------ - | | | - | | in this macro invocation (#3) +LL | () => {bar ! () ; } + | ------------------- + | | | + | | in this macro invocation (#3) | in this expansion of `foo!` (#2) | ::: <::ping::bar macros>:1:1 | -LL | ( ) => { ping ! ( ) ; } - | ------------------------- - | | | - | | in this macro invocation (#4) +LL | () => {ping ! () ; } + | -------------------- + | | | + | | in this macro invocation (#4) | in this expansion of `bar!` (#3) | ::: <::ping::ping macros>:1:1 | -LL | ( ) => { pong ! ( ) ; } - | ------------------------- - | | | - | | in this macro invocation (#5) +LL | () => {pong ! () ; } + | -------------------- + | | | + | | in this macro invocation (#5) | in this expansion of `ping!` (#4) error: aborting due to 3 previous errors diff --git a/src/test/ui/macros/trace-macro.stderr b/src/test/ui/macros/trace-macro.stderr index ebfed41bc280e..545ed33cd10be 100644 --- a/src/test/ui/macros/trace-macro.stderr +++ b/src/test/ui/macros/trace-macro.stderr @@ -5,5 +5,5 @@ LL | println!("Hello, World!"); | ^^^^^^^^^^^^^^^^^^^^^^^^^^ | = note: expanding `println! { "Hello, World!" }` - = note: to `{ $crate :: io :: _print ( format_args_nl ! ( "Hello, World!" ) ) ; }` + = note: to `{$crate :: io :: _print (format_args_nl ! ("Hello, World!")) ; }` diff --git a/src/test/ui/macros/trace_faulty_macros.stderr b/src/test/ui/macros/trace_faulty_macros.stderr index fc05012377b2a..f06e6581ff7fb 100644 --- a/src/test/ui/macros/trace_faulty_macros.stderr +++ b/src/test/ui/macros/trace_faulty_macros.stderr @@ -17,7 +17,7 @@ LL | my_faulty_macro!(); | ^^^^^^^^^^^^^^^^^^^ | = note: expanding `my_faulty_macro! { }` - = note: to `my_faulty_macro ! ( bcd ) ;` + = note: to `my_faulty_macro ! (bcd) ;` = note: expanding `my_faulty_macro! { bcd }` error: recursion limit reached while expanding the macro `my_recursive_macro` @@ -38,13 +38,13 @@ LL | my_recursive_macro!(); | ^^^^^^^^^^^^^^^^^^^^^^ | = note: expanding `my_recursive_macro! { }` - = note: to `my_recursive_macro ! ( ) ;` + = note: to `my_recursive_macro ! () ;` = note: expanding `my_recursive_macro! { }` - = note: to `my_recursive_macro ! ( ) ;` + = note: to `my_recursive_macro ! () ;` = note: expanding `my_recursive_macro! { }` - = note: to `my_recursive_macro ! ( ) ;` + = note: to `my_recursive_macro ! () ;` = note: expanding `my_recursive_macro! { }` - = note: to `my_recursive_macro ! ( ) ;` + = note: to `my_recursive_macro ! () ;` error: aborting due to 2 previous errors diff --git a/src/test/ui/proc-macro/attribute-spans-preserved.stdout b/src/test/ui/proc-macro/attribute-spans-preserved.stdout index b1487fcd5edbd..b2be35e825993 100644 --- a/src/test/ui/proc-macro/attribute-spans-preserved.stdout +++ b/src/test/ui/proc-macro/attribute-spans-preserved.stdout @@ -1 +1 @@ -fn main ( ) { let y : u32 = "z" ; { let x : u32 = "y" ; } } +fn main () {let y : u32 = "z" ; {let x : u32 = "y" ; } } diff --git a/src/test/ui/proc-macro/dollar-crate-issue-57089.stdout b/src/test/ui/proc-macro/dollar-crate-issue-57089.stdout index 0611fcb13f267..0fe02a9a34d18 100644 --- a/src/test/ui/proc-macro/dollar-crate-issue-57089.stdout +++ b/src/test/ui/proc-macro/dollar-crate-issue-57089.stdout @@ -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", @@ -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", diff --git a/src/test/ui/proc-macro/dollar-crate-issue-62325.stdout b/src/test/ui/proc-macro/dollar-crate-issue-62325.stdout index 6c483d7a91bba..a499e1362ec0b 100644 --- a/src/test/ui/proc-macro/dollar-crate-issue-62325.stdout +++ b/src/test/ui/proc-macro/dollar-crate-issue-62325.stdout @@ -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", @@ -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 ! ( $crate :: S ) ) ; +PRINT-ATTR RE-COLLECTED (DISPLAY): struct B (identity ! ($crate :: S)) ; PRINT-ATTR INPUT (DEBUG): TokenStream [ Ident { ident: "struct", diff --git a/src/test/ui/proc-macro/dollar-crate.stdout b/src/test/ui/proc-macro/dollar-crate.stdout index 3c88ee99842a2..72b02ebcb76f4 100644 --- a/src/test/ui/proc-macro/dollar-crate.stdout +++ b/src/test/ui/proc-macro/dollar-crate.stdout @@ -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", @@ -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", @@ -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", @@ -120,7 +120,7 @@ PRINT-DERIVE INPUT (DEBUG): TokenStream [ span: #2 bytes(LO..HI), }, ] -PRINT-BANG INPUT (DISPLAY): struct M ( $crate :: S ) ; +PRINT-BANG INPUT (DISPLAY): struct M ($crate :: S) ; PRINT-BANG INPUT (DEBUG): TokenStream [ Ident { ident: "struct", @@ -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 ( $crate :: S ) ; +PRINT-ATTR RE-COLLECTED (DISPLAY): struct A ($crate :: S) ; PRINT-ATTR INPUT (DEBUG): TokenStream [ Ident { ident: "struct", @@ -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 ( $crate :: S ) ; +PRINT-DERIVE RE-COLLECTED (DISPLAY): struct D ($crate :: S) ; PRINT-DERIVE INPUT (DEBUG): TokenStream [ Ident { ident: "struct", From 160c40bc88706965d268106e02c3d43422114a02 Mon Sep 17 00:00:00 2001 From: Vadim Petrochenkov Date: Sat, 13 Jul 2019 23:11:04 +0300 Subject: [PATCH 5/8] pprust: Remove the box from `print_tts` Wrap the whole attribute into a box instead --- src/libsyntax/print/pprust.rs | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/libsyntax/print/pprust.rs b/src/libsyntax/print/pprust.rs index 77f2dff0d8eba..e101168a1370a 100644 --- a/src/libsyntax/print/pprust.rs +++ b/src/libsyntax/print/pprust.rs @@ -616,6 +616,7 @@ pub trait PrintState<'a>: std::ops::Deref + std::ops::DerefM ast::AttrStyle::Inner => self.word("#!["), ast::AttrStyle::Outer => self.word("#["), } + self.ibox(0); if let Some(mi) = attr.meta() { self.print_meta_item(&mi); } else { @@ -634,6 +635,7 @@ pub trait PrintState<'a>: std::ops::Deref + std::ops::DerefM } } } + self.end(); self.word("]"); } } @@ -698,14 +700,12 @@ pub trait PrintState<'a>: std::ops::Deref + std::ops::DerefM } 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 { self.space(); } self.print_tt(tt, convert_dollar_crate); } - self.end(); } fn print_mac_common( @@ -738,7 +738,9 @@ pub trait PrintState<'a>: std::ops::Deref + std::ops::DerefM self.bopen(); } } + self.ibox(0); self.print_tts(tts, convert_dollar_crate); + self.end(); match delim { DelimToken::Paren => self.pclose(), DelimToken::Bracket => self.word("]"), From 75896011dc2a137d96a4941f5c415c2c108f5cf9 Mon Sep 17 00:00:00 2001 From: Vadim Petrochenkov Date: Sat, 13 Jul 2019 23:24:58 +0300 Subject: [PATCH 6/8] pprust: Do not convert attributes into `MetaItem`s for printing Fixes https://github.com/rust-lang/rust/issues/62628 --- src/libsyntax/print/pprust.rs | 26 ++++++++++-------------- src/test/pretty/attr-literals.rs | 4 ++-- src/test/pretty/attr-tokens-raw-ident.rs | 7 +++++++ 3 files changed, 20 insertions(+), 17 deletions(-) create mode 100644 src/test/pretty/attr-tokens-raw-ident.rs diff --git a/src/libsyntax/print/pprust.rs b/src/libsyntax/print/pprust.rs index e101168a1370a..a07bba044898b 100644 --- a/src/libsyntax/print/pprust.rs +++ b/src/libsyntax/print/pprust.rs @@ -617,21 +617,17 @@ pub trait PrintState<'a>: std::ops::Deref + std::ops::DerefM ast::AttrStyle::Outer => self.word("#["), } self.ibox(0); - if let Some(mi) = attr.meta() { - self.print_meta_item(&mi); - } else { - match attr.tokens.trees().next() { - Some(TokenTree::Delimited(_, delim, tts)) => { - self.print_mac_common( - Some(&attr.path), false, None, delim, tts, true, attr.span - ); - } - tree => { - self.print_path(&attr.path, false, 0); - if tree.is_some() { - self.space(); - self.print_tts(attr.tokens.clone(), true); - } + match attr.tokens.trees().next() { + Some(TokenTree::Delimited(_, delim, tts)) => { + self.print_mac_common( + Some(&attr.path), false, None, delim, tts, true, attr.span + ); + } + tree => { + self.print_path(&attr.path, false, 0); + if tree.is_some() { + self.space(); + self.print_tts(attr.tokens.clone(), true); } } } diff --git a/src/test/pretty/attr-literals.rs b/src/test/pretty/attr-literals.rs index 44d2c5db3e668..bcd6ffaaf815b 100644 --- a/src/test/pretty/attr-literals.rs +++ b/src/test/pretty/attr-literals.rs @@ -5,10 +5,10 @@ #![feature(rustc_attrs)] fn main() { - #![rustc_dummy("hi", 1, 2, 1.012, pi = 3.14, bye, name("John"))] + #![rustc_dummy("hi" , 1 , 2 , 1.012 , pi = 3.14 , bye , name ("John"))] #[rustc_dummy = 8] fn f() { } - #[rustc_dummy(1, 2, 3)] + #[rustc_dummy(1 , 2 , 3)] fn g() { } } diff --git a/src/test/pretty/attr-tokens-raw-ident.rs b/src/test/pretty/attr-tokens-raw-ident.rs new file mode 100644 index 0000000000000..bb2c4bb558e57 --- /dev/null +++ b/src/test/pretty/attr-tokens-raw-ident.rs @@ -0,0 +1,7 @@ +// Keywords in attribute paths are printed as raw idents, +// but keywords in attribute arguments are not. + +// pp-exact + +#[rustfmt::r#final(final)] +fn main() { } From a2a1cd186481c49d1cde611e4f3f2a707e77eab9 Mon Sep 17 00:00:00 2001 From: Vadim Petrochenkov Date: Sun, 14 Jul 2019 00:11:59 +0300 Subject: [PATCH 7/8] pprust: Fix formatting regressions from the previous commits Fix some remaining cases of bad formatting Update some failing tests --- src/libsyntax/mut_visit.rs | 2 +- src/libsyntax/print/pprust.rs | 23 +++++---- src/test/pretty/cast-lt.pp | 2 +- src/test/pretty/delimited-token-groups.rs | 49 +++++++++++++++++++ src/test/pretty/issue-30731.rs | 2 +- src/test/pretty/stmt_expr_attributes.rs | 14 +++--- .../pretty-expanded-hygiene/input.pp.rs | 10 ++-- .../macros/syntax-extension-source-utils.rs | 4 +- src/test/ui/macro_backtrace/main.stderr | 40 +++++++-------- src/test/ui/macros/trace-macro.stderr | 2 +- .../attribute-spans-preserved.stdout | 2 +- 11 files changed, 100 insertions(+), 50 deletions(-) create mode 100644 src/test/pretty/delimited-token-groups.rs diff --git a/src/libsyntax/mut_visit.rs b/src/libsyntax/mut_visit.rs index 11a1de13fc217..dc656222fbc10 100644 --- a/src/libsyntax/mut_visit.rs +++ b/src/libsyntax/mut_visit.rs @@ -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()); }) } } diff --git a/src/libsyntax/print/pprust.rs b/src/libsyntax/print/pprust.rs index a07bba044898b..47985351f7d33 100644 --- a/src/libsyntax/print/pprust.rs +++ b/src/libsyntax/print/pprust.rs @@ -714,6 +714,9 @@ pub trait PrintState<'a>: std::ops::Deref + std::ops::DerefM convert_dollar_crate: bool, span: Span, ) { + if delim == DelimToken::Brace { + self.cbox(INDENT_UNIT); + } if let Some(path) = path { self.print_path(path, false, 0); } @@ -721,27 +724,27 @@ pub trait PrintState<'a>: std::ops::Deref + std::ops::DerefM self.word("!"); } if let Some(ident) = ident { - self.space(); + self.nbsp(); self.print_ident(ident); - self.space(); } match delim { - DelimToken::Paren => self.popen(), - DelimToken::Bracket => self.word("["), - DelimToken::NoDelim => self.word(" "), DelimToken::Brace => { - self.head(""); - self.bopen(); + if path.is_some() || has_bang || ident.is_some() { + self.nbsp(); + } + self.word("{"); + if !tts.is_empty() { + self.space(); + } } + _ => self.word(token_kind_to_string(&token::OpenDelim(delim))), } self.ibox(0); self.print_tts(tts, convert_dollar_crate); self.end(); match delim { - DelimToken::Paren => self.pclose(), - DelimToken::Bracket => self.word("]"), - DelimToken::NoDelim => self.word(" "), DelimToken::Brace => self.bclose(span), + _ => self.word(token_kind_to_string(&token::CloseDelim(delim))), } } diff --git a/src/test/pretty/cast-lt.pp b/src/test/pretty/cast-lt.pp index 1ae30983d202a..47a7dac95b9c5 100644 --- a/src/test/pretty/cast-lt.pp +++ b/src/test/pretty/cast-lt.pp @@ -8,6 +8,6 @@ // pretty-mode:expanded // pp-exact:cast-lt.pp -macro_rules! negative {($ e : expr) => {$ e < 0 } } +macro_rules! negative { ($ e : expr) => { $ e < 0 } } fn main() { (1 as i32) < 0; } diff --git a/src/test/pretty/delimited-token-groups.rs b/src/test/pretty/delimited-token-groups.rs new file mode 100644 index 0000000000000..768f27ad23a8e --- /dev/null +++ b/src/test/pretty/delimited-token-groups.rs @@ -0,0 +1,49 @@ +// pp-exact + +#![feature(rustc_attrs)] + +macro_rules! mac { ($ ($ tt : tt) *) => () } + +mac! { + struct S { field1 : u8 , field2 : u16 , } impl Clone for S + { + fn clone () -> S + { + panic ! () ; + + } + } +} + +mac! { + a + (aaaaaaaa aaaaaaaa aaaaaaaa aaaaaaaa aaaaaaaa aaaaaaaa aaaaaaaa aaaaaaaa + aaaaaaaa aaaaaaaa) a + [aaaaaaaa aaaaaaaa aaaaaaaa aaaaaaaa aaaaaaaa aaaaaaaa aaaaaaaa aaaaaaaa + aaaaaaaa aaaaaaaa] a + { + aaaaaaaa aaaaaaaa aaaaaaaa aaaaaaaa aaaaaaaa aaaaaaaa aaaaaaaa + aaaaaaaa aaaaaaaa aaaaaaaa + } a +} + +mac!(aaaaaaaa aaaaaaaa aaaaaaaa aaaaaaaa aaaaaaaa aaaaaaaa aaaaaaaa aaaaaaaa + aaaaaaaa aaaaaaaa); +mac![aaaaaaaa aaaaaaaa aaaaaaaa aaaaaaaa aaaaaaaa aaaaaaaa aaaaaaaa aaaaaaaa + aaaaaaaa aaaaaaaa]; +mac! { + aaaaaaaa aaaaaaaa aaaaaaaa aaaaaaaa aaaaaaaa aaaaaaaa aaaaaaaa aaaaaaaa + aaaaaaaa aaaaaaaa +} + +#[rustc_dummy(aaaaaaaa aaaaaaaa aaaaaaaa aaaaaaaa aaaaaaaa aaaaaaaa aaaaaaaa + aaaaaaaa aaaaaaaa aaaaaaaa)] +#[rustc_dummy[aaaaaaaa aaaaaaaa aaaaaaaa aaaaaaaa aaaaaaaa aaaaaaaa aaaaaaaa + aaaaaaaa aaaaaaaa aaaaaaaa]] +#[rustc_dummy { + aaaaaaaa aaaaaaaa aaaaaaaa aaaaaaaa aaaaaaaa aaaaaaaa aaaaaaaa aaaaaaaa + aaaaaaaa aaaaaaaa + }] +#[rustc_dummy = + "aaaaaaaa aaaaaaaa aaaaaaaa aaaaaaaa aaaaaaaa aaaaaaaa aaaaaaaa aaaaaaaa aaaaaaaa aaaaaaaa"] +fn main() { } diff --git a/src/test/pretty/issue-30731.rs b/src/test/pretty/issue-30731.rs index ee81cce864171..02951395e70b4 100644 --- a/src/test/pretty/issue-30731.rs +++ b/src/test/pretty/issue-30731.rs @@ -5,4 +5,4 @@ // pretty-compare-only // pp-exact -fn main() { b!{ } c } +fn main() { b! { } c } diff --git a/src/test/pretty/stmt_expr_attributes.rs b/src/test/pretty/stmt_expr_attributes.rs index acc11f3f84aba..02d93238dd643 100644 --- a/src/test/pretty/stmt_expr_attributes.rs +++ b/src/test/pretty/stmt_expr_attributes.rs @@ -111,31 +111,29 @@ fn _8() { } fn _9() { - macro_rules! - stmt_mac - {() => {let _ = () ; } } + macro_rules! stmt_mac { () => { let _ = () ; } } #[rustc_dummy] stmt_mac!(); #[rustc_dummy] - stmt_mac!{ }; + stmt_mac! { }; #[rustc_dummy] stmt_mac![]; #[rustc_dummy] - stmt_mac!{ } + stmt_mac! { } let _ = (); } -macro_rules! expr_mac {() => {() } } +macro_rules! expr_mac { () => { () } } fn _10() { let _ = #[rustc_dummy] expr_mac!(); let _ = #[rustc_dummy] expr_mac![]; - let _ = #[rustc_dummy] expr_mac!{ }; + let _ = #[rustc_dummy] expr_mac! { }; } fn _11() { @@ -238,7 +236,7 @@ fn _11() { || #[rustc_dummy] return; let _ = #[rustc_dummy] expr_mac!(); let _ = #[rustc_dummy] expr_mac![]; - let _ = #[rustc_dummy] expr_mac!{ }; + let _ = #[rustc_dummy] expr_mac! { }; let _ = #[rustc_dummy] Foo{#![rustc_dummy] data: (),}; let _ = #[rustc_dummy] Foo{#![rustc_dummy] ..s}; let _ = #[rustc_dummy] Foo{#![rustc_dummy] data: (), ..s}; diff --git a/src/test/run-make-fulldeps/pretty-expanded-hygiene/input.pp.rs b/src/test/run-make-fulldeps/pretty-expanded-hygiene/input.pp.rs index b835bf8782cc3..9d081c970dabc 100644 --- a/src/test/run-make-fulldeps/pretty-expanded-hygiene/input.pp.rs +++ b/src/test/run-make-fulldeps/pretty-expanded-hygiene/input.pp.rs @@ -1,9 +1,9 @@ // minimal junk -#![feature(no_core)] -#![no_core] +#![feature /* 0#0 */(no_core)] +#![no_core /* 0#0 */] -macro_rules! foo /* 60#0 */(( $ x : ident ) => { y + $ x }); +macro_rules /* 0#0 */! foo /* 0#0 */ { ($ x : ident) => { y + $ x } } -fn bar /* 62#0 */() { let x /* 59#2 */ = 1; y /* 61#4 */ + x /* 59#5 */ } +fn bar /* 0#0 */() { let x /* 0#0 */ = 1; y /* 0#1 */ + x /* 0#0 */ } -fn y /* 61#0 */() { } +fn y /* 0#0 */() { } diff --git a/src/test/run-pass/macros/syntax-extension-source-utils.rs b/src/test/run-pass/macros/syntax-extension-source-utils.rs index 129311599141f..7e46260d5163f 100644 --- a/src/test/run-pass/macros/syntax-extension-source-utils.rs +++ b/src/test/run-pass/macros/syntax-extension-source-utils.rs @@ -18,7 +18,7 @@ pub fn main() { assert_eq!(column!(), 16); assert_eq!(indirect_line!(), 19); assert!((file!().ends_with("syntax-extension-source-utils.rs"))); - assert_eq!(stringify!((2*3) + 5).to_string(), "( 2 * 3 ) + 5".to_string()); + assert_eq!(stringify!((2*3) + 5).to_string(), "(2 * 3) + 5".to_string()); assert!(include!("syntax-extension-source-utils-files/includeme.\ fragment").to_string() == "victory robot 6".to_string()); @@ -33,5 +33,5 @@ pub fn main() { // The Windows tests are wrapped in an extra module for some reason assert!((m1::m2::where_am_i().ends_with("m1::m2"))); - assert_eq!((36, "( 2 * 3 ) + 5"), (line!(), stringify!((2*3) + 5))); + assert_eq!((36, "(2 * 3) + 5"), (line!(), stringify!((2*3) + 5))); } diff --git a/src/test/ui/macro_backtrace/main.stderr b/src/test/ui/macro_backtrace/main.stderr index 6f82d4040bce5..e7bd141ccd5ae 100644 --- a/src/test/ui/macro_backtrace/main.stderr +++ b/src/test/ui/macro_backtrace/main.stderr @@ -24,10 +24,10 @@ LL | ping!(); | ::: <::ping::ping macros>:1:1 | -LL | () => {pong ! () ; } - | -------------------- - | | | - | | in this macro invocation +LL | () => { pong ! () ; } + | --------------------- + | | | + | | in this macro invocation | in this expansion of `ping!` error: expected one of `!`, `.`, `::`, `;`, `?`, `{`, `}`, or an operator, found `error` @@ -44,34 +44,34 @@ LL | deep!(); | ::: <::ping::deep macros>:1:1 | -LL | () => {foo ! () ; } - | ------------------- - | | | - | | in this macro invocation (#2) +LL | () => { foo ! () ; } + | -------------------- + | | | + | | in this macro invocation (#2) | in this expansion of `deep!` (#1) | ::: <::ping::foo macros>:1:1 | -LL | () => {bar ! () ; } - | ------------------- - | | | - | | in this macro invocation (#3) +LL | () => { bar ! () ; } + | -------------------- + | | | + | | in this macro invocation (#3) | in this expansion of `foo!` (#2) | ::: <::ping::bar macros>:1:1 | -LL | () => {ping ! () ; } - | -------------------- - | | | - | | in this macro invocation (#4) +LL | () => { ping ! () ; } + | --------------------- + | | | + | | in this macro invocation (#4) | in this expansion of `bar!` (#3) | ::: <::ping::ping macros>:1:1 | -LL | () => {pong ! () ; } - | -------------------- - | | | - | | in this macro invocation (#5) +LL | () => { pong ! () ; } + | --------------------- + | | | + | | in this macro invocation (#5) | in this expansion of `ping!` (#4) error: aborting due to 3 previous errors diff --git a/src/test/ui/macros/trace-macro.stderr b/src/test/ui/macros/trace-macro.stderr index 545ed33cd10be..287f7b297d5c6 100644 --- a/src/test/ui/macros/trace-macro.stderr +++ b/src/test/ui/macros/trace-macro.stderr @@ -5,5 +5,5 @@ LL | println!("Hello, World!"); | ^^^^^^^^^^^^^^^^^^^^^^^^^^ | = note: expanding `println! { "Hello, World!" }` - = note: to `{$crate :: io :: _print (format_args_nl ! ("Hello, World!")) ; }` + = note: to `{ $crate :: io :: _print (format_args_nl ! ("Hello, World!")) ; }` diff --git a/src/test/ui/proc-macro/attribute-spans-preserved.stdout b/src/test/ui/proc-macro/attribute-spans-preserved.stdout index b2be35e825993..faf3171215665 100644 --- a/src/test/ui/proc-macro/attribute-spans-preserved.stdout +++ b/src/test/ui/proc-macro/attribute-spans-preserved.stdout @@ -1 +1 @@ -fn main () {let y : u32 = "z" ; {let x : u32 = "y" ; } } +fn main () { let y : u32 = "z" ; { let x : u32 = "y" ; } } From 0cdd18d0a7b9d3c52017b57eecda6e198e9512b1 Mon Sep 17 00:00:00 2001 From: Vadim Petrochenkov Date: Sun, 14 Jul 2019 16:09:39 +0300 Subject: [PATCH 8/8] pprust: Support `macro` macros --- src/libsyntax/print/pprust.rs | 31 ++++++++++++++----- src/test/pretty/macro.rs | 7 +++++ .../pretty-expanded-hygiene/input.pp.rs | 2 +- 3 files changed, 31 insertions(+), 9 deletions(-) create mode 100644 src/test/pretty/macro.rs diff --git a/src/libsyntax/print/pprust.rs b/src/libsyntax/print/pprust.rs index 47985351f7d33..16e0bace92584 100644 --- a/src/libsyntax/print/pprust.rs +++ b/src/libsyntax/print/pprust.rs @@ -20,6 +20,11 @@ use syntax_pos::{DUMMY_SP, FileName, Span}; use std::borrow::Cow; +pub enum MacHeader<'a> { + Path(&'a ast::Path), + Keyword(&'static str), +} + pub enum AnnNode<'a> { Ident(&'a ast::Ident), Name(&'a ast::Name), @@ -620,7 +625,7 @@ pub trait PrintState<'a>: std::ops::Deref + std::ops::DerefM match attr.tokens.trees().next() { Some(TokenTree::Delimited(_, delim, tts)) => { self.print_mac_common( - Some(&attr.path), false, None, delim, tts, true, attr.span + Some(MacHeader::Path(&attr.path)), false, None, delim, tts, true, attr.span ); } tree => { @@ -706,7 +711,7 @@ pub trait PrintState<'a>: std::ops::Deref + std::ops::DerefM fn print_mac_common( &mut self, - path: Option<&ast::Path>, + header: Option>, has_bang: bool, ident: Option, delim: DelimToken, @@ -717,8 +722,10 @@ pub trait PrintState<'a>: std::ops::Deref + std::ops::DerefM if delim == DelimToken::Brace { self.cbox(INDENT_UNIT); } - if let Some(path) = path { - self.print_path(path, false, 0); + match header { + Some(MacHeader::Path(path)) => self.print_path(path, false, 0), + Some(MacHeader::Keyword(kw)) => self.word(kw), + None => {} } if has_bang { self.word("!"); @@ -729,7 +736,7 @@ pub trait PrintState<'a>: std::ops::Deref + std::ops::DerefM } match delim { DelimToken::Brace => { - if path.is_some() || has_bang || ident.is_some() { + if header.is_some() || has_bang || ident.is_some() { self.nbsp(); } self.word("{"); @@ -1357,9 +1364,11 @@ impl<'a> State<'a> { } } ast::ItemKind::MacroDef(ref macro_def) => { + let (kw, has_bang) = + if macro_def.legacy { ("macro_rules", true) } else { ("macro", false) }; self.print_mac_common( - Some(&ast::Path::from_ident(ast::Ident::with_empty_ctxt(sym::macro_rules))), - true, + Some(MacHeader::Keyword(kw)), + has_bang, Some(item.ident), DelimToken::Brace, macro_def.stream(), @@ -1754,7 +1763,13 @@ impl<'a> State<'a> { crate fn print_mac(&mut self, m: &ast::Mac) { self.print_mac_common( - Some(&m.node.path), true, None, m.node.delim.to_token(), m.node.stream(), true, m.span + Some(MacHeader::Path(&m.node.path)), + true, + None, + m.node.delim.to_token(), + m.node.stream(), + true, + m.span, ); } diff --git a/src/test/pretty/macro.rs b/src/test/pretty/macro.rs new file mode 100644 index 0000000000000..39677d1dc2da3 --- /dev/null +++ b/src/test/pretty/macro.rs @@ -0,0 +1,7 @@ +// pp-exact + +#![feature(decl_macro)] + +macro mac { ($ arg : expr) => { $ arg + $ arg } } + +fn main() { } diff --git a/src/test/run-make-fulldeps/pretty-expanded-hygiene/input.pp.rs b/src/test/run-make-fulldeps/pretty-expanded-hygiene/input.pp.rs index 9d081c970dabc..570dece023dea 100644 --- a/src/test/run-make-fulldeps/pretty-expanded-hygiene/input.pp.rs +++ b/src/test/run-make-fulldeps/pretty-expanded-hygiene/input.pp.rs @@ -2,7 +2,7 @@ #![feature /* 0#0 */(no_core)] #![no_core /* 0#0 */] -macro_rules /* 0#0 */! foo /* 0#0 */ { ($ x : ident) => { y + $ x } } +macro_rules! foo /* 0#0 */ { ($ x : ident) => { y + $ x } } fn bar /* 0#0 */() { let x /* 0#0 */ = 1; y /* 0#1 */ + x /* 0#0 */ }