fix(codegen): #836 — emit producer aliases for $-prefixed re-exports and namespace wrappers#883
Merged
Merged
Conversation
…orts and namespace-import wrappers
Two link-time failures in the zod compilePackages repro:
1. Sanitize mismatch — `export const $ZodCheck = …` lands at
`perry_fn_<src>___ZodCheck` (producer-side `sanitize()` rewrites `$`
to `_`), but consumer-side `import_origin_suffix()` returns the
exported name verbatim, so consumer references
`perry_fn_<src>__$ZodCheck` and link-fails. Same mismatch hits the
`__perry_wrap_perry_fn_<src>__$ZodCheck` closure-wrapper symbol.
2. `local == exported` non-function exports — `import * as z from
"…"; export { z };` produces `Export::Named { local: "z", exported:
"z" }`, which every wrapper-emission loop skipped (regular
`__perry_wrap_<fn>` loop keys on `hir.functions`; the v0.5.916 #837
rename loop filters on `local != exported`). Consumers that read
`z` as a value link-failed on `__perry_wrap_perry_fn_<index_ts>__z`.
Fix: new emission block in `crates/perry-codegen/src/codegen.rs`
right after the #837 renamed-wrapper loop. For every `Export::Named
{ local, exported }` it emits two kinds of aliases as needed:
- Raw-name aliases when `sanitize(exported) != exported`:
`perry_fn_<src>__<raw_exported>` forwards to the sanitized
symbol; `__perry_wrap_perry_fn_<src>__<raw_exported>` forwards to
the sanitized wrapper or no-ops to undefined.
- No-op `__perry_wrap_perry_fn_<src>__<exported>` for `local ==
exported` exports whose local is not a HIR function.
LLVM IR allows `$` unquoted in identifiers per LangRef, so raw-name
aliases serialize without quoting changes. Both alias paths are
idempotent (HashSet + `llmod.has_function`).
Effect on the zod repro: 82 → 1 undefined symbols. The remaining one
(`_perry_fn_..._en_ts__default`) comes from anonymous `export default
function () {…}` getting dropped by the HIR lowerer — separate bug,
out of scope here.
Validation: new regression test
`test-files/test_issue_836_zod_class_reexports.ts` with fixture
`test-files/fixtures/issue_836_pkg/` mirrors the failing zod shape
self-contained. Output is byte-for-byte vs.
`node --experimental-strip-types`. Workspace tests pass.
Refs #836, #793, #805, #837.
f0d6161 to
407619e
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
perry.compilePackages: ["zod"]; import { z } from "zod";).sanitize()rewrites$→_, soexport const $ZodChecklands atperry_fn_<src>___ZodCheckwhile consumer-sideimport_origin_suffix()returns names verbatim →perry_fn_<src>__$ZodCheckreference link-fails. Fix emits raw-name aliases whensanitize(exported) != exported.import * as z from "…"; export { z };producesExport::Named { local: "z", exported: "z" }which every wrapper-emission loop skipped. Fix emits a no-op__perry_wrap_perry_fn_<src>__<exported>forlocal == exportedexports whose local is not a HIR function.Remaining undefined symbol on zod (
_perry_fn_..._en_ts__default, fromexport default function () {…}) is a separate HIR-lowerer bug, out of scope here.Test plan
cargo build --release -p perry-runtime -p perry-stdlib -p perrycleancargo test --release --workspace --exclude perry-ui-* --exclude perry-jsruntime— all pass, no regressionstest-files/test_issue_836_zod_class_reexports.ts+test-files/fixtures/issue_836_pkg/compiles and runs byte-for-byte vs.node --experimental-strip-typestest_issue_678_reexport_default.ts,test_issue_838_prototype_methods.ts) still pass