cgen: fix method call checking against none (fix #20711) (#20717)

This commit is contained in:
Felipe Pena 2024-02-03 08:16:16 -03:00 committed by GitHub
parent d977154b21
commit f72f1fc893
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 29 additions and 1 deletions

View File

@ -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) { 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 old_inside_opt_or_res := g.inside_opt_or_res
g.inside_opt_or_res = true g.inside_opt_or_res = true
g.expr(node.left) g.expr(node.left)

View File

@ -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
}