diff --git a/releasenotes.md b/releasenotes.md index 2b31da6f1..bd65d29b3 100644 --- a/releasenotes.md +++ b/releasenotes.md @@ -29,6 +29,7 @@ - Update to dstring.append_string to take any type converting to String. - Flag `--cpu-flags` doesn't work if the first item is an exclusion. #2905 - Reallocating overaligned memory with the LibcAllocator was unsafe. +- Using [] or .foo on $$ functions would not raise error but instead crash ## 0.7.9 Change list diff --git a/src/compiler/sema_expr.c b/src/compiler/sema_expr.c index 5e4917988..65b16e43e 100644 --- a/src/compiler/sema_expr.c +++ b/src/compiler/sema_expr.c @@ -4198,7 +4198,10 @@ DEFAULT: } if (!sema_expr_check_assign(context, expr, NULL)) return false; - + if (subscripted->expr_kind == EXPR_BUILTIN) + { + RETURN_SEMA_ERROR(expr, "Builtins cannot be subscripted."); + } Expr *index = exprptr(expr->subscript_expr.index.expr); // 3. Check failability due to value. @@ -4295,7 +4298,10 @@ static inline bool sema_expr_analyse_subscript(SemaContext *context, Expr *expr, // Evaluate the expression to index. Expr *subscripted = exprptr(expr->subscript_expr.expr); if (!sema_analyse_expr(context, subscripted)) return false; - + if (subscripted->expr_kind == EXPR_BUILTIN) + { + RETURN_SEMA_ERROR(expr, "Builtins cannot be subscripted."); + } // 3. Check failability due to value. bool optional = IS_OPTIONAL(subscripted); @@ -4752,6 +4758,10 @@ static inline bool sema_expr_analyse_slice(SemaContext *context, Expr *expr) ASSERT_SPAN(expr, expr->expr_kind == EXPR_SLICE); Expr *subscripted = exprptr(expr->slice_expr.expr); if (!sema_analyse_expr(context, subscripted)) return false; + if (subscripted->expr_kind == EXPR_BUILTIN) + { + RETURN_SEMA_ERROR(expr, "A builtin cannot be sliced."); + } bool optional = IS_OPTIONAL(subscripted); Type *type = type_flatten(subscripted->type); Type *original_type = type_no_optional(subscripted->type); diff --git a/test/test_suite/builtins/builtin_props.c3 b/test/test_suite/builtins/builtin_props.c3 new file mode 100644 index 000000000..20eb047af --- /dev/null +++ b/test/test_suite/builtins/builtin_props.c3 @@ -0,0 +1,5 @@ +fn int main() +{ + if ($$veccompne.type == int.typeid) // #error: A builtin has no support for properties + return 0; +} \ No newline at end of file diff --git a/test/test_suite/builtins/builtin_subscript.c3 b/test/test_suite/builtins/builtin_subscript.c3 new file mode 100644 index 000000000..679dd5ddc --- /dev/null +++ b/test/test_suite/builtins/builtin_subscript.c3 @@ -0,0 +1,13 @@ +fn void main() +{ + $$veccompgt[1]; // #error: Builtins cannot be subscripted. +} +fn void test() +{ + $$veccompeq[1] = 33; // #error: Builtins cannot be subscripted. +} + +fn void test2() +{ + $$veccomple[..] = 52; // #error: A builtin cannot be sliced. +} \ No newline at end of file