From 74b8da1e1515f24df49ff38afdbc45edc1d28f7c Mon Sep 17 00:00:00 2001 From: Christoffer Lerno Date: Fri, 16 Aug 2024 21:49:21 +0200 Subject: [PATCH] Avoid any constants that have the "untyped list" type but isn't a CONST_UNTYPED_LIST. --- src/compiler/compiler_internal.h | 1 + src/compiler/sema_casts.c | 27 ++++----------------------- src/compiler/sema_expr.c | 6 +++--- src/compiler/sema_initializers.c | 5 +++++ 4 files changed, 13 insertions(+), 26 deletions(-) diff --git a/src/compiler/compiler_internal.h b/src/compiler/compiler_internal.h index 7d2c65c20..7296e11f3 100644 --- a/src/compiler/compiler_internal.h +++ b/src/compiler/compiler_internal.h @@ -3440,6 +3440,7 @@ INLINE void expr_rewrite_const_untyped_list(Expr *expr, Expr **elements) INLINE void expr_rewrite_const_initializer(Expr *expr, Type *type, ConstInitializer *initializer) { + assert(type != type_untypedlist); expr->expr_kind = EXPR_CONST; expr->type = type; expr->const_expr = (ExprConst) { .initializer = initializer, .const_kind = CONST_INITIALIZER }; diff --git a/src/compiler/sema_casts.c b/src/compiler/sema_casts.c index 47f11d795..1e7943820 100644 --- a/src/compiler/sema_casts.c +++ b/src/compiler/sema_casts.c @@ -806,11 +806,6 @@ static bool rule_arrptr_to_slice(CastContext *cc, bool is_explicit, bool is_sile static bool rule_ulist_to_struct(CastContext *cc, bool is_explicit, bool is_silent) { - if (expr_is_const_initializer(cc->expr)) - { - assert(cc->expr->const_expr.initializer->kind == CONST_INIT_ZERO); - return true; - } assert(expr_is_const_untyped_list(cc->expr)); Expr **expressions = cc->expr->const_expr.untyped_list; unsigned size = vec_size(expressions); @@ -854,11 +849,7 @@ static bool rule_ulist_to_vecarr(CastContext *cc, bool is_explicit, bool is_sile static bool rule_ulist_to_slice(CastContext *cc, bool is_explicit, bool is_silent) { - if (cc->expr->const_expr.const_kind == CONST_INITIALIZER) - { - assert(cc->expr->const_expr.initializer->kind == CONST_INIT_ZERO); - return true; - } + assert(expr_is_const_untyped_list(cc->expr)); Type *base = cc->to->array.base; FOREACH(Expr *, expr, cc->expr->const_expr.untyped_list) { @@ -1801,19 +1792,9 @@ static void cast_vec_to_vec(SemaContext *context, Expr *expr, Type *to_type) static void cast_untyped_list_to_other(SemaContext *context, Expr *expr, Type *to_type) { - if (expr->const_expr.const_kind == CONST_UNTYPED_LIST) - { - // Recursively set the type of all ConstInitializer inside. - expr_recursively_rewrite_untyped_list(expr, expr->const_expr.untyped_list); - } - if (expr_is_const_initializer(expr) && expr->const_expr.initializer->kind == CONST_INIT_ZERO) - { - expr->type = to_type; - expr->inner_expr->type = type_flatten(to_type); - expr->resolve_status = RESOLVE_DONE; - return; - } - expr->resolve_status = RESOLVE_NOT_DONE; + assert(expr_is_const_untyped_list(expr)); + // Recursively set the type of all ConstInitializer inside. + expr_recursively_rewrite_untyped_list(expr, expr->const_expr.untyped_list); // We can now analyse the list (this is where the actual check happens) bool success = sema_expr_analyse_initializer_list(context, type_flatten(to_type), expr); assert(success); diff --git a/src/compiler/sema_expr.c b/src/compiler/sema_expr.c index 212fbe007..da8391198 100644 --- a/src/compiler/sema_expr.c +++ b/src/compiler/sema_expr.c @@ -7451,7 +7451,7 @@ static inline bool sema_expr_analyse_ct_concat(SemaContext *context, Expr *conca ConstInitializer *init = single_expr->const_expr.initializer; if (init->kind != CONST_INIT_ARRAY_FULL) { - if (init->kind == CONST_INIT_ZERO && init->type == type_untypedlist) continue; + assert(init->type != type_untypedlist); RETURN_SEMA_ERROR(single_expr, "Expected a full array here."); } FOREACH(ConstInitializer *, val, init->init_array_full) @@ -9374,8 +9374,8 @@ static inline bool sema_expr_analyse_ct_append(SemaContext *context, Expr *appen switch (list->const_expr.const_kind) { case CONST_INITIALIZER: - if (list->type != type_untypedlist) return sema_append_const_array(context, append_expr, list, exprs); - break; + assert(list->type != type_untypedlist); + return sema_append_const_array(context, append_expr, list, exprs); case CONST_UNTYPED_LIST: untyped_list = list->const_expr.untyped_list; break; diff --git a/src/compiler/sema_initializers.c b/src/compiler/sema_initializers.c index 3b794742a..855f4db01 100644 --- a/src/compiler/sema_initializers.c +++ b/src/compiler/sema_initializers.c @@ -498,6 +498,11 @@ static inline bool sema_expr_analyse_initializer(SemaContext *context, Type *ass SEMA_ERROR(expr, "Zero length arrays / vectors are not permitted."); return false; } + if (flattened == type_untypedlist) + { + expr_rewrite_const_untyped_list(expr, NULL); + return true; + } ConstInitializer *initializer = CALLOCS(ConstInitializer); initializer->kind = CONST_INIT_ZERO; initializer->type = flattened;