Skip to content

Commit 0fac567

Browse files
committed
syntax: Remove redundant span from ast::Mac
Also remove a couple of redundant `visit_mac` asserts
1 parent a81804b commit 0fac567

File tree

16 files changed

+34
-56
lines changed

16 files changed

+34
-56
lines changed

src/librustc_parse/parser/expr.rs

-1
Original file line numberDiff line numberDiff line change
@@ -927,7 +927,6 @@ impl<'a> Parser<'a> {
927927
ex = ExprKind::Mac(Mac {
928928
path,
929929
args,
930-
span: lo.to(hi),
931930
prior_type_ascription: self.last_type_ascription,
932931
});
933932
} else if self.check(&token::OpenDelim(token::Brace)) {

src/librustc_parse/parser/item.rs

-5
Original file line numberDiff line numberDiff line change
@@ -432,8 +432,6 @@ impl<'a> Parser<'a> {
432432
let prev_span = self.prev_span;
433433
self.complain_if_pub_macro(&visibility.node, prev_span);
434434

435-
let mac_lo = self.token.span;
436-
437435
// Item macro
438436
let path = self.parse_path(PathStyle::Mod)?;
439437
self.expect(&token::Not)?;
@@ -446,7 +444,6 @@ impl<'a> Parser<'a> {
446444
let mac = Mac {
447445
path,
448446
args,
449-
span: mac_lo.to(hi),
450447
prior_type_ascription: self.last_type_ascription,
451448
};
452449
let item =
@@ -499,7 +496,6 @@ impl<'a> Parser<'a> {
499496
if self.token.is_path_start() &&
500497
!(self.is_async_fn() && self.token.span.rust_2015()) {
501498
let prev_span = self.prev_span;
502-
let lo = self.token.span;
503499
let path = self.parse_path(PathStyle::Mod)?;
504500

505501
if path.segments.len() == 1 {
@@ -525,7 +521,6 @@ impl<'a> Parser<'a> {
525521
Ok(Some(Mac {
526522
path,
527523
args,
528-
span: lo.to(self.prev_span),
529524
prior_type_ascription: self.last_type_ascription,
530525
}))
531526
} else {

src/librustc_parse/parser/pat.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -338,7 +338,7 @@ impl<'a> Parser<'a> {
338338
(None, self.parse_path(PathStyle::Expr)?)
339339
};
340340
match self.token.kind {
341-
token::Not if qself.is_none() => self.parse_pat_mac_invoc(lo, path)?,
341+
token::Not if qself.is_none() => self.parse_pat_mac_invoc(path)?,
342342
token::DotDotDot | token::DotDotEq | token::DotDot => {
343343
self.parse_pat_range_starting_with_path(lo, qself, path)?
344344
}
@@ -593,13 +593,12 @@ impl<'a> Parser<'a> {
593593
}
594594

595595
/// Parse macro invocation
596-
fn parse_pat_mac_invoc(&mut self, lo: Span, path: Path) -> PResult<'a, PatKind> {
596+
fn parse_pat_mac_invoc(&mut self, path: Path) -> PResult<'a, PatKind> {
597597
self.bump();
598598
let args = self.parse_mac_args()?;
599599
let mac = Mac {
600600
path,
601601
args,
602-
span: lo.to(self.prev_span),
603602
prior_type_ascription: self.last_type_ascription,
604603
};
605604
Ok(PatKind::Mac(mac))

src/librustc_parse/parser/stmt.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,6 @@ impl<'a> Parser<'a> {
106106
let mac = Mac {
107107
path,
108108
args,
109-
span: lo.to(hi),
110109
prior_type_ascription: self.last_type_ascription,
111110
};
112111
let kind = if delim == token::Brace ||
@@ -130,7 +129,7 @@ impl<'a> Parser<'a> {
130129
self.warn_missing_semicolon();
131130
StmtKind::Mac(P((mac, style, attrs.into())))
132131
} else {
133-
let e = self.mk_expr(mac.span, ExprKind::Mac(mac), ThinVec::new());
132+
let e = self.mk_expr(lo.to(hi), ExprKind::Mac(mac), ThinVec::new());
134133
let e = self.maybe_recover_from_bad_qpath(e, true)?;
135134
let e = self.parse_dot_or_call_expr_with(e, lo, attrs.into())?;
136135
let e = self.parse_assoc_expr_with(0, LhsExpr::AlreadyParsed(e))?;

src/librustc_parse/parser/ty.rs

-1
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,6 @@ impl<'a> Parser<'a> {
181181
let mac = Mac {
182182
path,
183183
args,
184-
span: lo.to(self.prev_span),
185184
prior_type_ascription: self.last_type_ascription,
186185
};
187186
TyKind::Mac(mac)

src/librustc_passes/ast_validation.rs

-8
Original file line numberDiff line numberDiff line change
@@ -737,14 +737,6 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
737737
|this| visit::walk_enum_def(this, enum_definition, generics, item_id))
738738
}
739739

740-
fn visit_mac(&mut self, mac: &Mac) {
741-
// when a new macro kind is added but the author forgets to set it up for expansion
742-
// because that's the only part that won't cause a compiler error
743-
self.session.diagnostic()
744-
.span_bug(mac.span, "macro invocation missed in expansion; did you forget to override \
745-
the relevant `fold_*()` method in `PlaceholderExpander`?");
746-
}
747-
748740
fn visit_impl_item(&mut self, ii: &'a ImplItem) {
749741
if let ImplItemKind::Method(ref sig, _) = ii.kind {
750742
self.check_fn_decl(&sig.decl);

src/librustc_save_analysis/dump_visitor.rs

-8
Original file line numberDiff line numberDiff line change
@@ -1515,14 +1515,6 @@ impl<'l, 'tcx> Visitor<'l> for DumpVisitor<'l, 'tcx> {
15151515
}
15161516
}
15171517

1518-
fn visit_mac(&mut self, mac: &'l ast::Mac) {
1519-
// These shouldn't exist in the AST at this point, log a span bug.
1520-
span_bug!(
1521-
mac.span,
1522-
"macro invocation should have been expanded out of AST"
1523-
);
1524-
}
1525-
15261518
fn visit_pat(&mut self, p: &'l ast::Pat) {
15271519
self.process_macro_use(p.span);
15281520
self.process_pat(p);

src/libsyntax/ast.rs

+14-7
Original file line numberDiff line numberDiff line change
@@ -1379,10 +1379,15 @@ pub enum Movability {
13791379
pub struct Mac {
13801380
pub path: Path,
13811381
pub args: P<MacArgs>,
1382-
pub span: Span,
13831382
pub prior_type_ascription: Option<(Span, bool)>,
13841383
}
13851384

1385+
impl Mac {
1386+
pub fn span(&self) -> Span {
1387+
self.path.span.to(self.args.span().unwrap_or(self.path.span))
1388+
}
1389+
}
1390+
13861391
/// Arguments passed to an attribute or a function-like macro.
13871392
#[derive(Clone, RustcEncodable, RustcDecodable, Debug)]
13881393
pub enum MacArgs {
@@ -1403,6 +1408,14 @@ impl MacArgs {
14031408
}
14041409
}
14051410

1411+
pub fn span(&self) -> Option<Span> {
1412+
match *self {
1413+
MacArgs::Empty => None,
1414+
MacArgs::Delimited(dspan, ..) => Some(dspan.entire()),
1415+
MacArgs::Eq(eq_span, ref tokens) => Some(eq_span.to(tokens.span().unwrap_or(eq_span))),
1416+
}
1417+
}
1418+
14061419
/// Tokens inside the delimiters or after `=`.
14071420
/// Proc macros see these tokens, for example.
14081421
pub fn inner_tokens(&self) -> TokenStream {
@@ -1432,12 +1445,6 @@ impl MacArgs {
14321445
}
14331446
}
14341447

1435-
impl Mac {
1436-
pub fn stream(&self) -> TokenStream {
1437-
self.args.inner_tokens()
1438-
}
1439-
}
1440-
14411448
#[derive(Copy, Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Debug)]
14421449
pub enum MacDelimiter {
14431450
Parenthesis,

src/libsyntax/mut_visit.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -580,10 +580,9 @@ pub fn noop_visit_attribute<T: MutVisitor>(attr: &mut Attribute, vis: &mut T) {
580580
}
581581

582582
pub fn noop_visit_mac<T: MutVisitor>(mac: &mut Mac, vis: &mut T) {
583-
let Mac { path, args, span, prior_type_ascription: _ } = mac;
583+
let Mac { path, args, prior_type_ascription: _ } = mac;
584584
vis.visit_path(path);
585585
visit_mac_args(args, vis);
586-
vis.visit_span(span);
587586
}
588587

589588
pub fn noop_visit_macro_def<T: MutVisitor>(macro_def: &mut MacroDef, vis: &mut T) {

src/libsyntax/print/pprust.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1772,9 +1772,9 @@ impl<'a> State<'a> {
17721772
true,
17731773
None,
17741774
m.args.delim(),
1775-
m.stream(),
1775+
m.args.inner_tokens(),
17761776
true,
1777-
m.span,
1777+
m.span(),
17781778
);
17791779
}
17801780

src/libsyntax/tokenstream.rs

+8
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,14 @@ impl TokenStream {
225225
self.0.len()
226226
}
227227

228+
pub fn span(&self) -> Option<Span> {
229+
match &**self.0 {
230+
[] => None,
231+
[(tt, _)] => Some(tt.span()),
232+
[(tt_start, _), .., (tt_end, _)] => Some(tt_start.span().to(tt_end.span())),
233+
}
234+
}
235+
228236
pub fn from_streams(mut streams: SmallVec<[TokenStream; 2]>) -> TokenStream {
229237
match streams.len() {
230238
0 => TokenStream::default(),

src/libsyntax_expand/expand.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -597,13 +597,13 @@ impl<'a, 'b> MacroExpander<'a, 'b> {
597597
InvocationKind::Bang { mac, .. } => match ext {
598598
SyntaxExtensionKind::Bang(expander) => {
599599
self.gate_proc_macro_expansion_kind(span, fragment_kind);
600-
let tok_result = expander.expand(self.cx, span, mac.stream());
600+
let tok_result = expander.expand(self.cx, span, mac.args.inner_tokens());
601601
self.parse_ast_fragment(tok_result, fragment_kind, &mac.path, span)
602602
}
603603
SyntaxExtensionKind::LegacyBang(expander) => {
604604
let prev = self.cx.current_expansion.prior_type_ascription;
605605
self.cx.current_expansion.prior_type_ascription = mac.prior_type_ascription;
606-
let tok_result = expander.expand(self.cx, span, mac.stream());
606+
let tok_result = expander.expand(self.cx, span, mac.args.inner_tokens());
607607
let result = if let Some(result) = fragment_kind.make_from(tok_result) {
608608
result
609609
} else {

src/libsyntax_expand/parse/tests.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -272,7 +272,7 @@ fn ttdelim_span() {
272272
"foo!( fn main() { body } )".to_string(), &sess).unwrap();
273273

274274
let tts: Vec<_> = match expr.kind {
275-
ast::ExprKind::Mac(ref mac) => mac.stream().trees().collect(),
275+
ast::ExprKind::Mac(ref mac) => mac.args.inner_tokens().trees().collect(),
276276
_ => panic!("not a macro"),
277277
};
278278

src/libsyntax_expand/placeholders.rs

-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ pub fn placeholder(kind: AstFragmentKind, id: ast::NodeId, vis: Option<ast::Visi
1717
ast::Mac {
1818
path: ast::Path { span: DUMMY_SP, segments: Vec::new() },
1919
args: P(ast::MacArgs::Empty),
20-
span: DUMMY_SP,
2120
prior_type_ascription: None,
2221
}
2322
}

src/libsyntax_ext/assert.rs

-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@ pub fn expand_assert<'cx>(
3939
let panic_call = Mac {
4040
path: Path::from_ident(Ident::new(sym::panic, sp)),
4141
args,
42-
span: sp,
4342
prior_type_ascription: None,
4443
};
4544
let if_expr = cx.expr_if(

src/libsyntax_ext/deriving/generic/mod.rs

+3-12
Original file line numberDiff line numberDiff line change
@@ -340,14 +340,12 @@ pub fn combine_substructure(f: CombineSubstructureFunc<'_>)
340340
fn find_type_parameters(
341341
ty: &ast::Ty,
342342
ty_param_names: &[ast::Name],
343-
span: Span,
344343
cx: &ExtCtxt<'_>,
345344
) -> Vec<P<ast::Ty>> {
346345
use syntax::visit;
347346

348347
struct Visitor<'a, 'b> {
349348
cx: &'a ExtCtxt<'b>,
350-
span: Span,
351349
ty_param_names: &'a [ast::Name],
352350
types: Vec<P<ast::Ty>>,
353351
}
@@ -366,18 +364,11 @@ fn find_type_parameters(
366364
}
367365

368366
fn visit_mac(&mut self, mac: &ast::Mac) {
369-
let span = mac.span.with_ctxt(self.span.ctxt());
370-
self.cx.span_err(span, "`derive` cannot be used on items with type macros");
367+
self.cx.span_err(mac.span(), "`derive` cannot be used on items with type macros");
371368
}
372369
}
373370

374-
let mut visitor = Visitor {
375-
ty_param_names,
376-
types: Vec::new(),
377-
span,
378-
cx,
379-
};
380-
371+
let mut visitor = Visitor { cx, ty_param_names, types: Vec::new() };
381372
visit::Visitor::visit_ty(&mut visitor, ty);
382373

383374
visitor.types
@@ -605,7 +596,7 @@ impl<'a> TraitDef<'a> {
605596
.collect();
606597

607598
for field_ty in field_tys {
608-
let tys = find_type_parameters(&field_ty, &ty_param_names, self.span, cx);
599+
let tys = find_type_parameters(&field_ty, &ty_param_names, cx);
609600

610601
for ty in tys {
611602
// if we have already handled this type, skip it

0 commit comments

Comments
 (0)