]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/compile/internal/types2: don't set a Config.Context if none is provided
authorRobert Griesemer <gri@golang.org>
Tue, 16 Nov 2021 19:18:53 +0000 (11:18 -0800)
committerRobert Griesemer <gri@golang.org>
Wed, 17 Nov 2021 04:31:50 +0000 (04:31 +0000)
This CL is a clean port of CL 363175 from go/types to types2.

Change-Id: I149789be07c0ca7ddef7bfaa4ea9507778a63775
Reviewed-on: https://go-review.googlesource.com/c/go/+/364454
Trust: Robert Griesemer <gri@golang.org>
Reviewed-by: Robert Findley <rfindley@google.com>
src/cmd/compile/internal/types2/call.go
src/cmd/compile/internal/types2/check.go
src/cmd/compile/internal/types2/decl.go
src/cmd/compile/internal/types2/named.go
src/cmd/compile/internal/types2/signature.go
src/cmd/compile/internal/types2/typexpr.go

index fef493b2ae9d97da79b7d29faf32c451cabffa2e..4e2c2a29894a0683a4350e1b8ca3a16ba267773b 100644 (file)
@@ -78,7 +78,7 @@ func (check *Checker) instantiateSignature(pos syntax.Pos, typ *Signature, targs
                }()
        }
 
-       inst := check.instance(pos, typ, targs, check.conf.Context).(*Signature)
+       inst := check.instance(pos, typ, targs, check.bestContext(nil)).(*Signature)
        assert(len(posList) <= len(targs))
        tparams := typ.TypeParams().list()
        if i, err := check.verify(pos, tparams, targs); err != nil {
index 247bb5a649732a2194de320ea9a9979daad3e252..faf4ccac0b0e2eb457ace5bfba15a6820df975c9 100644 (file)
@@ -103,6 +103,7 @@ type Checker struct {
        // package information
        // (initialized by NewChecker, valid for the life-time of checker)
        conf *Config
+       ctxt *Context // context for de-duplicating instances
        pkg  *Package
        *Info
        version version                // accepted language version
@@ -200,11 +201,6 @@ func NewChecker(conf *Config, pkg *Package, info *Info) *Checker {
                conf = new(Config)
        }
 
-       // make sure we have a context
-       if conf.Context == nil {
-               conf.Context = NewContext()
-       }
-
        // make sure we have an info struct
        if info == nil {
                info = new(Info)
@@ -217,6 +213,7 @@ func NewChecker(conf *Config, pkg *Package, info *Info) *Checker {
 
        return &Checker{
                conf:    conf,
+               ctxt:    conf.Context,
                pkg:     pkg,
                Info:    info,
                version: version,
@@ -333,6 +330,7 @@ func (check *Checker) checkFiles(files []*syntax.File) (err error) {
        check.seenPkgMap = nil
        check.recvTParamMap = nil
        check.defTypes = nil
+       check.ctxt = nil
 
        // TODO(gri) There's more memory we should release at this point.
 
index 91503f1fcd33ab0457854ee46093057c91f28313..e85abbb82faeeddea860b52603f3b567d030b7e9 100644 (file)
@@ -69,7 +69,7 @@ func (check *Checker) objDecl(obj Object, def *Named) {
        // Funcs with m.instRecv set have not yet be completed. Complete them now
        // so that they have a type when objDecl exits.
        if m, _ := obj.(*Func); m != nil && m.instRecv != nil {
-               check.completeMethod(check.conf.Context, m)
+               check.completeMethod(nil, m)
        }
 
        // Checking the declaration of obj means inferring its type
index a455489cd6bd6c3f36387d13d11ce77d362418d7..51ea27a6dbd19dab05d799c8315bbb28d33e0102 100644 (file)
@@ -220,15 +220,17 @@ func (n *Named) setUnderlying(typ Type) {
 
 // bestContext returns the best available context. In order of preference:
 // - the given ctxt, if non-nil
-// - check.Config.Context, if check is non-nil
+// - check.ctxt, if check is non-nil
 // - a new Context
 func (check *Checker) bestContext(ctxt *Context) *Context {
        if ctxt != nil {
                return ctxt
        }
        if check != nil {
-               assert(check.conf.Context != nil)
-               return check.conf.Context
+               if check.ctxt == nil {
+                       check.ctxt = NewContext()
+               }
+               return check.ctxt
        }
        return NewContext()
 }
index b0b8ad49d981239a836bd011972fe5467704ffb4..06dcd9131a42566763e9784190802b37913150ed 100644 (file)
@@ -216,7 +216,7 @@ func (check *Checker) funcType(sig *Signature, recvPar *syntax.Field, tparams []
                        var err string
                        switch T := rtyp.(type) {
                        case *Named:
-                               T.resolve(check.conf.Context)
+                               T.resolve(check.bestContext(nil))
                                // The receiver type may be an instantiated type referred to
                                // by an alias (which cannot have receiver parameters for now).
                                if T.TypeArgs() != nil && sig.RecvTypeParams() == nil {
index 0380c3461d61d43ba1d185dca7d0fb2c6fec7186..862a31544ac93cfb9fe5627c7c68554ad24abf5f 100644 (file)
@@ -437,9 +437,10 @@ func (check *Checker) instantiatedType(x syntax.Expr, targsx []syntax.Expr, def
        }
 
        // create the instance
-       h := check.conf.Context.instanceHash(orig, targs)
+       ctxt := check.bestContext(nil)
+       h := ctxt.instanceHash(orig, targs)
        // targs may be incomplete, and require inference. In any case we should de-duplicate.
-       inst, _ := check.conf.Context.lookup(h, orig, targs).(*Named)
+       inst, _ := ctxt.lookup(h, orig, targs).(*Named)
        // If inst is non-nil, we can't just return here. Inst may have been
        // constructed via recursive substitution, in which case we wouldn't do the
        // validation below. Ensure that the validation (and resulting errors) runs
@@ -448,7 +449,7 @@ func (check *Checker) instantiatedType(x syntax.Expr, targsx []syntax.Expr, def
                tname := NewTypeName(x.Pos(), orig.obj.pkg, orig.obj.name, nil)
                inst = check.newNamed(tname, orig, nil, nil, nil) // underlying, methods and tparams are set when named is resolved
                inst.targs = NewTypeList(targs)
-               inst = check.conf.Context.update(h, orig, targs, inst).(*Named)
+               inst = ctxt.update(h, orig, targs, inst).(*Named)
        }
        def.setUnderlying(inst)
 
@@ -474,7 +475,7 @@ func (check *Checker) instantiatedType(x syntax.Expr, targsx []syntax.Expr, def
                // This is an instance from the source, not from recursive substitution,
                // and so it must be resolved during type-checking so that we can report
                // errors.
-               inst.resolve(check.conf.Context)
+               inst.resolve(ctxt)
                // Since check is non-nil, we can still mutate inst. Unpinning the resolver
                // frees some memory.
                inst.resolver = nil