checker: fix cast to sum type (fix #5690) (#5692)

pull/5654/head^2
yuyi 2020-07-06 18:32:59 +08:00 committed by GitHub
parent f41edef4c6
commit fc7237be7b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 15 additions and 8 deletions

View File

@ -2131,7 +2131,7 @@ pub fn (mut c Checker) expr(node ast.Expr) table.Type {
} }
if node.expr_type == table.string_type { if node.expr_type == table.string_type {
cast_to_type_sym := c.table.get_type_symbol(node.typ) cast_to_type_sym := c.table.get_type_symbol(node.typ)
if cast_to_type_sym.kind != .alias { if cast_to_type_sym.kind !in [.alias, .sum_type] {
mut error_msg := 'cannot cast a string' mut error_msg := 'cannot cast a string'
if node.expr is ast.StringLiteral { if node.expr is ast.StringLiteral {
str_lit := node.expr as ast.StringLiteral str_lit := node.expr as ast.StringLiteral

View File

@ -1,10 +1,3 @@
vlib/v/checker/tests/match_expr_else.v:4:10: error: cannot cast a string
2 |
3 | fn main() {
4 | x := AA('test')
| ~~~~~~
5 | _ = match x {
6 | int {
vlib/v/checker/tests/match_expr_else.v:5:6: error: match must be exhaustive (add match branches for: `f64` or `else {}` at the end) vlib/v/checker/tests/match_expr_else.v:5:6: error: match must be exhaustive (add match branches for: `f64` or `else {}` at the end)
3 | fn main() { 3 | fn main() {
4 | x := AA('test') 4 | x := AA('test')

View File

@ -128,3 +128,17 @@ fn test_nested_sumtype() {
assert false assert false
} }
} }
type Abc = int | string
fn test_string_cast_to_sumtype() {
a := Abc('test')
match a {
string {
assert true
}
int {
assert false
}
}
}