about summary refs log tree commit diff
diff options
context:
space:
mode:
authorPatrick Walton <pcwalton@mimiga.net>2013-01-22 16:00:07 -0800
committerPatrick Walton <pcwalton@mimiga.net>2013-01-23 14:46:24 -0800
commit154488df1999e4c42f96ebaa6ee6c08be3f999a6 (patch)
treea40399560573d6d8709fceae1bc2ffcc2df230c8
parent54b2cad8b341434c7c0edb153a0fa662fb2981f1 (diff)
downloadrust-154488df1999e4c42f96ebaa6ee6c08be3f999a6.tar.gz
rust-154488df1999e4c42f96ebaa6ee6c08be3f999a6.zip
libsyntax: Implement `assert` as a macro (called `fail_unless!` on a transitionary basis to avoid conflicting with the keyword right now). r=brson
-rw-r--r--src/libsyntax/ext/expand.rs8
-rw-r--r--src/libsyntax/parse/token.rs29
-rw-r--r--src/test/run-fail/assert-as-macro.rs6
3 files changed, 31 insertions, 12 deletions
diff --git a/src/libsyntax/ext/expand.rs b/src/libsyntax/ext/expand.rs
index 7c641b0e985..068b372c069 100644
--- a/src/libsyntax/ext/expand.rs
+++ b/src/libsyntax/ext/expand.rs
@@ -300,6 +300,14 @@ fn core_macros() -> ~str {
         )
     )
 
+    macro_rules! fail_unless(
+        ($cond:expr) => {
+            if !$cond {
+                die!(~\"assertion failed: \" + stringify!($cond))
+            }
+        }
+    )
+
     macro_rules! condition (
 
         { $c:ident: $in:ty -> $out:ty; } => {
diff --git a/src/libsyntax/parse/token.rs b/src/libsyntax/parse/token.rs
index 93c03063623..2136499f8f0 100644
--- a/src/libsyntax/parse/token.rs
+++ b/src/libsyntax/parse/token.rs
@@ -200,19 +200,24 @@ fn to_str(in: @ident_interner, t: Token) -> ~str {
       DOC_COMMENT(s) => *in.get(s),
       EOF => ~"<eof>",
       INTERPOLATED(ref nt) => {
-        ~"an interpolated " +
-            match (*nt) {
-              nt_item(*) => ~"item",
-              nt_block(*) => ~"block",
-              nt_stmt(*) => ~"statement",
-              nt_pat(*) => ~"pattern",
-              nt_expr(*) => ~"expression",
-              nt_ty(*) => ~"type",
-              nt_ident(*) => ~"identifier",
-              nt_path(*) => ~"path",
-              nt_tt(*) => ~"tt",
-              nt_matchers(*) => ~"matcher sequence"
+        match nt {
+            &nt_expr(e) => ::print::pprust::expr_to_str(e, in),
+            _ => {
+                ~"an interpolated " +
+                    match (*nt) {
+                      nt_item(*) => ~"item",
+                      nt_block(*) => ~"block",
+                      nt_stmt(*) => ~"statement",
+                      nt_pat(*) => ~"pattern",
+                      nt_expr(*) => fail ~"should have been handled above",
+                      nt_ty(*) => ~"type",
+                      nt_ident(*) => ~"identifier",
+                      nt_path(*) => ~"path",
+                      nt_tt(*) => ~"tt",
+                      nt_matchers(*) => ~"matcher sequence"
+                    }
             }
+        }
       }
     }
 }
diff --git a/src/test/run-fail/assert-as-macro.rs b/src/test/run-fail/assert-as-macro.rs
new file mode 100644
index 00000000000..8bb1e278fd1
--- /dev/null
+++ b/src/test/run-fail/assert-as-macro.rs
@@ -0,0 +1,6 @@
+// error-pattern:assertion failed: 1 == 2
+
+fn main() {
+    fail_unless!(1 == 2);
+}
+