Skip to content

bugc: make a bytes literal local hold the literal's bytes - #345

Merged
gnidan merged 1 commit into
mainfrom
bugc-bytes-literal
Oct 6, 2026
Merged

gnidan merged 1 commit into
mainfrom
bugc-bytes-literal

Conversation

@gnidan

@gnidan gnidan commented Oct 6, 2026

Copy link
Copy Markdown
Member

A local initialized with a bytes literal, such as data in examples/intermediate/memory-slices.bug, never held the literal's bytes. At every optimization level, its data read as zeros followed by a word such as 0x154, both in the program (data[0:32]) and through its debug pointer from #328.

The pointer was right; the code was wrong. A bytes literal compiles to a const of type ref<memory>, and code generation for it already writes the literal to memory as bugc lays out bytes (a length word, then the data) and gives its address. The declaration treated that address as the data: it allocated a second copy, wrote the length there, then stored the first copy's address after it, and the local referred to the second copy. Now the local refers to the constant's memory, as a string literal local does, and the hex-specific allocation is gone.

bytes-literal.test.ts checks a 40-byte literal's length and contents (through slices cast to bytes32) at levels 0 to 3, and the local-variable soundness tests gain a bytes literal whose pointer must read the literal's bytes.

@github-actions

github-actions Bot commented Oct 6, 2026 •

Copy link
Copy Markdown
Contributor
PR Preview Action v1.8.1
Preview removed because the pull request was closed.
2026-10-06 15:17 UTC

@gnidan
gnidan force-pushed the bugc-bytes-literal branch from bee9579 to 76b6551 Compare October 6, 2026 14:31
A `bytes` literal compiles to a constant that already lays the literal
out in memory (its length word, then its data) and gives its address.
A declaration initialized with one allocated a second copy anyway, wrote
the length there, and then wrote the first copy's address as the data.
So the local's data read as zeros followed by that address, both in the
program (as in `data[0:32]`) and through its debug pointer. Let the
local refer to the constant, as a string literal local does.
@gnidan
gnidan force-pushed the bugc-bytes-literal branch from 76b6551 to 8b5f1e8 Compare October 6, 2026 14:59
@gnidan
gnidan merged commit 2fd7e78 into main Oct 6, 2026
7 checks passed
@gnidan
gnidan deleted the bugc-bytes-literal branch October 6, 2026 15:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant