From cf0a04977a9cd42be99e1ddd8e77aca7a7c8e1dd Mon Sep 17 00:00:00 2001 From: Christoffer Lerno Date: Wed, 22 Dec 2021 01:03:15 +0100 Subject: [PATCH] Prevent circular initializers. --- src/compiler/sema_expr.c | 19 ++++++++++++++++++- test/test_suite/globals/recursive_globals.c3 | 3 +++ .../globals/self_referencing_local.c3 | 5 +++++ 3 files changed, 26 insertions(+), 1 deletion(-) create mode 100644 test/test_suite/globals/recursive_globals.c3 create mode 100644 test/test_suite/globals/self_referencing_local.c3 diff --git a/src/compiler/sema_expr.c b/src/compiler/sema_expr.c index a50da2421..a7928bdad 100644 --- a/src/compiler/sema_expr.c +++ b/src/compiler/sema_expr.c @@ -640,6 +640,22 @@ static inline bool sema_cast_ident_rvalue(Context *context, Expr *expr) UNREACHABLE } switch (decl->var.kind) + { + case VARDECL_CONST: + case VARDECL_GLOBAL: + case VARDECL_LOCAL: + case VARDECL_LOCAL_CT: + case VARDECL_LOCAL_CT_TYPE: + if (decl->var.init_expr && decl->var.init_expr->resolve_status != RESOLVE_DONE) + { + SEMA_ERROR(expr, "This looks like the initialization of the variable was circular."); + return false; + } + break; + default: + break; + } + switch (decl->var.kind) { case VARDECL_CONST: if (!expr_is_constant_eval(decl->var.init_expr, CONSTANT_EVAL_ANY)) @@ -7118,7 +7134,8 @@ bool sema_analyse_inferred_expr(Context *context, Type *infer_type, Expr *expr) if (!sema_analyse_expr_dispatch(context, expr)) return expr_poison(expr); break; } + if (!sema_cast_rvalue(context, expr)) return false; expr->resolve_status = RESOLVE_DONE; - return sema_cast_rvalue(context, expr); + return true; } diff --git a/test/test_suite/globals/recursive_globals.c3 b/test/test_suite/globals/recursive_globals.c3 new file mode 100644 index 000000000..d6d2a725f --- /dev/null +++ b/test/test_suite/globals/recursive_globals.c3 @@ -0,0 +1,3 @@ +void *x = &x; + +int y = 1 + y; // #error: This looks like the initialization of the variable was circular. \ No newline at end of file diff --git a/test/test_suite/globals/self_referencing_local.c3 b/test/test_suite/globals/self_referencing_local.c3 new file mode 100644 index 000000000..6da61b7a8 --- /dev/null +++ b/test/test_suite/globals/self_referencing_local.c3 @@ -0,0 +1,5 @@ +fn void test() +{ + void* x = &x; + int y = y; // #error: This looks like the initialization of the variable was circular. +} \ No newline at end of file