From b68897c6d7b13254a41f827e3c8b91b48705459c Mon Sep 17 00:00:00 2001 From: Felipe Pena Date: Fri, 24 Oct 2025 07:46:16 -0300 Subject: [PATCH] comptime: fix comptime parameter resolve for `unsafe { nil }` to be `voidptr` (fix #25558) (#25570) --- vlib/v/checker/check_types.v | 3 +++ vlib/v/checker/if.v | 2 +- .../comptime/comptime_voidptr_unsafe_nil_test.v | 16 ++++++++++++++++ 3 files changed, 20 insertions(+), 1 deletion(-) create mode 100644 vlib/v/tests/comptime/comptime_voidptr_unsafe_nil_test.v diff --git a/vlib/v/checker/check_types.v b/vlib/v/checker/check_types.v index 4d9f3f0f8..31a8f349e 100644 --- a/vlib/v/checker/check_types.v +++ b/vlib/v/checker/check_types.v @@ -1032,6 +1032,9 @@ fn (mut c Checker) infer_fn_generic_types(func &ast.Fn, mut node ast.CallExpr) { if param.typ.has_flag(.generic) && param_sym.name == gt_name { typ = ast.mktyp(arg.typ) + if typ == ast.nil_type { + typ = ast.voidptr_type + } sym := c.table.final_sym(arg.typ) if sym.info is ast.FnType { mut func_ := sym.info.func diff --git a/vlib/v/checker/if.v b/vlib/v/checker/if.v index 5a2d72204..c9b31dc73 100644 --- a/vlib/v/checker/if.v +++ b/vlib/v/checker/if.v @@ -137,7 +137,7 @@ fn (mut c Checker) if_expr(mut node ast.IfExpr) ast.Type { } if old_val := c.table.comptime_is_true[idx_str] { if old_val.val != comptime_if_result { - c.error('checker erro1r : branch eval wrong', branch.cond.pos()) + c.error('checker error : branch eval wrong', branch.cond.pos()) } } diff --git a/vlib/v/tests/comptime/comptime_voidptr_unsafe_nil_test.v b/vlib/v/tests/comptime/comptime_voidptr_unsafe_nil_test.v new file mode 100644 index 000000000..3027a83e2 --- /dev/null +++ b/vlib/v/tests/comptime/comptime_voidptr_unsafe_nil_test.v @@ -0,0 +1,16 @@ +fn encode[T](t T) { + $if T is voidptr { + assert true + } $else $if T is $pointer { + if voidptr(t) == unsafe { nil } { + assert false + } + } +} + +// vfmt off +fn test_main() { + encode(unsafe { voidptr(0) }) + encode(unsafe { nil }) +} +// vfmt on -- 2.39.5