From 2aa5651b558668f399514f06eaca948c2bb3cf60 Mon Sep 17 00:00:00 2001 From: Swastik Baranwal Date: Mon, 6 Jan 2025 12:03:51 +0530 Subject: [PATCH] checker: allow `none` to be passed to `?T` param (fix #23381) (#23385) --- vlib/v/checker/fn.v | 5 +++-- vlib/v/tests/fn_generic_option_none_test.v | 6 ++++++ 2 files changed, 9 insertions(+), 2 deletions(-) create mode 100644 vlib/v/tests/fn_generic_option_none_test.v diff --git a/vlib/v/checker/fn.v b/vlib/v/checker/fn.v index 65a99cc1b098ab..0418fb3eb3665d 100644 --- a/vlib/v/checker/fn.v +++ b/vlib/v/checker/fn.v @@ -1531,7 +1531,7 @@ fn (mut c Checker) fn_call(mut node ast.CallExpr, mut continue_check &bool) ast. } } arg_typ_sym := c.table.sym(arg_typ) - if arg_typ_sym.kind == .none && param.typ.has_flag(.generic) { + if arg_typ_sym.kind == .none && param.typ.has_flag(.generic) && !param.typ.has_flag(.option) { c.error('cannot use `none` as generic argument', call_arg.pos) } param_typ_sym := c.table.sym(param.typ) @@ -2546,7 +2546,8 @@ fn (mut c Checker) method_call(mut node ast.CallExpr, mut continue_check &bool) } continue } - if final_arg_sym.kind == .none && param.typ.has_flag(.generic) { + if final_arg_sym.kind == .none && param.typ.has_flag(.generic) + && !param.typ.has_flag(.option) { c.error('cannot use `none` as generic argument', arg.pos) } if param.typ.is_ptr() && !arg.typ.is_any_kind_of_pointer() && arg.expr.is_literal() diff --git a/vlib/v/tests/fn_generic_option_none_test.v b/vlib/v/tests/fn_generic_option_none_test.v new file mode 100644 index 00000000000000..b799254d8ec510 --- /dev/null +++ b/vlib/v/tests/fn_generic_option_none_test.v @@ -0,0 +1,6 @@ +fn test[T](x T, b ?T) { +} + +fn test_main() { + test[int](123, none) +}