diff --git a/vlib/v/checker/if.v b/vlib/v/checker/if.v index af48f98f1f..56a90d9a0e 100644 --- a/vlib/v/checker/if.v +++ b/vlib/v/checker/if.v @@ -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] { diff --git a/vlib/v/checker/tests/if_mismatch_option_err.out b/vlib/v/checker/tests/if_mismatch_option_err.out new file mode 100644 index 0000000000..00ea6a1761 --- /dev/null +++ b/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 | } diff --git a/vlib/v/checker/tests/if_mismatch_option_err.vv b/vlib/v/checker/tests/if_mismatch_option_err.vv new file mode 100644 index 0000000000..58f177eb2a --- /dev/null +++ b/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) } +} \ No newline at end of file