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 #14058 (pulled out from that PR)
Updates tailscale/corp#24485

Change-Id: Iac3548a9417ccda5987f98e0305745a6e178b375
Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
This commit is contained in:
Brad Fitzpatrick 2024-11-11 08:48:09 -08:00 committed by Brad Fitzpatrick
parent b9ecc50ce3
commit 00be1761b7
2 changed files with 32 additions and 3 deletions

View File

@ -277,11 +277,16 @@ func IsInvalid(t types.Type) bool {
// It has special handling for some types that contain pointers // It has special handling for some types that contain pointers
// that we know are free from memory aliasing/mutation concerns. // that we know are free from memory aliasing/mutation concerns.
func ContainsPointers(typ types.Type) bool { func ContainsPointers(typ types.Type) bool {
switch typ.String() { s := typ.String()
switch s {
case "time.Time": case "time.Time":
// time.Time contains a pointer that does not need copying // time.Time contains a pointer that does not need cloning.
return false return false
case "inet.af/netip.Addr", "net/netip.Addr", "net/netip.Prefix", "net/netip.AddrPort": case "inet.af/netip.Addr":
return false
}
if strings.HasPrefix(s, "unique.Handle[") {
// unique.Handle contains a pointer that does not need cloning.
return false return false
} }
switch ft := typ.Underlying().(type) { switch ft := typ.Underlying().(type) {

View File

@ -10,6 +10,8 @@
"strings" "strings"
"sync" "sync"
"testing" "testing"
"time"
"unique"
"unsafe" "unsafe"
"golang.org/x/exp/constraints" "golang.org/x/exp/constraints"
@ -84,6 +86,16 @@ type PointerUnionParam[T netip.Prefix | BasicType | IntPtr] struct {
V T V T
} }
type StructWithUniqueHandle struct{ _ unique.Handle[[32]byte] }
type StructWithTime struct{ _ time.Time }
type StructWithNetipTypes struct {
_ netip.Addr
_ netip.AddrPort
_ netip.Prefix
}
type Interface interface { type Interface interface {
Method() Method()
} }
@ -161,6 +173,18 @@ func TestGenericContainsPointers(t *testing.T) {
typ: "PointerUnionParam", typ: "PointerUnionParam",
wantPointer: true, wantPointer: true,
}, },
{
typ: "StructWithUniqueHandle",
wantPointer: false,
},
{
typ: "StructWithTime",
wantPointer: false,
},
{
typ: "StructWithNetipTypes",
wantPointer: false,
},
} }
for _, tt := range tests { for _, tt := range tests {