Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Shrink Nonterminal #67340

Merged
merged 2 commits into from
Jan 31, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 8 additions & 7 deletions src/librustc_builtin_macros/deriving/generic/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -531,13 +531,13 @@ impl<'a> TraitDef<'a> {
type_ident: Ident,
generics: &Generics,
field_tys: Vec<P<ast::Ty>>,
methods: Vec<ast::AssocItem>,
methods: Vec<P<ast::AssocItem>>,
) -> P<ast::Item> {
let trait_path = self.path.to_path(cx, self.span, type_ident, generics);

// Transform associated types from `deriving::ty::Ty` into `ast::AssocItem`
let associated_types =
self.associated_types.iter().map(|&(ident, ref type_def)| ast::AssocItem {
let associated_types = self.associated_types.iter().map(|&(ident, ref type_def)| {
P(ast::AssocItem {
id: ast::DUMMY_NODE_ID,
span: self.span,
ident,
Expand All @@ -550,7 +550,8 @@ impl<'a> TraitDef<'a> {
Some(type_def.to_ty(cx, self.span, type_ident, generics)),
),
tokens: None,
});
})
});

let Generics { mut params, mut where_clause, span } =
self.generics.to_generics(cx, self.span, type_ident, generics);
Expand Down Expand Up @@ -938,7 +939,7 @@ impl<'a> MethodDef<'a> {
explicit_self: Option<ast::ExplicitSelf>,
arg_types: Vec<(Ident, P<ast::Ty>)>,
body: P<Expr>,
) -> ast::AssocItem {
) -> P<ast::AssocItem> {
// Create the generics that aren't for `Self`.
let fn_generics = self.generics.to_generics(cx, trait_.span, type_ident, generics);

Expand Down Expand Up @@ -968,7 +969,7 @@ impl<'a> MethodDef<'a> {
};

// Create the method.
ast::AssocItem {
P(ast::AssocItem {
id: ast::DUMMY_NODE_ID,
attrs: self.attributes.clone(),
generics: fn_generics,
Expand All @@ -978,7 +979,7 @@ impl<'a> MethodDef<'a> {
ident: method_ident,
kind: ast::AssocItemKind::Fn(sig, Some(body_block)),
tokens: None,
}
})
}

/// ```
Expand Down
36 changes: 18 additions & 18 deletions src/librustc_expand/base.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,23 +136,23 @@ impl Annotatable {
}
}

pub fn expect_trait_item(self) -> ast::AssocItem {
pub fn expect_trait_item(self) -> P<ast::AssocItem> {
match self {
Annotatable::TraitItem(i) => i.into_inner(),
Annotatable::TraitItem(i) => i,
_ => panic!("expected Item"),
}
}

pub fn expect_impl_item(self) -> ast::AssocItem {
pub fn expect_impl_item(self) -> P<ast::AssocItem> {
match self {
Annotatable::ImplItem(i) => i.into_inner(),
Annotatable::ImplItem(i) => i,
_ => panic!("expected Item"),
}
}

pub fn expect_foreign_item(self) -> ast::ForeignItem {
pub fn expect_foreign_item(self) -> P<ast::ForeignItem> {
match self {
Annotatable::ForeignItem(i) => i.into_inner(),
Annotatable::ForeignItem(i) => i,
_ => panic!("expected foreign item"),
}
}
Expand Down Expand Up @@ -382,17 +382,17 @@ pub trait MacResult {
}

/// Creates zero or more impl items.
fn make_impl_items(self: Box<Self>) -> Option<SmallVec<[ast::AssocItem; 1]>> {
fn make_impl_items(self: Box<Self>) -> Option<SmallVec<[P<ast::AssocItem>; 1]>> {
None
}

/// Creates zero or more trait items.
fn make_trait_items(self: Box<Self>) -> Option<SmallVec<[ast::AssocItem; 1]>> {
fn make_trait_items(self: Box<Self>) -> Option<SmallVec<[P<ast::AssocItem>; 1]>> {
None
}

/// Creates zero or more items in an `extern {}` block
fn make_foreign_items(self: Box<Self>) -> Option<SmallVec<[ast::ForeignItem; 1]>> {
fn make_foreign_items(self: Box<Self>) -> Option<SmallVec<[P<ast::ForeignItem>; 1]>> {
None
}

Expand Down Expand Up @@ -470,9 +470,9 @@ make_MacEager! {
expr: P<ast::Expr>,
pat: P<ast::Pat>,
items: SmallVec<[P<ast::Item>; 1]>,
impl_items: SmallVec<[ast::AssocItem; 1]>,
trait_items: SmallVec<[ast::AssocItem; 1]>,
foreign_items: SmallVec<[ast::ForeignItem; 1]>,
impl_items: SmallVec<[P<ast::AssocItem>; 1]>,
trait_items: SmallVec<[P<ast::AssocItem>; 1]>,
foreign_items: SmallVec<[P<ast::ForeignItem>; 1]>,
stmts: SmallVec<[ast::Stmt; 1]>,
ty: P<ast::Ty>,
}
Expand All @@ -486,15 +486,15 @@ impl MacResult for MacEager {
self.items
}

fn make_impl_items(self: Box<Self>) -> Option<SmallVec<[ast::AssocItem; 1]>> {
fn make_impl_items(self: Box<Self>) -> Option<SmallVec<[P<ast::AssocItem>; 1]>> {
self.impl_items
}

fn make_trait_items(self: Box<Self>) -> Option<SmallVec<[ast::AssocItem; 1]>> {
fn make_trait_items(self: Box<Self>) -> Option<SmallVec<[P<ast::AssocItem>; 1]>> {
self.trait_items
}

fn make_foreign_items(self: Box<Self>) -> Option<SmallVec<[ast::ForeignItem; 1]>> {
fn make_foreign_items(self: Box<Self>) -> Option<SmallVec<[P<ast::ForeignItem>; 1]>> {
self.foreign_items
}

Expand Down Expand Up @@ -586,15 +586,15 @@ impl MacResult for DummyResult {
Some(SmallVec::new())
}

fn make_impl_items(self: Box<DummyResult>) -> Option<SmallVec<[ast::AssocItem; 1]>> {
fn make_impl_items(self: Box<DummyResult>) -> Option<SmallVec<[P<ast::AssocItem>; 1]>> {
Some(SmallVec::new())
}

fn make_trait_items(self: Box<DummyResult>) -> Option<SmallVec<[ast::AssocItem; 1]>> {
fn make_trait_items(self: Box<DummyResult>) -> Option<SmallVec<[P<ast::AssocItem>; 1]>> {
Some(SmallVec::new())
}

fn make_foreign_items(self: Box<Self>) -> Option<SmallVec<[ast::ForeignItem; 1]>> {
fn make_foreign_items(self: Box<Self>) -> Option<SmallVec<[P<ast::ForeignItem>; 1]>> {
Some(SmallVec::new())
}

Expand Down
84 changes: 47 additions & 37 deletions src/librustc_expand/expand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -152,13 +152,13 @@ ast_fragments! {
Items(SmallVec<[P<ast::Item>; 1]>) {
"item"; many fn flat_map_item; fn visit_item; fn make_items;
}
TraitItems(SmallVec<[ast::AssocItem; 1]>) {
TraitItems(SmallVec<[P<ast::AssocItem>; 1]>) {
"trait item"; many fn flat_map_trait_item; fn visit_trait_item; fn make_trait_items;
}
ImplItems(SmallVec<[ast::AssocItem; 1]>) {
ImplItems(SmallVec<[P<ast::AssocItem>; 1]>) {
"impl item"; many fn flat_map_impl_item; fn visit_impl_item; fn make_impl_items;
}
ForeignItems(SmallVec<[ast::ForeignItem; 1]>) {
ForeignItems(SmallVec<[P<ast::ForeignItem>; 1]>) {
"foreign item";
many fn flat_map_foreign_item;
fn visit_foreign_item;
Expand Down Expand Up @@ -554,15 +554,15 @@ impl<'a, 'b> MacroExpander<'a, 'b> {
// we know that fold result vector will contain exactly one element
match item {
Annotatable::Item(item) => Annotatable::Item(cfg.flat_map_item(item).pop().unwrap()),
Annotatable::TraitItem(item) => Annotatable::TraitItem(
item.map(|item| cfg.flat_map_trait_item(item).pop().unwrap()),
),
Annotatable::TraitItem(item) => {
Annotatable::TraitItem(cfg.flat_map_trait_item(item).pop().unwrap())
}
Annotatable::ImplItem(item) => {
Annotatable::ImplItem(item.map(|item| cfg.flat_map_impl_item(item).pop().unwrap()))
Annotatable::ImplItem(cfg.flat_map_impl_item(item).pop().unwrap())
}
Annotatable::ForeignItem(item) => {
Annotatable::ForeignItem(cfg.flat_map_foreign_item(item).pop().unwrap())
}
Annotatable::ForeignItem(item) => Annotatable::ForeignItem(
item.map(|item| cfg.flat_map_foreign_item(item).pop().unwrap()),
),
Annotatable::Stmt(stmt) => {
Annotatable::Stmt(stmt.map(|stmt| cfg.flat_map_stmt(stmt).pop().unwrap()))
}
Expand Down Expand Up @@ -643,11 +643,9 @@ impl<'a, 'b> MacroExpander<'a, 'b> {
let item_tok = TokenTree::token(
token::Interpolated(Lrc::new(match item {
Annotatable::Item(item) => token::NtItem(item),
Annotatable::TraitItem(item) => token::NtTraitItem(item.into_inner()),
Annotatable::ImplItem(item) => token::NtImplItem(item.into_inner()),
Annotatable::ForeignItem(item) => {
token::NtForeignItem(item.into_inner())
}
Annotatable::TraitItem(item) => token::NtTraitItem(item),
Annotatable::ImplItem(item) => token::NtImplItem(item),
Annotatable::ForeignItem(item) => token::NtForeignItem(item),
Annotatable::Stmt(stmt) => token::NtStmt(stmt.into_inner()),
Annotatable::Expr(expr) => token::NtExpr(expr),
Annotatable::Arm(..)
Expand Down Expand Up @@ -1411,7 +1409,7 @@ impl<'a, 'b> MutVisitor for InvocationCollector<'a, 'b> {
}
}

fn flat_map_trait_item(&mut self, item: ast::AssocItem) -> SmallVec<[ast::AssocItem; 1]> {
fn flat_map_trait_item(&mut self, item: P<ast::AssocItem>) -> SmallVec<[P<ast::AssocItem>; 1]> {
let mut item = configure!(self, item);

let (attr, traits, after_derive) = self.classify_item(&mut item);
Expand All @@ -1420,24 +1418,28 @@ impl<'a, 'b> MutVisitor for InvocationCollector<'a, 'b> {
.collect_attr(
attr,
traits,
Annotatable::TraitItem(P(item)),
Annotatable::TraitItem(item),
AstFragmentKind::TraitItems,
after_derive,
)
.make_trait_items();
}

match item.kind {
ast::AssocItemKind::Macro(mac) => {
let ast::AssocItem { attrs, span, .. } = item;
self.check_attributes(&attrs);
self.collect_bang(mac, span, AstFragmentKind::TraitItems).make_trait_items()
ast::AssocItemKind::Macro(..) => {
self.check_attributes(&item.attrs);
item.and_then(|item| match item.kind {
ast::AssocItemKind::Macro(mac) => self
.collect_bang(mac, item.span, AstFragmentKind::TraitItems)
.make_trait_items(),
_ => unreachable!(),
})
}
_ => noop_flat_map_assoc_item(item, self),
}
}

fn flat_map_impl_item(&mut self, item: ast::AssocItem) -> SmallVec<[ast::AssocItem; 1]> {
fn flat_map_impl_item(&mut self, item: P<ast::AssocItem>) -> SmallVec<[P<ast::AssocItem>; 1]> {
let mut item = configure!(self, item);

let (attr, traits, after_derive) = self.classify_item(&mut item);
Expand All @@ -1446,18 +1448,22 @@ impl<'a, 'b> MutVisitor for InvocationCollector<'a, 'b> {
.collect_attr(
attr,
traits,
Annotatable::ImplItem(P(item)),
Annotatable::ImplItem(item),
AstFragmentKind::ImplItems,
after_derive,
)
.make_impl_items();
}

match item.kind {
ast::AssocItemKind::Macro(mac) => {
let ast::AssocItem { attrs, span, .. } = item;
self.check_attributes(&attrs);
self.collect_bang(mac, span, AstFragmentKind::ImplItems).make_impl_items()
ast::AssocItemKind::Macro(..) => {
self.check_attributes(&item.attrs);
item.and_then(|item| match item.kind {
ast::AssocItemKind::Macro(mac) => self
.collect_bang(mac, item.span, AstFragmentKind::ImplItems)
.make_impl_items(),
_ => unreachable!(),
})
}
_ => noop_flat_map_assoc_item(item, self),
}
Expand All @@ -1482,30 +1488,34 @@ impl<'a, 'b> MutVisitor for InvocationCollector<'a, 'b> {

fn flat_map_foreign_item(
&mut self,
mut foreign_item: ast::ForeignItem,
) -> SmallVec<[ast::ForeignItem; 1]> {
mut foreign_item: P<ast::ForeignItem>,
) -> SmallVec<[P<ast::ForeignItem>; 1]> {
let (attr, traits, after_derive) = self.classify_item(&mut foreign_item);

if attr.is_some() || !traits.is_empty() {
return self
.collect_attr(
attr,
traits,
Annotatable::ForeignItem(P(foreign_item)),
Annotatable::ForeignItem(foreign_item),
AstFragmentKind::ForeignItems,
after_derive,
)
.make_foreign_items();
}

if let ast::ForeignItemKind::Macro(mac) = foreign_item.kind {
self.check_attributes(&foreign_item.attrs);
return self
.collect_bang(mac, foreign_item.span, AstFragmentKind::ForeignItems)
.make_foreign_items();
match foreign_item.kind {
ast::ForeignItemKind::Macro(..) => {
self.check_attributes(&foreign_item.attrs);
foreign_item.and_then(|item| match item.kind {
ast::ForeignItemKind::Macro(mac) => self
.collect_bang(mac, item.span, AstFragmentKind::ForeignItems)
.make_foreign_items(),
_ => unreachable!(),
})
}
_ => noop_flat_map_foreign_item(foreign_item, self),
}

noop_flat_map_foreign_item(foreign_item, self)
}

fn visit_item_kind(&mut self, item: &mut ast::ItemKind) {
Expand Down
3 changes: 2 additions & 1 deletion src/librustc_expand/mbe/macro_parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ use rustc_parse::Directory;
use rustc_span::symbol::{kw, sym, Symbol};
use syntax::ast::{Ident, Name};
use syntax::print::pprust;
use syntax::ptr::P;
use syntax::sess::ParseSess;
use syntax::token::{self, DocComment, Nonterminal, Token};
use syntax::tokenstream::TokenStream;
Expand Down Expand Up @@ -914,7 +915,7 @@ fn parse_nt_inner<'a>(p: &mut Parser<'a>, sp: Span, name: Symbol) -> PResult<'a,
}
}
sym::path => token::NtPath(p.parse_path(PathStyle::Type)?),
sym::meta => token::NtMeta(p.parse_attr_item()?),
sym::meta => token::NtMeta(P(p.parse_attr_item()?)),
sym::vis => token::NtVis(p.parse_visibility(FollowedByType::Yes)?),
sym::lifetime => {
if p.check_lifetime() {
Expand Down
Loading