X-Git-Url: https://git.lizzy.rs/?a=blobdiff_plain;ds=sidebyside;f=src%2Fmacros.rs;h=190f4b599b0f7053654b3ac355183907a9bd33a8;hb=612e8d5b9be72713a081370c85cc5bed30b6fae6;hp=400daa6a5aa65c71b9d3194a68ade8a1747397aa;hpb=b173b42354ec8d77d1613de383c9a0ea801f9a9d;p=rust.git diff --git a/src/macros.rs b/src/macros.rs index 400daa6a5aa..190f4b599b0 100644 --- a/src/macros.rs +++ b/src/macros.rs @@ -13,11 +13,15 @@ use std::panic::{catch_unwind, AssertUnwindSafe}; use rustc_ast::token::{BinOpToken, DelimToken, Token, TokenKind}; -use rustc_ast::tokenstream::{Cursor, TokenStream, TokenTree}; +use rustc_ast::tokenstream::{Cursor, LazyTokenStream, TokenStream, TokenTree}; use rustc_ast::{ast, ptr}; use rustc_ast_pretty::pprust; -use rustc_parse::{new_parser_from_tts, parser::Parser}; -use rustc_span::{symbol::kw, BytePos, Span, Symbol, DUMMY_SP}; +use rustc_parse::parser::{ForceCollect, Parser}; +use rustc_parse::{stream_to_parser, MACRO_ARGUMENTS}; +use rustc_span::{ + symbol::{self, kw}, + BytePos, Span, Symbol, DUMMY_SP, +}; use crate::comment::{ contains_comment, CharClasses, FindUncommented, FullCodeCharKind, LineClasses, @@ -52,7 +56,7 @@ pub(crate) enum MacroArg { Ty(ptr::P), Pat(ptr::P), Item(ptr::P), - Keyword(ast::Ident, Span), + Keyword(symbol::Ident, Span), } impl MacroArg { @@ -86,6 +90,14 @@ fn rewrite(&self, context: &RewriteContext<'_>, shape: Shape) -> Option } } +fn build_parser<'a>(context: &RewriteContext<'a>, cursor: Cursor) -> Parser<'a> { + stream_to_parser( + context.parse_sess.inner(), + cursor.collect(), + MACRO_ARGUMENTS, + ) +} + fn parse_macro_arg<'a, 'b: 'a>(parser: &'a mut Parser<'b>) -> Option { macro_rules! parse_macro_arg { ($macro_arg:ident, $parser:expr, $f:expr) => { @@ -120,13 +132,13 @@ macro_rules! parse_macro_arg { ); parse_macro_arg!( Pat, - |parser: &mut rustc_parse::parser::Parser<'b>| parser.parse_pat(None), + |parser: &mut rustc_parse::parser::Parser<'b>| parser.parse_pat_no_top_alt(None), |x: ptr::P| Some(x) ); // `parse_item` returns `Option>`. parse_macro_arg!( Item, - |parser: &mut rustc_parse::parser::Parser<'b>| parser.parse_item(), + |parser: &mut rustc_parse::parser::Parser<'b>| parser.parse_item(ForceCollect::No), |x: Option>| x ); @@ -137,7 +149,7 @@ macro_rules! parse_macro_arg { fn rewrite_macro_name( context: &RewriteContext<'_>, path: &ast::Path, - extra_ident: Option, + extra_ident: Option, ) -> String { let name = if path.segments.len() == 1 { // Avoid using pretty-printer in the common case. @@ -146,7 +158,7 @@ fn rewrite_macro_name( format!("{}!", pprust::path_to_string(path)) }; match extra_ident { - Some(ident) if ident.name != kw::Invalid => format!("{} {}", name, ident), + Some(ident) if ident.name != kw::Empty => format!("{} {}", name, ident), _ => name, } } @@ -188,7 +200,7 @@ fn return_macro_parse_failure_fallback( pub(crate) fn rewrite_macro( mac: &ast::MacCall, - extra_ident: Option, + extra_ident: Option, context: &RewriteContext<'_>, shape: Shape, position: MacroPosition, @@ -230,9 +242,10 @@ fn check_keyword<'a, 'b: 'a>(parser: &'a mut Parser<'b>) -> Option { }) { parser.bump(); - let macro_arg = - MacroArg::Keyword(ast::Ident::with_dummy_span(keyword), parser.prev_token.span); - return Some(macro_arg); + return Some(MacroArg::Keyword( + symbol::Ident::with_dummy_span(keyword), + parser.prev_token.span, + )); } } None @@ -240,7 +253,7 @@ fn check_keyword<'a, 'b: 'a>(parser: &'a mut Parser<'b>) -> Option { fn rewrite_macro_inner( mac: &ast::MacCall, - extra_ident: Option, + extra_ident: Option, context: &RewriteContext<'_>, shape: Shape, position: MacroPosition, @@ -286,7 +299,7 @@ fn rewrite_macro_inner( } } - let mut parser = new_parser_from_tts(context.parse_sess.inner(), ts.trees().collect()); + let mut parser = build_parser(context, ts.trees()); let mut arg_vec = Vec::new(); let mut vec_with_semi = false; let mut trailing_comma = false; @@ -479,7 +492,7 @@ pub(crate) fn rewrite_macro_def( shape: Shape, indent: Indent, def: &ast::MacroDef, - ident: ast::Ident, + ident: symbol::Ident, vis: &ast::Visibility, span: Span, ) -> Option { @@ -620,7 +633,7 @@ fn replace_names(input: &str) -> Option<(String, HashMap)> { #[derive(Debug, Clone)] enum MacroArgKind { /// e.g., `$x: expr`. - MetaVariable(ast::Name, String), + MetaVariable(Symbol, String), /// e.g., `$($foo: expr),*` Repeat( /// `()`, `[]` or `{}`. @@ -1174,8 +1187,7 @@ fn next_space(tok: &TokenKind) -> SpaceState { | TokenKind::Pound | TokenKind::Dollar | TokenKind::OpenDelim(_) - | TokenKind::CloseDelim(_) - | TokenKind::Whitespace => SpaceState::Never, + | TokenKind::CloseDelim(_) => SpaceState::Never, TokenKind::Literal(..) | TokenKind::Ident(..) | TokenKind::Lifetime(_) => SpaceState::Ident, @@ -1193,13 +1205,14 @@ pub(crate) fn convert_try_mac( let path = &pprust::path_to_string(&mac.path); if path == "try" || path == "r#try" { let ts = mac.args.inner_tokens(); - let mut parser = new_parser_from_tts(context.parse_sess.inner(), ts.trees().collect()); + let mut parser = build_parser(context, ts.trees()); Some(ast::Expr { id: ast::NodeId::root(), // dummy value kind: ast::ExprKind::Try(parser.parse_expr().ok()?), span: mac.span(), // incorrect span, but shouldn't matter too much attrs: ast::AttrVec::new(), + tokens: Some(LazyTokenStream::new(ts)), }) } else { None @@ -1270,8 +1283,8 @@ fn parse_branch(&mut self) -> Option { span, })) = self.toks.look_ahead(0) { - self.toks.next(); hi = span.hi(); + self.toks.next(); } Some(MacroBranch { span: mk_sp(lo, hi), @@ -1348,12 +1361,12 @@ fn rewrite( config.set().max_width(new_width); // First try to format as items, then as statements. - let new_body_snippet = match crate::format_snippet(&body_str, &config) { + let new_body_snippet = match crate::format_snippet(&body_str, &config, true) { Some(new_body) => new_body, None => { let new_width = new_width + config.tab_spaces(); config.set().max_width(new_width); - match crate::format_code_block(&body_str, &config) { + match crate::format_code_block(&body_str, &config, true) { Some(new_body) => new_body, None => return None, } @@ -1425,7 +1438,7 @@ fn format_lazy_static( ts: &TokenStream, ) -> Option { let mut result = String::with_capacity(1024); - let mut parser = new_parser_from_tts(context.parse_sess.inner(), ts.trees().collect()); + let mut parser = build_parser(context, ts.trees()); let nested_shape = shape .block_indent(context.config.tab_spaces()) .with_max_width(context.config);