Skip to content

Commit

Permalink
Add array to void* implicit cast
Browse files Browse the repository at this point in the history
  • Loading branch information
Akuli committed Dec 31, 2024
1 parent e638fcd commit 3854762
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
7 changes: 5 additions & 2 deletions src/typecheck.c
Original file line number Diff line number Diff line change
Expand Up @@ -541,8 +541,11 @@ static bool can_cast_implicitly(const Type *from, const Type *to)
return
from == to
|| (
// array to pointer implicitly
// array to pointer with same item type
from->kind == TYPE_ARRAY && to->kind == TYPE_POINTER && from->data.array.membertype == to->data.valuetype
) || (
// array to void*
from->kind == TYPE_ARRAY && to->kind == TYPE_VOID_POINTER
) || (
// Cast to bigger integer types implicitly, unless it is signed-->unsigned.
is_integer_type(from)
Expand Down Expand Up @@ -594,7 +597,7 @@ static void do_implicit_cast(
fail_with_implicit_cast_error(location, errormsg_template, from, to);

types->implicit_cast_type = to;
types->implicit_array_to_pointer_cast = (from->kind == TYPE_ARRAY && to->kind == TYPE_POINTER);
types->implicit_array_to_pointer_cast = (from->kind == TYPE_ARRAY && is_pointer_type(to));
if (types->implicit_array_to_pointer_cast)
ensure_can_take_address(
fom,
Expand Down
10 changes: 10 additions & 0 deletions tests/should_succeed/array_to_voidptr.jou
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import "stdlib/io.jou"
import "stdlib/mem.jou"


def main() -> int:
array = [1, 2, 3]
p: void* = array
memset(p, 0, 4)
printf("[%d, %d, %d]\n", array[0], array[1], array[2]) # Output: [0, 2, 3]
return 0

0 comments on commit 3854762

Please sign in to comment.