Skip to content

fix: generate Reset for generic structs - #12

Merged
darxnet merged 2 commits into
mainfrom
fix/generic-structs
Nov 13, 2026
Merged

darxnet merged 2 commits into
mainfrom
fix/generic-structs

Conversation

@darxnet

@darxnet darxnet commented Nov 13, 2026

Copy link
Copy Markdown
Owner

The generator panicked on a generic struct: go/types cannot size a type parameter, and the cost model asks for the struct's size. The template would also have declared the receiver without its type parameters.

  • Reset names the type parameters: func (r *Pair[K, V]) Reset(), *r = Pair[K, V]{}.
  • A type-parameter field is reset to *new(T), even when its constraint has Reset(), since the call could not be guarded against nil.
  • A type parameter that only pointers satisfy and that has Reset() (P interface{ *T; Reset() }, or a union of pointers) is kept and reset when not nil, like any pointer with Reset().
  • call= on a type parameter that is not only pointers generated r.F != nil, which does not compile; the method is now called without the check.
  • The cost model sizes each type parameter as one pointer.
  • A type parameter named r (the receiver) is reported as an error.

Tests: TestGeneric generates Reset for four generic types in a throwaway module and runs go vet and go test there; TestGenericReceiverName.

darxnet and others added 2 commits November 12, 2026 18:50
The generator panicked on a generic struct (go/types cannot size a type
parameter), and the template would have declared the receiver without its
type parameters. Reset now names them (func (r *Pair[K, V]) Reset()), resets
a type-parameter field to *new(T), and the cost model sizes each type
parameter as a pointer.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…e parameters

A type-parameter field whose constraint lets only pointers in and has
Reset (P interface{ *T; Reset() }) is now kept and reset when not nil,
like any pointer with Reset. call= on a type parameter that is not only
pointers generated r.F != nil, which does not compile; it now calls the
method without the check.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@darxnet
darxnet enabled auto-merge (squash) November 13, 2026 10:29
@darxnet
darxnet merged commit 845faae into main Nov 13, 2026
8 checks passed
@darxnet
darxnet deleted the fix/generic-structs branch November 13, 2026 10:30
@codecov

codecov Bot commented Nov 13, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 94.64286% with 3 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
goreset.go 94.64% 3 Missing ⚠️

📢 Thoughts on this report? Let us know!

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