]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/compile/internal/types2: tweaks to ArgumentError to be more idiomatic
authorRobert Griesemer <gri@golang.org>
Wed, 17 Nov 2021 01:02:11 +0000 (17:02 -0800)
committerRobert Griesemer <gri@golang.org>
Wed, 17 Nov 2021 04:31:22 +0000 (04:31 +0000)
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 <gri@golang.org>
Reviewed-by: Robert Findley <rfindley@google.com>
src/cmd/compile/internal/types2/api.go
src/cmd/compile/internal/types2/api_test.go
src/cmd/compile/internal/types2/instantiate.go

index 83c4b02abf46db0103922eb94b7cc0e107cda3f9..367cb8f700e9585062cbb09e38d7db80d0126592 100644 (file)
@@ -55,17 +55,14 @@ func (err Error) FullError() string {
        return fmt.Sprintf("%s: %s", err.Pos, err.Full)
 }
 
-// An ArgumentError holds an error that is associated with an argument.
+// 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 866ebb8684fff8395a9d8c67d57aab6b0c7298cf..7ec10638436fb595a417f7de77c66b2bd0b8d275 100644 (file)
@@ -7,6 +7,7 @@ package types2_test
 import (
        "bytes"
        "cmd/compile/internal/syntax"
+       "errors"
        "fmt"
        "internal/testenv"
        "reflect"
@@ -2002,9 +2003,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 299d63dc60e6dc3552f67d5c60f073b7c1c4febf..09ca1b7c1670af1f709f5fe7e175a52e082c7550 100644 (file)
@@ -24,8 +24,8 @@ import (
 // instances with the same identity.
 //
 // 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.
@@ -42,7 +42,7 @@ func Instantiate(ctxt *Context, typ Type, targs []Type, validate bool) (Type, er
                        tparams = t.TypeParams().list()
                }
                if i, err := (*Checker)(nil).verify(nopos, tparams, targs); err != nil {
-                       return inst, ArgumentError{i, err}
+                       return inst, &ArgumentError{i, err}
                }
        }