Skip to content

Commit 39a4559

Browse files
committed
deepcopy: types.Invalid and types.UnsafePointer aren't like the other types.Basic kinds
1 parent 10e2b8b commit 39a4559

File tree

1 file changed

+26
-9
lines changed

1 file changed

+26
-9
lines changed

pkg/deepcopy/traverse.go

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -293,13 +293,18 @@ func (c *copyMethodMaker) genDeepCopyIntoBlock(actualName *namingInfo, typeInfo
293293

294294
switch last := last.(type) {
295295
case *types.Basic:
296-
// basic types themselves can be "shallow" copied, so all we need
297-
// to do is check if our *actual* type (not the underlying one) has
298-
// a custom method implemented.
299-
if hasMethod, _ := hasDeepCopyMethod(c.pkg, typeInfo); hasMethod {
300-
c.Line("*out = in.DeepCopy()")
296+
switch last.Kind() {
297+
case types.Invalid, types.UnsafePointer:
298+
c.pkg.AddError(fmt.Errorf("invalid type: %s", last))
299+
default:
300+
// basic types themselves can be "shallow" copied, so all we need
301+
// to do is check if our *actual* type (not the underlying one) has
302+
// a custom method implemented.
303+
if hasMethod, _ := hasDeepCopyMethod(c.pkg, typeInfo); hasMethod {
304+
c.Line("*out = in.DeepCopy()")
305+
}
306+
c.Line("*out = *in")
301307
}
302-
c.Line("*out = *in")
303308
case *types.Map:
304309
c.genMapDeepCopy(actualName, last)
305310
case *types.Slice:
@@ -483,7 +488,13 @@ func (c *copyMethodMaker) genStructDeepCopy(_ *namingInfo, structType *types.Str
483488
// otherwise...
484489
switch underlyingField := underlyingField.(type) {
485490
case *types.Basic:
486-
// nothing to do, initial assignment copied this
491+
switch underlyingField.Kind() {
492+
case types.Invalid, types.UnsafePointer:
493+
c.pkg.AddError(fmt.Errorf("invalid field type: %s", underlyingField))
494+
return
495+
default:
496+
// nothing to do, initial assignment copied this
497+
}
487498
case *types.Struct:
488499
if fineToShallowCopy(field.Type()) {
489500
c.Linef("out.%[1]s = in.%[1]s", field.Name())
@@ -735,8 +746,14 @@ func eventualUnderlyingType(typeInfo types.Type) types.Type {
735746
func fineToShallowCopy(typeInfo types.Type) bool {
736747
switch typeInfo := typeInfo.(type) {
737748
case *types.Basic:
738-
// basic types (int, string, etc) are always fine to shallow-copy
739-
return true
749+
// basic types (int, string, etc) are always fine to shallow-copy,
750+
// except for Invalid and UnsafePointer, which can't be copied at all.
751+
switch typeInfo.Kind() {
752+
case types.Invalid, types.UnsafePointer:
753+
return false
754+
default:
755+
return true
756+
}
740757
case *types.Named:
741758
// aliases are fine to shallow-copy as long as they resolve to a shallow-copyable type
742759
return fineToShallowCopy(typeInfo.Underlying())

0 commit comments

Comments
 (0)