Skip to content

Commit c8d4fe2

Browse files
committed
go/types: temporarily pin the Checker to Interface during checking
While type checking expressions involving interface types, it is possible that their type set is used before delayed actions are processed. As a result, computeInterfaceTypeSet is called with a nil checker, and errors in the interface type definition result in panics (see #48234). To avoid the panics, store a *Checker on Interface for use in between checking of the interface type expression and processing of delayed actions. Fixes #48234 Change-Id: I5509bc1c01b55edac52446b9e075fbe8fcc01874 Reviewed-on: https://go-review.googlesource.com/c/go/+/348371 Trust: Robert Findley <[email protected]> Run-TryBot: Robert Findley <[email protected]> TryBot-Result: Go Bot <[email protected]> Reviewed-by: Robert Griesemer <[email protected]>
1 parent d419f9c commit c8d4fe2

File tree

4 files changed

+25
-6
lines changed

4 files changed

+25
-6
lines changed

src/go/types/interface.go

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414

1515
// An Interface represents an interface type.
1616
type Interface struct {
17+
check *Checker // for error reporting; nil once type set is computed
1718
obj *TypeName // type name object defining this interface; or nil (for better error messages)
1819
methods []*Func // ordered list of explicitly declared methods
1920
embeddeds []Type // ordered list of explicitly embedded elements
@@ -24,7 +25,7 @@ type Interface struct {
2425
}
2526

2627
// typeSet returns the type set for interface t.
27-
func (t *Interface) typeSet() *_TypeSet { return computeInterfaceTypeSet(nil, token.NoPos, t) }
28+
func (t *Interface) typeSet() *_TypeSet { return computeInterfaceTypeSet(t.check, token.NoPos, t) }
2829

2930
// emptyInterface represents the empty (completed) interface
3031
var emptyInterface = Interface{complete: true, tset: &topTypeSet}
@@ -220,7 +221,7 @@ func (check *Checker) interfaceType(ityp *Interface, iface *ast.InterfaceType, d
220221
}
221222

222223
// All methods and embedded elements for this interface are collected;
223-
// i.e., this interface is may be used in a type set computation.
224+
// i.e., this interface may be used in a type set computation.
224225
ityp.complete = true
225226

226227
if len(ityp.methods) == 0 && len(ityp.embeddeds) == 0 {
@@ -236,7 +237,15 @@ func (check *Checker) interfaceType(ityp *Interface, iface *ast.InterfaceType, d
236237
// Compute type set with a non-nil *Checker as soon as possible
237238
// to report any errors. Subsequent uses of type sets will use
238239
// this computed type set and won't need to pass in a *Checker.
239-
check.later(func() { computeInterfaceTypeSet(check, iface.Pos(), ityp) })
240+
//
241+
// Pin the checker to the interface type in the interim, in case the type set
242+
// must be used before delayed funcs are processed (see issue #48234).
243+
// TODO(rfindley): clean up use of *Checker with computeInterfaceTypeSet
244+
ityp.check = check
245+
check.later(func() {
246+
computeInterfaceTypeSet(check, iface.Pos(), ityp)
247+
ityp.check = nil
248+
})
240249
}
241250

242251
func flattenUnion(list []ast.Expr, x ast.Expr) []ast.Expr {

src/go/types/sizeof_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ func TestSizeof(t *testing.T) {
2727
{Tuple{}, 12, 24},
2828
{Signature{}, 28, 56},
2929
{Union{}, 16, 32},
30-
{Interface{}, 40, 80},
30+
{Interface{}, 44, 88},
3131
{Map{}, 16, 32},
3232
{Chan{}, 12, 24},
3333
{Named{}, 72, 136},
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// Copyright 2021 The Go Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
5+
package p
6+
7+
var _ = interface{
8+
m()
9+
m /* ERROR "duplicate method" */ ()
10+
}(nil)

src/go/types/universe.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ func defPredeclaredTypes() {
8989
res := NewVar(token.NoPos, nil, "", Typ[String])
9090
sig := NewSignature(nil, nil, NewTuple(res), false)
9191
err := NewFunc(token.NoPos, nil, "Error", sig)
92-
ityp := &Interface{obj, []*Func{err}, nil, nil, true, nil}
92+
ityp := &Interface{nil, obj, []*Func{err}, nil, nil, true, nil}
9393
computeInterfaceTypeSet(nil, token.NoPos, ityp) // prevent races due to lazy computation of tset
9494
typ := NewNamed(obj, ityp, nil)
9595
sig.recv = NewVar(token.NoPos, nil, "", typ)
@@ -100,7 +100,7 @@ func defPredeclaredTypes() {
100100
{
101101
obj := NewTypeName(token.NoPos, nil, "comparable", nil)
102102
obj.setColor(black)
103-
ityp := &Interface{obj, nil, nil, nil, true, &_TypeSet{true, nil, allTermlist}}
103+
ityp := &Interface{nil, obj, nil, nil, nil, true, &_TypeSet{true, nil, allTermlist}}
104104
NewNamed(obj, ityp, nil)
105105
def(obj)
106106
}

0 commit comments

Comments
 (0)