From dd75c4b233f91e974c1fa1c3f6e023a9670688e8 Mon Sep 17 00:00:00 2001 From: Felipe Pena Date: Sun, 29 Dec 2024 17:55:58 -0300 Subject: [PATCH 1/2] 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)' +} From b3d3b490cff0e834b477e9636e5804396fa8bc5f Mon Sep 17 00:00:00 2001 From: Felipe Pena Date: Sun, 29 Dec 2024 18:44:14 -0300 Subject: [PATCH 2/2] fix --- vlib/v/checker/infix.v | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/vlib/v/checker/infix.v b/vlib/v/checker/infix.v index a1c8388285116d..66e6d212adf694 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 { - c.unwrap_generic(right_expr.typ) + right_expr.typ } ast.None { ast.none_type_idx @@ -794,7 +794,8 @@ fn (mut c Checker) infix_expr(mut node ast.InfixExpr) ast.Type { c.error('`${op}` can only be used with interfaces and sum types', node.pos) // can be used in sql too, but keep err simple } else if mut left_sym.info is ast.SumType { - if typ !in left_sym.info.variants { + if typ !in left_sym.info.variants + && c.unwrap_generic(typ) !in left_sym.info.variants { c.error('`${left_sym.name}` has no variant `${right_sym.name}`', right_pos) }