checker: don't disallow method call chains (#7128)

pull/7137/head
spaceface777 2020-12-04 21:27:38 +01:00 committed by GitHub
parent f14bd10c00
commit 76ed8e3750
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 16 additions and 2 deletions

View File

@ -994,8 +994,6 @@ fn (mut c Checker) fail_if_immutable(expr ast.Expr) (string, token.Position) {
// No automatic lock for array slicing (yet(?))
explicit_lock_needed = true
}
} else {
c.error('cannot use function call as mut', expr.pos)
}
}
ast.ArrayInit {

View File

@ -0,0 +1,16 @@
struct Test {
mut:
val int
}
// this must return a reference, or else you'll get a C error
// TODO: add a proper checker check for that case
fn new(x int) &Test { return &Test{ x } }
fn (mut t Test) inc() &Test { t.val++ return t }
fn (mut t Test) add(x int) &Test { t.val += x return t }
fn (mut t Test) div(x int) &Test { t.val /= x return t }
fn test_method_call_chains() {
mut x := new(4).inc().inc().inc().inc().add(4).div(2).inc()
assert x.val == 7
}