Skip to content

Commit

Permalink
feat: Error when attemping to mutate an immutable tuple type
Browse files Browse the repository at this point in the history
  • Loading branch information
tristanmenzel committed Oct 16, 2024
1 parent 86010a3 commit 88b9a10
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
6 changes: 5 additions & 1 deletion src/awst_build/eb/index.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand Down Expand Up @@ -236,14 +237,17 @@ 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,
})
}
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,
})
}
Expand Down
16 changes: 16 additions & 0 deletions tests/expected-output/mutating-tuples.algo.ts
Original file line number Diff line number Diff line change
@@ -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 }
}

0 comments on commit 88b9a10

Please sign in to comment.