Skip to content

Commit

Permalink
Allow syntax extensions to return multiple items, closes #16723.
Browse files Browse the repository at this point in the history
This patch replaces `MacItem` with `MacItems`.
  • Loading branch information
fhahn committed Sep 19, 2014
1 parent af3889f commit 89b0944
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 20 deletions.
8 changes: 4 additions & 4 deletions src/libsyntax/diagnostics/plugin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use std::collections::HashMap;
use ast;
use ast::{Ident, Name, TokenTree};
use codemap::Span;
use ext::base::{ExtCtxt, MacExpr, MacItem, MacResult};
use ext::base::{ExtCtxt, MacExpr, MacResult, MacItems};
use ext::build::AstBuilder;
use parse::token;
use ptr::P;
Expand Down Expand Up @@ -102,7 +102,7 @@ pub fn expand_register_diagnostic<'cx>(ecx: &'cx mut ExtCtxt,
let sym = Ident::new(token::gensym((
"__register_diagnostic_".to_string() + token::get_ident(*code).get()
).as_slice()));
MacItem::new(quote_item!(ecx, mod $sym {}).unwrap())
MacItems::new(vec![quote_item!(ecx, mod $sym {}).unwrap()].into_iter())
}

pub fn expand_build_diagnostic_array<'cx>(ecx: &'cx mut ExtCtxt,
Expand Down Expand Up @@ -133,7 +133,7 @@ pub fn expand_build_diagnostic_array<'cx>(ecx: &'cx mut ExtCtxt,
(descriptions.len(), ecx.expr_vec(span, descriptions))
})
});
MacItem::new(quote_item!(ecx,
MacItems::new(vec![quote_item!(ecx,
pub static $name: [(&'static str, &'static str), ..$count] = $expr;
).unwrap())
).unwrap()].into_iter())
}
27 changes: 11 additions & 16 deletions src/libsyntax/ext/base.rs
Original file line number Diff line number Diff line change
Expand Up @@ -203,25 +203,20 @@ impl MacResult for MacPat {
Some(self.p)
}
}
/// A convenience type for macros that return a single item.
pub struct MacItem {
i: P<ast::Item>
/// A type for macros that return multiple items.
pub struct MacItems {
items: SmallVector<P<ast::Item>>
}
impl MacItem {
pub fn new(i: P<ast::Item>) -> Box<MacResult+'static> {
box MacItem { i: i } as Box<MacResult+'static>

impl MacItems {
pub fn new<I: Iterator<P<ast::Item>>>(mut it: I) -> Box<MacResult+'static> {
box MacItems { items: it.collect() } as Box<MacResult+'static>
}
}
impl MacResult for MacItem {
fn make_items(self: Box<MacItem>) -> Option<SmallVector<P<ast::Item>>> {
Some(SmallVector::one(self.i))
}
fn make_stmt(self: Box<MacItem>) -> Option<P<ast::Stmt>> {
Some(P(codemap::respan(
self.i.span,
ast::StmtDecl(
P(codemap::respan(self.i.span, ast::DeclItem(self.i))),
ast::DUMMY_NODE_ID))))

impl MacResult for MacItems {
fn make_items(self: Box<MacItems>) -> Option<SmallVector<P<ast::Item>>> {
Some(self.items)
}
}

Expand Down
33 changes: 33 additions & 0 deletions src/test/auxiliary/issue_16723_multiple_items_syntax_ext.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// Copyright 2012 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
//
// ignore-stage1
#![feature(plugin_registrar, managed_boxes, quote)]
#![crate_type = "dylib"]

extern crate syntax;
extern crate rustc;

use syntax::ast;
use syntax::codemap;
use syntax::ext::base::{ExtCtxt, MacResult, MacItems};
use rustc::plugin::Registry;

#[plugin_registrar]
pub fn plugin_registrar(reg: &mut Registry) {
reg.register_macro("multiple_items", expand)
}

fn expand(cx: &mut ExtCtxt, _: codemap::Span, _: &[ast::TokenTree]) -> Box<MacResult+'static> {
MacItems::new(vec![
quote_item!(cx, struct Struct1;).unwrap(),
quote_item!(cx, struct Struct2;).unwrap()
].into_iter())
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// Copyright 2012 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

// ignore-stage1
// aux-build:issue_16723_multiple_items_syntax_ext.rs
#![feature(phase)]

#[phase(plugin)] extern crate issue_16723_multiple_items_syntax_ext;

multiple_items!()

impl Struct1 {
fn foo() {}
}
impl Struct2 {
fn foo() {}
}

fn main() {
Struct1::foo();
Struct2::foo();
println!("hallo");
}

8 comments on commit 89b0944

@bors
Copy link
Contributor

@bors bors commented on 89b0944 Sep 19, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

saw approval from alexcrichton
at fhahn@89b0944

@bors
Copy link
Contributor

@bors bors commented on 89b0944 Sep 19, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

merging fhahn/rust/issue-16723-multiple-items = 89b0944 into auto

@bors
Copy link
Contributor

@bors bors commented on 89b0944 Sep 19, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fhahn/rust/issue-16723-multiple-items = 89b0944 merged ok, testing candidate = b5b72b75

@bors
Copy link
Contributor

@bors bors commented on 89b0944 Sep 20, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

saw approval from alexcrichton
at fhahn@89b0944

@bors
Copy link
Contributor

@bors bors commented on 89b0944 Sep 20, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

merging fhahn/rust/issue-16723-multiple-items = 89b0944 into auto

@bors
Copy link
Contributor

@bors bors commented on 89b0944 Sep 20, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fhahn/rust/issue-16723-multiple-items = 89b0944 merged ok, testing candidate = c8ea5403

@bors
Copy link
Contributor

@bors bors commented on 89b0944 Sep 20, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bors
Copy link
Contributor

@bors bors commented on 89b0944 Sep 20, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.