From f1efdf3d984c36b17deeeff8cf4692d4497a1f74 Mon Sep 17 00:00:00 2001 From: Christoffer Lerno Date: Fri, 16 Aug 2024 16:50:58 +0200 Subject: [PATCH] Incorrect zero analysis on `foo["test"] = {}` #1360 --- releasenotes.md | 1 + src/compiler/sema_initializers.c | 7 ++++++- test/test_suite/struct/const_zero_init_1360.c3t | 12 ++++++++++++ 3 files changed, 19 insertions(+), 1 deletion(-) create mode 100644 test/test_suite/struct/const_zero_init_1360.c3t diff --git a/releasenotes.md b/releasenotes.md index 08f3475ba..ddd6ceb63 100644 --- a/releasenotes.md +++ b/releasenotes.md @@ -75,6 +75,7 @@ - Issues with wincrt linking. - Debug info with recursive canonical type usage could cause segfault. - Missing check on optional left hand side for `s.x`. +- Incorrect zero analysis on `foo["test"] = {}` #1360. ### Stdlib changes diff --git a/src/compiler/sema_initializers.c b/src/compiler/sema_initializers.c index c9ddabbbb..a3ce92d5c 100644 --- a/src/compiler/sema_initializers.c +++ b/src/compiler/sema_initializers.c @@ -473,6 +473,10 @@ static inline bool sema_expr_analyse_initializer(SemaContext *context, Type *ass return sema_expr_analyse_designated_initializer(context, assigned_type, flattened, expr); } + if (expr->expr_kind == EXPR_CONST) + { + return cast_implicit(context, expr, assigned_type, false); + } assert(expr->expr_kind == EXPR_INITIALIZER_LIST); // 2. Grab the expressions inside. @@ -689,7 +693,8 @@ bool sema_expr_analyse_initializer_list(SemaContext *context, Type *to, Expr *ex if (!to) to = type_untypedlist; assert(to); Type *flattened = type_flatten(to); - bool is_zero_init = expr->expr_kind == EXPR_INITIALIZER_LIST && !vec_size(expr->initializer_list); + bool is_zero_init = (expr->expr_kind == EXPR_INITIALIZER_LIST && !vec_size(expr->initializer_list)) || sema_initializer_list_is_empty(expr); + if (!sema_resolve_type_structure(context, to, expr->span)) return false; switch (flattened->type_kind) { diff --git a/test/test_suite/struct/const_zero_init_1360.c3t b/test/test_suite/struct/const_zero_init_1360.c3t new file mode 100644 index 000000000..eefb8b01d --- /dev/null +++ b/test/test_suite/struct/const_zero_init_1360.c3t @@ -0,0 +1,12 @@ +module test; +import std::collections::map; + +fn void main() +{ + HashMap() map; + map["c3c"] = {}; +} + +/* #expect: test.ll + +main \ No newline at end of file