Skip to content

Commit

Permalink
Add an error for trying to pass an array as vararg (#161)
Browse files Browse the repository at this point in the history
  • Loading branch information
Akuli authored Jan 30, 2023
1 parent 77f7957 commit bdb57bd
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 1 deletion.
7 changes: 7 additions & 0 deletions src/typecheck.c
Original file line number Diff line number Diff line change
Expand Up @@ -478,6 +478,13 @@ static const Type *typecheck_function_call(TypeContext *ctx, const AstCall *call
for (int i = sig->nargs; i < call->nargs; i++) {
// This code runs for varargs, e.g. the things to format in printf().
ExpressionTypes *types = typecheck_expression_not_void(ctx, &call->args[i]);

if (types->type->kind == TYPE_ARRAY) {
fail_with_error(
call->args[i].location,
"arrays cannot be passed as varargs (try &array[0] instead of array)");
}

if ((is_integer_type(types->type) && types->type->data.width_in_bits < 32)
|| types->type == boolType)
{
Expand Down
1 change: 0 additions & 1 deletion tests/should_succeed/file.jou
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ def read_hello_123() -> void:
rewind(f)
text: byte[100]
fgets(&text[0], 100, f)
# FIXME: crashes at runtime if you pass the array without &
printf("got %s", &text[0]) # Output: got hello 123

fclose(f)
Expand Down
10 changes: 10 additions & 0 deletions tests/wrong_type/array_vararg.jou
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
from "stdlib/io.jou" import printf

def main() -> int:
s: byte[3]
s[0] = 'h'
s[1] = 'i'
s[2] = '\0'
printf("%s\n", &s[0]) # This works. No error
printf("%s\n", s) # Error: arrays cannot be passed as varargs (try &array[0] instead of array)
return 0

0 comments on commit bdb57bd

Please sign in to comment.