Compiler crash when passing an untyped list as an argument to assert #2108.

This commit is contained in:
Christoffer Lerno
2025-04-25 15:02:23 +02:00
parent 8b47673524
commit 3eecaf9e29
3 changed files with 38 additions and 1 deletions

View File

@@ -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.

View File

@@ -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));
}
}
}

View File

@@ -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 });
}