1
0
mirror of https://github.com/golang/go synced 2024-11-26 05:17:58 -07:00

cmd/compile/internal/types2: use an opaque environment for Instantiate

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>
This commit is contained in:
Robert Griesemer 2021-08-23 17:38:55 -07:00
parent bba460499c
commit bd97763577
2 changed files with 24 additions and 4 deletions

View 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()

View 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