Title: IR DCE mishandles Let bindings: no unshift after dropping a dead shadowing binder, body scope resolved in reverse · Issue #134 · purescript-lua/purescript-lua · GitHub
Open Graph Title: IR DCE mishandles Let bindings: no unshift after dropping a dead shadowing binder, body scope resolved in reverse · Issue #134 · purescript-lua/purescript-lua
X Title: IR DCE mishandles Let bindings: no unshift after dropping a dead shadowing binder, body scope resolved in reverse · Issue #134 · purescript-lua/purescript-lua
Description: Two defects in the Let handling of Language.PureScript.Backend.IR.DCE, the same bug class as #37 and #56 (De Bruijn index bookkeeping under Note [Sequential scoping of Let bindings]). Bug 1: dropping a dead Let binder leaves references p...
Open Graph Description: Two defects in the Let handling of Language.PureScript.Backend.IR.DCE, the same bug class as #37 and #56 (De Bruijn index bookkeeping under Note [Sequential scoping of Let bindings]). Bug 1: droppi...
X Description: Two defects in the Let handling of Language.PureScript.Backend.IR.DCE, the same bug class as #37 and #56 (De Bruijn index bookkeeping under Note [Sequential scoping of Let bindings]). Bug 1: droppi...
Opengraph URL: https://github.com/purescript-lua/purescript-lua/issues/134
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"IR DCE mishandles Let bindings: no unshift after dropping a dead shadowing binder, body scope resolved in reverse","articleBody":"Two defects in the `Let` handling of `Language.PureScript.Backend.IR.DCE`, the same bug class as #37 and #56 (De Bruijn index bookkeeping under Note [Sequential scoping of Let bindings]).\n\n## Bug 1: dropping a dead `Let` binder leaves references pointing one binder too far\n\nWhen `dceAnnotatedExp` blanks an unused `Abs` parameter it lowers the body references with `unshift` (that was the #56 fix). The `Let` case has the same obligation and skips it: a dead binding is removed from the group, but references with index >= 1 that skipped over it are left as they were.\n\n### Repro\n\n```haskell\n-- let x = 1 in (let x = \u003cdead> in x@1) -- x@1 refers to the outer x\neliminateDeadCode $ UberModule [] [] $ pure . (Name \"main\",) $\n lets (Standalone (noAnn, x, literalInt 1) :| [])\n (lets (Standalone (noAnn, x, exception \"dead\") :| [])\n (refLocal x 1))\n```\n\nActual: `let x = 1 in x@1`. The inner binding is gone but the reference still has index 1, and only one `x` binder remains. Expected: `let x = 1 in x@0`.\n\nIn the full pipeline the dangling reference survives `renameShadowedNames` (index 1 with a single binder in scope means no rename) and the Lua codegen rejects it with `UnexpectedRefBound`, so the compiler crashes on valid input. When more same-name binders remain above the removed one, the reference instead resolves to the wrong binder and the miscompile is silent. Reachable the same way #56 was: inlining shifts a reference past a shadowing binder (creating `x@1`), and a later pass kills that binder's uses.\n\nFix: when a binder is dropped from a `Let`, apply `unshift name 0` to the body and to the RHSs of the groupings that follow it in the same `Let`, mirroring the `Abs` case.\n\n## Bug 2: the scope for the `Let` body is built in reverse order\n\n`adjacencyListForExpr` builds the scope the body's references are resolved against with a right fold:\n\n```haskell\nscope' = foldr addToScope scope (listGrouping =\u003c\u003c toList groupings)\n```\n\n`addLocalToScope` puts the most recently added binder at index 0, so with `foldr` the *first* binding of a name lands at index 0. The convention (Note [Sequential scoping of Let bindings]) is the opposite: the body's index 0 picks the *last* binding, like `let*`. Every other walker (`countFreeRefs`, `substitute`, `qualifyTopRefs`, `renameShadowedNamesInExpr`, and the RHS-side `adjacencyListForGrouping` right next to this code) implements the convention correctly; only the body scope of the DCE pass disagrees.\n\n### Repro\n\n```haskell\n-- let x = 1; x = 2 in x@0 -- by convention x@0 is the second binding\neliminateDeadCode $ UberModule [] [] $ pure . (Name \"main\",) $\n lets (Standalone (noAnn, x, literalInt 1) :| [Standalone (noAnn, x, literalInt 2)])\n (refLocal x 0)\n```\n\nActual: `let x = 1 in x@0`, i.e. reachability marked the first binding live and eliminated the second, so the expression now evaluates to 1 instead of 2. Expected: `let x = 2 in x@0`.\n\nSame-name siblings in one `Let` cannot currently be produced from PureScript source (CoreFn conversion uses source names, which are unique per let block, or fresh generated names), so this is a latent landmine rather than a live miscompile, but it directly contradicts the documented convention that issue #37 established as load-bearing.\n\nFix: the correctly threaded scope already exists one line below; the body should use `fst (foldl' adjacencyListForGrouping (scope, mempty) groupings)` instead of the separate `foldr`.\n\n## Tests\n\nRed-first regression tests for both belong in `test/Language/PureScript/Backend/IR/DCE/Spec.hs` next to the existing `dceExpression` helper; the repros above drop in as-is. Bug 1 additionally deserves an end-to-end case once a source-level reproduction is pinned down (the #56 golden shows the shape to imitate).\n\nFound during a backend audit on 2026-07-02; bug 1 was first noted during the #37 work and now has a confirmed reproduction.\n","author":{"url":"https://github.com/Unisay","@type":"Person","name":"Unisay"},"datePublished":"2026-07-02T08:22:02Z","url":"https://github.com/purescript-lua/purescript-lua/issues/134"}
| 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:cd570144-1867-02eb-5cab-e8983b3db863 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 9EA6:19E4BB:945625:BF0C54:6B00EF33 |
| html-safe-nonce | 72846d4ba208d11dd988736da2b537148f9c6ecfa733a29f3df7b2ceaae61cf9 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI5RUE2OjE5RTRCQjo5NDU2MjU6QkYwQzU0OjZCMDBFRjMzIiwidmlzaXRvcl9pZCI6IjU1NjE2Nzk5MTA3NTU0MzAxOTUiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | 419e1e246d5954a3186091b7a0eafc1fef905004a02ccf5da903f935c07f4c91 |
| hovercard-subject-tag | issue:4792612220 |
| 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/134/issue_layout |
| twitter:image | https://opengraph.githubassets.com/798e9cc8186d204a41fa82e0db20a90c09d61d04c0bf3d1407712217a523ba22/purescript-lua/purescript-lua/issues/134 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/798e9cc8186d204a41fa82e0db20a90c09d61d04c0bf3d1407712217a523ba22/purescript-lua/purescript-lua/issues/134 |
| og:image:alt | Two defects in the Let handling of Language.PureScript.Backend.IR.DCE, the same bug class as #37 and #56 (De Bruijn index bookkeeping under Note [Sequential scoping of Let bindings]). Bug 1: droppi... |
| 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 | 4ef498cbabcbe3c50befc7a5b743e7072ee770b4daf0e15e54801180d1fa91fc |
| 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 | 9983dd5da06943f833c971dd5b6bc988e2858be5 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width