1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

checker: fix sumtype assign error (fix #7988) (#8655)

This commit is contained in:
yuyi 2021-02-10 17:59:56 +08:00 committed by GitHub
parent 035a163454
commit f2e74bce7a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 49 additions and 0 deletions

View File

@ -1199,6 +1199,9 @@ fn (mut c Checker) fail_if_immutable(expr ast.Expr) (string, token.Position) {
c.error('`$typ_sym.kind` can not be modified', expr.pos)
}
}
.aggregate {
c.fail_if_immutable(expr.expr)
}
else {
c.error('unexpected symbol `$typ_sym.kind`', expr.pos)
}

View File

@ -0,0 +1,46 @@
struct Foo {
mut:
num int
}
struct Bar {
mut:
text string
}
struct Baz {
mut:
text string
}
type FBB = Bar | Baz | Foo
fn test_sumtype_assign() {
mut arr := []FBB{}
arr << Foo{
num: 22
}
arr << Bar{
text: 'bar'
}
arr << Baz{
text: 'baz'
}
mut results := []string{}
for a in arr {
match mut a {
Bar, Baz {
a.text = 'I am ' + a.text
println(a.text)
results << a.text
}
Foo {
a.num = 11
results << 'Num is $a.num'
}
}
}
assert results[0] == 'Num is 11'
assert results[1] == 'I am bar'
assert results[2] == 'I am baz'
}