Skip to content

Commit f384c70

Browse files
committed
cmd/compile/internal/types2: tweaks to ArgumentError to be more idiomatic
This CL is a clean port of CL 351335 from go/types to types2. Updates #47916 Change-Id: Idc377fb71d480a51d5e93a348f3a880346011974 Reviewed-on: https://go-review.googlesource.com/c/go/+/364535 Trust: Robert Griesemer <[email protected]> Reviewed-by: Robert Findley <[email protected]>
1 parent 3d7cb23 commit f384c70

File tree

3 files changed

+16
-14
lines changed

3 files changed

+16
-14
lines changed

src/cmd/compile/internal/types2/api.go

+5-8
Original file line numberDiff line numberDiff line change
@@ -55,17 +55,14 @@ func (err Error) FullError() string {
5555
return fmt.Sprintf("%s: %s", err.Pos, err.Full)
5656
}
5757

58-
// An ArgumentError holds an error that is associated with an argument.
58+
// An ArgumentError holds an error associated with an argument index.
5959
type ArgumentError struct {
60-
index int
61-
error
60+
Index int
61+
Err error
6262
}
6363

64-
// Index returns the positional index of the argument associated with the
65-
// error.
66-
func (e ArgumentError) Index() int {
67-
return e.index
68-
}
64+
func (e *ArgumentError) Error() string { return e.Err.Error() }
65+
func (e *ArgumentError) Unwrap() error { return e.Err }
6966

7067
// An Importer resolves import paths to Packages.
7168
//

src/cmd/compile/internal/types2/api_test.go

+8-3
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ package types2_test
77
import (
88
"bytes"
99
"cmd/compile/internal/syntax"
10+
"errors"
1011
"fmt"
1112
"internal/testenv"
1213
"reflect"
@@ -2002,9 +2003,13 @@ func TestInstantiateErrors(t *testing.T) {
20022003
t.Fatalf("Instantiate(%v, %v) returned nil error, want non-nil", T, test.targs)
20032004
}
20042005

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

src/cmd/compile/internal/types2/instantiate.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ import (
2424
// instances with the same identity.
2525
//
2626
// If verify is set and constraint satisfaction fails, the returned error may
27-
// be of dynamic type ArgumentError indicating which type argument did not
28-
// satisfy its corresponding type parameter constraint, and why.
27+
// wrap an *ArgumentError indicating which type argument did not satisfy its
28+
// corresponding type parameter constraint, and why.
2929
//
3030
// TODO(rfindley): change this function to also return an error if lengths of
3131
// tparams and targs do not match.
@@ -42,7 +42,7 @@ func Instantiate(ctxt *Context, typ Type, targs []Type, validate bool) (Type, er
4242
tparams = t.TypeParams().list()
4343
}
4444
if i, err := (*Checker)(nil).verify(nopos, tparams, targs); err != nil {
45-
return inst, ArgumentError{i, err}
45+
return inst, &ArgumentError{i, err}
4646
}
4747
}
4848

0 commit comments

Comments
 (0)