]> Cypherpunks.ru repositories - gostls13.git/commitdiff
go/types, types2: remove redundant calls to Named.resolve
authorRobert Findley <rfindley@google.com>
Sat, 7 May 2022 19:50:05 +0000 (15:50 -0400)
committerRobert Findley <rfindley@google.com>
Tue, 24 May 2022 17:47:42 +0000 (17:47 +0000)
The resolved status of a Named type should be owned by its API, and
callers should access resolved data via methods. Remove several
instances where Named.resolve is explicitly invoked, only to be followed
by a method that also resolves.

Also make two minor cleanups:
- Remove the tparams parameter to Checker.newNamed, as it was unused.
- Include position information when assertions fail, so that one doesn't
  need to go digging in the panicking stack to find the assertion
  location.

Updates #52728

Change-Id: Icbe8c89e9cfe02d60af7d9ba907eaebe1f00193e
Reviewed-on: https://go-review.googlesource.com/c/go/+/404874
Run-TryBot: Robert Findley <rfindley@google.com>
Reviewed-by: Robert Griesemer <gri@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>

16 files changed:
src/cmd/compile/internal/types2/decl.go
src/cmd/compile/internal/types2/errors.go
src/cmd/compile/internal/types2/instantiate.go
src/cmd/compile/internal/types2/lookup.go
src/cmd/compile/internal/types2/named.go
src/cmd/compile/internal/types2/signature.go
src/cmd/compile/internal/types2/subst.go
src/cmd/compile/internal/types2/typexpr.go
src/go/types/decl.go
src/go/types/errors.go
src/go/types/instantiate.go
src/go/types/lookup.go
src/go/types/named.go
src/go/types/signature.go
src/go/types/subst.go
src/go/types/typexpr.go

index 9176358dd5f6aa6dea72d5e080051581f6297282..b6f81aa8a5f50a5bf88181466207319b8ffdfee1 100644 (file)
@@ -508,7 +508,7 @@ func (check *Checker) typeDecl(obj *TypeName, tdecl *syntax.TypeDecl, def *Named
        }
 
        // type definition or generic type declaration
-       named := check.newNamed(obj, nil, nil, nil, nil)
+       named := check.newNamed(obj, nil, nil, nil)
        def.setUnderlying(named)
 
        if tdecl.TParamList != nil {
@@ -671,7 +671,6 @@ func (check *Checker) collectMethods(obj *TypeName) {
                }
 
                if base != nil {
-                       base.resolve(nil) // TODO(mdempsky): Probably unnecessary.
                        base.AddMethod(m)
                }
        }
index 422f52079590f064c6f42f9c5c07c566ceeda67d..2a3e88a2fe095f89d4ef1721552b0da8ea99cdfd 100644 (file)
@@ -10,6 +10,7 @@ import (
        "bytes"
        "cmd/compile/internal/syntax"
        "fmt"
+       "runtime"
        "strconv"
        "strings"
 )
