From dd75c4b233f91e974c1fa1c3f6e023a9670688e8 Mon Sep 17 00:00:00 2001 From: Felipe Pena Date: Sun, 29 Dec 2024 17:55:58 -0300 Subject: [PATCH] fix --- vlib/v/checker/infix.v | 2 +- .../sumtypes/sumtype_generic_checking_test.v | 20 +++++++++++++++++++ 2 files changed, 21 insertions(+), 1 deletion(-) create mode 100644 vlib/v/tests/sumtypes/sumtype_generic_checking_test.v diff --git a/vlib/v/checker/infix.v b/vlib/v/checker/infix.v index 47206adabed330..a1c8388285116d 100644 --- a/vlib/v/checker/infix.v +++ b/vlib/v/checker/infix.v @@ -756,7 +756,7 @@ fn (mut c Checker) infix_expr(mut node ast.InfixExpr) ast.Type { right_expr := node.right mut typ := match right_expr { ast.TypeNode { - right_expr.typ + c.unwrap_generic(right_expr.typ) } ast.None { ast.none_type_idx diff --git a/vlib/v/tests/sumtypes/sumtype_generic_checking_test.v b/vlib/v/tests/sumtypes/sumtype_generic_checking_test.v new file mode 100644 index 00000000000000..66e1f6734cb721 --- /dev/null +++ b/vlib/v/tests/sumtypes/sumtype_generic_checking_test.v @@ -0,0 +1,20 @@ +type Sumtype = string | int + +fn generic_fn[T]() ?T { + a := Sumtype('123') + if a is T { + return a + } + + b := Sumtype(123) + if b is T { + return b + } + + return none +} + +fn test_main() { + assert generic_fn[string]().str() == "Option('123')" + assert generic_fn[int]().str() == 'Option(123)' +}