Skip to content

Refactor name resolution of components in the text format - #2621

Merged
fitzgen merged 2 commits into
bytecodealliance:mainfrom
alexcrichton:refactor-component-text-parsing
Aug 25, 2026
Merged

fitzgen merged 2 commits into
bytecodealliance:mainfrom
alexcrichton:refactor-component-text-parsing

Conversation

@alexcrichton

Copy link
Copy Markdown
Member

This commit is an update to how name resolution happens for components during the text-to-binary transform. The main purpose of this commit is to enable running the upstream component model test suite in this repository as a submodule, and minor issues in the text format are preventing that. No valid component should change as a result of this reimplementation, but invalid components will now have their errors reported at a slightly different time.

Name references in the text format are generally expected to be possible to do in a forwards and backwards way in the text format. For example in core wasm you can do call $foo before actually defining function $foo. This works because the resolver will first register all names defined and then actually resolve references to those names. Components have historically not worked this way, however, because unlike core modules components cannot refer to names defined later in a component. This influenced the resolver algorithm in the text-to-binary transform in the wast crate where it currently resolves a field and then registers the name of the field. This was done originally because the process of resolving a field may inject aliases into a component due to shorthands possible in the text format. This injection requires name resolution information and cannot happen in an earlier expansion phase for outer aliases as well. This meant that if a $foo identifier was referring to an item defined later this turned out to be a text-parse-time error.

With a text-parse-time error this clashes with wasmprinter's default printing of components unfortunately. For wasmprinter it loads the name section and then prints identifiers for all kinds of references since all names are known up front. This means that for an invalid component wasmprinter will produce unparsable text by wast. This friction makes working with invalid components difficult and notably also makes it so assert_invalid cannot be used for some components that are indeed binary-parsable-but-invalid.

That all leads to the change in this commit. The main refactor here is to how a group of fields are processed. This affects components, nested components, instance types, module types, etc. The goal is to behave more like core wasm in terms of name resolution even if the output component would be invalid (it's not wast's job to validate). Notably the previous resolution is now replaced with a few more phases. Specifically:

  • First all names in a group of fields are collected. Indices are assigned here but are known to be invalid because aliases haven't yet been injected.
  • All fields are resolved to expand their implicit aliases. This will inject export/outer aliases as necessary into the list of items.
  • This temporary resolution state is now discarded now that aliases are fully expanded.
  • Next resolution proceeds like core wasm again, but this time indices are "for real". All fields are registered with an index and then resolved internally.

This behavior change means that components now behave more like core wasm where the text format allows for forward-references. This notably reduces complexity when handling rec groups, for example, which already needed special-casing since they support forward references.

The only affect on tests here is that some assert_malformed tests are, correctly, becoming assert_invalid. Otherwise test output isn't changing and everything that parsed yesterday should still parse today. No changes are made to validation of components here, this only affects text parsing.

@alexcrichton
alexcrichton requested a review from a team as a code owner August 24, 2026 22:00
@alexcrichton
alexcrichton requested review from fitzgen and removed request for a team August 24, 2026 22:00
This commit is an update to how name resolution happens for components
during the text-to-binary transform. The main purpose of this commit is
to enable running the upstream component model test suite in this
repository as a submodule, and minor issues in the text format are
preventing that. No valid component should change as a result of this
reimplementation, but invalid components will now have their errors
reported at a slightly different time.

Name references in the text format are generally expected to be possible
to do in a forwards and backwards way in the text format. For example in
core wasm you can do `call $foo` before actually defining function
`$foo`. This works because the resolver will first register all names
defined and then actually resolve references to those names. Components
have historically not worked this way, however, because unlike core
modules components cannot refer to names defined later in a
component. This influenced the resolver algorithm in the text-to-binary
transform in the `wast` crate where it currently resolves a field and
then registers the name of the field. This was done originally because
the process of resolving a field may inject aliases into a component due
to shorthands possible in the text format. This injection requires name
resolution information and cannot happen in an earlier expansion phase
for outer aliases as well. This meant that if a `$foo` identifier was
referring to an item defined later this turned out to be a
text-parse-time error.

With a text-parse-time error this clashes with `wasmprinter`'s default
printing of components unfortunately. For `wasmprinter` it loads the
name section and then prints identifiers for all kinds of references
since all names are known up front. This means that for an invalid
component `wasmprinter` will produce unparsable text by `wast`. This
friction makes working with invalid components difficult and notably
also makes it so `assert_invalid` cannot be used for some components
that are indeed binary-parsable-but-invalid.

That all leads to the change in this commit. The main refactor here is
to how a group of fields are processed. This affects components, nested
components, instance types, module types, etc. The goal is to behave
more like core wasm in terms of name resolution even if the output
component would be invalid (it's not `wast`'s job to validate). Notably
the previous resolution is now replaced with a few more phases.
Specifically:

* First all names in a group of fields are collected. Indices are
  assigned here but are known to be invalid because aliases haven't yet
  been injected.
* All fields are resolved to expand their implicit aliases. This will
  inject export/outer aliases as necessary into the list of items.
* This temporary resolution state is now discarded now that aliases are
  fully expanded.
* Next resolution proceeds like core wasm again, but this time indices
  are "for real". All fields are registered with an index and then
  resolved internally.

