checker: fix #9330 (#9342)

pull/9358/head
zakuro 2021-03-18 03:57:14 +09:00 committed by GitHub
parent 49eeddcd1c
commit 6cfc2c217b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 38 additions and 18 deletions

View File

@ -2626,22 +2626,25 @@ pub fn (mut c Checker) assign_stmt(mut assign_stmt ast.AssignStmt) {
right_first := assign_stmt.right[0] right_first := assign_stmt.right[0]
mut right_len := assign_stmt.right.len mut right_len := assign_stmt.right.len
mut right_type0 := table.void_type mut right_type0 := table.void_type
for right in assign_stmt.right { for i, right in assign_stmt.right {
if right is ast.CallExpr || right is ast.IfExpr || right is ast.LockExpr if right is ast.CallExpr || right is ast.IfExpr || right is ast.LockExpr
|| right is ast.MatchExpr { || right is ast.MatchExpr {
right_type0 = c.expr(right) right_type := c.expr(right)
if i == 0 {
right_type0 = right_type
assign_stmt.right_types = [ assign_stmt.right_types = [
c.check_expr_opt_call(right, right_type0), c.check_expr_opt_call(right, right_type0),
] ]
right_type_sym0 := c.table.get_type_symbol(right_type0) }
if right_type_sym0.kind == .multi_return { right_type_sym := c.table.get_type_symbol(right_type)
if right_type_sym.kind == .multi_return {
if assign_stmt.right.len > 1 { if assign_stmt.right.len > 1 {
c.error('cannot use multi-value $right_type_sym0.name in signle-value context', c.error('cannot use multi-value $right_type_sym.name in single-value context',
right.position()) right.position())
} }
assign_stmt.right_types = right_type_sym0.mr_info().types assign_stmt.right_types = right_type_sym.mr_info().types
right_len = assign_stmt.right_types.len right_len = assign_stmt.right_types.len
} else if right_type0 == table.void_type { } else if right_type == table.void_type {
right_len = 0 right_len = 0
} }
} }

View File

@ -12,42 +12,42 @@ vlib/v/checker/tests/assign_multi_mismatch.vv:6:3: error: assignment mismatch: 1
| ~~ | ~~
7 | _, _ := f() 7 | _, _ := f()
8 | _, _ := 0, f() 8 | _, _ := 0, f()
vlib/v/checker/tests/assign_multi_mismatch.vv:8:12: error: cannot use multi-value (int, int) in signle-value context vlib/v/checker/tests/assign_multi_mismatch.vv:8:12: error: cannot use multi-value (int, int) in single-value context
6 | _ := f() 6 | _ := f()
7 | _, _ := f() 7 | _, _ := f()
8 | _, _ := 0, f() 8 | _, _ := 0, f()
| ~~~ | ~~~
9 | _, _ := f(), 0 9 | _, _ := f(), 0
10 | _, _, _ := 0, f() 10 | _, _, _ := 0, f()
vlib/v/checker/tests/assign_multi_mismatch.vv:9:9: error: cannot use multi-value (int, int) in signle-value context vlib/v/checker/tests/assign_multi_mismatch.vv:9:9: error: cannot use multi-value (int, int) in single-value context
7 | _, _ := f() 7 | _, _ := f()
8 | _, _ := 0, f() 8 | _, _ := 0, f()
9 | _, _ := f(), 0 9 | _, _ := f(), 0
| ~~~ | ~~~
10 | _, _, _ := 0, f() 10 | _, _, _ := 0, f()
11 | _, _, _ := f(), 0 11 | _, _, _ := f(), 0
vlib/v/checker/tests/assign_multi_mismatch.vv:10:15: error: cannot use multi-value (int, int) in signle-value context vlib/v/checker/tests/assign_multi_mismatch.vv:10:15: error: cannot use multi-value (int, int) in single-value context
8 | _, _ := 0, f() 8 | _, _ := 0, f()
9 | _, _ := f(), 0 9 | _, _ := f(), 0
10 | _, _, _ := 0, f() 10 | _, _, _ := 0, f()
| ~~~ | ~~~
11 | _, _, _ := f(), 0 11 | _, _, _ := f(), 0
12 | _, _ := f(), f() 12 | _, _ := f(), f()
vlib/v/checker/tests/assign_multi_mismatch.vv:11:12: error: cannot use multi-value (int, int) in signle-value context vlib/v/checker/tests/assign_multi_mismatch.vv:11:12: error: cannot use multi-value (int, int) in single-value context
9 | _, _ := f(), 0 9 | _, _ := f(), 0
10 | _, _, _ := 0, f() 10 | _, _, _ := 0, f()
11 | _, _, _ := f(), 0 11 | _, _, _ := f(), 0
| ~~~ | ~~~
12 | _, _ := f(), f() 12 | _, _ := f(), f()
13 | _, _, _, _ := f(), f() 13 | _, _, _, _ := f(), f()
vlib/v/checker/tests/assign_multi_mismatch.vv:12:9: error: cannot use multi-value (int, int) in signle-value context vlib/v/checker/tests/assign_multi_mismatch.vv:12:9: error: cannot use multi-value (int, int) in single-value context
10 | _, _, _ := 0, f() 10 | _, _, _ := 0, f()
11 | _, _, _ := f(), 0 11 | _, _, _ := f(), 0
12 | _, _ := f(), f() 12 | _, _ := f(), f()
| ~~~ | ~~~
13 | _, _, _, _ := f(), f() 13 | _, _, _, _ := f(), f()
14 | 14 |
vlib/v/checker/tests/assign_multi_mismatch.vv:13:15: error: cannot use multi-value (int, int) in signle-value context vlib/v/checker/tests/assign_multi_mismatch.vv:13:15: error: cannot use multi-value (int, int) in single-value context
11 | _, _, _ := f(), 0 11 | _, _, _ := f(), 0
12 | _, _ := f(), f() 12 | _, _ := f(), f()
13 | _, _, _, _ := f(), f() 13 | _, _, _, _ := f(), f()
@ -61,7 +61,7 @@ vlib/v/checker/tests/assign_multi_mismatch.vv:19:3: error: assignment mismatch:
| ~~ | ~~
20 | 1 { f() } 20 | 1 { f() }
21 | else { f() } 21 | else { f() }
vlib/v/checker/tests/assign_multi_mismatch.vv:23:12: error: cannot use multi-value (int, int) in signle-value context vlib/v/checker/tests/assign_multi_mismatch.vv:23:12: error: cannot use multi-value (int, int) in single-value context
21 | else { f() } 21 | else { f() }
22 | } 22 | }
23 | _, _ := 0, match 4 { 23 | _, _ := 0, match 4 {
@ -74,7 +74,7 @@ vlib/v/checker/tests/assign_multi_mismatch.vv:29:3: error: assignment mismatch:
29 | _ := if true { f() } else { f() } 29 | _ := if true { f() } else { f() }
| ~~ | ~~
30 | _, _ := 0, if true { f() } else { f() } 30 | _, _ := 0, if true { f() } else { f() }
vlib/v/checker/tests/assign_multi_mismatch.vv:30:12: error: cannot use multi-value (int, int) in signle-value context vlib/v/checker/tests/assign_multi_mismatch.vv:30:12: error: cannot use multi-value (int, int) in single-value context
28 | _, _ := 0, if true { 0 } else { 1 } 28 | _, _ := 0, if true { 0 } else { 1 }
29 | _ := if true { f() } else { f() } 29 | _ := if true { f() } else { f() }
30 | _, _ := 0, if true { f() } else { f() } 30 | _, _ := 0, if true { f() } else { f() }

View File

@ -104,3 +104,20 @@ fn test_assign_multi_expr() {
assert s.value == 555 assert s.value == 555
assert s.name == 'initial' assert s.name == 'initial'
} }
fn test_issue_9330() {
arr := "0.1".split('.')
a0, a1 := arr[0], arr[1].int()
assert a0 == '0'
assert a1 == 1
b0, b1 := arr[0].int(), arr[1]
assert b0 == 0
assert b1 == '1'
c0, c1 := arr[0], arr[1]
assert c0 == '0'
assert c1 == '1'
d0, d1 := arr[0].int(), arr[1].f64()
assert d0 == 0
assert d1 == 1.0
}