Skip to content

Commit

Permalink
Add compiler error for incrementing/decrementing void pointers (#587)
Browse files Browse the repository at this point in the history
  • Loading branch information
Akuli authored Jan 10, 2025
1 parent 4c260aa commit da700b1
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 1 deletion.
2 changes: 1 addition & 1 deletion compiler/typecheck/step3_function_and_method_bodies.jou
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
6 changes: 6 additions & 0 deletions tests/wrong_type/voidptr_increment.jou
Original file line number Diff line number Diff line change
@@ -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
6 changes: 6 additions & 0 deletions tests/wrong_type/voidptr_index.jou
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit da700b1

Please sign in to comment.