mirror of
https://github.com/vlang/v.git
synced 2025-09-16 16:02:29 +03:00
This commit is contained in:
parent
fac8bb8694
commit
89089ab1d5
6 changed files with 50 additions and 11 deletions
|
@ -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.inside_sql {
|
||||
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())
|
||||
}
|
||||
|
|
7
vlib/v/checker/tests/opt_is_op_check_err.out
Normal file
7
vlib/v/checker/tests/opt_is_op_check_err.out
Normal file
|
@ -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 | }
|
21
vlib/v/checker/tests/opt_is_op_check_err.vv
Normal file
21
vlib/v/checker/tests/opt_is_op_check_err.vv
Normal file
|
@ -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)
|
||||
}
|
|
@ -20,9 +20,11 @@ fn sumtype() {
|
|||
b := ?MySum(Test{
|
||||
a: 1
|
||||
})
|
||||
if b is Test {
|
||||
dump(b)
|
||||
$dbg;
|
||||
if b != none {
|
||||
if b is Test {
|
||||
dump(b)
|
||||
$dbg;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -4370,17 +4370,20 @@ fn (mut g Gen) debugger_stmt(node ast.DebuggerStmt) {
|
|||
cast_sym := g.table.sym(var_typ)
|
||||
|
||||
mut param_var := strings.new_builder(50)
|
||||
is_option := obj.typ.has_flag(.option)
|
||||
is_option := obj.orig_type.has_flag(.option)
|
||||
var_typ_is_option := var_typ.has_flag(.option)
|
||||
if obj.smartcasts.len > 0 {
|
||||
is_option_unwrap := is_option && var_typ == obj.typ.clear_flag(.option)
|
||||
is_option_unwrap := is_option && !obj.typ.has_flag(.option)
|
||||
&& obj.orig_type.has_flag(.option)
|
||||
mut opt_cast := false
|
||||
mut func := if cast_sym.info is ast.Aggregate {
|
||||
''
|
||||
} else {
|
||||
g.get_str_fn(var_typ)
|
||||
}
|
||||
|
||||
if obj.smartcasts.len > 1 && obj_sym.kind == .sum_type {
|
||||
param_var.write_string('*(')
|
||||
}
|
||||
param_var.write_string('(')
|
||||
if obj_sym.kind == .sum_type && !obj.is_auto_heap {
|
||||
if is_option {
|
||||
|
|
|
@ -6,11 +6,13 @@ pub fn teste() ?TType {
|
|||
|
||||
fn test_main() {
|
||||
x := teste()
|
||||
if x is []int {
|
||||
dump(x)
|
||||
y := x as []int
|
||||
assert y == [1, 2]
|
||||
return
|
||||
if x != none {
|
||||
if x is []int {
|
||||
dump(x)
|
||||
y := x as []int
|
||||
assert y == [1, 2]
|
||||
return
|
||||
}
|
||||
}
|
||||
assert false
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue