Fix dues to crash when converting a const vector to another vector #1864.

This commit is contained in:
Christoffer Lerno
2025-01-20 16:26:26 +01:00
parent 5183370773
commit ad3cd88350
4 changed files with 17 additions and 2 deletions

View File

@@ -14,6 +14,7 @@
- Const strings and bytes were not properly converted to compile time bools.
- Concatenating a const empty slice with another array caused a null pointer access.
- Fix `linux-crt` and `linux-crtbegin` not getting recognized as a project paramater
- Fix dues to crash when converting a const vector to another vector #1864.
### Stdlib changes
- Added '%h' and '%H' for printing out binary data in hexadecimal using the formatter.

View File

@@ -1286,11 +1286,20 @@ static bool rule_vec_to_vec(CastContext *cc, bool is_explicit, bool is_silent)
{
if (cc->from->array.len != cc->to->array.len) return sema_cast_error(cc, false, is_silent);
Type *from_base = cc->from->array.base;
CastContext cc_copy = *cc;
cast_context_set_to(cc, cc->to->array.base);
// Allow bool vectors to expand to any int.
if (from_base == type_bool && cc->to_group == CONV_INT) return true;
// Create a fake expression that can't be folded for some checking if the
// the elements could be cast.
Expr temp = { .expr_kind = EXPR_EXPR_BLOCK, .type = from_base, .span = cc->expr->span, .resolve_status = RESOLVE_DONE };
cc->expr = &temp;
cast_context_set_from(cc, from_base);
return cast_is_allowed(cc, is_explicit, is_silent);
bool success = cast_is_allowed(cc, is_explicit, true);
*cc = cc_copy;
if (is_silent) return success;
if (success) return true;
return sema_cast_error(cc, is_explicit ? false : rule_vec_to_vec(cc, true, true), false);
}
static bool rule_expand_to_vec(CastContext *cc, bool is_explicit, bool is_silent)

View File

@@ -9,5 +9,5 @@ fn void main()
int[<*>] z = x;
int[<*>] w = y;
double[<2>] ww = x;
short[<2>] www = y; // #error: implicitly be converted
short[<2>] www = y; // #error: Implicitly casting 'int[2]' to 'short[<2>]'
}

View File

@@ -0,0 +1,5 @@
fn int main(String[] args)
{
const double[4] ONE = { 1.0, 1.0, 1.0, 1.0 };
float[<4>] vec4 = ONE; // #error: Implicitly casting 'double[4]' to 'float[<4>]'
}