From 0fac56717a1bce4e362d91d8f4e71d65676d49a3 Mon Sep 17 00:00:00 2001 From: Vadim Petrochenkov Date: Sun, 1 Dec 2019 15:55:32 +0300 Subject: [PATCH] syntax: Remove redundant span from `ast::Mac` Also remove a couple of redundant `visit_mac` asserts --- src/librustc_parse/parser/expr.rs | 1 - src/librustc_parse/parser/item.rs | 5 ----- src/librustc_parse/parser/pat.rs | 5 ++--- src/librustc_parse/parser/stmt.rs | 3 +-- src/librustc_parse/parser/ty.rs | 1 - src/librustc_passes/ast_validation.rs | 8 -------- src/librustc_save_analysis/dump_visitor.rs | 8 -------- src/libsyntax/ast.rs | 21 ++++++++++++++------- src/libsyntax/mut_visit.rs | 3 +-- src/libsyntax/print/pprust.rs | 4 ++-- src/libsyntax/tokenstream.rs | 8 ++++++++ src/libsyntax_expand/expand.rs | 4 ++-- src/libsyntax_expand/parse/tests.rs | 2 +- src/libsyntax_expand/placeholders.rs | 1 - src/libsyntax_ext/assert.rs | 1 - src/libsyntax_ext/deriving/generic/mod.rs | 15 +++------------ 16 files changed, 34 insertions(+), 56 deletions(-) diff --git a/src/librustc_parse/parser/expr.rs b/src/librustc_parse/parser/expr.rs index a6629aef1eeff..1112274dc46a5 100644 --- a/src/librustc_parse/parser/expr.rs +++ b/src/librustc_parse/parser/expr.rs @@ -927,7 +927,6 @@ impl<'a> Parser<'a> { ex = ExprKind::Mac(Mac { path, args, - span: lo.to(hi), prior_type_ascription: self.last_type_ascription, }); } else if self.check(&token::OpenDelim(token::Brace)) { diff --git a/src/librustc_parse/parser/item.rs b/src/librustc_parse/parser/item.rs index 9bf5ae3cc5af0..9f3f6414b3ea1 100644 --- a/src/librustc_parse/parser/item.rs +++ b/src/librustc_parse/parser/item.rs @@ -432,8 +432,6 @@ impl<'a> Parser<'a> { let prev_span = self.prev_span; self.complain_if_pub_macro(&visibility.node, prev_span); - let mac_lo = self.token.span; - // Item macro let path = self.parse_path(PathStyle::Mod)?; self.expect(&token::Not)?; @@ -446,7 +444,6 @@ impl<'a> Parser<'a> { let mac = Mac { path, args, - span: mac_lo.to(hi), prior_type_ascription: self.last_type_ascription, }; let item = @@ -499,7 +496,6 @@ impl<'a> Parser<'a> { if self.token.is_path_start() && !(self.is_async_fn() && self.token.span.rust_2015()) { let prev_span = self.prev_span; - let lo = self.token.span; let path = self.parse_path(PathStyle::Mod)?; if path.segments.len() == 1 { @@ -525,7 +521,6 @@ impl<'a> Parser<'a> { Ok(Some(Mac { path, args, - span: lo.to(self.prev_span), prior_type_ascription: self.last_type_ascription, })) } else { diff --git a/src/librustc_parse/parser/pat.rs b/src/librustc_parse/parser/pat.rs index c16b5f5574afc..1127c4b2d5f88 100644 --- a/src/librustc_parse/parser/pat.rs +++ b/src/librustc_parse/parser/pat.rs @@ -338,7 +338,7 @@ impl<'a> Parser<'a> { (None, self.parse_path(PathStyle::Expr)?) }; match self.token.kind { - token::Not if qself.is_none() => self.parse_pat_mac_invoc(lo, path)?, + token::Not if qself.is_none() => self.parse_pat_mac_invoc(path)?, token::DotDotDot | token::DotDotEq | token::DotDot => { self.parse_pat_range_starting_with_path(lo, qself, path)? } @@ -593,13 +593,12 @@ impl<'a> Parser<'a> { } /// Parse macro invocation - fn parse_pat_mac_invoc(&mut self, lo: Span, path: Path) -> PResult<'a, PatKind> { + fn parse_pat_mac_invoc(&mut self, path: Path) -> PResult<'a, PatKind> { self.bump(); let args = self.parse_mac_args()?; let mac = Mac { path, args, - span: lo.to(self.prev_span), prior_type_ascription: self.last_type_ascription, }; Ok(PatKind::Mac(mac)) diff --git a/src/librustc_parse/parser/stmt.rs b/src/librustc_parse/parser/stmt.rs index 68c85ad8abfb3..b952e8814a361 100644 --- a/src/librustc_parse/parser/stmt.rs +++ b/src/librustc_parse/parser/stmt.rs @@ -106,7 +106,6 @@ impl<'a> Parser<'a> { let mac = Mac { path, args, - span: lo.to(hi), prior_type_ascription: self.last_type_ascription, }; let kind = if delim == token::Brace || @@ -130,7 +129,7 @@ impl<'a> Parser<'a> { self.warn_missing_semicolon(); StmtKind::Mac(P((mac, style, attrs.into()))) } else { - let e = self.mk_expr(mac.span, ExprKind::Mac(mac), ThinVec::new()); + let e = self.mk_expr(lo.to(hi), ExprKind::Mac(mac), ThinVec::new()); let e = self.maybe_recover_from_bad_qpath(e, true)?; let e = self.parse_dot_or_call_expr_with(e, lo, attrs.into())?; let e = self.parse_assoc_expr_with(0, LhsExpr::AlreadyParsed(e))?; diff --git a/src/librustc_parse/parser/ty.rs b/src/librustc_parse/parser/ty.rs index 802bef525dbaa..321427969051c 100644 --- a/src/librustc_parse/parser/ty.rs +++ b/src/librustc_parse/parser/ty.rs @@ -181,7 +181,6 @@ impl<'a> Parser<'a> { let mac = Mac { path, args, - span: lo.to(self.prev_span), prior_type_ascription: self.last_type_ascription, }; TyKind::Mac(mac) diff --git a/src/librustc_passes/ast_validation.rs b/src/librustc_passes/ast_validation.rs index 5a29a56ad5472..29cfee8408f30 100644 --- a/src/librustc_passes/ast_validation.rs +++ b/src/librustc_passes/ast_validation.rs @@ -737,14 +737,6 @@ impl<'a> Visitor<'a> for AstValidator<'a> { |this| visit::walk_enum_def(this, enum_definition, generics, item_id)) } - fn visit_mac(&mut self, mac: &Mac) { - // when a new macro kind is added but the author forgets to set it up for expansion - // because that's the only part that won't cause a compiler error - self.session.diagnostic() - .span_bug(mac.span, "macro invocation missed in expansion; did you forget to override \ - the relevant `fold_*()` method in `PlaceholderExpander`?"); - } - fn visit_impl_item(&mut self, ii: &'a ImplItem) { if let ImplItemKind::Method(ref sig, _) = ii.kind { self.check_fn_decl(&sig.decl); diff --git a/src/librustc_save_analysis/dump_visitor.rs b/src/librustc_save_analysis/dump_visitor.rs index 5bec5b5eb6bfd..396d948433961 100644 --- a/src/librustc_save_analysis/dump_visitor.rs +++ b/src/librustc_save_analysis/dump_visitor.rs @@ -1515,14 +1515,6 @@ impl<'l, 'tcx> Visitor<'l> for DumpVisitor<'l, 'tcx> { } } - fn visit_mac(&mut self, mac: &'l ast::Mac) { - // These shouldn't exist in the AST at this point, log a span bug. - span_bug!( - mac.span, - "macro invocation should have been expanded out of AST" - ); - } - fn visit_pat(&mut self, p: &'l ast::Pat) { self.process_macro_use(p.span); self.process_pat(p); diff --git a/src/libsyntax/ast.rs b/src/libsyntax/ast.rs index 21126f8301a2a..c537d43a4d657 100644 --- a/src/libsyntax/ast.rs +++ b/src/libsyntax/ast.rs @@ -1379,10 +1379,15 @@ pub enum Movability { pub struct Mac { pub path: Path, pub args: P, - pub span: Span, pub prior_type_ascription: Option<(Span, bool)>, } +impl Mac { + pub fn span(&self) -> Span { + self.path.span.to(self.args.span().unwrap_or(self.path.span)) + } +} + /// Arguments passed to an attribute or a function-like macro. #[derive(Clone, RustcEncodable, RustcDecodable, Debug)] pub enum MacArgs { @@ -1403,6 +1408,14 @@ impl MacArgs { } } + pub fn span(&self) -> Option { + match *self { + MacArgs::Empty => None, + MacArgs::Delimited(dspan, ..) => Some(dspan.entire()), + MacArgs::Eq(eq_span, ref tokens) => Some(eq_span.to(tokens.span().unwrap_or(eq_span))), + } + } + /// Tokens inside the delimiters or after `=`. /// Proc macros see these tokens, for example. pub fn inner_tokens(&self) -> TokenStream { @@ -1432,12 +1445,6 @@ impl MacArgs { } } -impl Mac { - pub fn stream(&self) -> TokenStream { - self.args.inner_tokens() - } -} - #[derive(Copy, Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Debug)] pub enum MacDelimiter { Parenthesis, diff --git a/src/libsyntax/mut_visit.rs b/src/libsyntax/mut_visit.rs index 7c86fc5cba599..2651c46773454 100644 --- a/src/libsyntax/mut_visit.rs +++ b/src/libsyntax/mut_visit.rs @@ -580,10 +580,9 @@ pub fn noop_visit_attribute(attr: &mut Attribute, vis: &mut T) { } pub fn noop_visit_mac(mac: &mut Mac, vis: &mut T) { - let Mac { path, args, span, prior_type_ascription: _ } = mac; + let Mac { path, args, prior_type_ascription: _ } = mac; vis.visit_path(path); visit_mac_args(args, vis); - vis.visit_span(span); } pub fn noop_visit_macro_def(macro_def: &mut MacroDef, vis: &mut T) { diff --git a/src/libsyntax/print/pprust.rs b/src/libsyntax/print/pprust.rs index 416704e255eac..cb68fe8f4ff88 100644 --- a/src/libsyntax/print/pprust.rs +++ b/src/libsyntax/print/pprust.rs @@ -1772,9 +1772,9 @@ impl<'a> State<'a> { true, None, m.args.delim(), - m.stream(), + m.args.inner_tokens(), true, - m.span, + m.span(), ); } diff --git a/src/libsyntax/tokenstream.rs b/src/libsyntax/tokenstream.rs index 6a0523dd655b8..491b9a9ade47a 100644 --- a/src/libsyntax/tokenstream.rs +++ b/src/libsyntax/tokenstream.rs @@ -225,6 +225,14 @@ impl TokenStream { self.0.len() } + pub fn span(&self) -> Option { + match &**self.0 { + [] => None, + [(tt, _)] => Some(tt.span()), + [(tt_start, _), .., (tt_end, _)] => Some(tt_start.span().to(tt_end.span())), + } + } + pub fn from_streams(mut streams: SmallVec<[TokenStream; 2]>) -> TokenStream { match streams.len() { 0 => TokenStream::default(), diff --git a/src/libsyntax_expand/expand.rs b/src/libsyntax_expand/expand.rs index a6ced1439c5d9..f1071cea9ab76 100644 --- a/src/libsyntax_expand/expand.rs +++ b/src/libsyntax_expand/expand.rs @@ -597,13 +597,13 @@ impl<'a, 'b> MacroExpander<'a, 'b> { InvocationKind::Bang { mac, .. } => match ext { SyntaxExtensionKind::Bang(expander) => { self.gate_proc_macro_expansion_kind(span, fragment_kind); - let tok_result = expander.expand(self.cx, span, mac.stream()); + let tok_result = expander.expand(self.cx, span, mac.args.inner_tokens()); self.parse_ast_fragment(tok_result, fragment_kind, &mac.path, span) } SyntaxExtensionKind::LegacyBang(expander) => { let prev = self.cx.current_expansion.prior_type_ascription; self.cx.current_expansion.prior_type_ascription = mac.prior_type_ascription; - let tok_result = expander.expand(self.cx, span, mac.stream()); + let tok_result = expander.expand(self.cx, span, mac.args.inner_tokens()); let result = if let Some(result) = fragment_kind.make_from(tok_result) { result } else { diff --git a/src/libsyntax_expand/parse/tests.rs b/src/libsyntax_expand/parse/tests.rs index 08950ddefbaee..30e83c151e255 100644 --- a/src/libsyntax_expand/parse/tests.rs +++ b/src/libsyntax_expand/parse/tests.rs @@ -272,7 +272,7 @@ fn ttdelim_span() { "foo!( fn main() { body } )".to_string(), &sess).unwrap(); let tts: Vec<_> = match expr.kind { - ast::ExprKind::Mac(ref mac) => mac.stream().trees().collect(), + ast::ExprKind::Mac(ref mac) => mac.args.inner_tokens().trees().collect(), _ => panic!("not a macro"), }; diff --git a/src/libsyntax_expand/placeholders.rs b/src/libsyntax_expand/placeholders.rs index 11fe860fc8102..74ade1de20e2a 100644 --- a/src/libsyntax_expand/placeholders.rs +++ b/src/libsyntax_expand/placeholders.rs @@ -17,7 +17,6 @@ pub fn placeholder(kind: AstFragmentKind, id: ast::NodeId, vis: Option( let panic_call = Mac { path: Path::from_ident(Ident::new(sym::panic, sp)), args, - span: sp, prior_type_ascription: None, }; let if_expr = cx.expr_if( diff --git a/src/libsyntax_ext/deriving/generic/mod.rs b/src/libsyntax_ext/deriving/generic/mod.rs index b6bf2f881616f..5bd84b43a7801 100644 --- a/src/libsyntax_ext/deriving/generic/mod.rs +++ b/src/libsyntax_ext/deriving/generic/mod.rs @@ -340,14 +340,12 @@ pub fn combine_substructure(f: CombineSubstructureFunc<'_>) fn find_type_parameters( ty: &ast::Ty, ty_param_names: &[ast::Name], - span: Span, cx: &ExtCtxt<'_>, ) -> Vec> { use syntax::visit; struct Visitor<'a, 'b> { cx: &'a ExtCtxt<'b>, - span: Span, ty_param_names: &'a [ast::Name], types: Vec>, } @@ -366,18 +364,11 @@ fn find_type_parameters( } fn visit_mac(&mut self, mac: &ast::Mac) { - let span = mac.span.with_ctxt(self.span.ctxt()); - self.cx.span_err(span, "`derive` cannot be used on items with type macros"); + self.cx.span_err(mac.span(), "`derive` cannot be used on items with type macros"); } } - let mut visitor = Visitor { - ty_param_names, - types: Vec::new(), - span, - cx, - }; - + let mut visitor = Visitor { cx, ty_param_names, types: Vec::new() }; visit::Visitor::visit_ty(&mut visitor, ty); visitor.types @@ -605,7 +596,7 @@ impl<'a> TraitDef<'a> { .collect(); for field_ty in field_tys { - let tys = find_type_parameters(&field_ty, &ty_param_names, self.span, cx); + let tys = find_type_parameters(&field_ty, &ty_param_names, cx); for ty in tys { // if we have already handled this type, skip it