@@ -293,13 +293,18 @@ func (c *copyMethodMaker) genDeepCopyIntoBlock(actualName *namingInfo, typeInfo
293
293
294
294
switch last := last .(type ) {
295
295
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" )
301
307
}
302
- c .Line ("*out = *in" )
303
308
case * types.Map :
304
309
c .genMapDeepCopy (actualName , last )
305
310
case * types.Slice :
@@ -483,7 +488,13 @@ func (c *copyMethodMaker) genStructDeepCopy(_ *namingInfo, structType *types.Str
483
488
// otherwise...
484
489
switch underlyingField := underlyingField .(type ) {
485
490
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
+ }
487
498
case * types.Struct :
488
499
if fineToShallowCopy (field .Type ()) {
489
500
c .Linef ("out.%[1]s = in.%[1]s" , field .Name ())
@@ -735,8 +746,14 @@ func eventualUnderlyingType(typeInfo types.Type) types.Type {
735
746
func fineToShallowCopy (typeInfo types.Type ) bool {
736
747
switch typeInfo := typeInfo .(type ) {
737
748
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
+ }
740
757
case * types.Named :
741
758
// aliases are fine to shallow-copy as long as they resolve to a shallow-copyable type
742
759
return fineToShallowCopy (typeInfo .Underlying ())
0 commit comments