1
Fork 0

syntax: Remove redundant span from ast::Mac

Also remove a couple of redundant `visit_mac` asserts
This commit is contained in:
Vadim Petrochenkov 2019-12-01 15:55:32 +03:00
parent a81804b4d5
commit 0fac56717a
16 changed files with 34 additions and 56 deletions

View file

@ -927,7 +927,6 @@ impl<'a> Parser<'a> {
ex = ExprKind::Mac(Mac { ex = ExprKind::Mac(Mac {
path, path,
args, args,
span: lo.to(hi),
prior_type_ascription: self.last_type_ascription, prior_type_ascription: self.last_type_ascription,
}); });
} else if self.check(&token::OpenDelim(token::Brace)) { } else if self.check(&token::OpenDelim(token::Brace)) {

View file

@ -432,8 +432,6 @@ impl<'a> Parser<'a> {
let prev_span = self.prev_span; let prev_span = self.prev_span;
self.complain_if_pub_macro(&visibility.node, prev_span); self.complain_if_pub_macro(&visibility.node, prev_span);
let mac_lo = self.token.span;
// Item macro // Item macro
let path = self.parse_path(PathStyle::Mod)?; let path = self.parse_path(PathStyle::Mod)?;
self.expect(&token::Not)?; self.expect(&token::Not)?;
@ -446,7 +444,6 @@ impl<'a> Parser<'a> {
let mac = Mac { let mac = Mac {
path, path,
args, args,
span: mac_lo.to(hi),
prior_type_ascription: self.last_type_ascription, prior_type_ascription: self.last_type_ascription,
}; };
let item = let item =
@ -499,7 +496,6 @@ impl<'a> Parser<'a> {
if self.token.is_path_start() && if self.token.is_path_start() &&
!(self.is_async_fn() && self.token.span.rust_2015()) { !(self.is_async_fn() && self.token.span.rust_2015()) {
let prev_span = self.prev_span; let prev_span = self.prev_span;
let lo = self.token.span;
let path = self.parse_path(PathStyle::Mod)?; let path = self.parse_path(PathStyle::Mod)?;
if path.segments.len() == 1 { if path.segments.len() == 1 {
@ -525,7 +521,6 @@ impl<'a> Parser<'a> {
Ok(Some(Mac { Ok(Some(Mac {
path, path,
args, args,
span: lo.to(self.prev_span),
prior_type_ascription: self.last_type_ascription, prior_type_ascription: self.last_type_ascription,
})) }))
} else { } else {

View file

@ -338,7 +338,7 @@ impl<'a> Parser<'a> {
(None, self.parse_path(PathStyle::Expr)?) (None, self.parse_path(PathStyle::Expr)?)
}; };
match self.token.kind { 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 => { token::DotDotDot | token::DotDotEq | token::DotDot => {
self.parse_pat_range_starting_with_path(lo, qself, path)? self.parse_pat_range_starting_with_path(lo, qself, path)?
} }
@ -593,13 +593,12 @@ impl<'a> Parser<'a> {
} }
/// Parse macro invocation /// 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(); self.bump();
let args = self.parse_mac_args()?; let args = self.parse_mac_args()?;
let mac = Mac { let mac = Mac {
path, path,
args, args,
span: lo.to(self.prev_span),
prior_type_ascription: self.last_type_ascription, prior_type_ascription: self.last_type_ascription,
}; };
Ok(PatKind::Mac(mac)) Ok(PatKind::Mac(mac))

View file

@ -106,7 +106,6 @@ impl<'a> Parser<'a> {
let mac = Mac { let mac = Mac {
path, path,
args, args,
span: lo.to(hi),
prior_type_ascription: self.last_type_ascription, prior_type_ascription: self.last_type_ascription,
}; };
let kind = if delim == token::Brace || let kind = if delim == token::Brace ||
@ -130,7 +129,7 @@ impl<'a> Parser<'a> {
self.warn_missing_semicolon(); self.warn_missing_semicolon();
StmtKind::Mac(P((mac, style, attrs.into()))) StmtKind::Mac(P((mac, style, attrs.into())))
} else { } 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.maybe_recover_from_bad_qpath(e, true)?;
let e = self.parse_dot_or_call_expr_with(e, lo, attrs.into())?; let e = self.parse_dot_or_call_expr_with(e, lo, attrs.into())?;
let e = self.parse_assoc_expr_with(0, LhsExpr::AlreadyParsed(e))?; let e = self.parse_assoc_expr_with(0, LhsExpr::AlreadyParsed(e))?;

View file

@ -181,7 +181,6 @@ impl<'a> Parser<'a> {
let mac = Mac { let mac = Mac {
path, path,
args, args,
span: lo.to(self.prev_span),
prior_type_ascription: self.last_type_ascription, prior_type_ascription: self.last_type_ascription,
}; };
TyKind::Mac(mac) TyKind::Mac(mac)

View file

@ -737,14 +737,6 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
|this| visit::walk_enum_def(this, enum_definition, generics, item_id)) |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) { fn visit_impl_item(&mut self, ii: &'a ImplItem) {
if let ImplItemKind::Method(ref sig, _) = ii.kind { if let ImplItemKind::Method(ref sig, _) = ii.kind {
self.check_fn_decl(&sig.decl); self.check_fn_decl(&sig.decl);

View file

@ -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) { fn visit_pat(&mut self, p: &'l ast::Pat) {
self.process_macro_use(p.span); self.process_macro_use(p.span);
self.process_pat(p); self.process_pat(p);

View file

@ -1379,10 +1379,15 @@ pub enum Movability {
pub struct Mac { pub struct Mac {
pub path: Path, pub path: Path,
pub args: P<MacArgs>, pub args: P<MacArgs>,
pub span: Span,
pub prior_type_ascription: Option<(Span, bool)>, 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. /// Arguments passed to an attribute or a function-like macro.
#[derive(Clone, RustcEncodable, RustcDecodable, Debug)] #[derive(Clone, RustcEncodable, RustcDecodable, Debug)]
pub enum MacArgs { pub enum MacArgs {
@ -1403,6 +1408,14 @@ impl MacArgs {
} }
} }
pub fn span(&self) -> Option<Span> {
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 `=`. /// Tokens inside the delimiters or after `=`.
/// Proc macros see these tokens, for example. /// Proc macros see these tokens, for example.
pub fn inner_tokens(&self) -> TokenStream { 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)] #[derive(Copy, Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Debug)]
pub enum MacDelimiter { pub enum MacDelimiter {
Parenthesis, Parenthesis,

View file

@ -580,10 +580,9 @@ pub fn noop_visit_attribute<T: MutVisitor>(attr: &mut Attribute, vis: &mut T) {
} }
pub fn noop_visit_mac<T: MutVisitor>(mac: &mut Mac, vis: &mut T) { pub fn noop_visit_mac<T: MutVisitor>(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); vis.visit_path(path);
visit_mac_args(args, vis); visit_mac_args(args, vis);
vis.visit_span(span);
} }
pub fn noop_visit_macro_def<T: MutVisitor>(macro_def: &mut MacroDef, vis: &mut T) { pub fn noop_visit_macro_def<T: MutVisitor>(macro_def: &mut MacroDef, vis: &mut T) {

View file

@ -1772,9 +1772,9 @@ impl<'a> State<'a> {
true, true,
None, None,
m.args.delim(), m.args.delim(),
m.stream(), m.args.inner_tokens(),
true, true,
m.span, m.span(),
); );
} }

View file

@ -225,6 +225,14 @@ impl TokenStream {
self.0.len() self.0.len()
} }
pub fn span(&self) -> Option<Span> {
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 { pub fn from_streams(mut streams: SmallVec<[TokenStream; 2]>) -> TokenStream {
match streams.len() { match streams.len() {
0 => TokenStream::default(), 0 => TokenStream::default(),

View file

@ -597,13 +597,13 @@ impl<'a, 'b> MacroExpander<'a, 'b> {
InvocationKind::Bang { mac, .. } => match ext { InvocationKind::Bang { mac, .. } => match ext {
SyntaxExtensionKind::Bang(expander) => { SyntaxExtensionKind::Bang(expander) => {
self.gate_proc_macro_expansion_kind(span, fragment_kind); 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) self.parse_ast_fragment(tok_result, fragment_kind, &mac.path, span)
} }
SyntaxExtensionKind::LegacyBang(expander) => { SyntaxExtensionKind::LegacyBang(expander) => {
let prev = self.cx.current_expansion.prior_type_ascription; let prev = self.cx.current_expansion.prior_type_ascription;
self.cx.current_expansion.prior_type_ascription = mac.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) { let result = if let Some(result) = fragment_kind.make_from(tok_result) {
result result
} else { } else {

View file

@ -272,7 +272,7 @@ fn ttdelim_span() {
"foo!( fn main() { body } )".to_string(), &sess).unwrap(); "foo!( fn main() { body } )".to_string(), &sess).unwrap();
let tts: Vec<_> = match expr.kind { 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"), _ => panic!("not a macro"),
}; };

View file

@ -17,7 +17,6 @@ pub fn placeholder(kind: AstFragmentKind, id: ast::NodeId, vis: Option<ast::Visi
ast::Mac { ast::Mac {
path: ast::Path { span: DUMMY_SP, segments: Vec::new() }, path: ast::Path { span: DUMMY_SP, segments: Vec::new() },
args: P(ast::MacArgs::Empty), args: P(ast::MacArgs::Empty),
span: DUMMY_SP,
prior_type_ascription: None, prior_type_ascription: None,
} }
} }

View file

@ -39,7 +39,6 @@ pub fn expand_assert<'cx>(
let panic_call = Mac { let panic_call = Mac {
path: Path::from_ident(Ident::new(sym::panic, sp)), path: Path::from_ident(Ident::new(sym::panic, sp)),
args, args,
span: sp,
prior_type_ascription: None, prior_type_ascription: None,
}; };
let if_expr = cx.expr_if( let if_expr = cx.expr_if(

View file

@ -340,14 +340,12 @@ pub fn combine_substructure(f: CombineSubstructureFunc<'_>)
fn find_type_parameters( fn find_type_parameters(
ty: &ast::Ty, ty: &ast::Ty,
ty_param_names: &[ast::Name], ty_param_names: &[ast::Name],
span: Span,
cx: &ExtCtxt<'_>, cx: &ExtCtxt<'_>,
) -> Vec<P<ast::Ty>> { ) -> Vec<P<ast::Ty>> {
use syntax::visit; use syntax::visit;
struct Visitor<'a, 'b> { struct Visitor<'a, 'b> {
cx: &'a ExtCtxt<'b>, cx: &'a ExtCtxt<'b>,
span: Span,
ty_param_names: &'a [ast::Name], ty_param_names: &'a [ast::Name],
types: Vec<P<ast::Ty>>, types: Vec<P<ast::Ty>>,
} }
@ -366,18 +364,11 @@ fn find_type_parameters(
} }
fn visit_mac(&mut self, mac: &ast::Mac) { fn visit_mac(&mut self, mac: &ast::Mac) {
let span = mac.span.with_ctxt(self.span.ctxt()); self.cx.span_err(mac.span(), "`derive` cannot be used on items with type macros");
self.cx.span_err(span, "`derive` cannot be used on items with type macros");
} }
} }
let mut visitor = Visitor { let mut visitor = Visitor { cx, ty_param_names, types: Vec::new() };
ty_param_names,
types: Vec::new(),
span,
cx,
};
visit::Visitor::visit_ty(&mut visitor, ty); visit::Visitor::visit_ty(&mut visitor, ty);
visitor.types visitor.types
@ -605,7 +596,7 @@ impl<'a> TraitDef<'a> {
.collect(); .collect();
for field_ty in field_tys { 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 { for ty in tys {
// if we have already handled this type, skip it // if we have already handled this type, skip it