From 0fac56717a1bce4e362d91d8f4e71d65676d49a3 Mon Sep 17 00:00:00 2001 From: Vadim Petrochenkov Date: Sun, 1 Dec 2019 15:55:32 +0300 Subject: syntax: Remove redundant span from `ast::Mac` Also remove a couple of redundant `visit_mac` asserts --- src/libsyntax/ast.rs | 21 ++++++++++++++------- src/libsyntax/mut_visit.rs | 3 +-- src/libsyntax/print/pprust.rs | 4 ++-- src/libsyntax/tokenstream.rs | 8 ++++++++ 4 files changed, 25 insertions(+), 11 deletions(-) (limited to 'src/libsyntax') diff --git a/src/libsyntax/ast.rs b/src/libsyntax/ast.rs index 21126f8301a..c537d43a4d6 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 7c86fc5cba5..2651c467734 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 416704e255e..cb68fe8f4ff 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 6a0523dd655..491b9a9ade4 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(), -- cgit 1.4.1-3-g733a5