Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
syntax: Remove redundant span from ast::Mac
Also remove a couple of redundant `visit_mac` asserts
  • Loading branch information
petrochenkov committed Dec 2, 2019
1 parent a81804b commit 0fac567
Show file tree
Hide file tree
Showing 16 changed files with 34 additions and 56 deletions.
1 change: 0 additions & 1 deletion src/librustc_parse/parser/expr.rs
Expand Up @@ -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)) {
Expand Down
5 changes: 0 additions & 5 deletions src/librustc_parse/parser/item.rs
Expand Up @@ -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)?;
Expand All @@ -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 =
Expand Down Expand Up @@ -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 {
Expand All @@ -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 {
Expand Down
5 changes: 2 additions & 3 deletions src/librustc_parse/parser/pat.rs
Expand Up @@ -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)?
}
Expand Down Expand Up @@ -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))
Expand Down
3 changes: 1 addition & 2 deletions src/librustc_parse/parser/stmt.rs
Expand Up @@ -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 ||
Expand All @@ -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))?;
Expand Down
1 change: 0 additions & 1 deletion src/librustc_parse/parser/ty.rs
Expand Up @@ -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)
Expand Down
8 changes: 0 additions & 8 deletions src/librustc_passes/ast_validation.rs
Expand Up @@ -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);
Expand Down
8 changes: 0 additions & 8 deletions src/librustc_save_analysis/dump_visitor.rs
Expand Up @@ -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);
Expand Down
21 changes: 14 additions & 7 deletions src/libsyntax/ast.rs
Expand Up @@ -1379,10 +1379,15 @@ pub enum Movability {
pub struct Mac {
pub path: Path,
pub args: P<MacArgs>,
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 {
Expand All @@ -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 `=`.
/// Proc macros see these tokens, for example.
pub fn inner_tokens(&self) -> TokenStream {
Expand Down Expand Up @@ -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,
Expand Down
3 changes: 1 addition & 2 deletions src/libsyntax/mut_visit.rs
Expand Up @@ -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) {
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<T: MutVisitor>(macro_def: &mut MacroDef, vis: &mut T) {
Expand Down
4 changes: 2 additions & 2 deletions src/libsyntax/print/pprust.rs
Expand Up @@ -1772,9 +1772,9 @@ impl<'a> State<'a> {
true,
None,
m.args.delim(),
m.stream(),
m.args.inner_tokens(),
true,
m.span,
m.span(),
);
}

Expand Down
8 changes: 8 additions & 0 deletions src/libsyntax/tokenstream.rs
Expand Up @@ -225,6 +225,14 @@ impl TokenStream {
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 {
match streams.len() {
0 => TokenStream::default(),
Expand Down
4 changes: 2 additions & 2 deletions src/libsyntax_expand/expand.rs
Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion src/libsyntax_expand/parse/tests.rs
Expand Up @@ -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"),
};

Expand Down
1 change: 0 additions & 1 deletion src/libsyntax_expand/placeholders.rs
Expand Up @@ -17,7 +17,6 @@ pub fn placeholder(kind: AstFragmentKind, id: ast::NodeId, vis: Option<ast::Visi
ast::Mac {
path: ast::Path { span: DUMMY_SP, segments: Vec::new() },
args: P(ast::MacArgs::Empty),
span: DUMMY_SP,
prior_type_ascription: None,
}
}
Expand Down
1 change: 0 additions & 1 deletion src/libsyntax_ext/assert.rs
Expand Up @@ -39,7 +39,6 @@ pub fn expand_assert<'cx>(
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(
Expand Down
15 changes: 3 additions & 12 deletions src/libsyntax_ext/deriving/generic/mod.rs
Expand Up @@ -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<P<ast::Ty>> {
use syntax::visit;

struct Visitor<'a, 'b> {
cx: &'a ExtCtxt<'b>,
span: Span,
ty_param_names: &'a [ast::Name],
types: Vec<P<ast::Ty>>,
}
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 0fac567

Please sign in to comment.