checker: add error for `for x in t {`, where `t.next()` returns multiple values

(Fix #9459).
pull/13036/head
Delyan Angelov 2022-01-05 00:37:39 +02:00
parent b3930c3d6a
commit 89ac2a37c5
No known key found for this signature in database
GPG Key ID: 66886C0F12D595ED
3 changed files with 22 additions and 0 deletions

View File

@ -61,6 +61,10 @@ fn (mut c Checker) for_in_stmt(mut node ast.ForInStmt) {
if !next_fn.return_type.has_flag(.optional) {
c.error('iterator method `next()` must return an optional', node.cond.position())
}
return_sym := c.table.sym(next_fn.return_type)
if return_sym.kind == .multi_return {
c.error('iterator method `next()` must not return multiple values', node.cond.position())
}
// the receiver
if next_fn.params.len != 1 {
c.error('iterator method `next()` must have 0 parameters', node.cond.position())

View File

@ -0,0 +1,7 @@
vlib/v/checker/tests/for_in_iterator_returning_multiple_values_err.vv:8:10: error: iterator method `next()` must not return multiple values
6 |
7 | t := TestStruct{}
8 | for x in t {
| ^
9 | println(x) // ('foo', 'bar')
10 | break

View File

@ -0,0 +1,11 @@
struct TestStruct {}
fn (t TestStruct) next() ?(string, string) {
return 'foo', 'bar'
}
t := TestStruct{}
for x in t {
println(x) // ('foo', 'bar')
break
}