Skip to content

Commit 125436d

Browse files
committed
Better default assert messages when no message is specified #2122
1 parent 900365c commit 125436d

File tree

3 files changed

+41
-0
lines changed

3 files changed

+41
-0
lines changed

releasenotes.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22

33
## 0.7.2 Change list
44

5+
### Changes / improvements
6+
- Better default assert messages when no message is specified #2122
7+
58
### Fixes
69
- Assert triggered when casting from `int[2]` to `uint[2]` #2115
710
- Assert when a macro with compile time value is discarded, e.g. `foo();` where `foo()` returns an untyped list. #2117

src/compiler/sema_stmts.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,15 @@ static inline bool sema_analyse_assert_stmt(SemaContext *context, Ast *statement
181181
return true;
182182
case COND_MISSING:
183183
// If the assert isn't compile time resolvable, we keep the assert.
184+
if (!message_expr)
185+
{
186+
scratch_buffer_clear();
187+
scratch_buffer_append("Assert \"");
188+
span_to_scratch(expr->span);
189+
scratch_buffer_append("\" failed.");
190+
message_expr = expr_new_const_string(expr->span, scratch_buffer_copy());
191+
statement->assert_stmt.message = exprid(message_expr);
192+
}
184193
return true;
185194
}
186195
UNREACHABLE
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// #target: macos-x64
2+
// #safe: yes
3+
module test;
4+
fn void main()
5+
{
6+
int a = -1;
7+
assert(a > 0);
8+
}
9+
10+
/* #expect: test.ll
11+
12+
@.panic_msg = internal constant [23 x i8] c"Assert \22a > 0\22 failed.\00", align 1
13+
14+
define void @test.main() #0 {
15+
entry:
16+
%a = alloca i32, align 4
17+
store i32 -1, ptr %a, align 4
18+
%0 = load i32, ptr %a, align 4
19+
%gt = icmp sgt i32 %0, 0
20+
br i1 %gt, label %assert_ok, label %assert_fail
21+
22+
assert_fail: ; preds = %entry
23+
%1 = load ptr, ptr @std.core.builtin.panic, align 8
24+
call void %1(ptr @.panic_msg, i64 22, ptr @.file, i64 23, ptr @.func, i64 4, i32 5) #1
25+
unreachable
26+
27+
assert_ok: ; preds = %entry
28+
ret void
29+
}

0 commit comments

Comments
 (0)