]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/compile/internal/types2: temporarily pin the Checker to Interface during checking
authorRobert Griesemer <gri@golang.org>
Wed, 8 Sep 2021 20:52:46 +0000 (13:52 -0700)
committerRobert Griesemer <gri@golang.org>
Wed, 8 Sep 2021 22:41:32 +0000 (22:41 +0000)
This is a clean port of CL 348371.

Change-Id: I3a61a5a8928279bc783ef16739f7607de3b6ecf3
Reviewed-on: https://go-review.googlesource.com/c/go/+/348575
Trust: Robert Griesemer <gri@golang.org>
Reviewed-by: Robert Findley <rfindley@google.com>
src/cmd/compile/internal/types2/interface.go
src/cmd/compile/internal/types2/sizeof_test.go
src/cmd/compile/internal/types2/testdata/fixedbugs/issue48234.go2 [new file with mode: 0644]
src/cmd/compile/internal/types2/universe.go

index e57158d2d508ea870d6054e68adb951bc96974b1..340df51524b8584e4a2645437e8652ac78b9579a 100644 (file)
@@ -11,6 +11,7 @@ import "cmd/compile/internal/syntax"
 
 // An Interface represents an interface type.
 type Interface struct {
+       check     *Checker      // for error reporting; nil once type set is computed
        obj       *TypeName     // corresponding declared object; or nil (for better error messages)
        methods   []*Func       // ordered list of explicitly declared methods
        embeddeds []Type        // ordered list of explicitly embedded elements
@@ -21,7 +22,7 @@ type Interface struct {
 }
 
 // typeSet returns the type set for interface t.
-func (t *Interface) typeSet() *_TypeSet { return computeInterfaceTypeSet(nil, nopos, t) }
+func (t *Interface) typeSet() *_TypeSet { return computeInterfaceTypeSet(t.check, nopos, t) }
 
 // emptyInterface represents the empty interface
 var emptyInterface = Interface{complete: true, tset: &topTypeSet}
@@ -198,7 +199,7 @@ func (check *Checker) interfaceType(ityp *Interface, iface *syntax.InterfaceType
        }
 
        // All methods and embedded elements for this interface are collected;
-       // i.e., this interface is may be used in a type set computation.
+       // i.e., this interface may be used in a type set computation.
        ityp.complete = true
 
        if len(ityp.methods) == 0 && len(ityp.embeddeds) == 0 {
@@ -214,7 +215,15 @@ func (check *Checker) interfaceType(ityp *Interface, iface *syntax.InterfaceType
        // Compute type set with a non-nil *Checker as soon as possible
        // to report any errors. Subsequent uses of type sets will use
        // this computed type set and won't need to pass in a *Checker.
-       check.later(func() { computeInterfaceTypeSet(check, iface.Pos(), ityp) })
+       //
+       // Pin the checker to the interface type in the interim, in case the type set
+       // must be used before delayed funcs are processed (see issue #48234).
+       // TODO(rfindley): clean up use of *Checker with computeInterfaceTypeSet
+       ityp.check = check
+       check.later(func() {
+               computeInterfaceTypeSet(check, iface.Pos(), ityp)
+               ityp.check = nil
+       })
 }
 
 func flattenUnion(list []syntax.Expr, x syntax.Expr) []syntax.Expr {
index 5be369d84389c873b7395212a6779ccdb5611cd3..bbaca8e0aab007ccc232d079028f7f98bab641f7 100644 (file)
@@ -28,7 +28,7 @@ func TestSizeof(t *testing.T) {
                {Tuple{}, 12, 24},
                {Signature{}, 28, 56},
                {Union{}, 16, 32},
-               {Interface{}, 40, 80},
+               {Interface{}, 44, 88},
                {Map{}, 16, 32},
                {Chan{}, 12, 24},
                {Named{}, 72, 136},
diff --git a/src/cmd/compile/internal/types2/testdata/fixedbugs/issue48234.go2 b/src/cmd/compile/internal/types2/testdata/fixedbugs/issue48234.go2
new file mode 100644 (file)
index 0000000..e069930
--- /dev/null
@@ -0,0 +1,10 @@
+// Copyright 2021 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+package p
+
+var _ = interface{
+       m()
+       m /* ERROR "duplicate method" */ ()
+}(nil)
index a615b4c8760086d1b29fa8c4b89b665aa0ddeb9e..af3ab9732598abffe051c304488b451dc985362f 100644 (file)
@@ -88,7 +88,7 @@ func defPredeclaredTypes() {
                res := NewVar(nopos, nil, "", Typ[String])
                sig := NewSignature(nil, nil, NewTuple(res), false)
                err := NewFunc(nopos, nil, "Error", sig)
-               ityp := &Interface{obj, []*Func{err}, nil, nil, true, nil}
+               ityp := &Interface{nil, obj, []*Func{err}, nil, nil, true, nil}
                computeInterfaceTypeSet(nil, nopos, ityp) // prevent races due to lazy computation of tset
                typ := NewNamed(obj, ityp, nil)
                sig.recv = NewVar(nopos, nil, "", typ)
@@ -99,7 +99,7 @@ func defPredeclaredTypes() {
        {
                obj := NewTypeName(nopos, nil, "comparable", nil)
                obj.setColor(black)
-               ityp := &Interface{obj, nil, nil, nil, true, &_TypeSet{true, nil, allTermlist}}
+               ityp := &Interface{nil, obj, nil, nil, nil, true, &_TypeSet{true, nil, allTermlist}}
                NewNamed(obj, ityp, nil)
                def(obj)
        }