Skip to content

fix(compiler): avoid ICE for @example with large int64 - #12085

Open
Tanvir Alam (tanvir-ux) wants to merge 1 commit into
microsoft:mainfrom
tanvir-ux:fix-example-int64-precision-ice
Open

Tanvir Alam (tanvir-ux) wants to merge 1 commit into
microsoft:mainfrom
tanvir-ux:fix-example-int64-precision-ice

Conversation

@tanvir-ux

Copy link
Copy Markdown

Why

Fixes #12067

@example is declared as valueof unknown. When marshalling a numeric decorator argument against that open-ended constraint, canNumericConstraintBeJsNumber returns true, so numericValueToJs requires an exact JavaScript number. Literals above Number.MAX_SAFE_INTEGER (common with int64) make asNumber() return null, and the marshaller previously raised an internal compiler error:

Error: Numeric value '123456789123456789' is not able to convert to a number without losing precision.

Repro:

model Test {
  @example(123456789123456789)
  b: int64;
}

Changes

  • In numericValueToJs, if the constraint looks JS-number-safe but asNumber() still returns null, keep the value as Numeric instead of asserting.
  • Scalars that truly require a JS number (int32, float64, …) continue to reject out-of-range literals in the checker before marshalling.
  • Regression test: @example on int64 above Number.MAX_SAFE_INTEGER compiles and stores the NumericValue.

Tests

pnpm --filter @typespec/compiler exec vitest run test/decorators/examples.test.ts
# Test Files  1 passed (1)
# Tests  45 passed (45)

Notes

After this fix, OpenAPI emission may still write example: null for oversized integers (same as today's default for large int64 without @encode(string)). That is separate from the ICE; #12059 / #12060 cover the @encode(string) serialization path.

@example takes valueof unknown, so numeric marshalling treats the
constraint as JS-number-safe. Literals above Number.MAX_SAFE_INTEGER
then hit an assert in numericValueToJs and abort compilation.

Keep the value as Numeric when asNumber() returns null instead of
raising an internal compiler error. Fixes microsoft#12067.
@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 1 pipeline(s).
1 pipeline(s) were filtered out due to trigger conditions.
There may be pipelines that require an authorized user to comment /azp run to run.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot review overview

🟡 Changes recommended

The fallback can violate explicit float decorator contracts, and its returned value is not directly tested.

Review effort: Balanced
Findings: 1 Medium severity · 1 Low severity

Open (2)
What changed in this PR

Fixes a compiler crash when @example receives integers exceeding JavaScript’s safe range.

Changes:

  • Falls back to Numeric when exact number conversion fails.
  • Adds an int64 regression test.
  • Adds a compiler changelog entry.
File Description
packages/​compiler/​src/​core/​js-marshaller.ts Adds numeric fallback behavior.
packages/​compiler/​test/​decorators/​examples.test.ts Tests oversized int64 examples.
.chronus/​changes/​fix-example-int64-precision-ice-2026-09-30.md Documents the fix.

💡 Configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment on lines +82 to +87
// Open-ended constraints such as `valueof unknown` (used by @example) report
// canBeANumber=true, but the literal may still exceed Number.MAX_SAFE_INTEGER.
// Fall back to Numeric instead of raising an internal compiler error. Scalars
// that require a JS number (int32, float64, ...) reject out-of-range values in
// the checker before marshalling.
return type.value;
Comment on lines +93 to +97
expect(examples).toHaveLength(1);
expect(examples[0].value.valueKind).toBe("NumericValue");
expect((examples[0].value as { value: { toString(): string } }).value.toString()).toBe(
"123456789123456789",
);

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

compiler:core Issues for @typespec/compiler

Projects

None yet

Development

Successfully merging this pull request may close these issues.

@example on int64 too large crash

2 participants