]> git.lizzy.rs Git - rust.git/commitdiff
Allow $foo:block nonterminals in expression position
authorKevin Ballard <kevin@sb.org>
Mon, 26 May 2014 01:33:52 +0000 (18:33 -0700)
committerKevin Ballard <kevin@sb.org>
Mon, 26 May 2014 05:33:12 +0000 (22:33 -0700)
Fixes #13678.

src/libsyntax/parse/parser.rs
src/test/run-pass/macro-block-nonterminal.rs [new file with mode: 0644]

index bfdf0361f053c6d8fa16a49de3aa091753d652b8..ae5f16c2580e2c920d6aa4027e44cbd4e01285ba 100644 (file)
@@ -135,17 +135,21 @@ enum ItemOrViewItem {
 macro_rules! maybe_whole_expr (
     ($p:expr) => (
         {
-            let mut maybe_path = match ($p).token {
-                INTERPOLATED(token::NtPath(ref pt)) => Some((**pt).clone()),
-                _ => None,
-            };
-            let found = match ($p).token {
+            let found = match $p.token {
                 INTERPOLATED(token::NtExpr(e)) => {
                     Some(e)
                 }
                 INTERPOLATED(token::NtPath(_)) => {
-                    let pt = maybe_path.take_unwrap();
-                    Some($p.mk_expr(($p).span.lo, ($p).span.hi, ExprPath(pt)))
+                    // FIXME: The following avoids an issue with lexical borrowck scopes,
+                    // but the clone is unfortunate.
+                    let pt = match $p.token {
+                        INTERPOLATED(token::NtPath(ref pt)) => (**pt).clone(),
+                        _ => unreachable!()
+                    };
+                    Some($p.mk_expr($p.span.lo, $p.span.hi, ExprPath(pt)))
+                }
+                INTERPOLATED(token::NtBlock(b)) => {
+                    Some($p.mk_expr($p.span.lo, $p.span.hi, ExprBlock(b)))
                 }
                 _ => None
             };
diff --git a/src/test/run-pass/macro-block-nonterminal.rs b/src/test/run-pass/macro-block-nonterminal.rs
new file mode 100644 (file)
index 0000000..8a9fbbe
--- /dev/null
@@ -0,0 +1,21 @@
+// Copyright 2014 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.
+
+#![feature(macro_rules)]
+
+macro_rules! do_block{
+    ($val:block) => {$val}
+}
+
+fn main() {
+    let s;
+    do_block!({ s = "it works!"; });
+    assert_eq!(s, "it works!");
+}