René's URL Explorer Experiment


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

direct link

Domain: github.com


Hey, it has json ld scripts:
{"@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-profiling0
react-import-mapreact,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-controllervoltron_issues_fragments
route-actionissue_layout
fetch-noncev2:33bc476a-fc0a-1afc-9eff-0b7afcd4f556
current-catalog-service-hash81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114
request-idB072:21AC3F:7F5DD4:A501D8:6B00D60C
html-safe-nonce5b97f4f0d980c1cb16ce17468cf89fcab15573050c021217b0dc3c4b8ddcdab1
visitor-payloadeyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJCMDcyOjIxQUMzRjo3RjVERDQ6QTUwMUQ4OjZCMDBENjBDIiwidmlzaXRvcl9pZCI6IjYzNjMzNTgxOTEwMzE0NzM2NzYiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ==
visitor-hmacf7bbe83b1fd6c8075d753b9aa3fda6fa21b31f188068b28997d564d4642c5d83
hovercard-subject-tagissue:4658461087
github-keyboard-shortcutsrepository,issues,copilot
google-site-verificationApib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I
octolytics-urlhttps://collector.github.com/github/collect
analytics-location///voltron/issues_fragments/issue_layout
fb:app_id1401488693436528
apple-itunes-appapp-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/purescript-lua/purescript-lua/56/issue_layout
twitter:imagehttps://opengraph.githubassets.com/675e0edf03ce5cef5b30298614b08180a2f511e9d8728871f4473e8acc3eef70/purescript-lua/purescript-lua/issues/56
twitter:cardsummary_large_image
og:imagehttps://opengraph.githubassets.com/675e0edf03ce5cef5b30298614b08180a2f511e9d8728871f4473e8acc3eef70/purescript-lua/purescript-lua/issues/56
og:image:altSummary 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:width1200
og:image:height600
og:site_nameGitHub
og:typeobject
og:author:usernameUnisay
hostnamegithub.com
expected-hostnamegithub.com
Nonea481f62a1e04c9b9bdcb5628230682c66d59006f5b13b080f79cba84a176905b
turbo-cache-controlno-preview
go-importgithub.com/purescript-lua/purescript-lua git https://github.com/purescript-lua/purescript-lua.git
octolytics-dimension-user_id131581790
octolytics-dimension-user_loginpurescript-lua
octolytics-dimension-repository_id587200487
octolytics-dimension-repository_nwopurescript-lua/purescript-lua
octolytics-dimension-repository_publictrue
octolytics-dimension-repository_is_forkfalse
octolytics-dimension-repository_network_root_id587200487
octolytics-dimension-repository_network_root_nwopurescript-lua/purescript-lua
turbo-body-classeslogged-out env-production page-responsive
disable-turbofalse
browser-stats-urlhttps://api.github.com/_private/browser/stats
browser-errors-urlhttps://api.github.com/_private/browser/errors
release84c8c5cc25e2fb7f2308428458538436baecc6d2
ui-targetfull
theme-color#1e2327
color-schemelight dark
react-app-nameissues

Links:

Skip to contenthttps://github.com/purescript-lua/purescript-lua/issues/56#start-of-content
https://github.com/
Sign inhttps://github.com/login?return_to=https%3A%2F%2Fgithub.com%2Fpurescript-lua%2Fpurescript-lua%2Fissues%2F56
GitHubRepository hosting, code review, and collaborationhttps://github.com/platform
CopilotAI coding agent for tasks, projects, and automated workhttps://github.com/features/copilot
ActionsCI/CD and automationhttps://github.com/features/actions
Code QualityQuality analysis, fixes, and enforcementhttps://github.com/features/code-quality
CodespacesCloud development environmentshttps://github.com/features/codespaces
Code SecurityVulnerability detection and remediationhttps://github.com/security/advanced-security/code-security
Secret ProtectionSecret scanning and leak preventionhttps://github.com/security/advanced-security/secret-protection
All Featureshttps://github.com/features
Documentationhttps://docs.github.com/en
Traininghttps://learn.github.com/skills
Certificationshttps://learn.github.com/credentials
Build with Copilothttps://github.com/features/ai
Customize Copilothttps://docs.github.com/en/copilot/how-tos/copilot-on-github/customize-copilot/customize-copilot-overview
Integrationshttps://github.com/marketplace
Trending reposhttps://github.com/trending
GitHub Sponsorshttps://github.com/open-source/sponsors
Developer articleshttps://github.com/resources/articles
ChangelogYour source for the latest features, improvements, and fixes across GitHub products, all in one place.https://github.blog/changelog/
Enterpriseshttps://github.com/enterprise
Teamhttps://github.com/team
Startupshttps://github.com/enterprise/startups
Nonprofitshttps://github.com/solutions/industry/nonprofits
Healthcarehttps://github.com/solutions/industry/healthcare
Financial serviceshttps://github.com/solutions/industry/financial-services
Manufacturinghttps://github.com/solutions/industry/manufacturing
Governmenthttps://github.com/solutions/industry/government
See all industrieshttps://github.com/solutions/industry
Why GitHubhttps://github.com/why-github
Customer Storieshttps://github.com/customer-stories
Business insightshttps://github.com/solutions/executive-insights
Trust Centerhttps://github.com/trust-center
GitHub Advanced SecurityEnterprise-grade code securityhttps://github.com/security/advanced-security
Copilot for businessEnterprise-grade AI featureshttps://github.com/features/copilot/copilot-business
Premium SupportEnterprise-grade 24/7 supporthttps://github.com/enterprise/premium-support
Documentationhttps://docs.github.com/en
Resource centerhttps://github.com/resources
Bloghttps://github.blog/
Events & webinarshttps://github.com/resources/events
Community Forumhttps://github.com/orgs/community/discussions
Customer Storieshttps://github.com/customer-stories
Integrationshttps://github.com/marketplace
Customer supporthttps://help.github.com/
Traininghttps://learn.github.com/skills
GitHub Statushttps://www.githubstatus.com/
Abouthttps://github.com/about
Partnershttps://github.com/partners
Changeloghttps://github.blog/changelog/
GitHub UniverseThe event for developers, by developers — talks, workshops, and what’s next for GitHub.https://githubuniverse.com/
Pricinghttps://github.com/pricing
Sign inhttps://github.com/login?return_to=https%3A%2F%2Fgithub.com%2Fpurescript-lua%2Fpurescript-lua%2Fissues%2F56
Sign uphttps://github.com/signup?ref_cta=Sign+up&ref_loc=header+logged+out&ref_page=%2F%3Cuser-name%3E%2F%3Crepo-name%3E%2Fvoltron%2Fissues_fragments%2Fissue_layout&source=header-repo&source_repo=purescript-lua%2Fpurescript-lua
Reloadhttps://github.com/purescript-lua/purescript-lua/issues/56
Reloadhttps://github.com/purescript-lua/purescript-lua/issues/56
Reloadhttps://github.com/purescript-lua/purescript-lua/issues/56
purescript-luahttps://github.com/purescript-lua
purescript-luahttps://github.com/purescript-lua/purescript-lua
Codehttps://github.com/purescript-lua/purescript-lua
Issues48 (48)https://github.com/purescript-lua/purescript-lua/issues
Pull requestshttps://github.com/purescript-lua/purescript-lua/pulls
Discussionshttps://github.com/purescript-lua/purescript-lua/discussions
Actionshttps://github.com/purescript-lua/purescript-lua/actions
Projectshttps://github.com/purescript-lua/purescript-lua/projects
Security and qualityhttps://github.com/purescript-lua/purescript-lua/security
Insightshttps://github.com/purescript-lua/purescript-lua/insights
https://github.com/purescript-lua/purescript-lua
New issuehttps://github.com/login?return_to=https://github.com/purescript-lua/purescript-lua/issues/56
New issuehttps://github.com/login?return_to=https://github.com/purescript-lua/purescript-lua/issues/56
#70https://github.com/purescript-lua/purescript-lua/pull/70
Codegen: 'Unexpected bound reference Local b index 1' compiling Data.Array.foldRecM (index inflated by optimizer)https://github.com/purescript-lua/purescript-lua/issues/56#top
#70https://github.com/purescript-lua/purescript-lua/pull/70
https://github.com/Unisay
https://github.com/Unisay
Unisayhttps://github.com/Unisay
on Jun 14, 2026https://github.com/purescript-lua/purescript-lua/issues/56#issue-4658461087
#37https://github.com/purescript-lua/purescript-lua/issues/37
Unisayhttps://github.com/Unisay
on Jun 14, 2026https://github.com/purescript-lua/purescript-lua/issues/56#event-26726432929
Unisayhttps://github.com/Unisay
completedhttps://github.com/purescript-lua/purescript-lua/issues?q=is%3Aissue%20state%3Aclosed%20archived%3Afalse%20reason%3Acompleted
#70https://github.com/purescript-lua/purescript-lua/pull/70
on Jun 14, 2026https://github.com/purescript-lua/purescript-lua/issues/56#event-26727376751
Unisayhttps://github.com/Unisay
on Jun 14, 2026https://github.com/purescript-lua/purescript-lua/issues/56#event-26727376782
Merge pull request #70 from Unisay/issue-56/beta-reduce-unshifthttps://github.com/purescript-lua/purescript-lua/commit/94c13cecd1146494de56196a7badb3b1374d364d
#70https://github.com/purescript-lua/purescript-lua/pull/70
94c13cehttps://github.com/purescript-lua/purescript-lua/commit/94c13cecd1146494de56196a7badb3b1374d364d
Sign up for freehttps://github.com/signup?return_to=https://github.com/purescript-lua/purescript-lua/issues/56
Sign in to commenthttps://github.com/login?return_to=https://github.com/purescript-lua/purescript-lua/issues/56
Unisayhttps://github.com/Unisay
https://github.com
Termshttps://docs.github.com/site-policy/github-terms/github-terms-of-service
Privacyhttps://docs.github.com/site-policy/privacy-policies/github-privacy-statement
Securityhttps://github.com/security
Statushttps://www.githubstatus.com/
Communityhttps://github.community/
Docshttps://docs.github.com/
Contacthttps://support.github.com?tags=dotcom-footer

Viewport: width=device-width


URLs of crawlers that visited me.