1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

checker: add error for unwrapped result in infix expr (#16175)

This commit is contained in:
Swastik Baranwal 2022-10-24 14:23:35 +05:30 committed by GitHub
parent 48f43f11ea
commit f25dfa9d8c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 29 additions and 3 deletions

View File

@ -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))

View File

@ -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 |

View File

@ -0,0 +1,12 @@
fn f(k string) !bool {
return true
}
fn g() ! {
assert f('1')! == true
assert f('1') == true
}
fn main() {
g()!
}