mirror of
https://github.com/golang/go
synced 2024-11-17 05:54:46 -07:00
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 <rfindley@google.com> Run-TryBot: Robert Findley <rfindley@google.com> TryBot-Result: Go Bot <gobot@golang.org> Reviewed-by: Robert Griesemer <gri@golang.org>
This commit is contained in:
parent
04572fa29b
commit
3664950ef6
@ -64,15 +64,12 @@ func (err Error) Error() string {
|
||||
|
||||
// An ArgumentError holds an error associated with an argument index.
|
||||
type ArgumentError struct {
|
||||
index int
|
||||
error
|
||||
Index int
|
||||
Err error
|
||||
}
|
||||
|
||||
// Index returns the positional index of the argument associated with the
|
||||
// error.
|
||||
func (e ArgumentError) Index() int {
|
||||
return e.index
|
||||
}
|
||||
func (e *ArgumentError) Error() string { return e.Err.Error() }
|
||||
func (e *ArgumentError) Unwrap() error { return e.Err }
|
||||
|
||||
// An Importer resolves import paths to Packages.
|
||||
//
|
||||
|
@ -6,6 +6,7 @@ package types_test
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"errors"
|
||||
"fmt"
|
||||
"go/ast"
|
||||
"go/importer"
|
||||
@ -2000,9 +2001,13 @@ func TestInstantiateErrors(t *testing.T) {
|
||||
t.Fatalf("Instantiate(%v, %v) returned nil error, want non-nil", T, test.targs)
|
||||
}
|
||||
|
||||
gotAt := err.(ArgumentError).Index()
|
||||
if gotAt != test.wantAt {
|
||||
t.Errorf("Instantate(%v, %v): error at index %d, want index %d", T, test.targs, gotAt, test.wantAt)
|
||||
var argErr *ArgumentError
|
||||
if !errors.As(err, &argErr) {
|
||||
t.Fatalf("Instantiate(%v, %v): error is not an *ArgumentError", T, test.targs)
|
||||
}
|
||||
|
||||
if argErr.Index != test.wantAt {
|
||||
t.Errorf("Instantate(%v, %v): error at index %d, want index %d", T, test.targs, argErr.Index, test.wantAt)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -25,8 +25,8 @@ import (
|
||||
// unimplemented.
|
||||
//
|
||||
// If verify is set and constraint satisfaction fails, the returned error may
|
||||
// be of dynamic type ArgumentError indicating which type argument did not
|
||||
// satisfy its corresponding type parameter constraint, and why.
|
||||
// wrap an *ArgumentError indicating which type argument did not satisfy its
|
||||
// corresponding type parameter constraint, and why.
|
||||
//
|
||||
// TODO(rfindley): change this function to also return an error if lengths of
|
||||
// tparams and targs do not match.
|
||||
@ -43,7 +43,7 @@ func Instantiate(env *Environment, typ Type, targs []Type, validate bool) (Type,
|
||||
tparams = t.TypeParams().list()
|
||||
}
|
||||
if i, err := (*Checker)(nil).verify(token.NoPos, tparams, targs); err != nil {
|
||||
return inst, ArgumentError{i, err}
|
||||
return inst, &ArgumentError{i, err}
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user