Title: collectDataDeclarations classifies types by constructor adjacency, so a reordered CoreFn would silently break pattern matching · Issue #168 · purescript-lua/purescript-lua · GitHub
Open Graph Title: collectDataDeclarations classifies types by constructor adjacency, so a reordered CoreFn would silently break pattern matching · Issue #168 · purescript-lua/purescript-lua
X Title: collectDataDeclarations classifies types by constructor adjacency, so a reordered CoreFn would silently break pattern matching · Issue #168 · purescript-lua/purescript-lua
Description: Problem collectDataDeclarations (lib/Language/PureScript/Backend/IR.hs) decides whether a type is a ProductType or a SumType by grouping the module's constructor list with List.groupBy, which only merges adjacent elements. The classifica...
Open Graph Description: Problem collectDataDeclarations (lib/Language/PureScript/Backend/IR.hs) decides whether a type is a ProductType or a SumType by grouping the module's constructor list with List.groupBy, which only ...
X Description: Problem collectDataDeclarations (lib/Language/PureScript/Backend/IR.hs) decides whether a type is a ProductType or a SumType by grouping the module's constructor list with List.groupBy, which o...
Opengraph URL: https://github.com/purescript-lua/purescript-lua/issues/168
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"collectDataDeclarations classifies types by constructor adjacency, so a reordered CoreFn would silently break pattern matching","articleBody":"## Problem\n\n`collectDataDeclarations` (`lib/Language/PureScript/Backend/IR.hs`) decides whether a type is a `ProductType` or a `SumType` by grouping the module's constructor list with `List.groupBy`, which only merges adjacent elements. The classification is therefore correct only under an invariant pslua does not control and never checks: upstream `purs` happens to list all constructors of one type consecutively in `moduleBindings`.\n\nIf a future `purs` version reorders those declarations, a two-constructor sum type splits into two single-element groups, each classified as `ProductType`, and one entry overwrites the other in the resulting map. The damage lands entirely on the matcher side: `AlgebraicType` has exactly one live consumer, the pattern compiler, which emits the `reflectCtor == ctorId` tag test for `SumType` and skips it for `ProductType`. A misclassified match stops testing tags, so its first clause always wins — no compile error, no load error, just wrong answers. Constructed values still look correctly tagged at runtime because the code generator tags every value regardless, which makes the failure especially confusing to debug.\n\n## Approach\n\nRemove the order dependence entirely: build the map with `Map.fromListWith (\u003c>)` keyed by module and type name, and classify by the size of the merged constructor set.\n\nWhile in there, drop the dead tag from product-type values. The Lua code generator ignores the `AlgebraicType` field that every `IR.Ctor` node carries (the `_algebraicTy` wildcard in `lib/Language/PureScript/Backend/Lua.hs`) and emits the `[\"$ctor\"] = \"...\"` row into every constructed table; no compiled match ever reads that field for a `ProductType` constructor, so every product value allocates a string field nothing consumes. Emit the row only for `SumType` constructors.\n\n## Prerequisites / Relations\n\nLatent correctness bug, independent. The product-tag change touches the FFI contract, so it must come with a sweep of the package-set foreign files confirming no FFI peeks at `$ctor`.\n\n## Verification / Measurement\n\nA CoreFn with non-adjacent constructors of the same type still compiles to correct pattern matching (the sum-type tag test is emitted rather than the first clause silently winning). Product-type allocations no longer carry the unread `$ctor` field, and the eval goldens remain the semantic net that the field's removal changes no runtime behaviour.\n","author":{"url":"https://github.com/Unisay","@type":"Person","name":"Unisay"},"datePublished":"2026-07-05T20:45:25Z","url":"https://github.com/purescript-lua/purescript-lua/issues/168"}
| 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:b8f1dade-4922-5a50-6e50-7f069c1051cd |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | B830:A99C2:B14B67:E580C3:6B00E661 |
| html-safe-nonce | 791d2216af2de81a321e387788a5ad3bd850ce0dbe6f895313273fc40986ee44 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJCODMwOkE5OUMyOkIxNEI2NzpFNTgwQzM6NkIwMEU2NjEiLCJ2aXNpdG9yX2lkIjoiODQ1MzAyMjE3NjUyMTAyNzUzIiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0= |
| visitor-hmac | 81c219633e53da66b560cb9abcb170b6197a26178f7a59601736aef95085e6f4 |
| hovercard-subject-tag | issue:4814376984 |
| 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/168/issue_layout |
| twitter:image | https://opengraph.githubassets.com/d491963d7b44d59ad7f9b84af36088f53d4b3912927bbae4b234e44f50a52005/purescript-lua/purescript-lua/issues/168 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/d491963d7b44d59ad7f9b84af36088f53d4b3912927bbae4b234e44f50a52005/purescript-lua/purescript-lua/issues/168 |
| og:image:alt | Problem collectDataDeclarations (lib/Language/PureScript/Backend/IR.hs) decides whether a type is a ProductType or a SumType by grouping the module's constructor list with List.groupBy, which only ... |
| 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