René's URL Explorer Experiment


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

direct link

Domain: github.com


Hey, it has json ld scripts:
{"@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-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:a6cca999-c7b4-806a-84f6-87ec1ef33742
current-catalog-service-hash81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114
request-id8DB0:2E7EBB:7A577F:9C1365:6B00E45E
html-safe-noncec45f0b6d7de26ffeaf58a5fd196206525a6db951140ed00aac7270be9e5ad0a2
visitor-payloadeyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI4REIwOjJFN0VCQjo3QTU3N0Y6OUMxMzY1OjZCMDBFNDVFIiwidmlzaXRvcl9pZCI6IjI1MjMxMzU0Njk2MjE5MjkwNTUiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ==
visitor-hmac2608abc472ebb24b80eba2b328f7d33fbc39a96bb0a2570af0de818e5d44e811
hovercard-subject-tagissue:4969698491
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/297/issue_layout
twitter:imagehttps://opengraph.githubassets.com/0c7b2cf3c39f6876091cc6adeaf93f8c163921e12a7de9ec42f533746c522cac/purescript-lua/purescript-lua/issues/297
twitter:cardsummary_large_image
og:imagehttps://opengraph.githubassets.com/0c7b2cf3c39f6876091cc6adeaf93f8c163921e12a7de9ec42f533746c522cac/purescript-lua/purescript-lua/issues/297
og:image:altProblem 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:width1200
og:image:height600
og:site_nameGitHub
og:typeobject
og:author:usernameUnisay
hostnamegithub.com
expected-hostnamegithub.com
None27088b6977484d0d3321d9a11c907c6d6637b315cfa359ef7952dbf6292f9b14
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
release624179fb8b88d26ba5bba08894eb60a1c5a1d93d
ui-targetfull
theme-color#1e2327
color-schemelight dark
react-app-nameissues

Links:

Skip to contenthttps://github.com/purescript-lua/purescript-lua/issues/297#start-of-content
https://github.com/
Sign inhttps://github.com/login?return_to=https%3A%2F%2Fgithub.com%2Fpurescript-lua%2Fpurescript-lua%2Fissues%2F297
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%2F297
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/297
Reloadhttps://github.com/purescript-lua/purescript-lua/issues/297
Reloadhttps://github.com/purescript-lua/purescript-lua/issues/297
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/297
New issuehttps://github.com/login?return_to=https://github.com/purescript-lua/purescript-lua/issues/297
#298https://github.com/purescript-lua/purescript-lua/pull/298
Mixed ST/Effect discard chains miscompile: canonicalization resurrects a DCE'd foreign accessor (runtime nil call)https://github.com/purescript-lua/purescript-lua/issues/297#top
#298https://github.com/purescript-lua/purescript-lua/pull/298
https://github.com/Unisay
area: irIR / optimizer / DCE / inlinerhttps://github.com/purescript-lua/purescript-lua/issues?q=state%3Aopen%20label%3A%22area%3A%20ir%22
bugSomething isn't workinghttps://github.com/purescript-lua/purescript-lua/issues?q=state%3Aopen%20label%3A%22bug%22
https://github.com/Unisay
Unisayhttps://github.com/Unisay
on Jul 24, 2026https://github.com/purescript-lua/purescript-lua/issues/297#issue-4969698491
#182https://github.com/purescript-lua/purescript-lua/issues/182
#180https://github.com/purescript-lua/purescript-lua/issues/180
Long do blocks generate Lua that exceeds the parser nesting limit #46https://github.com/purescript-lua/purescript-lua/issues/46
#233https://github.com/purescript-lua/purescript-lua/issues/233
Unisayhttps://github.com/Unisay
bugSomething isn't workinghttps://github.com/purescript-lua/purescript-lua/issues?q=state%3Aopen%20label%3A%22bug%22
area: irIR / optimizer / DCE / inlinerhttps://github.com/purescript-lua/purescript-lua/issues?q=state%3Aopen%20label%3A%22area%3A%20ir%22
on Jul 24, 2026https://github.com/purescript-lua/purescript-lua/issues/297#event-28443075530
Unisayhttps://github.com/Unisay
on Jul 24, 2026https://github.com/purescript-lua/purescript-lua/issues/297#event-28443075711
Unisayhttps://github.com/Unisay
on Jul 24, 2026https://github.com/purescript-lua/purescript-lua/issues/297#event-28448977101
fix(optimizer): canonicalize Effect/ST heads through top-level aliaseshttps://github.com/purescript-lua/purescript-lua/commit/74c4fd68b159a64290bc366fc9611f6738145bf3
https://docs.github.com/articles/closing-issues-via-commit-messages
74c4fd6https://github.com/purescript-lua/purescript-lua/commit/74c4fd68b159a64290bc366fc9611f6738145bf3
Unisayhttps://github.com/Unisay
completedhttps://github.com/purescript-lua/purescript-lua/issues?q=is%3Aissue%20state%3Aclosed%20archived%3Afalse%20reason%3Acompleted
#298https://github.com/purescript-lua/purescript-lua/pull/298
on Jul 24, 2026https://github.com/purescript-lua/purescript-lua/issues/297#event-28449690375
Unisayhttps://github.com/Unisay
on Jul 24, 2026https://github.com/purescript-lua/purescript-lua/issues/297#event-28449690941
Merge pull request #298 from purescript-lua/issue-297/canonicalize-th…https://github.com/purescript-lua/purescript-lua/commit/f59f1a83da756667d05e94431b984fcd96471bf9
#298https://github.com/purescript-lua/purescript-lua/pull/298
f59f1a8https://github.com/purescript-lua/purescript-lua/commit/f59f1a83da756667d05e94431b984fcd96471bf9
Sign up for freehttps://github.com/signup?return_to=https://github.com/purescript-lua/purescript-lua/issues/297
Sign in to commenthttps://github.com/login?return_to=https://github.com/purescript-lua/purescript-lua/issues/297
Unisayhttps://github.com/Unisay
area: irIR / optimizer / DCE / inlinerhttps://github.com/purescript-lua/purescript-lua/issues?q=state%3Aopen%20label%3A%22area%3A%20ir%22
bugSomething isn't workinghttps://github.com/purescript-lua/purescript-lua/issues?q=state%3Aopen%20label%3A%22bug%22
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.