Skip to content

Commit a410b23

Browse files
committed
deepcopy: Attach location information to more errors
1 parent 39a4559 commit a410b23

File tree

2 files changed

+8
-4
lines changed

2 files changed

+8
-4
lines changed

pkg/deepcopy/traverse.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -490,7 +490,7 @@ func (c *copyMethodMaker) genStructDeepCopy(_ *namingInfo, structType *types.Str
490490
case *types.Basic:
491491
switch underlyingField.Kind() {
492492
case types.Invalid, types.UnsafePointer:
493-
c.pkg.AddError(fmt.Errorf("invalid field type: %s", underlyingField))
493+
c.pkg.AddError(loader.ErrFromNode(fmt.Errorf("invalid field type: %s", underlyingField), field))
494494
return
495495
default:
496496
// nothing to do, initial assignment copied this
@@ -502,7 +502,7 @@ func (c *copyMethodMaker) genStructDeepCopy(_ *namingInfo, structType *types.Str
502502
c.Linef("in.%[1]s.DeepCopyInto(&out.%[1]s)", field.Name())
503503
}
504504
default:
505-
c.pkg.AddError(fmt.Errorf("invalid field type: %s", underlyingField))
505+
c.pkg.AddError(loader.ErrFromNode(fmt.Errorf("invalid field type: %s", underlyingField), field))
506506
return
507507
}
508508
}

pkg/loader/errors.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ package loader
1818

1919
import (
2020
"fmt"
21-
"go/ast"
2221
"go/token"
2322
)
2423

@@ -29,10 +28,15 @@ type PositionedError struct {
2928
error
3029
}
3130

31+
// Node is the intersection of go/ast.Node and go/types.Var.
32+
type Node interface {
33+
Pos() token.Pos // position of first character belonging to the node
34+
}
35+
3236
// ErrFromNode returns the given error, with additional information
3337
// attaching it to the given AST node. It will automatically map
3438
// over error lists.
35-
func ErrFromNode(err error, node ast.Node) error {
39+
func ErrFromNode(err error, node Node) error {
3640
if asList, isList := err.(ErrList); isList {
3741
resList := make(ErrList, len(asList))
3842
for i, baseErr := range asList {

0 commit comments

Comments
 (0)