]> Cypherpunks repositories - gostls13.git/commit
go/types, types2: do not overwrite nest entries in Checker.validType
authorRobert Griesemer <gri@golang.org>
Thu, 14 Mar 2024 22:45:30 +0000 (15:45 -0700)
committerGopher Robot <gobot@golang.org>
Fri, 15 Mar 2024 13:22:37 +0000 (13:22 +0000)
commit83b2b47b5da8fd9d713909d0b0a10f5e13d9f177
tree13f96be9eeff6cfdb5be7e0aae1c2cdd7e93ae6f
parentd98b444839320302d35ddc9e0b56595f7e7da3d9
go/types, types2: do not overwrite nest entries in Checker.validType

In Checker.validType, when we encounter a type parameter, we evaluate
the validity of the respective type argument in the "type nest" of the
enclosing type (at the nesting depth at which the type argument was
passed) (*). Specifically, we call validType recursively, with the slice
representing the type nest shortened by 1. This recursive call continues
to use the nest slice and in the process may overwrite the (previously)
last entry. Upon return of that recursive call, validType proceeds with
the old length, possibly using an incorrect last nest entry.

In the concrete example for this issue we have the type S

type S[T any] struct {
a T
b time.Time
}

instantiated with time.Time. When validType encounters the type parameter
T inside the struct (S is in the type nest) it evaluates the type argument
(time.Time) in the empty type nest (outside of S). In the process of
evaluating the time.Time struct, the time.Time type is appended to the
(shortened) nest slice and overwrites the previous last nest entry (S).
Once processing of T is done, validType continues with struct field b,
using the original-length nest slice, which now has time.Time rather
than S as a last element. The type of b has type time.Time, which now
appears to be nested in time.Time (rather than S), which (incorrectly)
means that there's a type cycle. validType proceeds with reporting the
error. But time.Time is an imported type, imported types are correct
(otherwise they could not be imported in the first place), and the
assertion checking that package of time.Time is local fails.

The fix is trivial: restore the last entry of the nest slice when it
may have been overwriten.

(*) In hindsight we may be able to sigificantly simplify validType by
    evaluating type arguments when they are passed instead of when
    the respective type parameters are encountered. For another CL.

Fixes #66323.

Change-Id: I3bf23acb8ed14d349db342ca5c886323a6c7af58
Reviewed-on: https://go-review.googlesource.com/c/go/+/571836
Reviewed-by: Russ Cox <rsc@golang.org>
Reviewed-by: Robert Griesemer <gri@google.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Auto-Submit: Robert Griesemer <gri@google.com>
src/cmd/compile/internal/types2/validtype.go
src/go/types/validtype.go
src/internal/types/testdata/fixedbugs/issue66323.go [new file with mode: 0644]