diff --git a/vlib/v/checker/infix.v b/vlib/v/checker/infix.v index 2817b24e4b..cd12b1a2e7 100644 --- a/vlib/v/checker/infix.v +++ b/vlib/v/checker/infix.v @@ -613,12 +613,18 @@ pub fn (mut c Checker) infix_expr(mut node ast.InfixExpr) ast.Type { // TODO move this to symmetric_check? Right now it would break `return 0` for `fn()?int ` left_is_optional := left_type.has_flag(.optional) right_is_optional := right_type.has_flag(.optional) - if left_is_optional && right_is_optional { - c.error('unwrapped optionals cannot be used in an infix expression', left_right_pos) - } else if left_is_optional || right_is_optional { + if left_is_optional || right_is_optional { opt_infix_pos := if left_is_optional { left_pos } else { right_pos } c.error('unwrapped optional cannot be used in an infix expression', opt_infix_pos) } + + left_is_result := left_type.has_flag(.result) + right_is_result := right_type.has_flag(.result) + if left_is_result || right_is_result { + opt_infix_pos := if left_is_result { left_pos } else { right_pos } + c.error('unwrapped result cannot be used in an infix expression', opt_infix_pos) + } + // Dual sides check (compatibility check) if node.left !is ast.ComptimeCall && node.right !is ast.ComptimeCall { if !(c.symmetric_check(left_type, right_type) && c.symmetric_check(right_type, left_type)) diff --git a/vlib/v/checker/tests/unwrapped_result_infix_err.out b/vlib/v/checker/tests/unwrapped_result_infix_err.out new file mode 100644 index 0000000000..69de1f98dd --- /dev/null +++ b/vlib/v/checker/tests/unwrapped_result_infix_err.out @@ -0,0 +1,8 @@ +vlib/v/checker/tests/unwrapped_result_infix_err.vv:7:9: error: unwrapped result cannot be used in an infix expression + 5 | fn g() ! { + 6 | assert f('1')! == true + 7 | assert f('1') == true + | ~~~~~~ + 8 | } + 9 | + diff --git a/vlib/v/checker/tests/unwrapped_result_infix_err.vv b/vlib/v/checker/tests/unwrapped_result_infix_err.vv new file mode 100644 index 0000000000..06e423231b --- /dev/null +++ b/vlib/v/checker/tests/unwrapped_result_infix_err.vv @@ -0,0 +1,12 @@ +fn f(k string) !bool { + return true +} + +fn g() ! { + assert f('1')! == true + assert f('1') == true +} + +fn main() { + g()! +}