diff --git a/releasenotes.md b/releasenotes.md index ede49ce8e..2ac8d53d9 100644 --- a/releasenotes.md +++ b/releasenotes.md @@ -39,6 +39,7 @@ - Atomic max was incorrect. - `"+".to_float()` would panic. - `import` can now both be @public and @norecurse. +- Crash when trying to convert a struct slice to a vector #2039. ### Stdlib changes - `new_*` functions in general moved to version without `new_` prefix. diff --git a/src/compiler/sema_casts.c b/src/compiler/sema_casts.c index e60f975d2..d90e58d76 100644 --- a/src/compiler/sema_casts.c +++ b/src/compiler/sema_casts.c @@ -1045,16 +1045,21 @@ static bool rule_slice_to_vecarr(CastContext *cc, bool is_explicit, bool is_sile if (is_silent) return false; RETURN_CAST_ERROR(expr, "Zero sized slices can't be converted to arrays or vectors."); } + Type *base = cc->from->array.base; if (cc->to_group == CONV_ARRAY) { if (expr_is_const_string(expr) || expr_is_const_bytes(expr)) { if (cc->to->array.len > size) size = cc->to->array.len; } - cast_context_set_from(cc, type_get_array(cc->from->array.base, size)); + cast_context_set_from(cc, type_get_array(base, size)); } else { + if (!type_is_valid_for_vector(base)) + { + return report_cast_error(cc, false); + } cast_context_set_from(cc, type_get_vector(cc->from->array.base, size)); } return cast_is_allowed(cc, is_explicit, is_silent); diff --git a/test/test_suite/vector/slice_to_vector_but_not_allowed_base.c3 b/test/test_suite/vector/slice_to_vector_but_not_allowed_base.c3 new file mode 100644 index 000000000..71b1c67ee --- /dev/null +++ b/test/test_suite/vector/slice_to_vector_but_not_allowed_base.c3 @@ -0,0 +1,8 @@ +// Repro issue #2039 +module foo; +struct Foo { void* x; } +fn void main() +{ + Foo* foo; + float[<2>] a = foo[0..1]; // #error: It is not possible to cast +} \ No newline at end of file