Prevent circular initializers.

This commit is contained in:
Christoffer Lerno
2021-12-22 01:03:15 +01:00
parent 1a9b8095b6
commit cf0a04977a
3 changed files with 26 additions and 1 deletions

View File

@@ -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;
}

View File

@@ -0,0 +1,3 @@
void *x = &x;
int y = 1 + y; // #error: This looks like the initialization of the variable was circular.

View 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.
}