This behavior change means that components now behave more like core
wasm where the text format allows for forward-references. This notably
reduces complexity when handling `rec` groups, for example, which
already needed special-casing since they support forward references.

The only affect on tests here is that some `assert_malformed` tests are,
correctly, becoming `assert_invalid`. Otherwise test output isn't
changing and everything that parsed yesterday should still parse today.
No changes are made to validation of components here, this only affects
text parsing.

@fitzgen fitzgen left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

LGTM

@fitzgen
fitzgen added this pull request to the merge queue Aug 25, 2026
Merged via the queue into bytecodealliance:main with commit f1ae393 Aug 25, 2026
37 checks passed
@alexcrichton
alexcrichton deleted the refactor-component-text-parsing branch August 25, 2026 20:21
sauliusbc pushed a commit to SavantEnvs/wasm-tools that referenced this pull request Sep 1, 2026
…2631)

This commit fixes a regression from bytecodealliance#2621 where the resolution of `$foo`
names in the text format of components was refactored. Specifically
after that refactoring this component no longer produced a valid binary:

    (component $A
      (type $a string)

      (component $B
        (type (instance (export "a" (type (eq $a)))))
        (type $b (record (field "x" $a)))
      )
    )

The reason for this is that `$a` is used twice as implicitly-injected
outer aliases here, but the higher-depth one comes first. The previous
behavior would leave the `$a` name on the injected outer alias type,
which then caused the resolution of `(eq $a)` to resolve to the `$a`
within the definition of `$B` (despite it being later and invalid to
reference), instead of the definition within `$A` which was the target.

The fix here is to leave off the name in the injected outer alias. This
preserves the behavior pre-bytecodealliance#2621 which was accidentally removed, and
causes the above component to generate a valid binary instead of an
invalid binary.

This has various bits and pieces of impact throughout tests which now
resolve slightly differently than before, or are missing a `$foo` name,
as expected.
social4hyq pushed a commit to social4hyq/homebrew-core that referenced this pull request Sep 20, 2026
wasm-tools 1.259.0

Created-by: HarmonybrewBot
Commit-by: HarmonybrewBot
Merged-by: HarmonybrewBot
Description: Created by `brew bump`

---

Created with `brew bump-formula-pr`.<details>
  <summary>release notes</summary>
  <pre>## What's Changed
* Refactor name resolution of components in the text format by @alexcrichton in bytecodealliance/wasm-tools#2621
* Import component spec tests as a submodule and run them. by @alexcrichton in bytecodealliance/wasm-tools#2623
* Fix name uniqueness with methods/interfaces by @alexcrichton in bytecodealliance/wasm-tools#2625
* Add compact import generation in wasm-smith by @chacailline in bytecodealliance/wasm-tools#2627
* Drop hyphens when testing equality of kebab names by @alexcrichton in bytecodealliance/wasm-tools#2626
* Enable wide-arithmetic by default in wasm-smith by @alexcrichton in bytecodealliance/wasm-tools#2628
* Validate the size of defined component types by @alexcrichton in bytecodealliance/wasm-tools#2624
* Update Wasmtime used in testing/fuzzing by @alexcrichton in bytecodealliance/wasm-tools#2629
* Fix text format outer aliases from recent refactor by @alexcrichton in bytecodealliance/wasm-tools#2631
* Remove `cancellable` immediate from component model intrinsics by @alexcrichton in bytecodealliance/wasm-tools#2630
* Start requiring "strict" indices in the text format by @alexcrichton in bytecodealliance/wasm-tools#2633
* Enable the `cm-implements` and `cm-map` features by default by @alexcrichton in bytecodealliance/wasm-tools#2634
* Clamp sizes of element segments in wasm-smith by @alexcrichton in bytecodealliance/wasm-tools#2632
* Fix wit-smith generating huge types by @alexcrichton in bytecodealliance/wasm-tools#2636
* Fix disallow_traps in wasm-smith with custom page sizes by @alexcrichton in bytecodealliance/wasm-tools#2635
* Resolve Cargo warnings on nightly by @alexcrichton in bytecodealliance/wasm-tools#2642
* Return an error instead of panicking on fixed-length-list size overflow by @zikk090 in bytecodealliance/wasm-tools#2643
* Try to improve performance of `deinline_import_export` by @alexcrichton in bytecodealliance/wasm-tools#2647
* Document a non-obvious method argument by @alexcrichton in bytecodealliance/wasm-tools#2645
* wasm-wave: Clean up syntax docs by @lann in bytecodealliance/wasm-tools#2644
* Update dependency major tracks by @alexcrichton in bytecodealliance/wasm-tools#2649
* Release wasm-tools 1.259.0 by @github-actions[bot] in bytecodealliance/wasm-tools#2650

## New Contributors
* @chacailline made their first contribution in bytecodealliance/wasm-tools#2627
* @zikk090 made their first contribution in bytecodealliance/wasm-tools#2643

**Full Changelog**: https://github.com/bytecodealliance/wasm-tools/compare/v1.258.0...v1.259.0</pre>
  <p>View the full release notes at <a href="https://github.com/bytecodealliance/wasm-tools/releases/tag/v1.259.0">https://github.com/bytecodealliance/wasm-tools/releases/tag/v1.259.0</a>.</p>
</details>
<hr>

See merge request: Harmonybrew/homebrew-core!20061
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.

2 participants