diff --git a/releasenotes.md b/releasenotes.md index 93dd4ece6..248c14526 100644 --- a/releasenotes.md +++ b/releasenotes.md @@ -8,6 +8,8 @@ - `-0xFF` will now be a signed integer. ### Fixes +- `-2147483648`, MIN literals work correctly. +- Splatting const slices would not be const. #2185 ### Stdlib changes @@ -72,7 +74,6 @@ - Bug using `#foo` arguments with `$defined` #2173 - Incorrect ensure on String.split. - Removed the naive check for compile time modification, which fixes #1997 but regresses in detection. -- `-2147483648`, MIN literals work correctly. ### Stdlib changes - Added `String.quick_ztr` and `String.is_zstr` diff --git a/src/compiler/sema_expr.c b/src/compiler/sema_expr.c index ca1182c21..e63df4dba 100644 --- a/src/compiler/sema_expr.c +++ b/src/compiler/sema_expr.c @@ -3221,6 +3221,11 @@ static Type *sema_subscript_find_indexable_type_recursively(Type **type, Expr ** static bool sema_subscript_rewrite_index_const_list(Expr *const_list, ArraySize index, bool from_back, Expr *result) { + if (const_list->const_expr.const_kind == CONST_SLICE) + { + ASSERT_SPAN(const_list, const_list->const_expr.slice_init); + return expr_rewrite_to_const_initializer_index(const_list->type, const_list->const_expr.slice_init, result, index, from_back); + } ASSERT_SPAN(const_list, const_list->const_expr.const_kind == CONST_INITIALIZER); return expr_rewrite_to_const_initializer_index(const_list->type, const_list->const_expr.initializer, result, index, from_back); } @@ -3609,7 +3614,7 @@ static inline bool sema_expr_analyse_subscript(SemaContext *context, Expr *expr, { ASSERT_SPAN(index, expr_is_const_int(index)); sema_cast_const(current_expr); - bool is_const_initializer = expr_is_const_initializer(current_expr); + bool is_const_initializer = expr_is_const_initializer(current_expr) || (expr_is_const_slice(current_expr) && current_expr->const_expr.slice_init); if (is_const_initializer || expr_is_const_string(current_expr) || expr_is_const_bytes(current_expr)) { if (!int_fits(index->const_expr.ixx, TYPE_U32)) diff --git a/test/test_suite/functions/splat_error.c3 b/test/test_suite/functions/splat_error.c3 new file mode 100644 index 000000000..6e23891ba --- /dev/null +++ b/test/test_suite/functions/splat_error.c3 @@ -0,0 +1,10 @@ +import std; + +fn void main() +{ + int[] $nums = { 0, 1, 2 }; + var $list = { ...$nums, 3 }; + $foreach $n : $list : + $echo $n; + $endforeach +}