checker: fix missing type bounding to match expr on or{} expr (fix #24656) (#24658)

This commit is contained in:
Felipe Pena 2025-06-05 13:00:28 -03:00 committed by GitHub
parent a13821a951
commit 1e71fa3e38
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 17 additions and 0 deletions

View file

@ -1487,6 +1487,7 @@ fn (mut c Checker) check_or_last_stmt(mut stmt ast.Stmt, ret_type ast.Type, expr
return
}
last_stmt_typ := c.expr(mut stmt.expr)
stmt.typ = last_stmt_typ
if last_stmt_typ.has_flag(.option) || last_stmt_typ == ast.none_type {
if stmt.expr in [ast.Ident, ast.SelectorExpr, ast.CallExpr, ast.None, ast.CastExpr] {
expected_type_name := c.table.type_to_str(ret_type.clear_option_and_result())

View file

@ -0,0 +1,16 @@
struct Foo {
option ?int = none
}
fn test_main() {
test := true
foo := Foo{}
result := foo.option or {
match test {
true { 1 }
else { 2 }
}
}
assert result == 1
}