Skip to content

Commit 51504f0

Browse files
committed
go/types: don't update package-external types when checking validity
The recently added type-validity check uses a new field of Named types for marking (to detect cycles). That field was modified even if the type was not part of the current package or belonged to the Universe scope (error type). This led to race conditions if the package's type was imported by multiple, concurrently type-checked packages. A test would be nice but it's a bit cumbersome to set one up. Verified manually that package-external types are left alone. Fixes #35049. Change-Id: I51686bef47fcca48b99b91ecb1b2e9d58e135ea6 Reviewed-on: https://go-review.googlesource.com/c/go/+/202483 Reviewed-by: Bryan C. Mills <[email protected]>
1 parent 19d2a1c commit 51504f0

File tree

1 file changed

+11
-1
lines changed

1 file changed

+11
-1
lines changed

src/go/types/decl.go

+11-1
Original file line numberDiff line numberDiff line change
@@ -311,19 +311,29 @@ func (check *Checker) validType(typ Type, path []Object) typeInfo {
311311
}
312312

313313
case *Named:
314+
// don't touch the type if it is from a different package or the Universe scope
315+
// (doing so would lead to a race condition - was issue #35049)
316+
if t.obj.pkg != check.pkg {
317+
return valid
318+
}
319+
314320
// don't report a 2nd error if we already know the type is invalid
315321
// (e.g., if a cycle was detected earlier, via Checker.underlying).
316322
if t.underlying == Typ[Invalid] {
317323
t.info = invalid
318324
return invalid
319325
}
326+
320327
switch t.info {
321328
case unknown:
322329
t.info = marked
323-
t.info = check.validType(t.orig, append(path, t.obj))
330+
t.info = check.validType(t.orig, append(path, t.obj)) // only types of current package added to path
324331
case marked:
325332
// cycle detected
326333
for i, tn := range path {
334+
if t.obj.pkg != check.pkg {
335+
panic("internal error: type cycle via package-external type")
336+
}
327337
if tn == t.obj {
328338
check.cycleError(path[i:])
329339
t.info = invalid

0 commit comments

Comments
 (0)