Skip to content

Commit

Permalink
syntax: Unquoting some statements requires trailing semicolons
Browse files Browse the repository at this point in the history
  • Loading branch information
erickt committed May 15, 2015
1 parent 7b00658 commit b62290d
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 3 deletions.
14 changes: 11 additions & 3 deletions src/libsyntax/ext/quote.rs
Expand Up @@ -28,8 +28,7 @@ pub mod rt {
use ast;
use codemap::Spanned;
use ext::base::ExtCtxt;
use parse::token;
use parse;
use parse::{self, token, classify};
use ptr::P;
use std::rc::Rc;

Expand Down Expand Up @@ -126,7 +125,16 @@ pub mod rt {

impl ToTokens for P<ast::Stmt> {
fn to_tokens(&self, _cx: &ExtCtxt) -> Vec<TokenTree> {
vec![ast::TtToken(self.span, token::Interpolated(token::NtStmt(self.clone())))]
let mut tts = vec![
ast::TtToken(self.span, token::Interpolated(token::NtStmt(self.clone())))
];

// Some statements require a trailing semicolon.
if classify::stmt_ends_with_semi(&self.node) {
tts.push(ast::TtToken(self.span, token::Semi));
}

tts
}
}

Expand Down
6 changes: 6 additions & 0 deletions src/test/run-pass-fulldeps/quote-tokens.rs
Expand Up @@ -43,6 +43,12 @@ fn syntax_extension(cx: &ExtCtxt) {
let _n: syntax::ast::Attribute = quote_attr!(cx, #![cfg(foo, bar = "baz")]);

let _o: Option<P<syntax::ast::Item>> = quote_item!(cx, fn foo<T: ?Sized>() {});

let stmts = vec![
quote_stmt!(cx, let x = 1;).unwrap(),
quote_stmt!(cx, let y = 2;).unwrap(),
];
let expr: P<syntax::ast::Expr> = quote_expr!(cx, x + y);
}

fn main() {
Expand Down

0 comments on commit b62290d

Please sign in to comment.