Skip to content

Commit 154488d

Browse files
committed
libsyntax: Implement assert as a macro (called fail_unless! on a transitionary basis to avoid conflicting with the keyword right now). r=brson
1 parent 54b2cad commit 154488d

File tree

3 files changed

+31
-12
lines changed

3 files changed

+31
-12
lines changed

src/libsyntax/ext/expand.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -300,6 +300,14 @@ fn core_macros() -> ~str {
300300
)
301301
)
302302

303+
macro_rules! fail_unless(
304+
($cond:expr) => {
305+
if !$cond {
306+
die!(~\"assertion failed: \" + stringify!($cond))
307+
}
308+
}
309+
)
310+
303311
macro_rules! condition (
304312

305313
{ $c:ident: $in:ty -> $out:ty; } => {

src/libsyntax/parse/token.rs

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -200,19 +200,24 @@ fn to_str(in: @ident_interner, t: Token) -> ~str {
200200
DOC_COMMENT(s) => *in.get(s),
201201
EOF => ~"<eof>",
202202
INTERPOLATED(ref nt) => {
203-
~"an interpolated " +
204-
match (*nt) {
205-
nt_item(*) => ~"item",
206-
nt_block(*) => ~"block",
207-
nt_stmt(*) => ~"statement",
208-
nt_pat(*) => ~"pattern",
209-
nt_expr(*) => ~"expression",
210-
nt_ty(*) => ~"type",
211-
nt_ident(*) => ~"identifier",
212-
nt_path(*) => ~"path",
213-
nt_tt(*) => ~"tt",
214-
nt_matchers(*) => ~"matcher sequence"
203+
match nt {
204+
&nt_expr(e) => ::print::pprust::expr_to_str(e, in),
205+
_ => {
206+
~"an interpolated " +
207+
match (*nt) {
208+
nt_item(*) => ~"item",
209+
nt_block(*) => ~"block",
210+
nt_stmt(*) => ~"statement",
211+
nt_pat(*) => ~"pattern",
212+
nt_expr(*) => fail ~"should have been handled above",
213+
nt_ty(*) => ~"type",
214+
nt_ident(*) => ~"identifier",
215+
nt_path(*) => ~"path",
216+
nt_tt(*) => ~"tt",
217+
nt_matchers(*) => ~"matcher sequence"
218+
}
215219
}
220+
}
216221
}
217222
}
218223
}

src/test/run-fail/assert-as-macro.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
// error-pattern:assertion failed: 1 == 2
2+
3+
fn main() {
4+
fail_unless!(1 == 2);
5+
}
6+

0 commit comments

Comments
 (0)