]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/compile: factor out access to thisT
authorgriesemer <gri@golang.org>
Tue, 25 Apr 2017 05:17:03 +0000 (22:17 -0700)
committerRobert Griesemer <gri@golang.org>
Tue, 25 Apr 2017 17:21:08 +0000 (17:21 +0000)
isifacemethod accessed thisT without checking if it was initialized,
opening the possibility for a bug during type checking. Give better
name, move it to package types, and provide accessor instead.

Change-Id: I29ffc408252a4ba4ef1de218fa154397786c9be6
Reviewed-on: https://go-review.googlesource.com/41673
Reviewed-by: Matthew Dempsky <mdempsky@google.com>
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
src/cmd/compile/internal/gc/bimport.go
src/cmd/compile/internal/gc/dcl.go
src/cmd/compile/internal/gc/noder.go
src/cmd/compile/internal/gc/universe.go
src/cmd/compile/internal/types/type.go

index 7766a5617a54363a71a93f88dea8b1c1e40592ca..734c03083fc3a3cd8341b6c59a9f02864950b739 100644 (file)
@@ -631,7 +631,7 @@ func (p *importer) method() *types.Field {
        f := types.NewField()
        f.Sym = sym
        f.Nname = asTypesNode(newname(sym))
-       f.Type = functypefield(fakethisfield(), params, result)
+       f.Type = functypefield(fakeRecvField(), params, result)
        return f
 }
 
index 99e05e79b18052358fd62b1fdf07861acf78f63b..2820b72a42d16b39e20d76d130df1e00675b0443 100644 (file)
@@ -783,30 +783,21 @@ func embedded(s *types.Sym, pkg *types.Pkg) *Node {
        return n
 }
 
-// thisT is the singleton type used for interface method receivers.
-var thisT *types.Type
-
-func fakethis() *Node {
-       if thisT == nil {
-               thisT = types.NewPtr(types.New(TSTRUCT))
-       }
-       return anonfield(thisT)
+func fakeRecv() *Node {
+       return anonfield(types.FakeRecvType())
 }
 
-func fakethisfield() *types.Field {
-       if thisT == nil {
-               thisT = types.NewPtr(types.New(TSTRUCT))
-       }
+func fakeRecvField() *types.Field {
        f := types.NewField()
-       f.Type = thisT
+       f.Type = types.FakeRecvType()
        return f
 }
 
-// Is this field a method on an interface?
-// Those methods have thisT as the receiver.
-// (See fakethis above.)
+// isifacemethod reports whether (field) m is
+// an interface method. Such methods have the
+// special receiver type types.FakeRecvType().
 func isifacemethod(f *types.Type) bool {
-       return f.Recv().Type == thisT
+       return f.Recv().Type == types.FakeRecvType()
 }
 
 // turn a parsed function declaration into a type
index 73b02ace38a37ab6dca0f1726441f4ea33028fea..ef7c72dfb60099804969419d2c48415449845b05 100644 (file)
@@ -594,7 +594,7 @@ func (p *noder) interfaceType(expr *syntax.InterfaceType) *Node {
                } else {
                        mname := p.newname(method.Name)
                        sig := p.typeExpr(method.Type)
-                       sig.Left = fakethis()
+                       sig.Left = fakeRecv()
                        n = p.nod(method, ODCLFIELD, mname, sig)
                        ifacedcl(n)
                }
index 7a098c7800b22c01f19d9639213ad1b9079361e3..47c138694441a8ed9d7c1501b2bb7c6402f6ee02 100644 (file)
@@ -369,7 +369,7 @@ func typeinit() {
 func makeErrorInterface() *types.Type {
        field := types.NewField()
        field.Type = types.Types[TSTRING]
-       f := functypefield(fakethisfield(), nil, []*types.Field{field})
+       f := functypefield(fakeRecvField(), nil, []*types.Field{field})
 
        field = types.NewField()
        field.Sym = lookup("Error")
index 11ea551b91cce8b7d80ff3b5916f52a0e34c4219..b0be122d0ae8545ff892fcd58e053011c3e86dba 100644 (file)
@@ -1322,3 +1322,13 @@ func (t *Type) Tie() byte {
        }
        return 'T'
 }
+
+var recvType *Type
+
+// FakeRecvType returns the singleton type used for interface method receivers.
+func FakeRecvType() *Type {
+       if recvType == nil {
+               recvType = NewPtr(New(TSTRUCT))
+       }
+       return recvType
+}