mirror of
https://github.com/c3lang/c3c.git
synced 2026-02-27 12:01:16 +00:00
Prevent circular initializers.
This commit is contained in:
@@ -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;
|
||||
}
|
||||
|
||||
|
||||
3
test/test_suite/globals/recursive_globals.c3
Normal file
3
test/test_suite/globals/recursive_globals.c3
Normal file
@@ -0,0 +1,3 @@
|
||||
void *x = &x;
|
||||
|
||||
int y = 1 + y; // #error: This looks like the initialization of the variable was circular.
|
||||
5
test/test_suite/globals/self_referencing_local.c3
Normal file
5
test/test_suite/globals/self_referencing_local.c3
Normal file
@@ -0,0 +1,5 @@
|
||||
fn void test()
|
||||
{
|
||||
void* x = &x;
|
||||
int y = y; // #error: This looks like the initialization of the variable was circular.
|
||||
}
|
||||
Reference in New Issue
Block a user