Title: Mixed ST/Effect discard chains miscompile: canonicalization resurrects a DCE'd foreign accessor (runtime nil call) · Issue #297 · purescript-lua/purescript-lua · GitHub
Open Graph Title: Mixed ST/Effect discard chains miscompile: canonicalization resurrects a DCE'd foreign accessor (runtime nil call) · Issue #297 · purescript-lua/purescript-lua
X Title: Mixed ST/Effect discard chains miscompile: canonicalization resurrects a DCE'd foreign accessor (runtime nil call) · Issue #297 · purescript-lua/purescript-lua
Description: Problem A module that instantiates discard with two different Bind dictionaries — Effect or ST plus any second monad (the repro uses ST + Effect; Effect + Maybe triggers the same shape) — miscompiles: the generated Lua reads the Effect/S...
Open Graph Description: Problem A module that instantiates discard with two different Bind dictionaries — Effect or ST plus any second monad (the repro uses ST + Effect; Effect + Maybe triggers the same shape) — miscompil...
X Description: Problem A module that instantiates discard with two different Bind dictionaries — Effect or ST plus any second monad (the repro uses ST + Effect; Effect + Maybe triggers the same shape) — miscompil...
Opengraph URL: https://github.com/purescript-lua/purescript-lua/issues/297
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Mixed ST/Effect discard chains miscompile: canonicalization resurrects a DCE'd foreign accessor (runtime nil call)","articleBody":"## Problem\n\nA module that instantiates `discard` with two different `Bind` dictionaries — Effect or ST plus any second monad (the repro uses ST + Effect; Effect + Maybe triggers the same shape) — miscompiles: the generated Lua reads the Effect/ST bind methods off module-table fields that are never assigned, and crashes on first use with `attempt to call field 'Control_Monad_ST_Internal_bind_' (a nil value)`.\n\nReproduction (crashes at runtime):\n\n```purescript\nmodule Golden.MixedDiscardFloat.Test where\n\nimport Prelude\n\nimport Control.Monad.ST as ST\nimport Control.Monad.ST.Ref as STRef\nimport Effect (Effect)\nimport Effect.Console (log, logShow)\n\nstCount :: Int\nstCount = ST.run do\n r \u003c- STRef.new 1\n void (STRef.write 2 r)\n STRef.read r\n\nmain :: Effect Unit\nmain = do\n log \"st:\"\n logShow stCount\n```\n\nGenerated Lua (abridged) — `M` is declared empty and these fields are never assigned:\n\n```lua\nlocal M = {}\n...\n return M.Control_Monad_ST_Internal_bind_(...)(...)() -- nil call\n...\nreturn M.Effect_bindE(Effect_Console_log(\"st:\"))(...)() -- nil call\n```\n\n## Mechanism\n\nWhen one module uses `discard` with two different `Bind` dictionaries, the PureScript compiler's own CSE (its CoreFn common-subexpression pass over compiler-synthesized dictionary applications) floats the dictionary application in two stages — the shared partial application gets its own top-level binding (verbatim in the repro's `corefn.json`):\n\n```\ndiscard = Control.Bind.discard discardUnit\ndiscard1 = discard bindST\ndiscard2 = discard bindEffect\n```\n\nNeither canonicalization tier of #182 matches the two-stage shape. Tier 1 (CoreFn translation) sees `discard1 = App (Ref Test.discard) (Ref bindST)`, whose head is the module-local float, not `Control.Bind.discard`. Tier 2 (`canonicalizeEffectHead`) keeps seeing the same two-node shape at every pre-magic-do pass boundary, because the float alias is only dissolved by call-site inlining, which is enabled post-magic-do (#180). Two consequences, in increasing severity:\n\n1. **Magic-do starvation.** `isCanonicalHead` resolves one top-level hop; this chain head needs two (`discard1` → `Test.discard` → `Control.Bind.discard discardUnit`). The Effect/ST chains stay nested, so the missed flattening re-opens the #46 nesting-limit exposure for long chains. Traced at the magic-do boundary: `discard1 = AppN (Ref (Imported Test \"discard\")) (Ref (Imported \"Control.Monad.ST.Internal\" \"bindST\"))`.\n\n2. **The crash.** In the pre-magic-do optimize+dce fixpoints nothing references the canonical foreign accessors (`Control.Monad.ST.Internal.bind_`, `Effect.bindE`) — every route goes through the dictionaries — so their accessor bindings are dissolved into use sites and the standalone bindings DCE'd. Post-magic-do, call-site inlining dissolves `Test.discard` into `discard1`/`discard2`, the dictionary application finally becomes canonical, and `canonicalizeEffectHead` rewrites it to `Ref (Imported Control.Monad.ST.Internal bind_)` — manufacturing a reference to a binding that no longer exists. No pass or lint validates `Imported` references, and codegen renders the dangling one as a read of a never-assigned module-table field.\n\nThe final IR shows the dangling end state: `bind_` occurs in the module's `ForeignImport` carried-name list (so the FFI table keeps the field), and as the manufactured `Ref` — but no `Control.Monad.ST.Internal.bind_` binding exists.\n\n## Approach\n\nTwo independent fixes:\n\n- **Normalize the spine through top-level aliases instead of counting hops.** The canonical rows apply only to Effect/ST, whose dictionaries are nullary constants, so the matched spines are structurally bounded (`bind`/`pure`: two nodes, `discard`: three) — but purs CSE may cut any node of that bounded spine into a top-level float. Tier 2 should resolve each head position through the top-level binding map (visited-bounded) before matching the table; that is closed under any CSE split, not just the observed two-stage one. It also fixes the starvation: tier 2 runs in the pre-magic-do fixpoints, so magic-do sees canonical heads again.\n- **No rewrite may manufacture a `Ref` to a binding that does not exist.** The dangling-import class outlives any particular recognizer: add a pass-boundary lint (checked runner) that every `Imported` reference outside `Prim` resolves to a live top-level binding, so the golden suite pins any future resurrect-after-DCE to the offending pass instead of miscompiling silently.\n\n## Found\n\nWhile implementing #233: the ST golden module mixes ST loop discards with an Effect `main` and crashed at baseline, before any lowering landed.\n\n","author":{"url":"https://github.com/Unisay","@type":"Person","name":"Unisay"},"datePublished":"2026-07-24T15:09:25Z","url":"https://github.com/purescript-lua/purescript-lua/issues/297"}
| react-profiling | 0 |
| react-import-map | react,react-dom,react-dom/client,react-dom/profiling,react-is,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@b8488ee2c322 |
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:a6cca999-c7b4-806a-84f6-87ec1ef33742 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 8DB0:2E7EBB:7A577F:9C1365:6B00E45E |
| html-safe-nonce | c45f0b6d7de26ffeaf58a5fd196206525a6db951140ed00aac7270be9e5ad0a2 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI4REIwOjJFN0VCQjo3QTU3N0Y6OUMxMzY1OjZCMDBFNDVFIiwidmlzaXRvcl9pZCI6IjI1MjMxMzU0Njk2MjE5MjkwNTUiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | 2608abc472ebb24b80eba2b328f7d33fbc39a96bb0a2570af0de818e5d44e811 |
| hovercard-subject-tag | issue:4969698491 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/purescript-lua/purescript-lua/297/issue_layout |
| twitter:image | https://opengraph.githubassets.com/0c7b2cf3c39f6876091cc6adeaf93f8c163921e12a7de9ec42f533746c522cac/purescript-lua/purescript-lua/issues/297 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/0c7b2cf3c39f6876091cc6adeaf93f8c163921e12a7de9ec42f533746c522cac/purescript-lua/purescript-lua/issues/297 |
| og:image:alt | Problem A module that instantiates discard with two different Bind dictionaries — Effect or ST plus any second monad (the repro uses ST + Effect; Effect + Maybe triggers the same shape) — miscompil... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | Unisay |
| hostname | github.com |
| expected-hostname | github.com |
| None | 27088b6977484d0d3321d9a11c907c6d6637b315cfa359ef7952dbf6292f9b14 |
| turbo-cache-control | no-preview |
| go-import | github.com/purescript-lua/purescript-lua git https://github.com/purescript-lua/purescript-lua.git |
| octolytics-dimension-user_id | 131581790 |
| octolytics-dimension-user_login | purescript-lua |
| octolytics-dimension-repository_id | 587200487 |
| octolytics-dimension-repository_nwo | purescript-lua/purescript-lua |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 587200487 |
| octolytics-dimension-repository_network_root_nwo | purescript-lua/purescript-lua |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 624179fb8b88d26ba5bba08894eb60a1c5a1d93d |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width