From f0d4c4d2ce4d81bfbda2714157d2f78ec5b393bf Mon Sep 17 00:00:00 2001 From: Christoffer Lerno Date: Fri, 11 Aug 2023 18:15:39 +0200 Subject: [PATCH] Ensure type is checked before analysis of compound literals. #919 --- src/compiler/sema_expr.c | 8 +++++--- src/compiler/sema_initializers.c | 1 + test/test_suite/initialize/init_non_resolved_type.c3 | 1 + 3 files changed, 7 insertions(+), 3 deletions(-) create mode 100644 test/test_suite/initialize/init_non_resolved_type.c3 diff --git a/src/compiler/sema_expr.c b/src/compiler/sema_expr.c index 340787ddf..66b92e344 100644 --- a/src/compiler/sema_expr.c +++ b/src/compiler/sema_expr.c @@ -7663,14 +7663,16 @@ static inline bool sema_expr_analyse_builtin(SemaContext *context, Expr *expr, b static inline bool sema_expr_analyse_compound_literal(SemaContext *context, Expr *expr) { - if (!sema_resolve_type_info(context, expr->expr_compound_literal.type_info)) return false; - Type *type = expr->expr_compound_literal.type_info->type; + TypeInfo *type_info = expr->expr_compound_literal.type_info; + if (!sema_resolve_type_info(context, type_info)) return false; + Type *type = type_info->type; if (type_is_optional(type)) { - SEMA_ERROR(expr->expr_compound_literal.type_info, + SEMA_ERROR(type_info, "The type here should always be written as a plain type and not an optional, please remove the '!'."); return false; } + if (!sema_resolve_type_structure(context, type, type_info->span)) return false; if (!sema_expr_analyse_initializer_list(context, type, expr->expr_compound_literal.initializer)) return false; expr_replace(expr, expr->expr_compound_literal.initializer); return true; diff --git a/src/compiler/sema_initializers.c b/src/compiler/sema_initializers.c index c4fec53d3..9b64f1011 100644 --- a/src/compiler/sema_initializers.c +++ b/src/compiler/sema_initializers.c @@ -566,6 +566,7 @@ bool sema_expr_analyse_initializer_list(SemaContext *context, Type *to, Expr *ex assert(to); Type *flattened = type_flatten(to); bool is_zero_init = expr->expr_kind == EXPR_INITIALIZER_LIST && !vec_size(expr->initializer_list); + if (!sema_resolve_type_structure(context, to, expr->span)) return false; switch (flattened->type_kind) { case TYPE_UNTYPED_LIST: diff --git a/test/test_suite/initialize/init_non_resolved_type.c3 b/test/test_suite/initialize/init_non_resolved_type.c3 new file mode 100644 index 000000000..892846c1c --- /dev/null +++ b/test/test_suite/initialize/init_non_resolved_type.c3 @@ -0,0 +1 @@ +const IS_ARCH_LITTLE_ENDIAN = (uint)(UIntLE{(uint)1}.val) == 1; // #error: This expression cannot be evaluated at compile time