diff --git a/vlib/v/checker/checker.v b/vlib/v/checker/checker.v index 6fb275302d..2c44feaaca 100644 --- a/vlib/v/checker/checker.v +++ b/vlib/v/checker/checker.v @@ -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) } diff --git a/vlib/v/tests/sumtype_assign_test.v b/vlib/v/tests/sumtype_assign_test.v new file mode 100644 index 0000000000..e44ae1b9a9 --- /dev/null +++ b/vlib/v/tests/sumtype_assign_test.v @@ -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' +}