Skip to content

Commit

Permalink
cgen: fix method call checking against none (fix #20711) (#20717)
Browse files Browse the repository at this point in the history
  • Loading branch information
felipensp committed Feb 3, 2024
1 parent d977154 commit f72f1fc
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
2 changes: 1 addition & 1 deletion vlib/v/gen/c/infix.v
Expand Up @@ -1048,7 +1048,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] {
if node.left in [ast.Ident, ast.SelectorExpr, ast.IndexExpr, ast.CallExpr] {
old_inside_opt_or_res := g.inside_opt_or_res
g.inside_opt_or_res = true
g.expr(node.left)
Expand Down
28 changes: 28 additions & 0 deletions vlib/v/tests/method_call_none_check_test.v
@@ -0,0 +1,28 @@
struct Foo {
x int
}

struct Bar {
x int
}

type Foobar = Bar | Foo

struct Foobars {
m map[string]Foobar
}

fn (f &Foobars) find_foobar(name string) ?Foobar {
return f.m[name] or { return none }
}

fn (mut f Foobars) is_known(name string) bool {
return f.find_foobar(name) != none
}

fn test_main() {
mut foobars := Foobars{
m: map[string]Foobar{}
}
assert foobars.is_known('deadbeef') == false
}

0 comments on commit f72f1fc

Please sign in to comment.