@ensure was not included when the function doesn't return a value #2098.

This commit is contained in:
Christoffer Lerno
2025-04-17 20:26:21 +02:00
parent 72d7813c20
commit ba10c8953d
3 changed files with 146 additions and 7 deletions

View File

@@ -26,6 +26,7 @@
- `@if` now does implicit conversion to bool like `$if`. #2086
- Fix broken enum inline -> bool conversions #2094.
- `@if` was ignored on attrdef, regression 0.7 #2093.
- `@ensure` was not included when the function doesn't return a value #2098.
### Stdlib changes
- Hash functions for integer vectors and arrays.

View File

@@ -651,6 +651,8 @@ static inline bool sema_analyse_return_stmt(SemaContext *context, Ast *statement
if (!sema_analyse_expr_rhs(context, expected_rtype, return_expr, type_is_optional(expected_rtype), NULL, false)) return false;
if (!sema_check_not_stack_variable_escape(context, return_expr)) return false;
if (!sema_check_return_matches_opt_returns(context, return_expr)) return false;
// Process any ensures.
sema_inline_return_defers(context, statement, context->active_scope.defer_last, 0);
}
else
{
@@ -660,11 +662,8 @@ static inline bool sema_analyse_return_stmt(SemaContext *context, Ast *statement
return false;
}
statement->return_stmt.cleanup = context_get_defers(context, context->active_scope.defer_last, 0, true);
return true;
}
// Process any ensures.
sema_inline_return_defers(context, statement, context->active_scope.defer_last, 0);
if (context->call_env.ensures)
{
// Never generate an expression.
@@ -700,7 +699,7 @@ static inline bool sema_analyse_return_stmt(SemaContext *context, Ast *statement
}
SKIP_ENSURE:;
ASSERT(type_no_optional(statement->return_stmt.expr->type)->canonical == type_no_optional(expected_rtype)->canonical);
ASSERT(!return_expr || type_no_optional(statement->return_stmt.expr->type)->canonical == type_no_optional(expected_rtype)->canonical);
return true;
}
@@ -1777,8 +1776,9 @@ static inline bool sema_analyse_if_stmt(SemaContext *context, Ast *statement)
}
AstId else_id = statement->if_stmt.else_body;
Ast *else_body = else_id ? astptr(else_id) : NULL;
CondResult result = COND_MISSING;
SCOPE_OUTER_START
CondResult result = COND_MISSING;
success = sema_analyse_cond(context, cond, COND_TYPE_UNWRAP_BOOL, &result);
if (success && !ast_ok(then))
@@ -1844,6 +1844,14 @@ END:
{
context->active_scope.jump_end = true;
}
else if (then_jump && result == COND_TRUE)
{
context->active_scope.jump_end = true;
}
else if (else_jump && result == COND_FALSE)
{
context->active_scope.jump_end = true;
}
return true;
}
@@ -3179,12 +3187,31 @@ bool sema_analyse_function_body(SemaContext *context, Decl *func)
bool is_naked = func->func_decl.attr_naked;
if (!is_naked) sema_append_contract_asserts(assert_first, body);
Type *canonical_rtype = type_no_optional(prototype->rtype)->canonical;
if (has_ensures && type_is_void(canonical_rtype))
{
AstId* append_pos = &body->compound_stmt.first_stmt;
if (*append_pos)
{
Ast *last = ast_last(astptr(*append_pos));
if (last->ast_kind == AST_RETURN_STMT) goto NEXT;
append_pos = &last->next;
}
Ast *ret = ast_calloc();
ret->ast_kind = AST_RETURN_STMT;
ret->span = body->span;
*append_pos = astid(ret);
}
NEXT:
if (!sema_analyse_compound_statement_no_scope(context, body)) return false;
ASSERT_SPAN(func,context->active_scope.depth == 1);
if (!context->active_scope.jump_end && canonical_rtype != type_void)
if (!context->active_scope.jump_end)
{
RETURN_SEMA_ERROR(func, "Missing return statement at the end of the function.");
if (canonical_rtype != type_void)
{
RETURN_SEMA_ERROR(func, "Missing return statement at the end of the function.");
}
}
SCOPE_END;
if (lambda_params)
{

View File

@@ -0,0 +1,111 @@
// #target: macos-x64
// #safe: yes
module test;
fn int main()
{
int a;
test(&a, 1);
return 0;
}
<*
@ensure *a < b
*>
fn void test(int* a, int b)
{
*a = b + 1;
}
/* #expect: test.ll
define void @test.test(ptr %0, i32 %1) #0 {
entry:
%taddr = alloca i64, align 8
%taddr2 = alloca i64, align 8
%varargslots = alloca [2 x %any], align 16
%indirectarg = alloca %"any[]", align 8
%taddr8 = alloca i64, align 8
%taddr9 = alloca i64, align 8
%varargslots10 = alloca [2 x %any], align 16
%indirectarg13 = alloca %"any[]", align 8
%checknull = icmp eq ptr %0, null
%2 = call i1 @llvm.expect.i1(i1 %checknull, i1 false)
br i1 %2, label %panic, label %checkok
checkok: ; preds = %entry
%3 = ptrtoint ptr %0 to i64
%4 = urem i64 %3, 4
%5 = icmp ne i64 %4, 0
%6 = call i1 @llvm.expect.i1(i1 %5, i1 false)
br i1 %6, label %panic1, label %checkok3
checkok3: ; preds = %checkok
%add = add i32 %1, 1
store i32 %add, ptr %0, align 4
%checknull4 = icmp eq ptr %0, null
%7 = call i1 @llvm.expect.i1(i1 %checknull4, i1 false)
br i1 %7, label %panic5, label %checkok6
checkok6: ; preds = %checkok3
%8 = ptrtoint ptr %0 to i64
%9 = urem i64 %8, 4
%10 = icmp ne i64 %9, 0
%11 = call i1 @llvm.expect.i1(i1 %10, i1 false)
br i1 %11, label %panic7, label %checkok14
checkok14: ; preds = %checkok6
%12 = load i32, ptr %0, align 4
%lt = icmp slt i32 %12, %1
br i1 %lt, label %assert_ok, label %assert_fail
assert_fail: ; preds = %checkok14
%13 = load ptr, ptr @std.core.builtin.panic, align 8
call void %13(ptr @.panic_msg.2, i64 26, ptr @.file, i64 24, ptr @.func, i64 4, i32 14) #2
unreachable
assert_ok: ; preds = %checkok14
ret void
panic: ; preds = %entry
%14 = load ptr, ptr @std.core.builtin.panic, align 8
call void %14(ptr @.panic_msg, i64 42, ptr @.file, i64 24, ptr @.func, i64 4, i32 15) #2
unreachable
panic1: ; preds = %checkok
store i64 4, ptr %taddr, align 8
%15 = insertvalue %any undef, ptr %taddr, 0
%16 = insertvalue %any %15, i64 ptrtoint (ptr @"$ct.ulong" to i64), 1
store i64 %4, ptr %taddr2, align 8
%17 = insertvalue %any undef, ptr %taddr2, 0
%18 = insertvalue %any %17, i64 ptrtoint (ptr @"$ct.ulong" to i64), 1
store %any %16, ptr %varargslots, align 16
%ptradd = getelementptr inbounds i8, ptr %varargslots, i64 16
store %any %18, ptr %ptradd, align 16
%19 = insertvalue %"any[]" undef, ptr %varargslots, 0
%"$$temp" = insertvalue %"any[]" %19, i64 2, 1
store %"any[]" %"$$temp", ptr %indirectarg, align 8
call void @std.core.builtin.panicf(ptr @.panic_msg.1, i64 94, ptr @.file, i64 24, ptr @.func, i64 4, i32 15, ptr byval(%"any[]") align 8 %indirectarg) #2
unreachable
panic5: ; preds = %checkok3
%20 = load ptr, ptr @std.core.builtin.panic, align 8
call void %20(ptr @.panic_msg, i64 42, ptr @.file, i64 24, ptr @.func, i64 4, i32 11) #2
unreachable
panic7: ; preds = %checkok6
store i64 4, ptr %taddr8, align 8
%21 = insertvalue %any undef, ptr %taddr8, 0
%22 = insertvalue %any %21, i64 ptrtoint (ptr @"$ct.ulong" to i64), 1
store i64 %9, ptr %taddr9, align 8
%23 = insertvalue %any undef, ptr %taddr9, 0
%24 = insertvalue %any %23, i64 ptrtoint (ptr @"$ct.ulong" to i64), 1
store %any %22, ptr %varargslots10, align 16
%ptradd11 = getelementptr inbounds i8, ptr %varargslots10, i64 16
store %any %24, ptr %ptradd11, align 16
%25 = insertvalue %"any[]" undef, ptr %varargslots10, 0
%"$$temp12" = insertvalue %"any[]" %25, i64 2, 1
store %"any[]" %"$$temp12", ptr %indirectarg13, align 8
call void @std.core.builtin.panicf(ptr @.panic_msg.1, i64 94, ptr @.file, i64 24, ptr @.func, i64 4, i32 11, ptr byval(%"any[]") align 8 %indirectarg13) #2
unreachable
}