From 1275305c55e213c720b83b6c1cbe248b8cba255f Mon Sep 17 00:00:00 2001 From: Crying Nights Date: Thu, 7 May 2026 14:50:51 +0800 Subject: [PATCH] cgen: allow comparison between Option[T] and `none` literal (#27097) --- vlib/v/gen/c/infix.v | 2 +- vlib/v/tests/options/option_cast_eq_none_test.v | 17 +++++++++++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) create mode 100644 vlib/v/tests/options/option_cast_eq_none_test.v diff --git a/vlib/v/gen/c/infix.v b/vlib/v/gen/c/infix.v index 28b8f2b5a..b223f6a55 100644 --- a/vlib/v/gen/c/infix.v +++ b/vlib/v/gen/c/infix.v @@ -1754,7 +1754,7 @@ fn (mut g Gen) infix_expr_and_or_op(node ast.InfixExpr) { } fn (mut g Gen) gen_is_none_check(node ast.InfixExpr) { - if node.left in [ast.Ident, ast.SelectorExpr, ast.IndexExpr, ast.CallExpr, ast.CTempVar] { + if node.left in [ast.Ident, ast.SelectorExpr, ast.IndexExpr, ast.CallExpr, ast.CTempVar, ast.CastExpr] { // When a sumtype variable has been comptime-smartcast to an option variant // (e.g. `$if t is ?string { if t == none { ... } }`), we need to access the // sumtype's variant field directly rather than using .data on the sumtype. diff --git a/vlib/v/tests/options/option_cast_eq_none_test.v b/vlib/v/tests/options/option_cast_eq_none_test.v new file mode 100644 index 000000000..0cd5225cd --- /dev/null +++ b/vlib/v/tests/options/option_cast_eq_none_test.v @@ -0,0 +1,17 @@ +struct Foo { + x int +} + +fn test_option_cast_eq_none() { + assert ?int(none) == none + assert !(?int(123) == none) + assert ?int(123) != none + + assert ?string(none) == none + assert ?string('hi') != none + + assert ?Foo(none) == none + assert ?Foo(Foo{ + x: 1 + }) != none +} -- 2.39.5