diff --git a/vlib/v/checker/infix.v b/vlib/v/checker/infix.v index 4466525eac1897..d2b0b94457f20b 100644 --- a/vlib/v/checker/infix.v +++ b/vlib/v/checker/infix.v @@ -781,6 +781,10 @@ fn (mut c Checker) infix_expr(mut node ast.InfixExpr) ast.Type { if typ != ast.no_type { typ_sym := c.table.sym(typ) op := node.op.str() + if left_type.has_flag(.option) { + c.error('${node.left} is an Optional, it needs to be unwrapped first', + node.left.pos()) + } if typ_sym.kind == .placeholder { c.error('${op}: type `${typ_sym.name}` does not exist', right_expr.pos()) } diff --git a/vlib/v/checker/tests/opt_is_op_check_err.out b/vlib/v/checker/tests/opt_is_op_check_err.out new file mode 100644 index 00000000000000..f10ce93db2bbfa --- /dev/null +++ b/vlib/v/checker/tests/opt_is_op_check_err.out @@ -0,0 +1,7 @@ +vlib/v/checker/tests/opt_is_op_check_err.vv:13:7: error: t.a is an Optional, it needs to be unwrapped first + 11 | a: s + 12 | } + 13 | if t.a is string { + | ^ + 14 | r(t.a) + 15 | } diff --git a/vlib/v/checker/tests/opt_is_op_check_err.vv b/vlib/v/checker/tests/opt_is_op_check_err.vv new file mode 100644 index 00000000000000..917bdacba26ff4 --- /dev/null +++ b/vlib/v/checker/tests/opt_is_op_check_err.vv @@ -0,0 +1,21 @@ +type Foo = int | string + +struct Struct { + a ?Foo +} + +fn r(a string) {} + +fn t(s ?Foo) { + mut t := Struct{ + a: s + } + if t.a is string { + r(t.a) + } +} + +fn main() { + s := ?Foo('hello') + t(s) +} diff --git a/vlib/v/checker/tests/orm_op_with_option_and_none.out b/vlib/v/checker/tests/orm_op_with_option_and_none.out index 01ee85cbe75fb2..29aeb2dcfb2129 100644 --- a/vlib/v/checker/tests/orm_op_with_option_and_none.out +++ b/vlib/v/checker/tests/orm_op_with_option_and_none.out @@ -47,3 +47,17 @@ vlib/v/checker/tests/orm_op_with_option_and_none.vv:45:30: warning: comparison w | ~~ 46 | }! 47 | +vlib/v/checker/tests/orm_op_with_option_and_none.vv:49:25: error: name is an Optional, it needs to be unwrapped first + 47 | + 48 | _ := sql db { + 49 | select from Foo where name is none + | ~~~~ + 50 | }! + 51 | +vlib/v/checker/tests/orm_op_with_option_and_none.vv:53:25: error: name is an Optional, it needs to be unwrapped first + 51 | + 52 | _ := sql db { + 53 | select from Foo where name !is none + | ~~~~ + 54 | }! + 55 | }