From d0c00b859bd62b466f7d5a813c2ec2a6efd47cbc Mon Sep 17 00:00:00 2001 From: Christoffer Lerno Date: Tue, 25 Jul 2023 15:16:42 +0200 Subject: [PATCH] Fixes incorrect type resolution of && and || with optionals. Fixes #879 --- src/compiler/sema_expr.c | 3 ++- src/version.h | 2 +- test/test_suite/expressions/optional_and_error.c3 | 13 +++++++++++++ 3 files changed, 16 insertions(+), 2 deletions(-) create mode 100644 test/test_suite/expressions/optional_and_error.c3 diff --git a/src/compiler/sema_expr.c b/src/compiler/sema_expr.c index f2b154b83..bdf665e79 100644 --- a/src/compiler/sema_expr.c +++ b/src/compiler/sema_expr.c @@ -5343,6 +5343,7 @@ static bool sema_expr_analyse_and_or(SemaContext *context, Expr *expr, Expr *lef if (!sema_binary_analyse_subexpr(context, expr, left, right)) return false; if (!cast_explicit(context, left, type_bool) || !cast_explicit(context, right, type_bool)) return false; + if (expr_both_const(left, right) && sema_constant_fold_ops(left)) { if (expr->binary_expr.operator == BINARYOP_AND) @@ -5356,7 +5357,7 @@ static bool sema_expr_analyse_and_or(SemaContext *context, Expr *expr, Expr *lef expr->const_expr.b |= right->const_expr.b; } } - expr->type = type_bool; + expr->type = type_add_optional(type_bool, IS_OPTIONAL(left) || IS_OPTIONAL(right)); return true; } diff --git a/src/version.h b/src/version.h index e9366a010..83a7c08a7 100644 --- a/src/version.h +++ b/src/version.h @@ -1 +1 @@ -#define COMPILER_VERSION "0.4.579" \ No newline at end of file +#define COMPILER_VERSION "0.4.580" \ No newline at end of file diff --git a/test/test_suite/expressions/optional_and_error.c3 b/test/test_suite/expressions/optional_and_error.c3 new file mode 100644 index 000000000..e9861d494 --- /dev/null +++ b/test/test_suite/expressions/optional_and_error.c3 @@ -0,0 +1,13 @@ +module testing; +import std::io; + +fn void! main() +{ + bool ok; + if (ok && !foo()) io::printfn("nok"); // #error: The expression may not be an optional +} + +fn bool! foo() +{ + return false; +} \ No newline at end of file