checker: fix if branch option type mismatch (fix #20809) (#20830)

This commit is contained in:
Felipe Pena 2024-02-14 18:22:55 -03:00 committed by GitHub
parent 9fb0685c59
commit 73caa8dff5
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 33 additions and 3 deletions

View File

@ -467,10 +467,19 @@ fn (mut c Checker) if_expr(mut node ast.IfExpr) ast.Type {
node.pos)
} else {
if c.inside_assign && node.is_expr && !node.typ.has_flag(.shared_f)
&& stmt.typ.is_ptr() != node.typ.is_ptr()
&& stmt.typ != ast.voidptr_type {
c.error('mismatched types `${c.table.type_to_str(node.typ)}` and `${c.table.type_to_str(stmt.typ)}`',
node.pos)
if stmt.typ.is_ptr() != node.typ.is_ptr() {
c.error('mismatched types `${c.table.type_to_str(node.typ)}` and `${c.table.type_to_str(stmt.typ)}`',
node.pos)
} else if stmt.typ != ast.none_type {
if !node.typ.has_flag(.option) && stmt.typ.has_flag(.option) {
c.error('mismatched types `${c.table.type_to_str(node.typ)}` and `${c.table.type_to_str(stmt.typ)}`',
node.pos)
} else if !node.typ.has_flag(.result) && stmt.typ.has_flag(.result) {
c.error('mismatched types `${c.table.type_to_str(node.typ)}` and `${c.table.type_to_str(stmt.typ)}`',
node.pos)
}
}
}
}
} else if !node.is_comptime && stmt !in [ast.Return, ast.BranchStmt] {

View File

@ -0,0 +1,12 @@
vlib/v/checker/tests/if_mismatch_option_err.vv:8:2: warning: unused variable: `operation`
6 | fn main() {
7 | operation_name := 'get_area'
8 | operation := if o := Operation.from(operation_name) { o } else { ?Operation(none) }
| ~~~~~~~~~
9 | }
vlib/v/checker/tests/if_mismatch_option_err.vv:8:15: error: mismatched types `Operation` and `?Operation`
6 | fn main() {
7 | operation_name := 'get_area'
8 | operation := if o := Operation.from(operation_name) { o } else { ?Operation(none) }
| ~~
9 | }

View File

@ -0,0 +1,9 @@
enum Operation {
get_area
get_sector
}
fn main() {
operation_name := 'get_area'
operation := if o := Operation.from(operation_name) { o } else { ?Operation(none) }
}