]> Cypherpunks.ru repositories - gostls13.git/commitdiff
go/types: tweaks to ArgumentError to be more idiomatic
authorRobert Findley <rfindley@google.com>
Tue, 21 Sep 2021 22:15:25 +0000 (18:15 -0400)
committerRobert Findley <rfindley@google.com>
Wed, 22 Sep 2021 01:41:09 +0000 (01:41 +0000)
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>
src/go/types/api.go
src/go/types/api_test.go
src/go/types/instantiate.go

index 4cf0eb123f9431d66dda012b4f1f41870e53827d..0bbd940d074d1d51e55077ef2a2bb40fb8596ad3 100644 (file)
@@ -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.
 //
index 9b584f390ce37593ab44d049c161500b07f38eea..d59d3d8923af2c40e04cac6ab3e6b4c77a56700c 100644 (file)
@@ -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)
                }
        }
 }
index b178d1eb3f98604bd0dc182e7372407ca8d165cf..6cafc2fbeda609258703830e1399efb359b7b2fe 100644 (file)
@@ -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}
                }
        }