1
0
mirror of https://github.com/golang/go synced 2024-09-29 14:14:29 -06: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:
Robert Findley 2021-09-21 18:15:25 -04:00
parent 04572fa29b
commit 3664950ef6
3 changed files with 15 additions and 13 deletions

View File

@ -64,15 +64,12 @@ func (err Error) Error() string {
// An ArgumentError holds an error associated with an argument index. // An ArgumentError holds an error associated with an argument index.
type ArgumentError struct { type ArgumentError struct {
index int Index int
error Err error
} }
// Index returns the positional index of the argument associated with the func (e *ArgumentError) Error() string { return e.Err.Error() }
// error. func (e *ArgumentError) Unwrap() error { return e.Err }
func (e ArgumentError) Index() int {
return e.index
}
// An Importer resolves import paths to Packages. // An Importer resolves import paths to Packages.
// //

View File

@ -6,6 +6,7 @@ package types_test
import ( import (
"bytes" "bytes"
"errors"
"fmt" "fmt"
"go/ast" "go/ast"
"go/importer" "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) t.Fatalf("Instantiate(%v, %v) returned nil error, want non-nil", T, test.targs)
} }
gotAt := err.(ArgumentError).Index() var argErr *ArgumentError
if gotAt != test.wantAt { if !errors.As(err, &argErr) {
t.Errorf("Instantate(%v, %v): error at index %d, want index %d", T, test.targs, gotAt, test.wantAt) 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)
} }
} }
} }

View File

@ -25,8 +25,8 @@ import (
// unimplemented. // unimplemented.
// //
// If verify is set and constraint satisfaction fails, the returned error may // If verify is set and constraint satisfaction fails, the returned error may
// be of dynamic type ArgumentError indicating which type argument did not // wrap an *ArgumentError indicating which type argument did not satisfy its
// satisfy its corresponding type parameter constraint, and why. // corresponding type parameter constraint, and why.
// //
// TODO(rfindley): change this function to also return an error if lengths of // TODO(rfindley): change this function to also return an error if lengths of
// tparams and targs do not match. // 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() tparams = t.TypeParams().list()
} }
if i, err := (*Checker)(nil).verify(token.NoPos, tparams, targs); err != nil { if i, err := (*Checker)(nil).verify(token.NoPos, tparams, targs); err != nil {
return inst, ArgumentError{i, err} return inst, &ArgumentError{i, err}
} }
} }