]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/compile/internal/types2: use an opaque environment for Instantiate
authorRobert Griesemer <gri@golang.org>
Tue, 24 Aug 2021 00:38:55 +0000 (17:38 -0700)
committerRobert Griesemer <gri@golang.org>
Tue, 24 Aug 2021 16:36:47 +0000 (16:36 +0000)
This is a port of CL 343930 from go/types, adjusted to work for
the compiler: here Environment carries a *Checker, if available.

Change-Id: I44544fad7da870fa0c02832baa6abd2909d50304
Reviewed-on: https://go-review.googlesource.com/c/go/+/344612
Trust: Robert Griesemer <gri@golang.org>
Run-TryBot: Robert Griesemer <gri@golang.org>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Matthew Dempsky <mdempsky@google.com>
src/cmd/compile/internal/noder/reader2.go
src/cmd/compile/internal/types2/instantiate.go

index 64c1612f70622962d855a717ab5832cca604f6fc..e72a6737ed4500c0a375ed7f24aba5ace5f727f0 100644 (file)
@@ -228,7 +228,7 @@ func (r *reader2) doTyp() (res types2.Type) {
                obj, targs := r.obj()
                name := obj.(*types2.TypeName)
                if len(targs) != 0 {
-                       t, _ := types2.Instantiate(r.p.check, name.Type(), targs, false)
+                       t, _ := types2.Instantiate(types2.NewEnvironment(r.p.check), name.Type(), targs, false)
                        return t
                }
                return name.Type()
index fdb8c405726517e9b1bac12fb2346a344365294c..9d6002166766bbd9ec464c23bfb6e2fada30c95c 100644 (file)
@@ -13,6 +13,21 @@ import (
        "fmt"
 )
 
+// An Environment is an opaque type checking environment. It may be used to
+// share identical type instances across type checked packages or calls to
+// Instantiate.
+type Environment struct {
+       // For now, Environment just hides a Checker.
+       // Eventually, we strive to remove the need for a checker.
+       check *Checker
+}
+
+// NewEnvironment returns a new Environment, initialized with the given
+// Checker, or nil.
+func NewEnvironment(check *Checker) *Environment {
+       return &Environment{check}
+}
+
 // Instantiate instantiates the type typ with the given type arguments targs.
 // typ must be a *Named or a *Signature type, and its number of type parameters
 // must match the number of provided type arguments. The result is a new,
@@ -20,8 +35,9 @@ import (
 // *Signature). Any methods attached to a *Named are simply copied; they are
 // not instantiated.
 //
-// If check is non-nil, it will be used to de-dupe the instance against
-// previous instances with the same identity.
+// If env is non-nil, it may be used to de-dupe the instance against previous
+// instances with the same identity. This functionality is implemented for
+// environments with non-nil Checkers.
 //
 // If verify is set and constraint satisfaction fails, the returned error may
 // be of dynamic type ArgumentError indicating which type argument did not
@@ -29,7 +45,11 @@ import (
 //
 // TODO(rfindley): change this function to also return an error if lengths of
 // tparams and targs do not match.
-func Instantiate(check *Checker, typ Type, targs []Type, validate bool) (Type, error) {
+func Instantiate(env *Environment, typ Type, targs []Type, validate bool) (Type, error) {
+       var check *Checker
+       if env != nil {
+               check = env.check
+       }
        inst := check.instance(nopos, typ, targs)
 
        var err error