From ee1ed73fc5703d71ecceddc9d547bc5957470f8e Mon Sep 17 00:00:00 2001 From: Christoffer Lerno Date: Thu, 3 Jul 2025 15:45:14 +0200 Subject: [PATCH] Non-const macros may not return untyped lists. --- releasenotes.md | 1 + src/compiler/sema_expr.c | 11 +++++++---- .../compile_time/compile_time_untyped.c3 | 14 ++++++++++++++ 3 files changed, 22 insertions(+), 4 deletions(-) create mode 100644 test/test_suite/compile_time/compile_time_untyped.c3 diff --git a/releasenotes.md b/releasenotes.md index 9987eb259..72b606a5f 100644 --- a/releasenotes.md +++ b/releasenotes.md @@ -18,6 +18,7 @@ - Missing bounds check on upper bound with const ranges `foo[1:3]`. - Check up the hierarchy when considering if an interface cast is valid #2267. - Fix issue with labelled break inside of a $switch. +- Non-const macros may not return untyped lists. ### Stdlib changes diff --git a/src/compiler/sema_expr.c b/src/compiler/sema_expr.c index 761990929..4132dbf4a 100644 --- a/src/compiler/sema_expr.c +++ b/src/compiler/sema_expr.c @@ -2859,11 +2859,14 @@ EXIT: } sema_context_destroy(¯o_context); call_expr->resolve_status = RESOLVE_DONE; - if (is_always_const && !expr_is_runtime_const(call_expr)) + if (!expr_is_runtime_const(call_expr)) { - SEMA_ERROR(call_expr, "The macro failed to fold to a constant value, despite being '@const'."); - SEMA_NOTE(decl, "The macro was declared here."); - return false; + if (is_always_const) + { + SEMA_ERROR(call_expr, "The macro failed to fold to a constant value, despite being '@const'."); + SEMA_NOTE(decl, "The macro was declared here."); + return false; + } } return true; EXIT_FAIL: diff --git a/test/test_suite/compile_time/compile_time_untyped.c3 b/test/test_suite/compile_time/compile_time_untyped.c3 new file mode 100644 index 000000000..b825ded60 --- /dev/null +++ b/test/test_suite/compile_time/compile_time_untyped.c3 @@ -0,0 +1,14 @@ +struct Test +{ + struct { int a; } +} + +macro test(int x) +{ + return {{}}; +} +fn int main() +{ + Test a = test(3); + return 0; +} \ No newline at end of file