diff --git a/releasenotes.md b/releasenotes.md index e1f9b46b1..34b14b6f9 100644 --- a/releasenotes.md +++ b/releasenotes.md @@ -22,6 +22,7 @@ - Correctly detect multiple overloads of the same type. - ABI bug on x64 Linux / MacOS when passing a union containing a struct of 3 floats. #2087 - Bug with slice acces as inline struct member #2088. +- `@if` now does implicit conversion to bool like `$if`. #2086 ### Stdlib changes - Hash functions for integer vectors and arrays. diff --git a/src/compiler/sema_decls.c b/src/compiler/sema_decls.c index f4d713e29..24f7582a8 100755 --- a/src/compiler/sema_decls.c +++ b/src/compiler/sema_decls.c @@ -3126,7 +3126,7 @@ static bool sema_analyse_attribute(SemaContext *context, ResolvedAttrData *attr_ case ATTRIBUTE_IF: if (!expr) RETURN_SEMA_ERROR(attr, "'@if' requires a boolean argument."); if (!sema_analyse_expr(context, expr)) return false; - if (expr->type->canonical != type_bool || !sema_cast_const(expr)) + if (!cast_explicit_silent(context, expr, type_bool) || !sema_cast_const(expr)) { RETURN_SEMA_ERROR(expr, "Expected a boolean compile time constant value."); } diff --git a/test/test_suite/compile_time/ct_if_bool_conversion_2086.c3 b/test/test_suite/compile_time/ct_if_bool_conversion_2086.c3 new file mode 100644 index 000000000..64d415280 --- /dev/null +++ b/test/test_suite/compile_time/ct_if_bool_conversion_2086.c3 @@ -0,0 +1,11 @@ +module test; + +fn void main() +{ + int $a = 0; + $if $a: + $endif; + int a @if(!$a); + int a @if($a); + int a @if((bool)$a); +} \ No newline at end of file