Skip to content

Commit

Permalink
Remove Spanned from ast::Mac
Browse files Browse the repository at this point in the history
  • Loading branch information
petrochenkov committed Aug 15, 2019
1 parent 73d2da0 commit 433b1e3
Show file tree
Hide file tree
Showing 17 changed files with 54 additions and 52 deletions.
2 changes: 1 addition & 1 deletion src/librustc/lint/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1345,7 +1345,7 @@ impl<'a, T: EarlyLintPass> ast_visit::Visitor<'a> for EarlyContextAndPass<'a, T>
// part of `walk_mac`, and (b) we should be calling
// `visit_path`, *but* that would require a `NodeId`, and I
// want to get #53686 fixed quickly. -nmatsakis
ast_visit::walk_path(self, &mac.node.path);
ast_visit::walk_path(self, &mac.path);

run_early_pass!(self, check_mac, mac);
}
Expand Down
2 changes: 1 addition & 1 deletion src/librustc_lint/builtin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1493,7 +1493,7 @@ impl EarlyLintPass for KeywordIdents {
self.check_tokens(cx, mac_def.stream());
}
fn check_mac(&mut self, cx: &EarlyContext<'_>, mac: &ast::Mac) {
self.check_tokens(cx, mac.node.tts.clone().into());
self.check_tokens(cx, mac.tts.clone().into());
}
fn check_ident(&mut self, cx: &EarlyContext<'_>, ident: ast::Ident) {
self.check_ident_token(cx, UnderMacro(false), ident);
Expand Down
2 changes: 1 addition & 1 deletion src/librustc_passes/ast_validation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -824,7 +824,7 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
|this| visit::walk_enum_def(this, enum_definition, generics, item_id))
}

