From: Robert Griesemer Date: Sat, 15 Jan 2022 01:34:59 +0000 (-0800) Subject: go/types, types2: remove special case for external types in validType X-Git-Tag: go1.18beta2~42 X-Git-Url: http://www.git.cypherpunks.su/?a=commitdiff_plain;h=9dfd458e64a2d3fa92fb7b5da393163151f99cf2;p=gostls13.git go/types, types2: remove special case for external types in validType Because validType doesn't modify global state anymore, there's no need to ignore imported types. When we start tracking type parameters, we need to include imported types because they may contribute to cycles that invalidate a type. This CL effectively reverts CL 202483 (issue #35049, which doesn't apply anymore because we don't change the state of imported objects). Preparation for fixing issue #48962. For #35049. For #48962. Change-Id: I06f15575ad197375c74ffd09c222250610186b15 Reviewed-on: https://go-review.googlesource.com/c/go/+/378675 Trust: Robert Griesemer Run-TryBot: Robert Griesemer Reviewed-by: Robert Findley TryBot-Result: Gopher Robot --- diff --git a/src/cmd/compile/internal/types2/validtype.go b/src/cmd/compile/internal/types2/validtype.go index c7f7c13169..101a8b3945 100644 --- a/src/cmd/compile/internal/types2/validtype.go +++ b/src/cmd/compile/internal/types2/validtype.go @@ -60,12 +60,6 @@ func (check *Checker) validType0(typ Type, path []Object) typeInfo { // will terminate. t = t.orig - // don't touch the type if it is from a different package or the Universe scope - // (doing so would lead to a race condition - was issue #35049) - if t.obj.pkg != check.pkg { - return valid - } - // don't report a 2nd error if we already know the type is invalid // (e.g., if a cycle was detected earlier, via under). if t.underlying == Typ[Invalid] { @@ -76,17 +70,25 @@ func (check *Checker) validType0(typ Type, path []Object) typeInfo { switch check.infoMap[t] { case unknown: check.infoMap[t] = marked - check.infoMap[t] = check.validType0(t.fromRHS, append(path, t.obj)) // only types of current package added to path + check.infoMap[t] = check.validType0(t.fromRHS, append(path, t.obj)) case marked: // cycle detected for i, tn := range path { + // Even though validType now can hande cycles through external + // types, we can't have cycles through external types because + // no such types are detected yet. + // TODO(gri) Remove this check once we can detect such cycles, + // and adjust cycleError accordingly. if t.obj.pkg != check.pkg { panic("type cycle via package-external type") } if tn == t.obj { check.cycleError(path[i:]) check.infoMap[t] = invalid - t.underlying = Typ[Invalid] + // don't modify imported types (leads to race condition, see #35049) + if t.obj.pkg == check.pkg { + t.underlying = Typ[Invalid] + } return invalid } } diff --git a/src/go/types/validtype.go b/src/go/types/validtype.go index c0e6ee34f6..865dc9528f 100644 --- a/src/go/types/validtype.go +++ b/src/go/types/validtype.go @@ -60,12 +60,6 @@ func (check *Checker) validType0(typ Type, path []Object) typeInfo { // will terminate. t = t.orig - // don't touch the type if it is from a different package or the Universe scope - // (doing so would lead to a race condition - was issue #35049) - if t.obj.pkg != check.pkg { - return valid - } - // don't report a 2nd error if we already know the type is invalid // (e.g., if a cycle was detected earlier, via under). if t.underlying == Typ[Invalid] { @@ -76,17 +70,25 @@ func (check *Checker) validType0(typ Type, path []Object) typeInfo { switch check.infoMap[t] { case unknown: check.infoMap[t] = marked - check.infoMap[t] = check.validType0(t.fromRHS, append(path, t.obj)) // only types of current package added to path + check.infoMap[t] = check.validType0(t.fromRHS, append(path, t.obj)) case marked: // cycle detected for i, tn := range path { + // Even though validType now can hande cycles through external + // types, we can't have cycles through external types because + // no such types are detected yet. + // TODO(gri) Remove this check once we can detect such cycles, + // and adjust cycleError accordingly. if t.obj.pkg != check.pkg { panic("type cycle via package-external type") } if tn == t.obj { check.cycleError(path[i:]) check.infoMap[t] = invalid - t.underlying = Typ[Invalid] + // don't modify imported types (leads to race condition, see #35049) + if t.obj.pkg == check.pkg { + t.underlying = Typ[Invalid] + } return invalid } }