Skip to content

Commit 9fb11fe

Browse files
committed
Extend macro machinery to expand macros in types
Reapplied the changes from https://github.com/freebroccolo/rust/commit/7aafe24139abc2d1f302bbb166bcaa006f12cf4d to a clean branch of master
1 parent edca562 commit 9fb11fe

File tree

3 files changed

+58
-0
lines changed

3 files changed

+58
-0
lines changed

src/libsyntax/ext/base.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -290,6 +290,10 @@ pub trait MacResult {
290290
fn make_stmts(self: Box<Self>) -> Option<SmallVector<P<ast::Stmt>>> {
291291
make_stmts_default!(self)
292292
}
293+
294+
fn make_ty(self: Box<Self>) -> Option<P<ast::Ty>> {
295+
None
296+
}
293297
}
294298

295299
macro_rules! make_MacEager {
@@ -322,6 +326,7 @@ make_MacEager! {
322326
items: SmallVector<P<ast::Item>>,
323327
impl_items: SmallVector<P<ast::ImplItem>>,
324328
stmts: SmallVector<P<ast::Stmt>>,
329+
ty: P<ast::Ty>,
325330
}
326331

327332
impl MacResult for MacEager {
@@ -359,6 +364,10 @@ impl MacResult for MacEager {
359364
}
360365
None
361366
}
367+
368+
fn make_ty(self: Box<Self>) -> Option<P<ast::Ty>> {
369+
self.ty
370+
}
362371
}
363372

364373
/// Fill-in macro expansion result, to allow compilation to continue
@@ -405,6 +414,12 @@ impl DummyResult {
405414
}
406415
}
407416

417+
pub fn raw_ty(sp: Span) -> P<ast::Ty> {
418+
P(ast::Ty {
419+
id: ast:DUMMY_NODE_ID,
420+
node: ast::TyInfer,
421+
span: sp
422+
})
408423
}
409424

410425
impl MacResult for DummyResult {

src/libsyntax/ext/expand.rs

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1552,6 +1552,35 @@ fn expand_and_rename_method(sig: ast::MethodSig, body: P<ast::Block>,
15521552
}, rewritten_body)
15531553
}
15541554

1555+
pub fn expand_type(t: P<ast::Ty>, fld: &mut MacroExpander) -> P<ast::Ty> {
1556+
let t = match t.node.clone() {
1557+
ast::Ty_::TyMac(mac) => {
1558+
let expanded_ty = match expand_mac_invoc(mac, t.span,
1559+
|r| r.make_ty(),
1560+
mark_ty,
1561+
fld) {
1562+
Some(ty) => ty,
1563+
None => {
1564+
return DummyResult::raw_ty(t.span);
1565+
}
1566+
};
1567+
1568+
// Keep going, outside-in.
1569+
//
1570+
let fully_expanded = fld.fold_ty(expanded_ty);
1571+
fld.cx.bt_pop();
1572+
1573+
fully_expanded.map(|t| ast::Ty {
1574+
id: ast::DUMMY_NODE_ID,
1575+
node: t.node,
1576+
span: t.span,
1577+
})
1578+
}
1579+
_ => t
1580+
};
1581+
fold::noop_fold_ty(t, fld)
1582+
}
1583+
15551584
/// A tree-folder that performs macro expansion
15561585
pub struct MacroExpander<'a, 'b:'a> {
15571586
pub cx: &'a mut ExtCtxt<'b>,
@@ -1602,6 +1631,10 @@ impl<'a, 'b> Folder for MacroExpander<'a, 'b> {
16021631
.into_iter().map(|i| i.expect_impl_item()).collect()
16031632
}
16041633

1634+
fn fold_ty(&mut self, ty: P<ast::Ty>) -> P<ast::Ty> {
1635+
expand_type(ty, self)
1636+
}
1637+
16051638
fn new_span(&mut self, span: Span) -> Span {
16061639
new_span(self.cx, span)
16071640
}
@@ -1748,6 +1781,10 @@ fn mark_impl_item(ii: P<ast::ImplItem>, m: Mrk) -> P<ast::ImplItem> {
17481781
.expect_one("marking an impl item didn't return exactly one impl item")
17491782
}
17501783

1784+
fn mark_ty(ty: P<ast::Ty>, m: Mrk) -> P<ast::Ty> {
1785+
Marker { mark: m }.fold_ty(ty)
1786+
}
1787+
17511788
/// Check that there are no macro invocations left in the AST:
17521789
pub fn check_for_macros(sess: &parse::ParseSess, krate: &ast::Crate) {
17531790
visit::walk_crate(&mut MacroExterminator{sess:sess}, krate);

src/libsyntax/ext/tt/macro_rules.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,12 @@ impl<'a> MacResult for ParserAnyMacro<'a> {
117117
self.ensure_complete_parse(false);
118118
Some(ret)
119119
}
120+
121+
fn make_ty(self: Box<ParserAnyMacro<'a>>) -> Option<P<ast::Ty>> {
122+
let ret = self.parser.borrow_mut().parse_ty();
123+
self.ensure_complete_parse(true);
124+
Some(ret)
125+
}
120126
}
121127

122128
struct MacroRulesMacroExpander {

0 commit comments

Comments
 (0)