diff options
| author | Bruce Hill <bruce@bruce-hill.com> | 2025-11-26 21:04:12 -0500 |
|---|---|---|
| committer | Bruce Hill <bruce@bruce-hill.com> | 2025-11-26 21:04:12 -0500 |
| commit | a21f9ddfd05c643049c22bb52ab3a60f41933492 (patch) | |
| tree | 8a4e3967f9d8b7863c238a357eb47e978d3a8c41 /test | |
| parent | 14c8cf34dd75fcf49cc56025efa93dd32e1958fd (diff) | |
Bugfix for accidental violation of immutable value guarantees due to
inner field members
Diffstat (limited to 'test')
| -rw-r--r-- | test/values.tm | 47 |
1 files changed, 47 insertions, 0 deletions
diff --git a/test/values.tm b/test/values.tm new file mode 100644 index 00000000..86f34a89 --- /dev/null +++ b/test/values.tm @@ -0,0 +1,47 @@ +# Tests for ensuring immutable value nature in various contexts +struct Inner(xs:[Int32]) + +struct Outer(inner:Inner) + +func sneaky(outer:Outer) + (&outer.inner.xs)[1] = 99 + +func sneaky2(outer:&Outer) + (&outer.inner.xs)[1] = 99 + +func main() + do + xs := [10, 20, 30] + copy := xs + (&xs)[1] = 99 + assert xs == [99, 20, 30] + assert copy == [10, 20, 30] + + do + t := {"A":10, "B":20} + copy := t + (&t)["A"] = 99 + assert t == {"A":99, "B":20} + assert copy == {"A":10, "B":20} + + do + foo := Outer(Inner([10, 20, 30])) + copy := foo + (&foo.inner.xs)[1] = 99 + assert foo.inner.xs == [99, 20, 30] + assert copy.inner.xs == [10, 20, 30] + + do + foo := Outer(Inner([10, 20, 30])) + copy := foo + sneaky(foo) + assert foo.inner.xs == [10, 20, 30] + assert copy.inner.xs == [10, 20, 30] + + do + foo := Outer(Inner([10, 20, 30])) + copy := foo + sneaky2(&foo) + assert foo.inner.xs == [99, 20, 30] + assert copy.inner.xs == [10, 20, 30] + |
