From fad19f574ba5ed80fbbaf2fd1b575336089af167 Mon Sep 17 00:00:00 2001 From: Alexander Medvednikov Date: Thu, 7 May 2026 20:23:33 +0300 Subject: [PATCH] checker: default expr fix --- vlib/v/checker/struct.v | 3 ++- .../struct_ref_field_nil_const_default_test.v | 21 +++++++++++++++++++ 2 files changed, 23 insertions(+), 1 deletion(-) create mode 100644 vlib/v/tests/structs/struct_ref_field_nil_const_default_test.v diff --git a/vlib/v/checker/struct.v b/vlib/v/checker/struct.v index 3120f4ad9..2d8230002 100644 --- a/vlib/v/checker/struct.v +++ b/vlib/v/checker/struct.v @@ -164,7 +164,8 @@ fn (mut c Checker) struct_decl(mut node ast.StructDecl) { if field.has_default_expr { c.expected_type = field.typ field.default_expr_typ = c.expr(mut field.default_expr) - if field.typ.is_ptr() != field.default_expr_typ.is_ptr() { + if field.typ.is_ptr() != field.default_expr_typ.is_ptr() + && field.default_expr_typ.idx() !in ast.pointer_type_idxs { default_pos := field.default_expr.pos() if field.default_expr is ast.CallExpr { err_desc := if field.typ.is_ptr() { 'is' } else { 'is not' } diff --git a/vlib/v/tests/structs/struct_ref_field_nil_const_default_test.v b/vlib/v/tests/structs/struct_ref_field_nil_const_default_test.v new file mode 100644 index 000000000..e4f6590ec --- /dev/null +++ b/vlib/v/tests/structs/struct_ref_field_nil_const_default_test.v @@ -0,0 +1,21 @@ +const null_ptr = unsafe { nil } + +struct RefDefaultWindow { + w int +} + +struct RefDefaultCtx { +mut: + window &RefDefaultWindow = null_ptr + voidp voidptr = null_ptr + bytep &u8 = null_ptr + charp &char = null_ptr +} + +fn test_ref_field_with_nil_const_default() { + c := RefDefaultCtx{} + assert c.window == unsafe { nil } + assert c.voidp == unsafe { nil } + assert c.bytep == unsafe { nil } + assert c.charp == unsafe { nil } +} -- 2.39.5