diff --git a/releasenotes.md b/releasenotes.md index c49d9f711..70fc6f806 100644 --- a/releasenotes.md +++ b/releasenotes.md @@ -33,6 +33,7 @@ - Added missing `@clone_aligned` and add checks to `@tclone` - Comparing a distinct type with an enum with an inline distinct type failed unexpectedly. - The `%s` would not properly print function pointers. +- Compiler crash when passing an untyped list as an argument to `assert` #2108. ### Stdlib changes - Hash functions for integer vectors and arrays. diff --git a/src/compiler/sema_stmts.c b/src/compiler/sema_stmts.c index 0f6687188..7684f12ed 100644 --- a/src/compiler/sema_stmts.c +++ b/src/compiler/sema_stmts.c @@ -115,7 +115,25 @@ static inline bool sema_analyse_assert_stmt(SemaContext *context, Ast *statement { if (!sema_analyse_expr(context, e)) return false; if (IS_OPTIONAL(e)) RETURN_SEMA_ERROR(e, "Optionals cannot be used as assert arguments, use '?""?', '!' or '!!' to fix this."); - if (type_is_void(e->type)) RETURN_SEMA_ERROR(e, "This expression is of type 'void', did you make a mistake?"); + switch (sema_resolve_storage_type(context, e->type)) + { + case STORAGE_ERROR: + return false; + case STORAGE_NORMAL: + break; + case STORAGE_WILDCARD: + UNREACHABLE + case STORAGE_VOID: + RETURN_SEMA_ERROR(e, "This expression is of type 'void', did you make a mistake?"); + case STORAGE_COMPILE_TIME: + if (e->type == type_untypedlist) + { + RETURN_SEMA_ERROR(e, "The type of an untyped list cannot be inferred, you can try adding an explicit type to solve this."); + } + RETURN_SEMA_ERROR(e, "You can't use a compile time type as an assert argument."); + case STORAGE_UNKNOWN: + RETURN_SEMA_ERROR(e, "You can't use an argument of type %s in an assert.", type_quoted_error_string(e->type)); + } } } diff --git a/test/test_suite/assert/assert_arguments_ct.c3 b/test/test_suite/assert/assert_arguments_ct.c3 new file mode 100644 index 000000000..d7b8626da --- /dev/null +++ b/test/test_suite/assert/assert_arguments_ct.c3 @@ -0,0 +1,18 @@ +import std; +macro @test(#e1, #e2) { + assert( + #e1 == #e2, + "Assertion '" +++ $stringify(#e1) +++ " == " +++ $stringify(#e2) + +++ "' failed, got '%s', expected '%s'.", #e1, #e2 // #error: untyped list cannot be inferred + ); +} + +fn usz[<2>] grapheme_length(char*) +{ + return { 1, 1 }; +} + +fn void main() +{ + @test(grapheme_length("a"), { 1, 1 }); +} \ No newline at end of file