Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
felipensp committed Feb 6, 2025
1 parent c9235b8 commit bffbc9a
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 2 deletions.
3 changes: 1 addition & 2 deletions vlib/v/checker/checker.v
Original file line number Diff line number Diff line change
Expand Up @@ -3402,8 +3402,7 @@ fn (mut c Checker) cast_expr(mut node ast.CastExpr) ast.Type {
node.expr_type = c.promote_num(node.expr_type, xx)
from_type = node.expr_type
}
if !c.table.sumtype_has_variant(to_type, from_type, false) && !to_type.has_flag(.option)
&& !to_type.has_flag(.result) {
if !c.table.sumtype_has_variant(to_type, from_type, false) {
ft := c.table.type_to_str(from_type)
tt := c.table.type_to_str(to_type)
c.error('cannot cast `${ft}` to `${tt}`', node.pos)
Expand Down
18 changes: 18 additions & 0 deletions vlib/v/checker/tests/sumtype_variant_mismatch.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
vlib/v/checker/tests/sumtype_variant_mismatch.vv:4:7: error: cannot cast `?string` to `?Any`
2 | type Any2 = ?int | ?string
3 |
4 | _ := ?Any(?string('baz'))
| ~~~~~~~~~~~~~~~~~~~
5 | _ := ?Any(string('baz'))
6 | _ := ?Any('baz')
vlib/v/checker/tests/sumtype_variant_mismatch.vv:10:7: error: cannot cast `string` to `?Any2`
8 |
9 | _ := ?Any2(?string('baz'))
10 | _ := ?Any2(string('baz'))
| ~~~~~~~~~~~~~~~~~~~
11 | _ := ?Any2('baz')
vlib/v/checker/tests/sumtype_variant_mismatch.vv:11:7: error: cannot cast `string` to `?Any2`
9 | _ := ?Any2(?string('baz'))
10 | _ := ?Any2(string('baz'))
11 | _ := ?Any2('baz')
| ~~~~~~~~~~~
10 changes: 10 additions & 0 deletions vlib/v/checker/tests/sumtype_variant_mismatch.vv
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
type Any = ?int | string
type Any2 = ?int | ?string

_ := ?Any(?string('baz'))

Check failure on line 4 in vlib/v/checker/tests/sumtype_variant_mismatch.vv

View workflow job for this annotation

GitHub Actions / docker-alpine-musl-gcc

cannot cast `?string` to `?Any`

Check failure on line 4 in vlib/v/checker/tests/sumtype_variant_mismatch.vv

View workflow job for this annotation

GitHub Actions / docker-alpine-musl-gcc

cannot cast `?string` to `?Any`

Check failure on line 4 in vlib/v/checker/tests/sumtype_variant_mismatch.vv

View workflow job for this annotation

GitHub Actions / docker-ubuntu-musl

cannot cast `?string` to `?Any`

Check failure on line 4 in vlib/v/checker/tests/sumtype_variant_mismatch.vv

View workflow job for this annotation

GitHub Actions / docker-ubuntu-musl

cannot cast `?string` to `?Any`
_ := ?Any(string('baz'))
_ := ?Any('baz')

_ := ?Any2(?string('baz'))
_ := ?Any2(string('baz'))

Check failure on line 9 in vlib/v/checker/tests/sumtype_variant_mismatch.vv

View workflow job for this annotation

GitHub Actions / docker-alpine-musl-gcc

cannot cast `string` to `?Any2`

Check failure on line 9 in vlib/v/checker/tests/sumtype_variant_mismatch.vv

View workflow job for this annotation

GitHub Actions / docker-ubuntu-musl

cannot cast `string` to `?Any2`
_ := ?Any2('baz')

Check failure on line 10 in vlib/v/checker/tests/sumtype_variant_mismatch.vv

View workflow job for this annotation

GitHub Actions / docker-alpine-musl-gcc

cannot cast `string` to `?Any2`

Check failure on line 10 in vlib/v/checker/tests/sumtype_variant_mismatch.vv

View workflow job for this annotation

GitHub Actions / docker-alpine-musl-gcc

cannot cast `string` to `?Any2`

Check failure on line 10 in vlib/v/checker/tests/sumtype_variant_mismatch.vv

View workflow job for this annotation

GitHub Actions / docker-ubuntu-musl

cannot cast `string` to `?Any2`

Check failure on line 10 in vlib/v/checker/tests/sumtype_variant_mismatch.vv

View workflow job for this annotation

GitHub Actions / docker-ubuntu-musl

cannot cast `string` to `?Any2`

0 comments on commit bffbc9a

Please sign in to comment.