Skip to content

Commit

Permalink
checker: fix if branch option type mismatch (fix #20809) (#20830)
Browse files Browse the repository at this point in the history
  • Loading branch information
felipensp committed Feb 14, 2024
1 parent 9fb0685 commit 73caa8d
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 3 deletions.
15 changes: 12 additions & 3 deletions vlib/v/checker/if.v
Expand Up @@ -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] {
Expand Down
12 changes: 12 additions & 0 deletions vlib/v/checker/tests/if_mismatch_option_err.out
@@ -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 | }
9 changes: 9 additions & 0 deletions vlib/v/checker/tests/if_mismatch_option_err.vv
@@ -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) }
}

0 comments on commit 73caa8d

Please sign in to comment.