From a46bf4fbe085ca7b0eaed17e93354642f809c433 Mon Sep 17 00:00:00 2001 From: Christoffer Lerno Date: Wed, 22 Nov 2023 18:35:29 +0100 Subject: [PATCH] Improve "const" error message #1079. --- src/build/build_options.c | 2 +- src/compiler/sema_expr.c | 15 ++++++--------- src/version.h | 2 +- test/test_suite/constants/assign_to_const.c3 | 8 ++++++++ .../expressions/arithmetics_sema_fail.c3 | 12 ++++++------ test/test_suite/expressions/assignability.c3 | 10 +++++----- 6 files changed, 27 insertions(+), 22 deletions(-) create mode 100644 test/test_suite/constants/assign_to_const.c3 diff --git a/src/build/build_options.c b/src/build/build_options.c index 09f798d35..482c62d22 100644 --- a/src/build/build_options.c +++ b/src/build/build_options.c @@ -387,7 +387,7 @@ static void print_all_targets(void) static void print_version(void) { - OUTPUT("C3 Compiler Version (pre-alpha): %s", COMPILER_VERSION); + OUTPUT("C3 Compiler Version (alpha): %s", COMPILER_VERSION); OUTPUT("Installed directory: %s", find_executable_path()); OUTPUT("LLVM version: %s", llvm_version); OUTPUT("LLVM default target: %s", llvm_target); diff --git a/src/compiler/sema_expr.c b/src/compiler/sema_expr.c index ff5ccaa83..84861f806 100644 --- a/src/compiler/sema_expr.c +++ b/src/compiler/sema_expr.c @@ -416,13 +416,11 @@ static bool sema_binary_is_expr_lvalue(Expr *top_expr, Expr *expr) Decl *decl = expr->identifier_expr.decl; if (decl->decl_kind != DECL_VAR) { - SEMA_ERROR(top_expr, "You cannot assign a value to %s.", decl_to_a_name(decl)); - return false; + RETURN_SEMA_ERROR(top_expr, "You cannot assign a value to %s.", decl_to_a_name(decl)); } if (decl->var.kind == VARDECL_CONST) { - SEMA_ERROR(top_expr, "You cannot assign to a constant."); - return false; + RETURN_SEMA_ERROR(top_expr, "You cannot assign to a constant."); } decl = decl_raw(decl); switch (decl->var.kind) @@ -469,16 +467,16 @@ static bool sema_binary_is_expr_lvalue(Expr *top_expr, Expr *expr) case EXPR_SUBSCRIPT_ADDR: if (IS_OPTIONAL(expr)) { - SEMA_ERROR(top_expr, "You cannot assign to an optional value."); - return false; + RETURN_SEMA_ERROR(top_expr, "You cannot assign to an optional value."); } return true; case EXPR_HASH_IDENT: - SEMA_ERROR(top_expr, "You cannot assign to an unevaluated expression."); - return false; + RETURN_SEMA_ERROR(top_expr, "You cannot assign to an unevaluated expression."); case EXPR_EXPRESSION_LIST: if (!vec_size(expr->expression_list)) return false; return sema_binary_is_expr_lvalue(top_expr, VECLAST(expr->expression_list)); + case EXPR_CONST: + RETURN_SEMA_ERROR(top_expr, "You cannot assign to a constant expression."); case EXPR_POISONED: case EXPR_ASM: case EXPR_BINARY: @@ -491,7 +489,6 @@ static bool sema_binary_is_expr_lvalue(Expr *top_expr, Expr *expr) case EXPR_COMPILER_CONST: case EXPR_COMPOUND_LITERAL: case EXPR_COND: - case EXPR_CONST: case EXPR_CT_ARG: case EXPR_CT_CALL: case EXPR_CT_AND_OR: diff --git a/src/version.h b/src/version.h index fbaf6a781..0e47f0154 100644 --- a/src/version.h +++ b/src/version.h @@ -1 +1 @@ -#define COMPILER_VERSION "0.5.0" +#define COMPILER_VERSION "0.5.1" diff --git a/test/test_suite/constants/assign_to_const.c3 b/test/test_suite/constants/assign_to_const.c3 new file mode 100644 index 000000000..b6574ec07 --- /dev/null +++ b/test/test_suite/constants/assign_to_const.c3 @@ -0,0 +1,8 @@ +module test; +import std::io; + +fn void main() +{ + const NUM = 4; + NUM += 1; // #error: You cannot assign to a constant expression +} \ No newline at end of file diff --git a/test/test_suite/expressions/arithmetics_sema_fail.c3 b/test/test_suite/expressions/arithmetics_sema_fail.c3 index ffb6dda03..5612ed71e 100644 --- a/test/test_suite/expressions/arithmetics_sema_fail.c3 +++ b/test/test_suite/expressions/arithmetics_sema_fail.c3 @@ -17,32 +17,32 @@ fn void test9() fn void test10() { - 10 = 20; // #error: An assignable expression + 10 = 20; // #error: to a constant expression } fn void test11() { - '10' = '20'; // #error: An assignable expression + '10' = '20'; // #error: to a constant expression } fn void test12() { - true = false; // #error: An assignable expression + true = false; // #error: to a constant expression } fn void test13() { - "a" = "b"; // #error: An assignable expression + "a" = "b"; // #error: to a constant expression } fn void test14() { - 1.2 = 1.3; // #error: An assignable expression + 1.2 = 1.3; // #error: to a constant expression } fn void test15() { - null = null; // #error: An assignable expression + null = null; // #error: to a constant expression } fn void test16() diff --git a/test/test_suite/expressions/assignability.c3 b/test/test_suite/expressions/assignability.c3 index 8cbc5bde7..4f8825393 100644 --- a/test/test_suite/expressions/assignability.c3 +++ b/test/test_suite/expressions/assignability.c3 @@ -2,27 +2,27 @@ def Number = int; fn void test1() { - 10 = 20; // #error: An assignable expression + 10 = 20; // #error: to a constant expression } fn void test2() { - "foo" = "bar"; // #error: An assignable expression + "foo" = "bar"; // #error: to a constant expression } fn void test3() { - true = false; // #error: An assignable expression + true = false; // #error: to a constant expression } fn void test4() { - 'c' = 'd'; // #error: An assignable expression + 'c' = 'd'; // #error: to a constant expression } fn void test5() { - 3.14 = 2.14; // #error: An assignable expression + 3.14 = 2.14; // #error: to a constant expression } fn void test21()