Skip to content

Commit a4541c2

Browse files
authored
checker: fix sumtype variant option type mismatch (#23659)
1 parent c01855c commit a4541c2

4 files changed

Lines changed: 30 additions & 3 deletions

File tree

‎vlib/v/checker/checker.v‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3402,8 +3402,7 @@ fn (mut c Checker) cast_expr(mut node ast.CastExpr) ast.Type {
34023402
node.expr_type = c.promote_num(node.expr_type, xx)
34033403
from_type = node.expr_type
34043404
}
3405-
if !c.table.sumtype_has_variant(to_type, from_type, false) && !to_type.has_flag(.option)
3406-
&& !to_type.has_flag(.result) {
3405+
if !c.table.sumtype_has_variant(to_type, from_type, false) {
34073406
ft := c.table.type_to_str(from_type)
34083407
tt := c.table.type_to_str(to_type)
34093408
c.error('cannot cast `${ft}` to `${tt}`', node.pos)
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
vlib/v/checker/tests/sumtype_variant_mismatch.vv:4:7: error: cannot cast `?string` to `?Any`
2+
2 | type Any2 = ?int | ?string
3+
3 |
4+
4 | _ := ?Any(?string('baz'))
5+
| ~~~~~~~~~~~~~~~~~~~
6+
5 | _ := ?Any(string('baz'))
7+
6 | _ := ?Any('baz')
8+
vlib/v/checker/tests/sumtype_variant_mismatch.vv:9:7: error: cannot cast `string` to `?Any2`
9+
7 |
10+
8 | _ := ?Any2(?string('baz'))
11+
9 | _ := ?Any2(string('baz'))
12+
| ~~~~~~~~~~~~~~~~~~~
13+
10 | _ := ?Any2('baz')
14+
vlib/v/checker/tests/sumtype_variant_mismatch.vv:10:7: error: cannot cast `string` to `?Any2`
15+
8 | _ := ?Any2(?string('baz'))
16+
9 | _ := ?Any2(string('baz'))
17+
10 | _ := ?Any2('baz')
18+
| ~~~~~~~~~~~
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
type Any = ?int | string
2+
type Any2 = ?int | ?string
3+
4+
_ := ?Any(?string('baz'))
5+
_ := ?Any(string('baz'))
6+
_ := ?Any('baz')
7+
8+
_ := ?Any2(?string('baz'))
9+
_ := ?Any2(string('baz'))
10+
_ := ?Any2('baz')

‎vlib/x/json2/decoder.v‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,7 @@ fn decode_array_item[T](mut field T, arr []Any) {
259259
typeof[[]time.Time]().idx { field = arr.map(it.to_time() or { time.Time{} }) }
260260
typeof[[]?time.Time]().idx { field = arr.map(?time.Time(it.to_time() or { time.Time{} })) }
261261
typeof[[]Any]().idx { field = arr.clone() }
262-
typeof[[]?Any]().idx { field = arr.map(?Any(it)) }
262+
typeof[[]?Any]().idx { field = arr.map(it) }
263263
typeof[[]u8]().idx { field = arr.map(it.u64()) }
264264
typeof[[]?u8]().idx { field = arr.map(?u8(it.u64())) }
265265
typeof[[]u16]().idx { field = arr.map(it.u64()) }

0 commit comments

Comments
 (0)