diff options
| author | bors <bors@rust-lang.org> | 2018-04-05 19:25:37 +0000 |
|---|---|---|
| committer | bors <bors@rust-lang.org> | 2018-04-05 19:25:37 +0000 |
| commit | 48fa6f9631868b07309b02f479e2ec523bb58c2b (patch) | |
| tree | f42e4285226c191c4448946f440ba79642ba1080 /src/libsyntax/ext | |
| parent | 7222241e7c2d7caf9ad6ee6e34748e4addfb8dd3 (diff) | |
| parent | cd615e9863fa4593ff87243920875ad5bb73906a (diff) | |
| download | rust-48fa6f9631868b07309b02f479e2ec523bb58c2b.tar.gz rust-48fa6f9631868b07309b02f479e2ec523bb58c2b.zip | |
Auto merge of #49696 - alexcrichton:rollup, r=alexcrichton
Rollup of 8 pull requests
Successful merges:
- #49045 (Make queries thread safe)
- #49350 (Expand macros in `extern {}` blocks)
- #49497 (Chalkify - Tweak `Clause` definition and HRTBs)
- #49597 (proc_macro: Reorganize public API)
- #49686 (typos)
- #49621
- #49697
- #49705
Failed merges:
Diffstat (limited to 'src/libsyntax/ext')
| -rw-r--r-- | src/libsyntax/ext/base.rs | 28 | ||||
| -rw-r--r-- | src/libsyntax/ext/expand.rs | 58 | ||||
| -rw-r--r-- | src/libsyntax/ext/placeholders.rs | 11 |
3 files changed, 97 insertions, 0 deletions
diff --git a/src/libsyntax/ext/base.rs b/src/libsyntax/ext/base.rs index d3157af984e..5a735be55c0 100644 --- a/src/libsyntax/ext/base.rs +++ b/src/libsyntax/ext/base.rs @@ -38,6 +38,7 @@ pub enum Annotatable { Item(P<ast::Item>), TraitItem(P<ast::TraitItem>), ImplItem(P<ast::ImplItem>), + ForeignItem(P<ast::ForeignItem>), Stmt(P<ast::Stmt>), Expr(P<ast::Expr>), } @@ -48,6 +49,7 @@ impl HasAttrs for Annotatable { Annotatable::Item(ref item) => &item.attrs, Annotatable::TraitItem(ref trait_item) => &trait_item.attrs, Annotatable::ImplItem(ref impl_item) => &impl_item.attrs, + Annotatable::ForeignItem(ref foreign_item) => &foreign_item.attrs, Annotatable::Stmt(ref stmt) => stmt.attrs(), Annotatable::Expr(ref expr) => &expr.attrs, } @@ -58,6 +60,8 @@ impl HasAttrs for Annotatable { Annotatable::Item(item) => Annotatable::Item(item.map_attrs(f)), Annotatable::TraitItem(trait_item) => Annotatable::TraitItem(trait_item.map_attrs(f)), Annotatable::ImplItem(impl_item) => Annotatable::ImplItem(impl_item.map_attrs(f)), + Annotatable::ForeignItem(foreign_item) => + Annotatable::ForeignItem(foreign_item.map_attrs(f)), Annotatable::Stmt(stmt) => Annotatable::Stmt(stmt.map_attrs(f)), Annotatable::Expr(expr) => Annotatable::Expr(expr.map_attrs(f)), } @@ -70,6 +74,7 @@ impl Annotatable { Annotatable::Item(ref item) => item.span, Annotatable::TraitItem(ref trait_item) => trait_item.span, Annotatable::ImplItem(ref impl_item) => impl_item.span, + Annotatable::ForeignItem(ref foreign_item) => foreign_item.span, Annotatable::Stmt(ref stmt) => stmt.span, Annotatable::Expr(ref expr) => expr.span, } @@ -106,6 +111,13 @@ impl Annotatable { } } + pub fn expect_foreign_item(self) -> ast::ForeignItem { + match self { + Annotatable::ForeignItem(i) => i.into_inner(), + _ => panic!("expected foreign item") + } + } + pub fn derive_allowed(&self) -> bool { match *self { Annotatable::Item(ref item) => match item.node { @@ -317,6 +329,9 @@ pub trait MacResult { None } + /// Create zero or more items in an `extern {}` block + fn make_foreign_items(self: Box<Self>) -> Option<SmallVector<ast::ForeignItem>> { None } + /// Create a pattern. fn make_pat(self: Box<Self>) -> Option<P<ast::Pat>> { None @@ -365,6 +380,7 @@ make_MacEager! { items: SmallVector<P<ast::Item>>, impl_items: SmallVector<ast::ImplItem>, trait_items: SmallVector<ast::TraitItem>, + foreign_items: SmallVector<ast::ForeignItem>, stmts: SmallVector<ast::Stmt>, ty: P<ast::Ty>, } @@ -386,6 +402,10 @@ impl MacResult for MacEager { self.trait_items } + fn make_foreign_items(self: Box<Self>) -> Option<SmallVector<ast::ForeignItem>> { + self.foreign_items + } + fn make_stmts(self: Box<Self>) -> Option<SmallVector<ast::Stmt>> { match self.stmts.as_ref().map_or(0, |s| s.len()) { 0 => make_stmts_default!(self), @@ -502,6 +522,14 @@ impl MacResult for DummyResult { } } + fn make_foreign_items(self: Box<Self>) -> Option<SmallVector<ast::ForeignItem>> { + if self.expr_only { + None + } else { + Some(SmallVector::new()) + } + } + fn make_stmts(self: Box<DummyResult>) -> Option<SmallVector<ast::Stmt>> { Some(SmallVector::one(ast::Stmt { id: ast::DUMMY_NODE_ID, diff --git a/src/libsyntax/ext/expand.rs b/src/libsyntax/ext/expand.rs index 864969c4075..105de13b976 100644 --- a/src/libsyntax/ext/expand.rs +++ b/src/libsyntax/ext/expand.rs @@ -133,6 +133,8 @@ expansions! { "trait item", .make_trait_items, lift .fold_trait_item, lift .visit_trait_item; ImplItems: SmallVector<ast::ImplItem> [SmallVector, ast::ImplItem], "impl item", .make_impl_items, lift .fold_impl_item, lift .visit_impl_item; + ForeignItems: SmallVector<ast::ForeignItem> [SmallVector, ast::ForeignItem], + "foreign item", .make_foreign_items, lift .fold_foreign_item, lift .visit_foreign_item; } impl ExpansionKind { @@ -149,6 +151,8 @@ impl ExpansionKind { Expansion::ImplItems(items.map(Annotatable::expect_impl_item).collect()), ExpansionKind::TraitItems => Expansion::TraitItems(items.map(Annotatable::expect_trait_item).collect()), + ExpansionKind::ForeignItems => + Expansion::ForeignItems(items.map(Annotatable::expect_foreign_item).collect()), _ => unreachable!(), } } @@ -435,6 +439,11 @@ impl<'a, 'b> MacroExpander<'a, 'b> { Annotatable::ImplItem(item) => { Annotatable::ImplItem(item.map(|item| cfg.fold_impl_item(item).pop().unwrap())) } + Annotatable::ForeignItem(item) => { + Annotatable::ForeignItem( + item.map(|item| cfg.fold_foreign_item(item).pop().unwrap()) + ) + } Annotatable::Stmt(stmt) => { Annotatable::Stmt(stmt.map(|stmt| cfg.fold_stmt(stmt).pop().unwrap())) } @@ -509,6 +518,7 @@ impl<'a, 'b> MacroExpander<'a, 'b> { 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::Stmt(stmt) => token::NtStmt(stmt.into_inner()), Annotatable::Expr(expr) => token::NtExpr(expr), })).into(); @@ -793,6 +803,15 @@ impl<'a> Parser<'a> { } Expansion::ImplItems(items) } + ExpansionKind::ForeignItems => { + let mut items = SmallVector::new(); + while self.token != token::Eof { + if let Some(item) = self.parse_foreign_item()? { + items.push(item); + } + } + Expansion::ForeignItems(items) + } ExpansionKind::Stmts => { let mut stmts = SmallVector::new(); while self.token != token::Eof && @@ -1166,6 +1185,44 @@ impl<'a, 'b> Folder for InvocationCollector<'a, 'b> { noop_fold_foreign_mod(self.cfg.configure_foreign_mod(foreign_mod), self) } + fn fold_foreign_item(&mut self, + foreign_item: ast::ForeignItem) -> SmallVector<ast::ForeignItem> { + let (attr, traits, foreign_item) = self.classify_item(foreign_item); + + let explain = if self.cx.ecfg.proc_macro_enabled() { + feature_gate::EXPLAIN_PROC_MACROS_IN_EXTERN + } else { + feature_gate::EXPLAIN_MACROS_IN_EXTERN + }; + + if attr.is_some() || !traits.is_empty() { + if !self.cx.ecfg.macros_in_extern_enabled() { + if let Some(ref attr) = attr { + emit_feature_err(&self.cx.parse_sess, "macros_in_extern", attr.span, + GateIssue::Language, explain); + } + } + + let item = Annotatable::ForeignItem(P(foreign_item)); + return self.collect_attr(attr, traits, item, ExpansionKind::ForeignItems) + .make_foreign_items(); + } + + if let ast::ForeignItemKind::Macro(mac) = foreign_item.node { + self.check_attributes(&foreign_item.attrs); + + if !self.cx.ecfg.macros_in_extern_enabled() { + emit_feature_err(&self.cx.parse_sess, "macros_in_extern", foreign_item.span, + GateIssue::Language, explain); + } + + return self.collect_bang(mac, foreign_item.span, ExpansionKind::ForeignItems) + .make_foreign_items(); + } + + noop_fold_foreign_item(foreign_item, self) + } + fn fold_item_kind(&mut self, item: ast::ItemKind) -> ast::ItemKind { match item { ast::ItemKind::MacroDef(..) => item, @@ -1311,6 +1368,7 @@ impl<'feat> ExpansionConfig<'feat> { fn enable_allow_internal_unstable = allow_internal_unstable, fn enable_custom_derive = custom_derive, fn proc_macro_enabled = proc_macro, + fn macros_in_extern_enabled = macros_in_extern, } } diff --git a/src/libsyntax/ext/placeholders.rs b/src/libsyntax/ext/placeholders.rs index 9c2c22476e9..9f60882ca29 100644 --- a/src/libsyntax/ext/placeholders.rs +++ b/src/libsyntax/ext/placeholders.rs @@ -60,6 +60,10 @@ pub fn placeholder(kind: ExpansionKind, id: ast::NodeId) -> Expansion { defaultness: ast::Defaultness::Final, tokens: None, })), + ExpansionKind::ForeignItems => Expansion::ForeignItems(SmallVector::one(ast::ForeignItem { + id, span, ident, vis, attrs, + node: ast::ForeignItemKind::Macro(mac_placeholder()), + })), ExpansionKind::Pat => Expansion::Pat(P(ast::Pat { id, span, node: ast::PatKind::Mac(mac_placeholder()), })), @@ -132,6 +136,13 @@ impl<'a, 'b> Folder for PlaceholderExpander<'a, 'b> { } } + fn fold_foreign_item(&mut self, item: ast::ForeignItem) -> SmallVector<ast::ForeignItem> { + match item.node { + ast::ForeignItemKind::Macro(_) => self.remove(item.id).make_foreign_items(), + _ => noop_fold_foreign_item(item, self), + } + } + fn fold_expr(&mut self, expr: P<ast::Expr>) -> P<ast::Expr> { match expr.node { ast::ExprKind::Mac(_) => self.remove(expr.id).make_expr(), |
