From 3e628d09d9bbd425c24b482297a574534c45005d Mon Sep 17 00:00:00 2001 From: JMD <56417208+StunxFS@users.noreply.github.com> Date: Fri, 24 Oct 2025 15:34:22 -0400 Subject: [PATCH] checker: fix cast from an empty struct to option (fix #25566) (#25581) --- vlib/v/checker/checker.v | 3 ++- vlib/v/tests/casts/cast_empty_struct_to_option.v | 7 +++++++ 2 files changed, 9 insertions(+), 1 deletion(-) create mode 100644 vlib/v/tests/casts/cast_empty_struct_to_option.v diff --git a/vlib/v/checker/checker.v b/vlib/v/checker/checker.v index 7fb181e62..1ec10f1e9 100644 --- a/vlib/v/checker/checker.v +++ b/vlib/v/checker/checker.v @@ -3550,7 +3550,8 @@ fn (mut c Checker) cast_expr(mut node ast.CastExpr) ast.Type { c.warn('casting to struct is deprecated, use e.g. `Struct{...expr}` instead', node.pos) } - if !c.check_struct_signature(from_sym.info, to_sym.info) { + if from_type.idx() != to_type.idx() + && !c.check_struct_signature(from_sym.info, to_sym.info) { c.error('cannot convert struct `${from_sym.name}` to struct `${to_sym.name}`', node.pos) } diff --git a/vlib/v/tests/casts/cast_empty_struct_to_option.v b/vlib/v/tests/casts/cast_empty_struct_to_option.v new file mode 100644 index 000000000..39a7e966e --- /dev/null +++ b/vlib/v/tests/casts/cast_empty_struct_to_option.v @@ -0,0 +1,7 @@ +struct Struct {} + +fn main() { + s := ?Struct(Struct{}) + a := s or { panic('none') } + assert a == Struct{} +} -- 2.39.5