Skip to content

Commit 3664950

Browse files
committed
go/types: tweaks to ArgumentError to be more idiomatic
This CL makes a few changes to the new ArgumentError type to be more idiomatic: - Use a pointer receiver for methods. - Export fields, similarly to Error. ArgumentError has a clear meaning (an error associated with an index), so there is no need to hide its representation. - Add an Unwrap method to access the underlying error. - Say explicitly that the error returned from Instantiate may wrap *ArgumentError. There is no need to commit to an API that always returns an error with dynamic type *ArgumentError. Updates #47916 Change-Id: Ib1a43e921f247794e7155280ccbf5a6775ed3978 Reviewed-on: https://go-review.googlesource.com/c/go/+/351335 Trust: Robert Findley <[email protected]> Run-TryBot: Robert Findley <[email protected]> TryBot-Result: Go Bot <[email protected]> Reviewed-by: Robert Griesemer <[email protected]>
1 parent 04572fa commit 3664950

File tree

3 files changed

+15
-13
lines changed

3 files changed

+15
-13
lines changed

src/go/types/api.go

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -64,15 +64,12 @@ func (err Error) Error() string {
6464

6565
// An ArgumentError holds an error associated with an argument index.
6666
type ArgumentError struct {
67-
index int
68-
error
67+
Index int
68+
Err error
6969
}
7070

71-
// Index returns the positional index of the argument associated with the
72-
// error.
73-
func (e ArgumentError) Index() int {
74-
return e.index
75-
}
71+
func (e *ArgumentError) Error() string { return e.Err.Error() }
72+
func (e *ArgumentError) Unwrap() error { return e.Err }
7673

7774
// An Importer resolves import paths to Packages.
7875
//

src/go/types/api_test.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ package types_test
66

77
import (
88
"bytes"
9+
"errors"
910
"fmt"
1011
"go/ast"
1112
"go/importer"
@@ -2000,9 +2001,13 @@ func TestInstantiateErrors(t *testing.T) {
20002001
t.Fatalf("Instantiate(%v, %v) returned nil error, want non-nil", T, test.targs)
20012002
}
20022003

2003-
gotAt := err.(ArgumentError).Index()
2004-
if gotAt != test.wantAt {
2005-
t.Errorf("Instantate(%v, %v): error at index %d, want index %d", T, test.targs, gotAt, test.wantAt)
2004+
var argErr *ArgumentError
2005+
if !errors.As(err, &argErr) {
2006+
t.Fatalf("Instantiate(%v, %v): error is not an *ArgumentError", T, test.targs)
2007+
}
2008+
2009+
if argErr.Index != test.wantAt {
2010+
t.Errorf("Instantate(%v, %v): error at index %d, want index %d", T, test.targs, argErr.Index, test.wantAt)
20062011
}
20072012
}
20082013
}

src/go/types/instantiate.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@ import (
2525
// unimplemented.
2626
//
2727
// If verify is set and constraint satisfaction fails, the returned error may
28-
// be of dynamic type ArgumentError indicating which type argument did not
29-
// satisfy its corresponding type parameter constraint, and why.
28+
// wrap an *ArgumentError indicating which type argument did not satisfy its
29+
// corresponding type parameter constraint, and why.
3030
//
3131
// TODO(rfindley): change this function to also return an error if lengths of
3232
// tparams and targs do not match.
@@ -43,7 +43,7 @@ func Instantiate(env *Environment, typ Type, targs []Type, validate bool) (Type,
4343
tparams = t.TypeParams().list()
4444
}
4545
if i, err := (*Checker)(nil).verify(token.NoPos, tparams, targs); err != nil {
46-
return inst, ArgumentError{i, err}
46+
return inst, &ArgumentError{i, err}
4747
}
4848
}
4949

0 commit comments

Comments
 (0)