@@ -20,7 +21,13 @@ func unimplemented() {
 
 func assert(p bool) {
        if !p {
-               panic("assertion failed")
+               msg := "assertion failed"
+               // Include information about the assertion location. Due to panic recovery,
+               // this location is otherwise buried in the middle of the panicking stack.
+               if _, file, line, ok := runtime.Caller(1); ok {
+                       msg = fmt.Sprintf("%s:%d: %s", file, line, msg)
+               }
+               panic(msg)
        }
 }
 
index a69a26ba648bd83ccfd03bc77a02cdb05201bb3a..bb90ab3736813d859d158858f52eb4bac7339f61 100644 (file)
@@ -77,7 +77,7 @@ func (check *Checker) instance(pos syntax.Pos, orig Type, targs []Type, ctxt *Co
        switch orig := orig.(type) {
        case *Named:
                tname := NewTypeName(pos, orig.obj.pkg, orig.obj.name, nil)
-               named := check.newNamed(tname, orig, nil, nil, nil) // underlying, tparams, and methods are set when named is resolved
+               named := check.newNamed(tname, orig, nil, nil) // underlying, tparams, and methods are set when named is resolved
                named.targs = newTypeList(targs)
                named.resolver = func(ctxt *Context, n *Named) (*TypeParamList, Type, *methodList) {
                        return expandNamed(ctxt, n, pos)
index 482b6bd8ef9f530b938faf9055721f0d60913fb0..42cd338e24572ef60746459595b8883870af634a 100644 (file)
@@ -134,7 +134,6 @@ func lookupFieldOrMethod(T Type, addressable bool, pkg *Package, name string, fo
                                seen.add(named)
 
                                // look for a matching attached method
-                               named.resolve(nil)
                                if i, m := named.lookupMethod(pkg, name, foldCase); m != nil {
                                        // potential match
                                        // caution: method may not have a proper signature yet
index 0a150a451cb54d9cae370761f7a78b6840c073ba..849398a6f43c5e4afe2bcc85cf52d3db19ffd375 100644 (file)
@@ -38,7 +38,7 @@ func NewNamed(obj *TypeName, underlying Type, methods []*Func) *Named {
        if _, ok := underlying.(*Named); ok {
                panic("underlying type must not be *Named")
        }
-       return (*Checker)(nil).newNamed(obj, nil, underlying, nil, newMethodList(methods))
+       return (*Checker)(nil).newNamed(obj, nil, underlying, newMethodList(methods))
 }
 
 func (t *Named) resolve(ctxt *Context) *Named {
@@ -62,8 +62,8 @@ func (t *Named) resolve(ctxt *Context) *Named {
 }
 
 // newNamed is like NewNamed but with a *Checker receiver and additional orig argument.
-func (check *Checker) newNamed(obj *TypeName, orig *Named, underlying Type, tparams *TypeParamList, methods *methodList) *Named {
-       typ := &Named{check: check, obj: obj, orig: orig, fromRHS: underlying, underlying: underlying, tparams: tparams, methods: methods}
+func (check *Checker) newNamed(obj *TypeName, orig *Named, underlying Type, methods *methodList) *Named {
+       typ := &Named{check: check, obj: obj, orig: orig, fromRHS: underlying, underlying: underlying, methods: methods}
        if typ.orig == nil {
                typ.orig = typ
        }
index 2dc4dd43f37a4e7026661b1c9f689edf40d699ea..92d3aadf888b4f841951ee141d1f05530aa2e258 100644 (file)
@@ -209,7 +209,6 @@ func (check *Checker) funcType(sig *Signature, recvPar *syntax.Field, tparams []
                        // as the method."
                        switch T := rtyp.(type) {
                        case *Named:
-                               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 6cbe57dab08a74e0e9af7ef79362b4dce653efe5..6e41ebdf53907207ac784538cd548e7cd9ce1cba 100644 (file)
@@ -258,7 +258,6 @@ func (subst *subster) typ(typ Type) Type {
                // recursion. The position used here is irrelevant because validation only
                // occurs on t (we don't call validType on named), but we use subst.pos to
                // help with debugging.
-               t.orig.resolve(subst.ctxt)
                return subst.check.instance(subst.pos, t.orig, newTArgs, subst.ctxt)
 
                // Note that if we were to expose substitution more generally (not just in
index 1610f8ff8f585f1523a2702740181ebacd86e8df..1f8b40dba69bfe899c280035cd3d61d91c339dc9 100644 (file)
@@ -448,7 +448,7 @@ func (check *Checker) instantiatedType(x syntax.Expr, xlist []syntax.Expr, def *
        if inst == nil {
                // x may be a selector for an imported type; use its start pos rather than x.Pos().
                tname := NewTypeName(syntax.StartPos(x), 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 = check.newNamed(tname, orig, nil, nil) // underlying, methods and tparams are set when named is resolved
                inst.targs = newTypeList(targs)
                inst = ctxt.update(h, orig, targs, inst).(*Named)
        }
index 7229104190278562fb67e41ca10bc0c559b3ddb5..123d296791b617f778788704b5a9dd349f2a312b 100644 (file)
@@ -565,7 +565,7 @@ func (check *Checker) typeDecl(obj *TypeName, tdecl *ast.TypeSpec, def *Named) {
        }
 
        // type definition or generic type declaration
-       named := check.newNamed(obj, nil, nil, nil, nil)
+       named := check.newNamed(obj, nil, nil, nil)
        def.setUnderlying(named)
 
        if tdecl.TypeParams != nil {
@@ -741,7 +741,6 @@ func (check *Checker) collectMethods(obj *TypeName) {
                }
 
                if base != nil {
-                       base.resolve(nil) // TODO(mdempsky): Probably unnecessary.
                        base.AddMethod(m)
                }
        }
index f3cb249f5e1387189138a4fe145732f6e55f4873..964f377984ac680595859d92c4673d6d59809a95 100644 (file)
@@ -11,13 +11,20 @@ import (
        "fmt"
        "go/ast"
        "go/token"
+       "runtime"
        "strconv"
        "strings"
 )
 
 func assert(p bool) {
        if !p {
-               panic("assertion failed")
+               msg := "assertion failed"
+               // Include information about the assertion location. Due to panic recovery,
+               // this location is otherwise buried in the middle of the panicking stack.
+               if _, file, line, ok := runtime.Caller(1); ok {
+                       msg = fmt.Sprintf("%s:%d: %s", file, line, msg)
+               }
+               panic(msg)
        }
 }
 
index 4450847dfdaff12f94e73962e18b405c70250ce3..964a4f907caf91819a66ee1a62b28991b1e481d1 100644 (file)
@@ -77,7 +77,7 @@ func (check *Checker) instance(pos token.Pos, orig Type, targs []Type, ctxt *Con
        switch orig := orig.(type) {
        case *Named:
                tname := NewTypeName(pos, orig.obj.pkg, orig.obj.name, nil)
-               named := check.newNamed(tname, orig, nil, nil, nil) // underlying, tparams, and methods are set when named is resolved
+               named := check.newNamed(tname, orig, nil, nil) // underlying, tparams, and methods are set when named is resolved
                named.targs = newTypeList(targs)
                named.resolver = func(ctxt *Context, n *Named) (*TypeParamList, Type, *methodList) {
                        return expandNamed(ctxt, n, pos)
index 22a62055d315a27c7b36ead38e7c6b7c57577a75..305b2003f7da279519a7da9e75dd110ef7404de7 100644 (file)
@@ -134,7 +134,6 @@ func lookupFieldOrMethod(T Type, addressable bool, pkg *Package, name string, fo
                                seen.add(named)
 
                                // look for a matching attached method
-                               named.resolve(nil)
                                if i, m := named.lookupMethod(pkg, name, foldCase); m != nil {
                                        // potential match
                                        // caution: method may not have a proper signature yet
index f8d319a5ec198806629fc92b9225ff343913cc74..a82679eb1004ea76431c61ace62143bb173b50c4 100644 (file)
@@ -38,7 +38,7 @@ func NewNamed(obj *TypeName, underlying Type, methods []*Func) *Named {
        if _, ok := underlying.(*Named); ok {
                panic("underlying type must not be *Named")
        }
-       return (*Checker)(nil).newNamed(obj, nil, underlying, nil, newMethodList(methods))
+       return (*Checker)(nil).newNamed(obj, nil, underlying, newMethodList(methods))
 }
 
 func (t *Named) resolve(ctxt *Context) *Named {
@@ -62,8 +62,8 @@ func (t *Named) resolve(ctxt *Context) *Named {
 }
 
 // newNamed is like NewNamed but with a *Checker receiver and additional orig argument.
-func (check *Checker) newNamed(obj *TypeName, orig *Named, underlying Type, tparams *TypeParamList, methods *methodList) *Named {
-       typ := &Named{check: check, obj: obj, orig: orig, fromRHS: underlying, underlying: underlying, tparams: tparams, methods: methods}
+func (check *Checker) newNamed(obj *TypeName, orig *Named, underlying Type, methods *methodList) *Named {
+       typ := &Named{check: check, obj: obj, orig: orig, fromRHS: underlying, underlying: underlying, methods: methods}
        if typ.orig == nil {
                typ.orig = typ
        }
index 9e7b63b4513e291d775bd5d0303be6c5494da7c5..4b63f0e6f0b42daf24789ed91028560e30c27e35 100644 (file)
@@ -216,7 +216,6 @@ func (check *Checker) funcType(sig *Signature, recvPar *ast.FieldList, ftyp *ast
                        // as the method."
                        switch T := rtyp.(type) {
                        case *Named:
-                               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 b1794ac32d140f281bfe021068824d3c24ef68d1..63849b921241ef70cb1f56a1ded477b2c2b70748 100644 (file)
@@ -258,7 +258,6 @@ func (subst *subster) typ(typ Type) Type {
                // recursion. The position used here is irrelevant because validation only
                // occurs on t (we don't call validType on named), but we use subst.pos to
                // help with debugging.
-               t.orig.resolve(subst.ctxt)
                return subst.check.instance(subst.pos, t.orig, newTArgs, subst.ctxt)
 
                // Note that if we were to expose substitution more generally (not just in
index d5fe9a5cc6dca7d5fa8947769db4ff6f2e19d2c7..7afc66a9251efc37a0333c9d91516532f56e09d9 100644 (file)
@@ -433,7 +433,7 @@ func (check *Checker) instantiatedType(ix *typeparams.IndexExpr, def *Named) (re
        if inst == nil {
                // x may be a selector for an imported type; use its start pos rather than x.Pos().
                tname := NewTypeName(ix.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 = check.newNamed(tname, orig, nil, nil) // underlying, methods and tparams are set when named is resolved
                inst.targs = newTypeList(targs)
                inst = ctxt.update(h, orig, targs, inst).(*Named)
        }