From da700b1c463507aadfee15c264903e1ce0d3ba51 Mon Sep 17 00:00:00 2001 From: Akuli Date: Fri, 10 Jan 2025 19:22:22 +0200 Subject: [PATCH] Add compiler error for incrementing/decrementing void pointers (#587) --- compiler/typecheck/step3_function_and_method_bodies.jou | 2 +- tests/wrong_type/voidptr_increment.jou | 6 ++++++ tests/wrong_type/voidptr_index.jou | 6 ++++++ 3 files changed, 13 insertions(+), 1 deletion(-) create mode 100644 tests/wrong_type/voidptr_increment.jou create mode 100644 tests/wrong_type/voidptr_index.jou diff --git a/compiler/typecheck/step3_function_and_method_bodies.jou b/compiler/typecheck/step3_function_and_method_bodies.jou index 236c194a..ca13b064 100644 --- a/compiler/typecheck/step3_function_and_method_bodies.jou +++ b/compiler/typecheck/step3_function_and_method_bodies.jou @@ -464,7 +464,7 @@ def check_increment_or_decrement(ft: FileTypes*, expr: AstExpression*) -> Type*: ensure_can_take_address(ft->current_fom_types, &expr->operands[0], bad_expr_fmt) t = typecheck_expression_not_void(ft, &expr->operands[0])->type - if not t->is_integer_type() and not t->is_pointer_type(): + if not t->is_integer_type() and t->kind != TypeKind::Pointer: msg: byte[500] snprintf(msg, sizeof(msg), bad_type_fmt, t->name) fail(expr->location, msg) diff --git a/tests/wrong_type/voidptr_increment.jou b/tests/wrong_type/voidptr_increment.jou new file mode 100644 index 00000000..e07ea766 --- /dev/null +++ b/tests/wrong_type/voidptr_increment.jou @@ -0,0 +1,6 @@ +import "stdlib/mem.jou" + +def main() -> int: + foo: void* = malloc(123) + foo++ # Error: cannot increment a value of type void* + return 0 diff --git a/tests/wrong_type/voidptr_index.jou b/tests/wrong_type/voidptr_index.jou new file mode 100644 index 00000000..cfc9824d --- /dev/null +++ b/tests/wrong_type/voidptr_index.jou @@ -0,0 +1,6 @@ +import "stdlib/mem.jou" + +def main() -> int: + foo: void* = malloc(123) + foo[2] = 'x' # Error: value of type void* cannot be indexed + return 0