From 2f34a12badb93dc7a88b09f73e2219d8ae7080c2 Mon Sep 17 00:00:00 2001 From: "g. nicholas d'andrea" Date: Wed, 14 Oct 2026 22:31:44 -0400 Subject: [PATCH] bugc: test token-registry example at every level Now that #388 and #391 are in, the example compiles and runs the same at O0-O3, so drop its @wip marker. Also note in constantBytes and foldCast that a string constant's bytes follow its type. --- packages/bugc/CHANGELOG.md | 4 ++++ packages/bugc/examples/advanced/token-registry.bug | 1 - packages/bugc/src/ir/utils/constant.ts | 4 ++++ packages/bugc/src/optimizer/steps/constant-folding.ts | 2 ++ packages/bugc/test/examples/examples.test.ts | 5 +++-- 5 files changed, 13 insertions(+), 3 deletions(-) diff --git a/packages/bugc/CHANGELOG.md b/packages/bugc/CHANGELOG.md index 1b5cb6a563..7bb8f32bdb 100644 --- a/packages/bugc/CHANGELOG.md +++ b/packages/bugc/CHANGELOG.md @@ -53,6 +53,9 @@ support. Changes to the specification itself are tracked in the root `struct Outer { x: uint8; inner: Inner; y: uint16; }`, `y` is now in slot 3 of an `Outer`, after `inner` in slots 1 and 2. Before, `y` was in slot 2 ([#377]). +- The `advanced/token-registry.bug` example is no longer marked work + in progress. It compiles, and runs the same, at every optimization + level ([#395]). ### Fixed @@ -572,3 +575,4 @@ First publication. [#384]: https://github.com/ethdebug/format/pull/384 [#388]: https://github.com/ethdebug/format/pull/388 [#391]: https://github.com/ethdebug/format/pull/391 +[#395]: https://github.com/ethdebug/format/pull/395 diff --git a/packages/bugc/examples/advanced/token-registry.bug b/packages/bugc/examples/advanced/token-registry.bug index a128ff052b..d8088588ab 100644 --- a/packages/bugc/examples/advanced/token-registry.bug +++ b/packages/bugc/examples/advanced/token-registry.bug @@ -1,4 +1,3 @@ -// @wip name TokenRegistry; // This example demonstrates: diff --git a/packages/bugc/src/ir/utils/constant.ts b/packages/bugc/src/ir/utils/constant.ts index 2df501f384..09e0dc502f 100644 --- a/packages/bugc/src/ir/utils/constant.ts +++ b/packages/bugc/src/ir/utils/constant.ts @@ -7,6 +7,10 @@ import { Type } from "../spec/type.js"; * `string` is its text, in UTF-8, and `bytes` (from a hex literal too * long for a word) are the bytes the "0x…" hex names. Code generation * stores these bytes, and constant folding hashes them. + * + * The encoding follows the constant's TYPE, so a fold of a string + * constant across `as bytes` must re-encode its value ("0x" + hex of + * the UTF-8), not only retype it. */ export function constantBytes(value: string, type: Type): Uint8Array { const isString = diff --git a/packages/bugc/src/optimizer/steps/constant-folding.ts b/packages/bugc/src/optimizer/steps/constant-folding.ts index 5d0d091803..bdc5873302 100644 --- a/packages/bugc/src/optimizer/steps/constant-folding.ts +++ b/packages/bugc/src/optimizer/steps/constant-folding.ts @@ -191,6 +191,8 @@ export class ConstantFoldingStep extends BaseOptimizationStep { constants: Map, ): Ir.Instruction.Const | undefined { const value = this.getConstantValue(inst.value, constants); + // A string constant's bytes follow its type (see constantBytes): + // to fold one across `as bytes`, re-encode its value as "0x" + hex if (typeof value !== "bigint") return undefined; const steps = Ir.Utils.castSteps(inst.value.type, inst.targetType); diff --git a/packages/bugc/test/examples/examples.test.ts b/packages/bugc/test/examples/examples.test.ts index 0714c1ad41..e6dcea09c8 100644 --- a/packages/bugc/test/examples/examples.test.ts +++ b/packages/bugc/test/examples/examples.test.ts @@ -297,9 +297,10 @@ describe("Example Files", async () => { expect(identifiers).toEqual(storageNames); } // A program lists its locals at every instruction, so a long - // one takes a while to validate + // one takes a while to validate (advanced/token-registry.bug + // takes over 10 seconds on CI) }, - 10_000, + 30_000, ); } }