Skip to content

Guard newGiven calls against CoEvBindsVar to prevent GHC panic on ill-kinded types#133

Open
martijnbastiaan with Copilot wants to merge 2 commits into
masterfrom
copilot/fix-ghc-panic-on-ill-kinded-expressions
Open

Guard newGiven calls against CoEvBindsVar to prevent GHC panic on ill-kinded types#133
martijnbastiaan with Copilot wants to merge 2 commits into
masterfrom
copilot/fix-ghc-panic-on-ill-kinded-expressions

Conversation

Copilot AI commented Apr 23, 2026

Copy link
Copy Markdown

When GHC encounters ill-kinded type expressions (e.g., BitSize Meta instead of BitSize (Meta a)), it may invoke the plugin with a CoEvBindsVar evidence bindings variable. The plugin's newGiven calls then trigger setEvBind on CoEvBindsVar, which panics unconditionally.

-- BitSize Meta is ill-kinded (Meta :: Type -> Type, not Type)
-- This panics GHC instead of producing a kind error
(_header, meta) = split @(n - BitSize Meta) word

Changes

  • reduceGivens: Check askEvBinds upfront; bail out returning original givens when CoEvBindsVar
  • simples in simplifyNats: Guard unifyItemToGiven calls with the same check via new canCreateGivens helper
  • canCreateGivens: New helper that returns False when evidence bindings variable is CoEvBindsVar
  • Cabal mixins: Add TcEvidence as GHC.Tc.Types.Evidence for GHC < 9.0 compatibility
  • Test: Add regression test verifying ill-kinded types produce a kind error, not a panic

…-kinded types

When GHC encounters ill-kinded type expressions (e.g., BitSize Meta instead
of BitSize (Meta a)), it may invoke the plugin with a CoEvBindsVar evidence
bindings variable. Calling newGiven in this context causes a GHC panic
because CoEvBindsVar does not support adding evidence bindings.

This fix checks askEvBinds before calling newGiven in:
- reduceGivens: skip reduction if CoEvBindsVar
- simples (via unifyItemToGiven): skip given creation if CoEvBindsVar

With this fix, ill-kinded expressions properly produce kind errors instead
of panicking GHC.

Agent-Logs-Url: https://github.com/clash-lang/ghc-typelits-natnormalise/sessions/e42555bb-d8c2-41bc-aaa1-17c8d5ccbc18

Co-authored-by: martijnbastiaan <5658854+martijnbastiaan@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix GHC panic on ill-kinded expressions Guard newGiven calls against CoEvBindsVar to prevent GHC panic on ill-kinded types Apr 23, 2026
Copilot AI requested a review from martijnbastiaan April 23, 2026 11:54
@martijnbastiaan
martijnbastiaan marked this pull request as ready for review April 23, 2026 12:55
@martijnbastiaan
martijnbastiaan requested review from christiaanb and rowanG077 and removed request for martijnbastiaan April 23, 2026 12:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

GHC panic on ill-kinded expressions

2 participants