Skip to content

Commit 1934386

Browse files
Improves handling of statement macros.
Statement macros are now treated somewhat like item macros, in that a statement macro can now expand into a series of statements, rather than just a single statement. This allows statement macros to be nested inside other kinds of macros and expand properly, where previously the expansion would only work when no nesting was present. See: src/test/run-pass/macro-stmt_macro_in_expr_macro.rs src/test/run-pass/macro-nested_stmt_macro.rs This changes the interface of the MacResult trait. make_stmt has become make_stmts and now returns a vector, rather than a single item. Plugin writers who were implementing MacResult will have breakage, as well as anyone using MacEager::stmt. See: src/libsyntax/ext/base.rs This also causes a minor difference in behavior to the diagnostics produced by certain malformed macros. See: src/test/compile-fail/macro-incomplete-parse.rs
1 parent de51bbe commit 1934386

File tree

6 files changed

+138
-45
lines changed

6 files changed

+138
-45
lines changed

Diff for: src/libsyntax/ext/base.rs

+16-14
Original file line numberDiff line numberDiff line change
@@ -208,10 +208,11 @@ impl<F> IdentMacroExpander for F
208208
}
209209

210210
// Use a macro because forwarding to a simple function has type system issues
211-
macro_rules! make_stmt_default {
211+
macro_rules! make_stmts_default {
212212
($me:expr) => {
213213
$me.make_expr().map(|e| {
214-
P(codemap::respan(e.span, ast::StmtExpr(e, ast::DUMMY_NODE_ID)))
214+
SmallVector::one(P(codemap::respan(
215+
e.span, ast::StmtExpr(e, ast::DUMMY_NODE_ID))))
215216
})
216217
}
217218
}
@@ -238,12 +239,12 @@ pub trait MacResult {
238239
None
239240
}
240241

241-
/// Create a statement.
242+
/// Create zero or more statements.
242243
///
243244
/// By default this attempts to create an expression statement,
244245
/// returning None if that fails.
245-
fn make_stmt(self: Box<Self>) -> Option<P<ast::Stmt>> {
246-
make_stmt_default!(self)
246+
fn make_stmts(self: Box<Self>) -> Option<SmallVector<P<ast::Stmt>>> {
247+
make_stmts_default!(self)
247248
}
248249
}
249250

@@ -276,7 +277,7 @@ make_MacEager! {
276277
pat: P<ast::Pat>,
277278
items: SmallVector<P<ast::Item>>,
278279
impl_items: SmallVector<P<ast::ImplItem>>,
279-
stmt: P<ast::Stmt>,
280+
stmts: SmallVector<P<ast::Stmt>>,
280281
}
281282

282283
impl MacResult for MacEager {
@@ -292,10 +293,10 @@ impl MacResult for MacEager {
292293
self.impl_items
293294
}
294295

295-
fn make_stmt(self: Box<Self>) -> Option<P<ast::Stmt>> {
296-
match self.stmt {
297-
None => make_stmt_default!(self),
298-
s => s,
296+
fn make_stmts(self: Box<Self>) -> Option<SmallVector<P<ast::Stmt>>> {
297+
match self.stmts.as_ref().map_or(0, |s| s.len()) {
298+
0 => make_stmts_default!(self),
299+
_ => self.stmts,
299300
}
300301
}
301302

@@ -384,10 +385,11 @@ impl MacResult for DummyResult {
384385
Some(SmallVector::zero())
385386
}
386387
}
387-
fn make_stmt(self: Box<DummyResult>) -> Option<P<ast::Stmt>> {
388-
Some(P(codemap::respan(self.span,
389-
ast::StmtExpr(DummyResult::raw_expr(self.span),
390-
ast::DUMMY_NODE_ID))))
388+
fn make_stmts(self: Box<DummyResult>) -> Option<SmallVector<P<ast::Stmt>>> {
389+
Some(SmallVector::one(P(
390+
codemap::respan(self.span,
391+
ast::StmtExpr(DummyResult::raw_expr(self.span),
392+
ast::DUMMY_NODE_ID)))))
391393
}
392394
}
393395

Diff for: src/libsyntax/ext/expand.rs

+41-26
Original file line numberDiff line numberDiff line change
@@ -745,34 +745,49 @@ pub fn expand_item_mac(it: P<ast::Item>,
745745
}
746746

747747
/// Expand a stmt
748-
fn expand_stmt(s: Stmt, fld: &mut MacroExpander) -> SmallVector<P<Stmt>> {
749-
let (mac, style) = match s.node {
748+
fn expand_stmt(stmt: P<Stmt>, fld: &mut MacroExpander) -> SmallVector<P<Stmt>> {
749+
let stmt = stmt.and_then(|stmt| stmt);
750+
let (mac, style) = match stmt.node {
750751
StmtMac(mac, style) => (mac, style),
751-
_ => return expand_non_macro_stmt(s, fld)
752+
_ => return expand_non_macro_stmt(stmt, fld)
752753
};
753-
let expanded_stmt = match expand_mac_invoc(mac.and_then(|m| m), s.span,
754-
|r| r.make_stmt(),
755-
mark_stmt, fld) {
756-
Some(stmt) => stmt,
757-
None => {
758-
return SmallVector::zero();
754+
755+
let maybe_new_items =
756+
expand_mac_invoc(mac.and_then(|m| m), stmt.span,
757+
|r| r.make_stmts(),
758+
|stmts, mark| stmts.move_map(|m| mark_stmt(m, mark)),
759+
fld);
760+
761+
let fully_expanded = match maybe_new_items {
762+
Some(stmts) => {
763+
// Keep going, outside-in.
764+
let new_items = stmts.into_iter().flat_map(|s| {
765+
fld.fold_stmt(s).into_iter()
766+
}).collect();
767+
fld.cx.bt_pop();
768+
new_items
759769
}
770+
None => SmallVector::zero()
760771
};
761772

762-
// Keep going, outside-in.
763-
let fully_expanded = fld.fold_stmt(expanded_stmt);
764-
fld.cx.bt_pop();
765-
766-
if style == MacStmtWithSemicolon {
767-
fully_expanded.into_iter().map(|s| s.map(|Spanned {node, span}| {
768-
Spanned {
769-
node: match node {
770-
StmtExpr(e, stmt_id) => StmtSemi(e, stmt_id),
771-
_ => node /* might already have a semi */
772-
},
773-
span: span
774-
}
775-
})).collect()
773+
// If this is a macro invocation with a semicolon, then apply that
774+
// semicolon to the final statement produced by expansion.
775+
if style == MacStmtWithSemicolon && fully_expanded.len() > 0 {
776+
let last_index = fully_expanded.len() - 1;
777+
fully_expanded.into_iter().enumerate().map(|(i, stmt)|
778+
if i == last_index {
779+
stmt.map(|Spanned {node, span}| {
780+
Spanned {
781+
node: match node {
782+
StmtExpr(e, stmt_id) => StmtSemi(e, stmt_id),
783+
_ => node /* might already have a semi */
784+
},
785+
span: span
786+
}
787+
})
788+
} else {
789+
stmt
790+
}).collect()
776791
} else {
777792
fully_expanded
778793
}
@@ -1389,7 +1404,7 @@ impl<'a, 'b> Folder for MacroExpander<'a, 'b> {
13891404
}
13901405

13911406
fn fold_stmt(&mut self, stmt: P<ast::Stmt>) -> SmallVector<P<ast::Stmt>> {
1392-
stmt.and_then(|stmt| expand_stmt(stmt, self))
1407+
expand_stmt(stmt, self)
13931408
}
13941409

13951410
fn fold_block(&mut self, block: P<Block>) -> P<Block> {
@@ -1541,8 +1556,8 @@ fn mark_pat(pat: P<ast::Pat>, m: Mrk) -> P<ast::Pat> {
15411556
}
15421557

15431558
// apply a given mark to the given stmt. Used following the expansion of a macro.
1544-
fn mark_stmt(expr: P<ast::Stmt>, m: Mrk) -> P<ast::Stmt> {
1545-
Marker{mark:m}.fold_stmt(expr)
1559+
fn mark_stmt(stmt: P<ast::Stmt>, m: Mrk) -> P<ast::Stmt> {
1560+
Marker{mark:m}.fold_stmt(stmt)
15461561
.expect_one("marking a stmt didn't return exactly one stmt")
15471562
}
15481563

Diff for: src/libsyntax/ext/tt/macro_rules.rs

+18-4
Original file line numberDiff line numberDiff line change
@@ -88,10 +88,24 @@ impl<'a> MacResult for ParserAnyMacro<'a> {
8888
Some(ret)
8989
}
9090

91-
fn make_stmt(self: Box<ParserAnyMacro<'a>>) -> Option<P<ast::Stmt>> {
92-
let ret = self.parser.borrow_mut().parse_stmt();
93-
self.ensure_complete_parse(true);
94-
ret
91+
fn make_stmts(self: Box<ParserAnyMacro<'a>>)
92+
-> Option<SmallVector<P<ast::Stmt>>> {
93+
let mut ret = SmallVector::zero();
94+
loop {
95+
let mut parser = self.parser.borrow_mut();
96+
match parser.token {
97+
token::Eof => break,
98+
_ => match parser.parse_stmt_nopanic() {
99+
Ok(maybe_stmt) => match maybe_stmt {
100+
Some(stmt) => ret.push(stmt),
101+
None => (),
102+
},
103+
Err(_) => break,
104+
}
105+
}
106+
}
107+
self.ensure_complete_parse(false);
108+
Some(ret)
95109
}
96110
}
97111

Diff for: src/test/compile-fail/macro-incomplete-parse.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ macro_rules! ignored_item {
1717
}
1818

1919
macro_rules! ignored_expr {
20-
() => ( 1, 2 ) //~ ERROR macro expansion ignores token `,`
20+
() => ( 1, //~ ERROR unexpected token: `,`
21+
2 ) //~ ERROR macro expansion ignores token `2`
2122
}
2223

2324
macro_rules! ignored_pat {

Diff for: src/test/run-pass/macro-nested_stmt_macros.rs

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// Copyright 2015 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
macro_rules! foo {
12+
() => {
13+
struct Bar;
14+
struct Baz;
15+
}
16+
}
17+
18+
macro_rules! grault {
19+
() => {
20+
foo!();
21+
struct Xyzzy;
22+
}
23+
}
24+
25+
fn static_assert_exists<T>() { }
26+
27+
fn main() {
28+
grault!();
29+
static_assert_exists::<Bar>();
30+
static_assert_exists::<Baz>();
31+
static_assert_exists::<Xyzzy>();
32+
}

Diff for: src/test/run-pass/macro-stmt_macro_in_expr_macro.rs

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// Copyright 2015 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
macro_rules! foo {
12+
() => {
13+
struct Bar;
14+
struct Baz;
15+
}
16+
}
17+
18+
macro_rules! grault {
19+
() => {{
20+
foo!();
21+
struct Xyzzy;
22+
0
23+
}}
24+
}
25+
26+
fn main() {
27+
let x = grault!();
28+
assert_eq!(x, 0);
29+
}

0 commit comments

Comments
 (0)