Skip to content

compiler: auto-derive blit for blittable sum types#1492

Open
sqrew wants to merge 2 commits intocarp-lang:masterfrom
sqrew:compiler/auto-blit-sumtypes
Open

compiler: auto-derive blit for blittable sum types#1492
sqrew wants to merge 2 commits intocarp-lang:masterfrom
sqrew:compiler/auto-blit-sumtypes

Conversation

@sqrew
Copy link
Contributor

@sqrew sqrew commented Feb 27, 2026

Summary

Sum types where all fields are non-managed and the type is not generic are trivially memcopyable, but users had to manually implement blit (an identity function + implements declaration). This PR auto-derives blit for those types, consistent with how copy, str, and delete are already auto-derived.

Rules:

  • Simple enums (no fields) → auto-blit ✓
  • Sum types with only primitive/blittable fields → auto-blit ✓
  • Sum types with managed fields (String, Array, etc.) → no blit ✓
  • Generic sum types → no blit ✓

Changes

  • src/Sumtypes.hs: generateBinders now conditionally appends a blit binder when the type is blittable. binderForBlit generates an identity C function ($p $NAME($p p) { return p; }).
  • src/Primitives.hs: getSig "blit" added to autoDerive; both deftype cases conditionally include blit in the auto-derive list (checks for existing impl to avoid duplicates).
  • test/memory.carp: removed manual blit impl from BlitEnum (now auto-derived).
  • core/Binary.carp: removed manual blit impl from ByteOrder module (now auto-derived).
  • test/sumtypes.carp: two explicit blit tests added (Direction simple enum, Axis with Float fields).

Tests

memory:   77 passed, 0 failed
binary:   30 passed, 0 failed
sumtypes:  7 passed, 0 failed

Co-Authored-By: Claude Sonnet 4.6 noreply@anthropic.com

sqrew and others added 2 commits February 26, 2026 17:33
Sum types with only non-managed field types (primitives, pointers, other
blittable types) are freely memcopyable but previously required users to
manually implement the blit interface. This change automatically derives
blit for such types at definition time.

Changes:
- Sumtypes.hs: add binderForBlit (identity fn template) and call it
  conditionally in generateBinders when all field types are non-managed
  and the type is not generic
- Primitives.hs: add getSig "blit" case to autoDerive and conditionally
  register the blit interface when the module has a blit implementation
- test/memory.carp: remove manual blit impl from BlitEnum (now auto-derived)
- core/Binary.carp: remove manual blit impl from ByteOrder (now auto-derived)

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Tests that blit is correctly auto-derived for:
- Simple enums (all unit variants)
- Enums with primitive-typed fields

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
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.

1 participant