Skip to content

feat: inspect generic structs (approximate layout + disclaimer)#17

Merged
peczenyj merged 2 commits into
develfrom
feature/inspect-generics
May 25, 2026
Merged

feat: inspect generic structs (approximate layout + disclaimer)#17
peczenyj merged 2 commits into
develfrom
feature/inspect-generics

Conversation

@peczenyj

Copy link
Copy Markdown
Owner

Inspect mode used to silently skip generic types. Now it shows a best-effort
layout
with a clear disclaimer.

Behavior

$ structalign -inspect -type=Generic ./_example
// generic type — layout assumes T=any; the real layout depends on the type argument(s)
type Generic[T] struct { // size: 32, align: 8, padding: 11
	Flag bool    // size:  1, align: 1, padding: 7
	Value any    // size: 16, align: 8
	Count uint32 // size:  4, align: 4, padding: 4
}

Each type parameter is replaced by a representative type — its constraint's core
type (~intint), or interface{} when unbounded (any/comparable/unions).
The disclaimer line + the [T] in the header make the approximation explicit.

Changes

  • common.Layout gains TypeParams + Note; ui.renderLayout renders them.
  • layout.resolveStruct/representativeType instantiate via types.Instantiate
    (validate=false); non-struct / non-instantiable types are still skipped.
  • README "Inspecting generic types" explainer (why it's hard + the disclaimer);
    AGENTS.md and the generic caveat updated.

task ci green; new layout/ui tests cover the any/core-type/non-struct paths.

…sclaimer

Inspect mode previously skipped generic types (no concrete layout until
instantiated). Instead, instantiate each type parameter with a representative
type — its constraint's core type (e.g. ~int -> int), or interface{} when
unbounded (any/comparable/unions) — and show the resulting layout, prefixed with
a disclaimer noting the assumption. The header renders the type-parameter names
(type Generic[T] struct {).

- common.Layout gains TypeParams and Note; ui.renderLayout prints the note above
  the declaration and the params in the header.
- layout.resolveStruct/representativeType do the instantiation (types.Instantiate
  with validate=false); non-struct and non-instantiable types are still skipped.
- README gains an "Inspecting generic types" explainer + example; AGENTS/caveats
  updated.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
@codecov-commenter

codecov-commenter commented May 25, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 82.72727% with 19 lines in your changes missing coverage. Please review.
✅ Project coverage is 83.83%. Comparing base (4ca09d1) to head (b676874).

Files with missing lines Patch % Lines
internal/layout/layout.go 77.10% 16 Missing and 3 partials ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##            devel      #17      +/-   ##
==========================================
+ Coverage   83.07%   83.83%   +0.76%     
==========================================
  Files          10       10              
  Lines         573      662      +89     
==========================================
+ Hits          476      555      +79     
- Misses         56       67      +11     
+ Partials       41       40       -1     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

…me marker

Inspect no longer substitutes a type parameter into the field declaration
(`Value any`). The field now reads as written (`Value T`) and the assumption
that drove its measured size moves to an aligned inline marker
(`-- assume T=any`), shown only on fields whose size depends on a type
parameter — directly or through a composite / nested generic (`map[K]V` →
`K=any, V=any`, `Inner[V]` → `V=any`).

- common.LayoutField gains Assume.
- layout sizes the instantiated struct but renders types + markers from the
  origin struct; fieldAssume/collectTypeParams walk the origin field type for
  referenced params (pointers, slices, arrays, chans, maps, named generics,
  structs, signatures).
- ui aligns the marker past the widest comment.
- Tests for one/two type params and an indirect nested-generic member; README
  and AGENTS updated.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
@peczenyj peczenyj merged commit a768bab into devel May 25, 2026
6 checks passed
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.

2 participants