Skip to content

Remove Generic-based Flat derivation machinery#7662

Open
Unisay wants to merge 1 commit intomasterfrom
yura/issue-7542-remove-dead-flat-code
Open

Remove Generic-based Flat derivation machinery#7662
Unisay wants to merge 1 commit intomasterfrom
yura/issue-7542-remove-dead-flat-code

Conversation

@Unisay
Copy link
Contributor

@Unisay Unisay commented Mar 13, 2026

Summary

Closes #7542.

The internal flat library contained ~440 lines of Generic-based Flat class derivation machinery (GFlat*, GEncode*, GSize* classes, NumConstructors type family) that had a known bug: deserializing enum types with 512+ constructors consumed infinite memory. While not a security risk (all on-chain UPLC code uses only manual Flat instances), the buggy dead code was distracting. This PR removes the entire Generic derivation machinery.

Changes

  • Add manual Flat instances to all types that previously relied on Generic defaults: Maybe, Either, Complex, [], [Char], NonEmpty, tuples (2-7), Tree, Filler, PostAligned, PreAligned, SrcSpan, SrcSpans, and all PIR types (Recursivity, Strictness, Datatype, Binding, Program, Term). Manual encoding is bit-identical to the Generic encoding.

  • Add missing size methods to 9 partial Flat instances in FlatInstances.hs (Name, Version, VarDecl, TyVarDecl, Program, NamedDeBruijn) and Value.hs (K, Quantity, Value) that previously relied on the Generic default for size.

  • Gut Class.hs: remove ~440 lines of Generic machinery, leaving only the abstract Flat class with encode, decode, size methods and getSize.

  • Remove ENUM_LARGE test infrastructure: E256/E258 types, their Flat/Arbitrary instances (~530 lines), and the -DENUM_LARGE cpp-option.

  • Delete Core.hs and Tutorial.hs: Core.hs contained GHC inspection tests verifying that Generic-derived code optimized well, and Tutorial.hs taught Flat usage via deriving (Generic, Flat) examples — both are meaningless without Generic derivation, and the original tutorial remains available in the upstream flat package on Hackage.

  • Fix transitive import breakage: files that got Generic from the PlutusCore.Flat re-export now import GHC.Generics directly. Remove stale hiding (to, from) from imports that no longer re-export Generics.

  • Add QuickCheck round-trip property test for PIR Flat instances: generates 200 random well-typed PIR terms via genTypeAndTerm_, encodes with flat, decodes with unflat, and verifies byte-level identity of re-encoded output.

@Unisay Unisay self-assigned this Mar 13, 2026
@Unisay Unisay force-pushed the yura/issue-7542-remove-dead-flat-code branch 5 times, most recently from 6825737 to 4f79329 Compare March 13, 2026 14:27
@Unisay Unisay marked this pull request as ready for review March 13, 2026 14:27
@Unisay Unisay force-pushed the yura/issue-7542-remove-dead-flat-code branch 2 times, most recently from 409fb17 to 5b0a29e Compare March 13, 2026 15:10
@Unisay Unisay requested a review from a team March 13, 2026 15:14
The internal flat library contained Generic-based Flat class derivation
via GHC.Generics that had a bug: deserializing large enum types (512+
constructors) consumed infinite memory. While not a security risk (UPLC
on-chain code uses only manual Flat instances), the buggy dead code was
distracting. This commit removes the entire Generic derivation machinery.

Changes:

1. Add manual Flat instances to all types that previously relied on
   Generic defaults: Maybe, Either, Complex, [], NonEmpty, tuples (2-7),
   Tree, Filler, PostAligned, PreAligned, SrcSpan, SrcSpans, and all PIR
   types (Recursivity, Strictness, Datatype, Binding, Program, Term).
   Manual encoding is bit-identical to the Generic encoding.

2. Add missing `size` methods to partial Flat instances in
   FlatInstances.hs (Name, Version, VarDecl, TyVarDecl, Program,
   NamedDeBruijn) and Value.hs (K, Quantity, Value) that previously
   relied on the Generic default for `size`.

3. Gut Class.hs: remove ~440 lines of GFlat*/GEncode*/GSize* classes,
   NumConstructors type family, all Generic default method implementations,
   and associated imports/pragmas/re-exports.

4. Remove ENUM_LARGE test infrastructure (E256/E258 types, Flat/Arbitrary
   instances, ~530 lines) and delete Core.hs inspection tests.

5. Fix transitive import breakage: files that got Generic from the
   PlutusCore.Flat re-export now import GHC.Generics directly.
   Remove `hiding (to)` from imports that no longer re-export Generics.

6. Strip Generic-specific doctests from Tutorial.hs.

All existing tests pass (5281 across 4 suites). Manual instances produce
bit-identical encoding verified by round-trip tests and golden files.
@Unisay Unisay force-pushed the yura/issue-7542-remove-dead-flat-code branch from 5b0a29e to a845510 Compare March 13, 2026 15:17
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.

Dead flat code has bugs in it

1 participant