Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

checker: fix sumtype generic checking #23309

Merged
merged 2 commits into from
Dec 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)'
}
Loading