From f912e5303869a41aa6bb3ccd2631ccd198867d27 Mon Sep 17 00:00:00 2001 From: Christoffer Lerno Date: Sun, 13 Aug 2023 20:57:50 +0200 Subject: [PATCH] Fix where designated initializers had optional arguments. See #923 --- releasenotes.md | 1 + src/compiler/sema_initializers.c | 6 ++++-- src/version.h | 2 +- test/test_suite/errors/optional_designated.c3 | 10 ++++++++++ 4 files changed, 16 insertions(+), 3 deletions(-) create mode 100644 test/test_suite/errors/optional_designated.c3 diff --git a/releasenotes.md b/releasenotes.md index 0db05449c..4df81b1e9 100644 --- a/releasenotes.md +++ b/releasenotes.md @@ -154,6 +154,7 @@ - Added posix socket functions. ### Fixes +- Fix issue of designated initializers that had optional arguments. - Fixed ++ and -- for bitstructs. - Fix to bug where library source files were sometimes ignored. - Types of arrays and vectors are consistently checked to be valid. diff --git a/src/compiler/sema_initializers.c b/src/compiler/sema_initializers.c index 9b64f1011..dec0525e2 100644 --- a/src/compiler/sema_initializers.c +++ b/src/compiler/sema_initializers.c @@ -426,14 +426,16 @@ static bool sema_expr_analyse_designated_initializer(SemaContext *context, Type continue; } } + Type *type; if (!is_structlike && is_inferred) { - initializer->type = type_from_inferred(flattened, type_get_indexed_type(assigned), (ArraySize)(max_index + 1)); + type = type_from_inferred(flattened, type_get_indexed_type(assigned), (ArraySize)(max_index + 1)); } else { - initializer->type = assigned; + type = assigned; } + initializer->type = type_add_optional(type, optional); initializer->resolve_status = RESOLVE_DONE; if (expr_is_constant_eval(initializer, env_eval_type(context))) { diff --git a/src/version.h b/src/version.h index b32561722..ddd3b943c 100644 --- a/src/version.h +++ b/src/version.h @@ -1 +1 @@ -#define COMPILER_VERSION "0.4.607" \ No newline at end of file +#define COMPILER_VERSION "0.4.608" \ No newline at end of file diff --git a/test/test_suite/errors/optional_designated.c3 b/test/test_suite/errors/optional_designated.c3 new file mode 100644 index 000000000..866bed82b --- /dev/null +++ b/test/test_suite/errors/optional_designated.c3 @@ -0,0 +1,10 @@ +module foo; + +struct Foo { int a; } +struct Bar { int b; Foo f; } +fn void! main() +{ + Bar { .f = Foo { foo() } }; // #error: not be discarded +} + +fn int! foo() => 1; \ No newline at end of file