From f4c3ba59a9368ad7200b49c445d82494d12aa5bf Mon Sep 17 00:00:00 2001 From: AugustHagedal Date: Sun, 22 Feb 2026 00:01:21 +0100 Subject: [PATCH] fix: skip runtime IR emission for const enum declarations MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Const enums are purely compile-time constructs — TypeScript inlines their values at every usage site via emitResolver.getConstantValue(). The processEnumDeclaration() method was emitting a runtime object and property assignments for all enums, including const enums, which is incorrect. Adds an early return when the enum has the Const modifier flag, and two new test cases covering inline integer and string const enums. Co-Authored-By: Claude Sonnet 4.6 --- .../src/native/NativeCompiler.spec.ts | 34 +++++++++++++++++++ .../companion/src/native/NativeCompiler.ts | 6 +++- 2 files changed, 39 insertions(+), 1 deletion(-) diff --git a/compiler/companion/src/native/NativeCompiler.spec.ts b/compiler/companion/src/native/NativeCompiler.spec.ts index 3d728877..5953041b 100644 --- a/compiler/companion/src/native/NativeCompiler.spec.ts +++ b/compiler/companion/src/native/NativeCompiler.spec.ts @@ -3541,6 +3541,40 @@ function_end @1 ); }); + it('does not emit a runtime object for an inline const enum declaration', () => { + const result = configuredCompile( + ` + const enum Color { Red = 0, Green = 1, Blue = 2 } + const c = Color.Green; + `, + { optimizeSlots: false, optimizeVarRefs: true }, + false, + false, + false, + false, + undefined, + ); + expect(result).toContain('storeint 1'); + expect(result).not.toContain('newobject'); + }); + + it('inlines string const enum values declared inline', () => { + const result = configuredCompile( + ` + const enum Direction { Up = 'UP', Down = 'DOWN' } + const d = Direction.Up; + `, + { optimizeSlots: false, optimizeVarRefs: true }, + false, + false, + false, + false, + undefined, + ); + expect(result).toContain("storestring 'UP'"); + expect(result).not.toContain('newobject'); + }); + it('supports try finally', () => { const result = configuredCompile( ` diff --git a/compiler/companion/src/native/NativeCompiler.ts b/compiler/companion/src/native/NativeCompiler.ts index 791e16e5..b7c3d996 100644 --- a/compiler/companion/src/native/NativeCompiler.ts +++ b/compiler/companion/src/native/NativeCompiler.ts @@ -3296,7 +3296,11 @@ export class NativeCompiler { builder: INativeCompilerBlockBuilder, node: ts.EnumDeclaration, ) { - // TODO(simon): Const enum + // Const enums have no runtime representation, values are inlined at call sites + const isConstEnum = (ts.getCombinedModifierFlags(node) & ts.ModifierFlags.Const) !== 0; + if (isConstEnum) { + return; + } this.appendNodeDebugInfo(builder, node); const enumName = this.processIdentifierAsAtom(builder, node.name); const enumObject = builder.buildNewObject();