Skip to content

Commit 00be176

Browse files
committed
util/codegen: treat unique.Handle as an opaque value type
It doesn't need a Clone method, like a time.Time, etc. And then, because Go 1.23+ uses unique.Handle internally for the netip package types, we can remove those special cases. Updates tailscale#14058 (pulled out from that PR) Updates tailscale/corp#24485 Change-Id: Iac3548a9417ccda5987f98e0305745a6e178b375 Signed-off-by: Brad Fitzpatrick <[email protected]>
1 parent b9ecc50 commit 00be176

File tree

2 files changed

+32
-3
lines changed

2 files changed

+32
-3
lines changed

util/codegen/codegen.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -277,11 +277,16 @@ func IsInvalid(t types.Type) bool {
277277
// It has special handling for some types that contain pointers
278278
// that we know are free from memory aliasing/mutation concerns.
279279
func ContainsPointers(typ types.Type) bool {
280-
switch typ.String() {
280+
s := typ.String()
281+
switch s {
281282
case "time.Time":
282-
// time.Time contains a pointer that does not need copying
283+
// time.Time contains a pointer that does not need cloning.
283284
return false
284-
case "inet.af/netip.Addr", "net/netip.Addr", "net/netip.Prefix", "net/netip.AddrPort":
285+
case "inet.af/netip.Addr":
286+
return false
287+
}
288+
if strings.HasPrefix(s, "unique.Handle[") {
289+
// unique.Handle contains a pointer that does not need cloning.
285290
return false
286291
}
287292
switch ft := typ.Underlying().(type) {

util/codegen/codegen_test.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ import (
1010
"strings"
1111
"sync"
1212
"testing"
13+
"time"
14+
"unique"
1315
"unsafe"
1416

1517
"golang.org/x/exp/constraints"
@@ -84,6 +86,16 @@ type PointerUnionParam[T netip.Prefix | BasicType | IntPtr] struct {
8486
V T
8587
}
8688

89+
type StructWithUniqueHandle struct{ _ unique.Handle[[32]byte] }
90+
91+
type StructWithTime struct{ _ time.Time }
92+
93+
type StructWithNetipTypes struct {
94+
_ netip.Addr
95+
_ netip.AddrPort
96+
_ netip.Prefix
97+
}
98+
8799
type Interface interface {
88100
Method()
89101
}
@@ -161,6 +173,18 @@ func TestGenericContainsPointers(t *testing.T) {
161173
typ: "PointerUnionParam",
162174
wantPointer: true,
163175
},
176+
{
177+
typ: "StructWithUniqueHandle",
178+
wantPointer: false,
179+
},
180+
{
181+
typ: "StructWithTime",
182+
wantPointer: false,
183+
},
184+
{
185+
typ: "StructWithNetipTypes",
186+
wantPointer: false,
187+
},
164188
}
165189

166190
for _, tt := range tests {

0 commit comments

Comments
 (0)