Skip to content

Commit fd64b3b

Browse files
committed
parser: make eat_macro_def redundant.
1 parent c202603 commit fd64b3b

File tree

2 files changed

+12
-33
lines changed

2 files changed

+12
-33
lines changed

src/librustc_parse/parser/item.rs

Lines changed: 9 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ impl<'a> Parser<'a> {
8686
let vis = self.parse_visibility(FollowedByType::No)?;
8787

8888
if let Some((ident, kind)) = self.parse_item_kind(&mut attrs, macros_allowed, lo, &vis)? {
89-
return Ok(Some(self.mk_item(lo.to(self.prev_span), ident, kind, vis, attrs)));
89+
return Ok(Some(P(self.mk_item(lo, ident, kind, vis, attrs))));
9090
}
9191

9292
// FAILURE TO PARSE ITEM
@@ -942,9 +942,7 @@ impl<'a> Parser<'a> {
942942
}
943943
self.unexpected()?
944944
};
945-
946-
let span = lo.to(self.prev_span);
947-
Ok(P(ast::ForeignItem { ident, attrs, kind, id: DUMMY_NODE_ID, span, vis, tokens: None }))
945+
Ok(P(self.mk_item(lo, ident, kind, vis, attrs)))
948946
}
949947

950948
/// Parses a static item from a foreign module.
@@ -1364,7 +1362,7 @@ impl<'a> Parser<'a> {
13641362
}
13651363

13661364
/// Is this unambiguously the start of a `macro_rules! foo` item defnition?
1367-
fn is_macro_rules_item(&mut self) -> bool {
1365+
pub(super) fn is_macro_rules_item(&mut self) -> bool {
13681366
self.check_keyword(sym::macro_rules)
13691367
&& self.look_ahead(1, |t| *t == token::Not)
13701368
&& self.look_ahead(2, |t| t.is_ident())
@@ -1385,22 +1383,6 @@ impl<'a> Parser<'a> {
13851383
Ok((ident, ItemKind::MacroDef(ast::MacroDef { body, legacy: true })))
13861384
}
13871385

1388-
pub(super) fn eat_macro_def(
1389-
&mut self,
1390-
attrs: &[Attribute],
1391-
vis: &Visibility,
1392-
lo: Span,
1393-
) -> PResult<'a, Option<P<Item>>> {
1394-
let (ident, kind) = if self.eat_keyword(kw::Macro) {
1395-
self.parse_item_decl_macro(lo)?
1396-
} else if self.is_macro_rules_item() {
1397-
self.parse_item_macro_rules(vis)?
1398-
} else {
1399-
return Ok(None);
1400-
};
1401-
Ok(Some(self.mk_item(lo.to(self.prev_span), ident, kind, vis.clone(), attrs.to_vec())))
1402-
}
1403-
14041386
fn complain_if_pub_macro(&self, vis: &VisibilityKind, sp: Span) {
14051387
match *vis {
14061388
VisibilityKind::Inherited => {}
@@ -1496,15 +1478,16 @@ impl<'a> Parser<'a> {
14961478
Ok(true)
14971479
}
14981480

1499-
fn mk_item(
1481+
fn mk_item<K>(
15001482
&self,
1501-
span: Span,
1483+
lo: Span,
15021484
ident: Ident,
1503-
kind: ItemKind,
1485+
kind: K,
15041486
vis: Visibility,
15051487
attrs: Vec<Attribute>,
1506-
) -> P<Item> {
1507-
P(Item { ident, attrs, id: DUMMY_NODE_ID, kind, vis, span, tokens: None })
1488+
) -> Item<K> {
1489+
let span = lo.to(self.prev_span);
1490+
Item { ident, attrs, id: DUMMY_NODE_ID, kind, vis, span, tokens: None }
15081491
}
15091492
}
15101493

src/librustc_parse/parser/stmt.rs

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@ use crate::maybe_whole;
77
use crate::DirectoryOwnership;
88

99
use rustc_errors::{Applicability, PResult};
10-
use rustc_span::source_map::{respan, BytePos, Span};
10+
use rustc_span::source_map::{BytePos, Span};
1111
use rustc_span::symbol::{kw, sym, Symbol};
1212
use syntax::ast;
13-
use syntax::ast::{AttrStyle, AttrVec, Attribute, Mac, MacStmtStyle, VisibilityKind};
13+
use syntax::ast::{AttrStyle, AttrVec, Attribute, Mac, MacStmtStyle};
1414
use syntax::ast::{Block, BlockCheckMode, Expr, ExprKind, Local, Stmt, StmtKind, DUMMY_NODE_ID};
1515
use syntax::ptr::P;
1616
use syntax::token::{self, TokenKind};
@@ -55,11 +55,6 @@ impl<'a> Parser<'a> {
5555
return self.recover_stmt_local(lo, attrs.into(), msg, "let");
5656
}
5757

58-
let mac_vis = respan(lo, VisibilityKind::Inherited);
59-
if let Some(macro_def) = self.eat_macro_def(&attrs, &mac_vis, lo)? {
60-
return Ok(Some(self.mk_stmt(lo.to(self.prev_span), StmtKind::Item(macro_def))));
61-
}
62-
6358
// Starts like a simple path, being careful to avoid contextual keywords
6459
// such as a union items, item with `crate` visibility or auto trait items.
6560
// Our goal here is to parse an arbitrary path `a::b::c` but not something that starts
@@ -70,6 +65,7 @@ impl<'a> Parser<'a> {
7065
&& !self.is_crate_vis() // `crate::b::c` - path, `crate struct S;` - not a path.
7166
&& !self.is_auto_trait_item()
7267
&& !self.is_async_fn()
68+
&& !self.is_macro_rules_item()
7369
{
7470
let path = self.parse_path(PathStyle::Expr)?;
7571

0 commit comments

Comments
 (0)