Skip to content

Commit

Permalink
fix: Error when parsing an unsafe numeric literal
Browse files Browse the repository at this point in the history
  • Loading branch information
tristanmenzel committed Nov 25, 2024
1 parent 991547c commit 9747254
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/awst_build/ast-visitors/base-visitor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,11 +100,19 @@ export abstract class BaseVisitor implements Visitor<Expressions, NodeBuilder> {
}

visitNumericLiteral(node: ts.NumericLiteral): InstanceBuilder {
const sourceLocation = this.sourceLocation(node)
codeInvariant(
!node.text.includes('.'),
'Literals with decimal points are not supported. Use a string literal to capture decimal values',
sourceLocation,
)
const literalValue = BigInt(node.text)
if (literalValue > Number.MAX_SAFE_INTEGER || literalValue < Number.MIN_SAFE_INTEGER) {
logger.error(
sourceLocation,
`This number will lose precision at runtime. Use the Uint64 constructor with a bigint or string literal for very large integers.`,
)
}
const ptype = this.context.getPTypeForNode(node)
invariant(ptype instanceof TransientType, 'Literals should resolve to transient PTypes')
return new BigIntLiteralExpressionBuilder(literalValue, ptype, this.sourceLocation(node))
Expand Down
4 changes: 4 additions & 0 deletions tests/expected-output/uint64-expressions.algo.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { Uint64 } from '@algorandfoundation/algorand-typescript'

/* eslint-disable no-loss-of-precision */

function test() {
// @expect-error uint64 overflow or underflow...
Uint64(-1)
Expand All @@ -13,6 +15,8 @@ function test() {
Uint64('-1')
// @expect-error uint64 overflow or underflow...
Uint64(18446744073709551617n)
// @expect-error This number will lose precision...
Uint64(1844674407370955161)
// @expect-error uint64 overflow or underflow...
Uint64('18446744073709551616')
const varStr = '123'
Expand Down

0 comments on commit 9747254

Please sign in to comment.