From 9c60a0968987ca3245b115a46d358d63b6b31b0f Mon Sep 17 00:00:00 2001 From: Robert Griesemer Date: Tue, 16 Nov 2021 11:18:53 -0800 Subject: [PATCH] cmd/compile/internal/types2: don't set a Config.Context if none is provided 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 Reviewed-by: Robert Findley --- src/cmd/compile/internal/types2/call.go | 2 +- src/cmd/compile/internal/types2/check.go | 8 +++----- src/cmd/compile/internal/types2/decl.go | 2 +- src/cmd/compile/internal/types2/named.go | 8 +++++--- src/cmd/compile/internal/types2/signature.go | 2 +- src/cmd/compile/internal/types2/typexpr.go | 9 +++++---- 6 files changed, 16 insertions(+), 15 deletions(-) diff --git a/src/cmd/compile/internal/types2/call.go b/src/cmd/compile/internal/types2/call.go index fef493b2ae9..4e2c2a29894 100644 --- a/src/cmd/compile/internal/types2/call.go +++ b/src/cmd/compile/internal/types2/call.go @@ -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 { diff --git a/src/cmd/compile/internal/types2/check.go b/src/cmd/compile/internal/types2/check.go index 247bb5a6497..faf4ccac0b0 100644 --- a/src/cmd/compile/internal/types2/check.go +++ b/src/cmd/compile/internal/types2/check.go @@ -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. diff --git a/src/cmd/compile/internal/types2/decl.go b/src/cmd/compile/internal/types2/decl.go index 91503f1fcd3..e85abbb82fa 100644 --- a/src/cmd/compile/internal/types2/decl.go +++ b/src/cmd/compile/internal/types2/decl.go @@ -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 diff --git a/src/cmd/compile/internal/types2/named.go b/src/cmd/compile/internal/types2/named.go index a455489cd6b..51ea27a6dbd 100644 --- a/src/cmd/compile/internal/types2/named.go +++ b/src/cmd/compile/internal/types2/named.go @@ -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() } diff --git a/src/cmd/compile/internal/types2/signature.go b/src/cmd/compile/internal/types2/signature.go index b0b8ad49d98..06dcd9131a4 100644 --- a/src/cmd/compile/internal/types2/signature.go +++ b/src/cmd/compile/internal/types2/signature.go @@ -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 { diff --git a/src/cmd/compile/internal/types2/typexpr.go b/src/cmd/compile/internal/types2/typexpr.go index 0380c3461d6..862a31544ac 100644 --- a/src/cmd/compile/internal/types2/typexpr.go +++ b/src/cmd/compile/internal/types2/typexpr.go @@ -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