Skip to content

Commit

Permalink
checker: fix sumtype generic checking (fix #23282) (#23309)
Browse files Browse the repository at this point in the history
  • Loading branch information
felipensp authored Dec 30, 2024
1 parent 99b93d9 commit 632ceeb
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
3 changes: 2 additions & 1 deletion vlib/v/checker/infix.v
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
20 changes: 20 additions & 0 deletions vlib/v/tests/sumtypes/sumtype_generic_checking_test.v
Original file line number Diff line number Diff line change
@@ -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)'
}

0 comments on commit 632ceeb

Please sign in to comment.