From 9d5b31dad58e6df14bf26addc02c8b7342fc202e Mon Sep 17 00:00:00 2001 From: Christoffer Lerno Date: Fri, 23 May 2025 18:57:21 +0200 Subject: [PATCH] Missing error on default values for body with default arguments #2148. --- releasenotes.md | 1 + src/compiler/sema_decls.c | 4 ++++ test/test_suite/macros/macro_body_error_2148.c3 | 14 ++++++++++++++ 3 files changed, 19 insertions(+) create mode 100644 test/test_suite/macros/macro_body_error_2148.c3 diff --git a/releasenotes.md b/releasenotes.md index 56da1de61..40013cdfe 100644 --- a/releasenotes.md +++ b/releasenotes.md @@ -36,6 +36,7 @@ - Designated const initializers with `{}` would overwrite the parent field. - Empty default case in @jump switch does not fallthrough #2147. - `&&&` was accidentally available as a valid prefix operator. +- Missing error on default values for body with default arguments #2148. ### Stdlib changes - Added `String.quick_ztr` and `String.is_zstr` diff --git a/src/compiler/sema_decls.c b/src/compiler/sema_decls.c index a64453d7a..0de700475 100755 --- a/src/compiler/sema_decls.c +++ b/src/compiler/sema_decls.c @@ -4094,6 +4094,10 @@ INLINE bool sema_analyse_macro_body(SemaContext *context, Decl **body_parameters ASSERT(param->decl_kind == DECL_VAR); TypeInfo *type_info = type_infoptrzero(param->var.type_info); VarDeclKind kind = param->var.kind; + if (param->var.init_expr) + { + RETURN_SEMA_ERROR(param->var.init_expr, "Body parameters cannot use default values."); + } switch (kind) { case VARDECL_PARAM: diff --git a/test/test_suite/macros/macro_body_error_2148.c3 b/test/test_suite/macros/macro_body_error_2148.c3 new file mode 100644 index 000000000..d7925fe04 --- /dev/null +++ b/test/test_suite/macros/macro_body_error_2148.c3 @@ -0,0 +1,14 @@ +import std; + +fn void main() +{ + @foo(;int x) + { + io::printn(x); + }; +} + +macro @foo(; @body(int x = 1)) // #error: Body parameters +{ + @body(3); +}