Title: Codegen: 'Unexpected bound reference Local b index 1' compiling Data.Array.foldRecM (index inflated by optimizer) · Issue #56 · purescript-lua/purescript-lua · GitHub
Open Graph Title: Codegen: 'Unexpected bound reference Local b index 1' compiling Data.Array.foldRecM (index inflated by optimizer) · Issue #56 · purescript-lua/purescript-lua
X Title: Codegen: 'Unexpected bound reference Local b index 1' compiling Data.Array.foldRecM (index inflated by optimizer) · Issue #56 · purescript-lua/purescript-lua
Description: Summary pslua aborts during Lua code generation with Unexpected bound reference: Ref Nothing (Local (Name "b")) 1 in module Data.Array when compiling Data.Array. The crash is raised by the Lua backend (Lua.UnexpectedRefBound, see lib/Lan...
Open Graph Description: Summary pslua aborts during Lua code generation with Unexpected bound reference: Ref Nothing (Local (Name "b")) 1 in module Data.Array when compiling Data.Array. The crash is raised by the Lua back...
X Description: Summary pslua aborts during Lua code generation with Unexpected bound reference: Ref Nothing (Local (Name "b")) 1 in module Data.Array when compiling Data.Array. The crash is raised by th...
Opengraph URL: https://github.com/purescript-lua/purescript-lua/issues/56
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Codegen: 'Unexpected bound reference Local b index 1' compiling Data.Array.foldRecM (index inflated by optimizer)","articleBody":"## Summary\n\n`pslua` aborts during Lua code generation with\n\n```\nUnexpected bound reference: Ref Nothing (Local (Name \"b\")) 1 in module Data.Array\n```\n\nwhen compiling `Data.Array`. The crash is raised by the Lua backend (`Lua.UnexpectedRefBound`, see `lib/Language/PureScript/Backend/Lua.hs:214`) and is reported to the user via `exe/Main.hs:105`. This blocks compiling the `purescript-lua-arrays` port entirely (`spago build` succeeds, but `pslua --entry Data.Array` fails).\n\nThis is a `pslua` code-generator bug, not a fork bug: the offending function is stock upstream `purescript-arrays` code, and the bad reference is produced inside `pslua`'s own IR optimizer, not present in the CoreFn that `purs` emits.\n\n## Triggering construct\n\nThe single declaration `Data.Array.foldRecM` is sufficient to trigger the crash. Verified by reducing the module's CoreFn to that one declaration: `pslua` still aborts with the identical error.\n\n```purescript\nfoldRecM :: forall m a b. MonadRec m => (b -> a -> m b) -> b -> Array a -> m b\nfoldRecM f b array = tailRecM2 go b 0\n where\n go res i\n | i >= length array = pure (Done res)\n | otherwise = do\n res' \u003c- f res (unsafePartial (unsafeIndex array i))\n pure (Loop { a: res', b: i + 1 })\n```\n\nThe shape that matters:\n\n1. A top-level `Abs` binder named `b` (the second parameter of `foldRecM`).\n2. A typeclass-dictionary `Let` group that `purs` floats around the lambdas (the `MonadRec`/`Monad`/`Bind`/`Applicative` accessors — `tailRecM2`, `bind1`, `pure1`, `Monad0`), which `pslua`'s `inlineLocalBindings` later inlines because each is used once.\n3. A single reference to that `b` binder, in the body `tailRecM2 go b 0`.\n\nThere is exactly one binder named `b` and exactly one reference to it in the whole declaration. The record literal `{ a: res', b: i + 1 }` uses `b` only as a record *label*, not as a binder, so there is no source-level shadowing of `b` at all. Therefore the only correct De Bruijn index for that reference is `0`.\n\n## Root cause\n\nBy the time the Lua backend runs, the reference to `b` carries index `1` instead of `0`. The Lua backend (`fromIR`, `Ref` case) only accepts local references at index `0` — see `Note [Locals are uniquely named after renameShadowedNames]` in `lib/Language/PureScript/Backend/IR/Optimizer.hs`. The renaming pass `renameShadowedNames` only rewrites a local reference when its scope map holds a rename entry at the matching index (`lib/Language/PureScript/Backend/IR/Optimizer.hs:136-142`); for `b` there is only one binder, so the scope has no entry at index `1`, the reference is left untouched, and the backend then rejects it.\n\nThe index `1` is introduced during IR optimization, not by `purs`. The likely mechanism is the inlining of the single-use dictionary `Let` bindings (`inlineLocalBindings` -> `inlineLocalBinding` -> `substitute`, `lib/Language/PureScript/Backend/IR/Optimizer.hs:381-396`). `substitute` descends through the enclosing `Abs b` and, on the way, applies `shift 1 b 0` to the replacement / threads the index through binders (`lib/Language/PureScript/Backend/IR/Types.hs:668-704`). `shift` increments the index of every `Local \"b\"` reference at or above `minIndex` (`lib/Language/PureScript/Backend/IR/Types.hs:752-757`). When this bookkeeping runs through the dictionary-`Let` + nested-`Abs` structure that `purs` produces for `foldRecM`, the one legitimate `b` reference (which should stay at index `0`, bound by the enclosing `Abs b`) gets bumped to index `1`, so it ends up pointing past its only binder. This is the same class of bug as #37 (a sibling/over-eager index leaving a local reference unbound after optimization), reached here through the dictionary-binding inlining path rather than DCE.\n\nI have intentionally not pinned the exact faulty line; the symptom (index inflated from 0 to 1 with only one binder in scope) is reproduced and isolated, and the fix should restore the invariant that `renameShadowedNames` relies on.\n\n## Impact\n\n- **Severity: high for the array port.** `Data.Array` cannot be compiled to Lua at all, which blocks `purescript-lua-arrays` and any downstream package depending on `Data.Array`.\n- `foldRecM` is ordinary upstream code; any module that combines a constrained polymorphic function (typeclass dictionary args, which `purs` lowers to floated `Let` bindings) with a `where`/`do` body that references one of the outer parameters can hit the same index-inflation path. So this is not specific to `Data.Array`.\n\n## Reproduction\n\nToolchain: `purs` 0.15.15, `pslua` at `Unisay/purescript-lua@6fdc70c` (current `master`).\n\nIn a checkout of `purescript-lua-arrays` (the `master` branch) after `spago build -u '-g corefn'`:\n\n```bash\npslua --foreign-path . --ps-output output --entry Data.Array --lua-output-file dist/Data.Array.lua\n# => Unexpected bound reference: Ref Nothing (Local (Name \"b\")) 1 in module Data.Array\n```\n\nMinimal isolation (reduce `output/Data.Array/corefn.json` to only the `foldRecM` declaration, keep everything else identical) still reproduces the exact same error, confirming `foldRecM` is the sole trigger.\n\n## How it was found\n\nTriaging a failing `nix develop -c ./scripts/build` in the `purescript-lua-arrays` fork. Bisected at the CoreFn declaration level: of the three top-level declarations in `Data.Array` that reference a local named `b` (`foldRecM`, `foldM`, `filterA`), removing only `foldRecM` from the CoreFn makes the error disappear; keeping `foldRecM` as the lone declaration reproduces it.\n","author":{"url":"https://github.com/Unisay","@type":"Person","name":"Unisay"},"datePublished":"2026-06-14T08:36:38Z","url":"https://github.com/purescript-lua/purescript-lua/issues/56"}
| 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:33bc476a-fc0a-1afc-9eff-0b7afcd4f556 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | B072:21AC3F:7F5DD4:A501D8:6B00D60C |
| html-safe-nonce | 5b97f4f0d980c1cb16ce17468cf89fcab15573050c021217b0dc3c4b8ddcdab1 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJCMDcyOjIxQUMzRjo3RjVERDQ6QTUwMUQ4OjZCMDBENjBDIiwidmlzaXRvcl9pZCI6IjYzNjMzNTgxOTEwMzE0NzM2NzYiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | f7bbe83b1fd6c8075d753b9aa3fda6fa21b31f188068b28997d564d4642c5d83 |
| hovercard-subject-tag | issue:4658461087 |
| 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/56/issue_layout |
| twitter:image | https://opengraph.githubassets.com/675e0edf03ce5cef5b30298614b08180a2f511e9d8728871f4473e8acc3eef70/purescript-lua/purescript-lua/issues/56 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/675e0edf03ce5cef5b30298614b08180a2f511e9d8728871f4473e8acc3eef70/purescript-lua/purescript-lua/issues/56 |
| og:image:alt | Summary pslua aborts during Lua code generation with Unexpected bound reference: Ref Nothing (Local (Name "b")) 1 in module Data.Array when compiling Data.Array. The crash is raised by the Lua back... |
| 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 | a481f62a1e04c9b9bdcb5628230682c66d59006f5b13b080f79cba84a176905b |
| 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 | 84c8c5cc25e2fb7f2308428458538436baecc6d2 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width