fn visit_mac(&mut self, mac: &Spanned<Mac_>) {
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()
Expand Down
2 changes: 1 addition & 1 deletion src/librustc_resolve/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ impl<'a> base::Resolver for Resolver<'a> {
InvocationKind::Attr { ref attr, ref derives, after_derive, .. } =>
(&attr.path, MacroKind::Attr, derives.clone(), after_derive),
InvocationKind::Bang { ref mac, .. } =>
(&mac.node.path, MacroKind::Bang, Vec::new(), false),
(&mac.path, MacroKind::Bang, Vec::new(), false),
InvocationKind::Derive { ref path, .. } =>
(path, MacroKind::Derive, Vec::new(), false),
InvocationKind::DeriveContainer { ref derives, .. } => {
Expand Down
7 changes: 3 additions & 4 deletions src/libsyntax/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1285,19 +1285,18 @@ pub enum Movability {
Movable,
}

pub type Mac = Spanned<Mac_>;

/// Represents a macro invocation. The `Path` indicates which macro
/// is being invoked, and the vector of token-trees contains the source
/// of the macro invocation.
///
/// N.B., the additional ident for a `macro_rules`-style macro is actually
/// stored in the enclosing item.
#[derive(Clone, RustcEncodable, RustcDecodable, Debug)]
pub struct Mac_ {
pub struct Mac {
pub path: Path,
pub delim: MacDelimiter,
pub tts: TokenStream,
pub span: Span,
pub prior_type_ascription: Option<(Span, bool)>,
}

Expand All @@ -1308,7 +1307,7 @@ pub enum MacDelimiter {
Brace,
}

impl Mac_ {
impl Mac {
pub fn stream(&self) -> TokenStream {
self.tts.clone()
}
Expand Down
11 changes: 5 additions & 6 deletions src/libsyntax/ext/expand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -492,22 +492,21 @@ 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.node.stream());
let tok_result = expander.expand(self.cx, span, mac.stream());
let result =
self.parse_ast_fragment(tok_result, fragment_kind, &mac.node.path, span);
self.parse_ast_fragment(tok_result, fragment_kind, &mac.path, span);
self.gate_proc_macro_expansion(span, &result);
result
}
SyntaxExtensionKind::LegacyBang(expander) => {
let prev = self.cx.current_expansion.prior_type_ascription;
self.cx.current_expansion.prior_type_ascription =
mac.node.prior_type_ascription;
let tok_result = expander.expand(self.cx, span, mac.node.stream());
self.cx.current_expansion.prior_type_ascription = mac.prior_type_ascription;
let tok_result = expander.expand(self.cx, span, mac.stream());
let result = if let Some(result) = fragment_kind.make_from(tok_result) {
result
} else {
let msg = format!("non-{kind} macro in {kind} position: {path}",
kind = fragment_kind.name(), path = mac.node.path);
kind = fragment_kind.name(), path = mac.path);
self.cx.span_err(span, &msg);
self.cx.trace_macros_diag();
fragment_kind.dummy(span)
Expand Down
5 changes: 3 additions & 2 deletions src/libsyntax/ext/placeholders.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,13 @@ use rustc_data_structures::fx::FxHashMap;

pub fn placeholder(kind: AstFragmentKind, id: ast::NodeId) -> AstFragment {
fn mac_placeholder() -> ast::Mac {
dummy_spanned(ast::Mac_ {
ast::Mac {
path: ast::Path { span: DUMMY_SP, segments: Vec::new() },
tts: TokenStream::empty().into(),
delim: ast::MacDelimiter::Brace,
span: DUMMY_SP,
prior_type_ascription: None,
})
}
}

let ident = ast::Ident::invalid();
Expand Down
4 changes: 2 additions & 2 deletions src/libsyntax/mut_visit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -533,8 +533,8 @@ pub fn noop_visit_attribute<T: MutVisitor>(attr: &mut Attribute, vis: &mut T) {
vis.visit_span(span);
}

pub fn noop_visit_mac<T: MutVisitor>(Spanned { node, span }: &mut Mac, vis: &mut T) {
let Mac_ { path, delim: _, tts, .. } = node;
pub fn noop_visit_mac<T: MutVisitor>(mac: &mut Mac, vis: &mut T) {
let Mac { path, delim: _, tts, span, prior_type_ascription: _ } = mac;
vis.visit_path(path);
vis.visit_tts(tts);
vis.visit_span(span);
Expand Down
9 changes: 5 additions & 4 deletions src/libsyntax/parse/parser/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@ use crate::ast::{self, Attribute, AttrStyle, Ident, CaptureBy, BlockCheckMode};
use crate::ast::{Expr, ExprKind, RangeLimits, Label, Movability, IsAsync, Arm};
use crate::ast::{Ty, TyKind, FunctionRetTy, Arg, FnDecl};
use crate::ast::{BinOpKind, BinOp, UnOp};
use crate::ast::{Mac_, AnonConst, Field};
use crate::ast::{Mac, AnonConst, Field};

use crate::parse::classify;
use crate::parse::token::{self, Token};
use crate::parse::diagnostics::{Error};
use crate::print::pprust;
use crate::source_map::{self, respan, Span};
use crate::source_map::{self, Span};
use crate::symbol::{kw, sym};
use crate::util::parser::{AssocOp, Fixity, prec_let_scrutinee_needs_par};

Expand Down Expand Up @@ -1011,12 +1011,13 @@ impl<'a> Parser<'a> {
// MACRO INVOCATION expression
let (delim, tts) = self.expect_delimited_token_tree()?;
hi = self.prev_span;
ex = ExprKind::Mac(respan(lo.to(hi), Mac_ {
ex = ExprKind::Mac(Mac {
path,
tts,
delim,
span: lo.to(hi),
prior_type_ascription: self.last_type_ascription,
}));
});
} else if self.check(&token::OpenDelim(token::Brace)) {
if let Some(expr) = self.maybe_parse_struct_expr(lo, &path, &attrs) {
return expr;
Expand Down
12 changes: 7 additions & 5 deletions src/libsyntax/parse/parser/item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use crate::ast::{Visibility, VisibilityKind, Mutability, FnDecl, FnHeader};
use crate::ast::{ForeignItem, ForeignItemKind};
use crate::ast::{Ty, TyKind, GenericBounds, TraitRef};
use crate::ast::{EnumDef, VariantData, StructField, AnonConst};
use crate::ast::{Mac, Mac_, MacDelimiter};
use crate::ast::{Mac, MacDelimiter};
use crate::ext::base::DummyResult;
use crate::parse::token;
use crate::parse::parser::maybe_append;
Expand Down Expand Up @@ -530,12 +530,13 @@ impl<'a> Parser<'a> {
}

let hi = self.prev_span;
let mac = respan(mac_lo.to(hi), Mac_ {
let mac = Mac {
path,
tts,
delim,
span: mac_lo.to(hi),
prior_type_ascription: self.last_type_ascription,
});
};
let item =
self.mk_item(lo.to(hi), Ident::invalid(), ItemKind::Mac(mac), visibility, attrs);
return Ok(Some(item));
Expand Down Expand Up @@ -604,12 +605,13 @@ impl<'a> Parser<'a> {
self.expect(&token::Semi)?;
}

Ok(Some(respan(lo.to(self.prev_span), Mac_ {
Ok(Some(Mac {
path,
tts,
delim,
span: lo.to(self.prev_span),
prior_type_ascription: self.last_type_ascription,
})))
}))
} else {
Ok(None)
}
Expand Down
7 changes: 4 additions & 3 deletions src/libsyntax/parse/parser/pat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use super::{Parser, PResult, PathStyle};

use crate::{maybe_recover_from_interpolated_ty_qpath, maybe_whole};
use crate::ptr::P;
use crate::ast::{self, Attribute, Pat, PatKind, FieldPat, RangeEnd, RangeSyntax, Mac_};
use crate::ast::{self, Attribute, Pat, PatKind, FieldPat, RangeEnd, RangeSyntax, Mac};
use crate::ast::{BindingMode, Ident, Mutability, Path, QSelf, Expr, ExprKind};
use crate::parse::token::{self};
use crate::print::pprust;
Expand Down Expand Up @@ -275,12 +275,13 @@ impl<'a> Parser<'a> {
fn parse_pat_mac_invoc(&mut self, lo: Span, path: Path) -> PResult<'a, PatKind> {
self.bump();
let (delim, tts) = self.expect_delimited_token_tree()?;
let mac = respan(lo.to(self.prev_span), Mac_ {
let mac = Mac {
path,
tts,
delim,
span: lo.to(self.prev_span),
prior_type_ascription: self.last_type_ascription,
});
};
Ok(PatKind::Mac(mac))
}

Expand Down
7 changes: 4 additions & 3 deletions src/libsyntax/parse/parser/stmt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use super::path::PathStyle;
use crate::ptr::P;
use crate::{maybe_whole, ThinVec};
use crate::ast::{self, Stmt, StmtKind, Local, Block, BlockCheckMode, Expr, ExprKind};
use crate::ast::{Attribute, AttrStyle, VisibilityKind, MacStmtStyle, Mac_, MacDelimiter};
use crate::ast::{Attribute, AttrStyle, VisibilityKind, MacStmtStyle, Mac, MacDelimiter};
use crate::ext::base::DummyResult;
use crate::parse::{classify, DirectoryOwnership};
use crate::parse::diagnostics::Error;
Expand Down Expand Up @@ -99,12 +99,13 @@ impl<'a> Parser<'a> {
MacStmtStyle::NoBraces
};

let mac = respan(lo.to(hi), Mac_ {
let mac = Mac {
path,
tts,
delim,
span: lo.to(hi),
prior_type_ascription: self.last_type_ascription,
});
};
let node = if delim == MacDelimiter::Brace ||
self.token == token::Semi || self.token == token::Eof {
StmtKind::Mac(P((mac, style, attrs.into())))
Expand Down
9 changes: 5 additions & 4 deletions src/libsyntax/parse/parser/ty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ use crate::{maybe_whole, maybe_recover_from_interpolated_ty_qpath};
use crate::ptr::P;
use crate::ast::{self, Ty, TyKind, MutTy, BareFnTy, FunctionRetTy, GenericParam, Lifetime, Ident};
use crate::ast::{TraitBoundModifier, TraitObjectSyntax, GenericBound, GenericBounds, PolyTraitRef};
use crate::ast::{Mutability, AnonConst, FnDecl, Mac_};
use crate::ast::{Mutability, AnonConst, FnDecl, Mac};
use crate::parse::token::{self, Token};
use crate::source_map::{respan, Span};
use crate::source_map::Span;
use crate::symbol::{kw};

use rustc_target::spec::abi::Abi;
Expand Down Expand Up @@ -175,13 +175,14 @@ impl<'a> Parser<'a> {
if self.eat(&token::Not) {
// Macro invocation in type position
let (delim, tts) = self.expect_delimited_token_tree()?;
let node = Mac_ {
let mac = Mac {
path,
tts,
delim,
span: lo.to(self.prev_span),
prior_type_ascription: self.last_type_ascription,
};
TyKind::Mac(respan(lo.to(self.prev_span), node))
TyKind::Mac(mac)
} else {
// Just a type path or bound list (trait object type) starting with a trait.
// `Type`
Expand Down
2 changes: 1 addition & 1 deletion src/libsyntax/parse/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,7 @@ fn ttdelim_span() {
"foo!( fn main() { body } )".to_string(), &sess).unwrap();

let tts: Vec<_> = match expr.node {
ast::ExprKind::Mac(ref mac) => mac.node.stream().trees().collect(),
ast::ExprKind::Mac(ref mac) => mac.stream().trees().collect(),
_ => panic!("not a macro"),
};

Expand Down
14 changes: 7 additions & 7 deletions src/libsyntax/print/pprust.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1067,7 +1067,7 @@ impl<'a> State<'a> {
}
ast::ForeignItemKind::Macro(ref m) => {
self.print_mac(m);
match m.node.delim {
match m.delim {
MacDelimiter::Brace => {},
_ => self.s.word(";")
}
Expand Down Expand Up @@ -1341,7 +1341,7 @@ impl<'a> State<'a> {
}
ast::ItemKind::Mac(ref mac) => {
self.print_mac(mac);
match mac.node.delim {
match mac.delim {
MacDelimiter::Brace => {}
_ => self.s.word(";"),
}
Expand Down Expand Up @@ -1554,7 +1554,7 @@ impl<'a> State<'a> {
}
ast::TraitItemKind::Macro(ref mac) => {
self.print_mac(mac);
match mac.node.delim {
match mac.delim {
MacDelimiter::Brace => {}
_ => self.s.word(";"),
}
Expand Down Expand Up @@ -1591,7 +1591,7 @@ impl<'a> State<'a> {
}
ast::ImplItemKind::Macro(ref mac) => {
self.print_mac(mac);
match mac.node.delim {
match mac.delim {
MacDelimiter::Brace => {}
_ => self.s.word(";"),
}
Expand Down Expand Up @@ -1749,11 +1749,11 @@ impl<'a> State<'a> {

crate fn print_mac(&mut self, m: &ast::Mac) {
self.print_mac_common(
Some(MacHeader::Path(&m.node.path)),
Some(MacHeader::Path(&m.path)),
true,
None,
m.node.delim.to_token(),
m.node.stream(),
m.delim.to_token(),
m.stream(),
true,
m.span,
);
Expand Down
2 changes: 1 addition & 1 deletion src/libsyntax/visit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -663,7 +663,7 @@ pub fn walk_stmt<'a, V: Visitor<'a>>(visitor: &mut V, statement: &'a Stmt) {
}

pub fn walk_mac<'a, V: Visitor<'a>>(visitor: &mut V, mac: &'a Mac) {
visitor.visit_path(&mac.node.path, DUMMY_NODE_ID);
visitor.visit_path(&mac.path, DUMMY_NODE_ID);
}

pub fn walk_anon_const<'a, V: Visitor<'a>>(visitor: &mut V, constant: &'a AnonConst) {
Expand Down
9 changes: 3 additions & 6 deletions src/libsyntax_ext/assert.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use errors::{Applicability, DiagnosticBuilder};

use syntax::ast::{self, *};
use syntax::source_map::Spanned;
use syntax::ext::base::*;
use syntax::parse::token::{self, TokenKind};
use syntax::parse::parser::Parser;
Expand All @@ -25,7 +24,7 @@ pub fn expand_assert<'cx>(
};

let sp = sp.apply_mark(cx.current_expansion.id);
let panic_call = Mac_ {
let panic_call = Mac {
path: Path::from_ident(Ident::new(sym::panic, sp)),
tts: custom_message.unwrap_or_else(|| {
TokenStream::from(TokenTree::token(
Expand All @@ -37,17 +36,15 @@ pub fn expand_assert<'cx>(
))
}).into(),
delim: MacDelimiter::Parenthesis,
span: sp,
prior_type_ascription: None,
};
let if_expr = cx.expr_if(
sp,
cx.expr(sp, ExprKind::Unary(UnOp::Not, cond_expr)),
cx.expr(
sp,
ExprKind::Mac(Spanned {
span: sp,
node: panic_call,
}),
ExprKind::Mac(panic_call),
),
None,
);
Expand Down

0 comments on commit 433b1e3

Please sign in to comment.