Skip to content

fix(compiler): a parameter shadows an outer const in SSR and class-field output - #5127

Merged
ryansolid merged 1 commit into
solidjs:mainfrom
Marve10s:fix-compiler-ssr-shadowed-params
Nov 20, 2026
Merged

ryansolid merged 1 commit into
solidjs:mainfrom
Marve10s:fix-compiler-ssr-shadowed-params

Conversation

@Marve10s

Copy link
Copy Markdown
Contributor

Summary

The native compiler's SSR output inlines an outer const into the server HTML even where a parameter of the same name shadows it:

const name = "outer";

const List = () => (
  <ul>
    {["a", "b"].map(name => <li>{name}</li>)}
  </ul>
);

With @solidjs/compiler 2.0.2 this renders <ul><li>outer</li><li>outer</li></ul> on the server, where babel-plugin renders a and b. The same happens for function parameters, destructured, default and rest parameters, attribute values (title={label}), and methods. The client compiles the parameter correctly, so server HTML and client render disagree, and hydration keeps the wrong server text.

Constant folding asks the binding table whether a name refers to a known constant, and the table resolves a name to its innermost declaration. The DOM and universal transforms declare each function's parameters when they enter it (declare_function_params), but the SSR transform has its own function and arrow visitors and kept the trait's no-op default. So inside an SSR function body a parameter name resolved to the outer const, or to the module-constant fallback from #4799, and was folded. The SSR transform now declares parameters in both visitors, in the same order the shared DOM/universal visitors use.

An expression-bodied arrow in a class field (class A { row = label => <li>{label}</li> }) is lowered by lower_class_field_value, which skips the visitors. That path had the same gap in SSR and in DOM output, and now opens a function scope and declares the arrow's parameters before lowering its body. The universal transform already visited these.

The JsxTransform::declare_function_params doc said "(dom only)", which was already out of date (universal overrides it too), so I dropped those words.

Not changed here, and the same on main in all three modes: catch (label) and for (const label of …) heads still fold an outer const, because the binding table never declares those names. That is a separate fix. While testing I also saw DOM class-field output that differs from babel without any shadowing: the no-capture path flattens babel's setup IIFE (same behavior), a non-arrow el = <li>{label}</li> field is not folded (same behavior, less optimized), and a fragment-bodied class-field arrow that reads this references _g2$ without declaring it (row = y => <>{this.n}<li title={y} /></> throws a ReferenceError). I left those alone.

Public API changes

None.

How did you test this change?

  • Added six cases to packages/compiler/__tests__/parity-probes.test.js: a function parameter, a .map callback parameter, a destructured parameter, a parameter in an attribute, a class-field arrow parameter, and a class-field arrow with a conditional body. With main's binary 26 checks fail (the first four in ssr, ssr-hydratable and tsrx-ssr; the class-field case in every DOM and SSR mode; the conditional case in the SSR modes). With the fix all 4200 pass.
  • cargo test, cargo clippy --all-targets -- -D warnings and rustfmt on the changed files pass. The compiler vitest suite (7027 passed, 2 skipped), babel-plugin (432) and pnpm test from the repo root (39 of 39 turbo tasks) pass.

Verification

A script that compiles each case with babel-plugin and the native compiler and renders it with renderToString. Published @solidjs/compiler 2.0.2:

param        DIFF  babel=<div>3arg</div>  native=<div>3items</div>
arrowParam   DIFF  babel=<div>3arg</div>  native=<div>3items</div>
attr         DIFF  babel=<div title="arg">3</div>  native=<div title="items">3</div>
mapCallback  DIFF  babel=<ul><li>a</li><li>b</li></ul>  native=<ul><li>outer</li><li>outer</li></ul>
destructured DIFF  babel=<div>arg</div>  native=<div>items</div>
classField   DIFF  babel=<li title="arg">7arg</li>  native=<li title="items">7items</li>

This branch:

param        same  babel=<div>3arg</div>  native=<div>3arg</div>
arrowParam   same  babel=<div>3arg</div>  native=<div>3arg</div>
attr         same  babel=<div title="arg">3</div>  native=<div title="arg">3</div>
mapCallback  same  babel=<ul><li>a</li><li>b</li></ul>  native=<ul><li>a</li><li>b</li></ul>
destructured same  babel=<div>arg</div>  native=<div>arg</div>
classField   same  babel=<li title="arg">7arg</li>  native=<li title="arg">7arg</li>

Code that does not shadow anything compiles to byte-identical output before and after.

@changeset-bot

changeset-bot Bot commented Nov 20, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: a98dce9

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@solidjs/compiler Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@codspeed

codspeed Bot commented Nov 20, 2026

Copy link
Copy Markdown

Merging this PR will degrade performance by 12.58%

3 warnings

⚠️ Upgrade your CodSpeed plugin for Node.js 24

@codspeed/* 5.4.0 is only tested on Node.js 18, 20, and 22, so your Node.js 24 measurements may be inaccurate. Upgrade your @codspeed/* packages to 6.0.0 or later.

See affected benchmarks

⚠️ 2 benchmarks are dominated by system calls

System calls cannot be simulated consistently and are left out of the measure. The reported value understates the real cost. Switch to the Walltime instrument to measure them.

Learn more about system calls

See affected benchmarks

⚠️ 1 benchmark with a significant change ran on different hardware

Its base and head results were measured on machines with a different CPU, core count or memory, so the change might not come from the code. Run base and head on the same runner type.

Learn more about Macro Runners

See affected benchmarks

❌ 1 regressed benchmark
✅ 200 untouched benchmarks
⏩ 5 skipped benchmarks1

Warning

Please fix the performance issues or acknowledge them on CodSpeed.

Performance Changes

Benchmark BASE HEAD Efficiency
❌ arrow in template 10.9 ms 12.5 ms -12.58%

Tip

Investigate this regression by commenting @codspeedbot fix this regression on this PR, or directly use the CodSpeed MCP with your agent.


Comparing Marve10s:fix-compiler-ssr-shadowed-params (a98dce9) with main (99db9bc)

Open in CodSpeed

Footnotes

  1. 5 benchmarks were skipped, so the baseline results were used instead. If they were deleted from the codebase, click here and archive them to remove them from the performance reports. ↩

@ryansolid
ryansolid merged commit 7044ab4 into solidjs:main Nov 20, 2026
20 of 21 checks passed
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