diff --git a/releasenotes.md b/releasenotes.md index 08e3265e4..70238cfb2 100644 --- a/releasenotes.md +++ b/releasenotes.md @@ -47,6 +47,7 @@ - `$foo` variables could be assigned non-compile time values. - `$foo[0] = ...` was incorrectly requiring that the assigned values were compile time constants. - "Inlined at" would sometimes show the current location. +- Fixed bug splatting constants into constants. ### Stdlib changes - Improve contract for readline. #2280 diff --git a/src/compiler/sema_expr.c b/src/compiler/sema_expr.c index 14c57b19a..1b98d60e7 100644 --- a/src/compiler/sema_expr.c +++ b/src/compiler/sema_expr.c @@ -1663,7 +1663,7 @@ INLINE bool sema_set_default_argument(SemaContext *context, CalledDecl *callee, INLINE Expr **sema_splat_arraylike_insert(SemaContext *context, Expr **args, Expr *arg, ArraySize len, ArrayIndex index) { args = sema_prepare_splat_insert(args, len, index); - if (expr_is_const(arg)) + if (sema_cast_const(arg)) { for (ArrayIndex i = 0; i < len; i++) { @@ -1675,6 +1675,11 @@ INLINE Expr **sema_splat_arraylike_insert(SemaContext *context, Expr **args, Exp } return args; } + if (context->call_env.kind != CALL_ENV_FUNCTION) + { + SEMA_ERROR(arg, "Cannot splat a non-constant value in a global context."); + return NULL; + } Decl *temp = decl_new_generated_var(arg->type, VARDECL_LOCAL, arg->span); Expr *decl_expr = expr_generate_decl(temp, arg); Expr *two = expr_new_expr(EXPR_TWO, arg); diff --git a/test/test_suite/functions/global_splat.c3t b/test/test_suite/functions/global_splat.c3t new file mode 100644 index 000000000..d2adf1cf6 --- /dev/null +++ b/test/test_suite/functions/global_splat.c3t @@ -0,0 +1,9 @@ + +const int[*] A = { 1, 2, 3 }; +const int[*] B = { 4, 5, 6 }; +const int[*] C = { ...A, ...B }; + +fn int main() +{ + return 0; +} \ No newline at end of file