Skip to content

Commit 6501145

Browse files
committed
fix
1 parent 2945629 commit 6501145

File tree

3 files changed

+18
-11
lines changed

3 files changed

+18
-11
lines changed

vlib/v/checker/checker.v

+11-4
Original file line numberDiff line numberDiff line change
@@ -2959,10 +2959,17 @@ pub fn (mut c Checker) expr(mut node ast.Expr) ast.Type {
29592959
c.table.used_features.as_cast = true
29602960
}
29612961
if mut node.expr is ast.Ident {
2962-
if !node.typ.has_flag(.option) && node.expr.obj.typ.has_flag(.option)
2963-
&& node.expr.or_expr.kind == .absent {
2964-
c.error('variable `${node.expr.name}` is an Option, it must be unwrapped first',
2965-
node.expr.pos)
2962+
if mut node.expr.obj is ast.Var {
2963+
ident_typ := if node.expr.obj.smartcasts.len > 0 {
2964+
node.expr.obj.smartcasts.last()
2965+
} else {
2966+
node.expr.obj.typ
2967+
}
2968+
if !node.typ.has_flag(.option) && ident_typ.has_flag(.option)
2969+
&& node.expr.or_expr.kind == .absent {
2970+
c.error('variable `${node.expr.name}` is an Option, it must be unwrapped first',
2971+
node.expr.pos)
2972+
}
29662973
}
29672974
}
29682975
if expr_type_sym.kind == .sum_type {

vlib/v/tests/options/option_concatexpr_test.v

+3-3
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,17 @@ fn foo(f int) !(int, ?int) {
1212

1313
fn test_main() {
1414
a, b := foo(-1) or { 2, 2 }
15-
c := b as int
15+
c := b? as int
1616
assert a == 2
1717
assert c == 2
1818

1919
a2, b2 := foo(0) or { 2, 2 }
20-
c2 := b2 as int
20+
c2 := b2? as int
2121
assert a2 == 0
2222
assert c2 == 0
2323

2424
a3, b3 := foo(1) or { 2, 2 }
25-
c3 := b3 as int
25+
c3 := b3? as int
2626
assert a3 == 1
2727
assert c3 == 1
2828
}

vlib/v/tests/options/option_unwrap_as_cast_test.v

+4-4
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@ fn get_opt_int(a int) ?int {
1515
fn test_option_unwrap_as_cast() {
1616
x := get_opt(1)
1717
d := get_opt_int(12)
18-
dump(d as int == 12)
19-
dump('${x as string}' == 'success')
18+
dump(d? as int == 12)
19+
dump('${x? as string}' == 'success')
2020

21-
assert d as int == 12
22-
assert x as string == 'success'
21+
assert d? as int == 12
22+
assert x? as string == 'success'
2323
}

0 commit comments

Comments
 (0)