From 88b9a1010b974c435e3a4c3360049da801caeb60 Mon Sep 17 00:00:00 2001 From: Tristan Menzel Date: Tue, 15 Oct 2024 18:43:24 -0700 Subject: [PATCH] feat: Error when attemping to mutate an immutable tuple type --- src/awst_build/eb/index.ts | 6 +++++- tests/expected-output/mutating-tuples.algo.ts | 16 ++++++++++++++++ 2 files changed, 21 insertions(+), 1 deletion(-) create mode 100644 tests/expected-output/mutating-tuples.algo.ts diff --git a/src/awst_build/eb/index.ts b/src/awst_build/eb/index.ts index 14e77339..ff65eead 100644 --- a/src/awst_build/eb/index.ts +++ b/src/awst_build/eb/index.ts @@ -1,6 +1,7 @@ import { awst } from '../../awst' import { nodeFactory } from '../../awst/node-factory' import type { Expression } from '../../awst/nodes' +import { TupleItemExpression } from '../../awst/nodes' import type { SourceLocation } from '../../awst/source-location' import { CodeError, NotSupported } from '../../errors' import { logger } from '../../logger' @@ -236,6 +237,9 @@ export function requireLValue(expr: awst.Expression): awst.LValue { awst.AppAccountStateExpression, awst.BoxValueExpression, ] + if (expr instanceof TupleItemExpression) { + throw new CodeError('Expression is not a valid assignment target - object is immutable', { sourceLocation: expr.sourceLocation }) + } if (!lValueNodes.some((l) => expr instanceof l)) { throw new CodeError(`Expression is not a valid assignment target`, { sourceLocation: expr.sourceLocation, @@ -243,7 +247,7 @@ export function requireLValue(expr: awst.Expression): awst.LValue { } if (expr instanceof awst.IndexExpression || expr instanceof awst.FieldExpression) { if (expr.base.wtype.immutable) { - throw new CodeError(`${expr.wtype} is not a valid assignment target as it is immutable`, { + throw new CodeError(`Expression is not a valid assignment target - object is immutable`, { sourceLocation: expr.sourceLocation, }) } diff --git a/tests/expected-output/mutating-tuples.algo.ts b/tests/expected-output/mutating-tuples.algo.ts new file mode 100644 index 00000000..138c6a31 --- /dev/null +++ b/tests/expected-output/mutating-tuples.algo.ts @@ -0,0 +1,16 @@ +import type { uint64 } from '@algorandfoundation/algorand-typescript' + +function test(a: uint64) { + const t1: [uint64, uint64, uint64] = [a, a, a] + let t2 = { a, b: a, c: a } + + a *= 2 + + // @expect-error Expression is not a valid assignment target - object is immutable + t1[0] = a + + // @expect-error Expression is not a valid assignment target - object is immutable + t2.a = a + + t2 = { ...t2, a } +}