]> Cypherpunks repositories - gostls13.git/commit
go/types, types2: add check for finite size at value observance
authorMark Freeman <mark@golang.org>
Wed, 3 Dec 2025 20:14:22 +0000 (15:14 -0500)
committerGopher Robot <gobot@golang.org>
Tue, 9 Dec 2025 16:51:02 +0000 (08:51 -0800)
commit1274d58dacc92204f5bb233b22a93c30a37f34e5
treea2bca272d6207b297cd8cc2632223674440308db
parent9e0981230803265f3245193e1d61cc3599c9ca54
go/types, types2: add check for finite size at value observance

Each type must be representable by a finite amount of Go source code
after replacing all alias type names, value names, and embedded
interfaces (per #56103) with the RHS from their respective declarations
("expansion"); otherwise the type is invalid.

Furthermore, each type must have a finite size.

Checking for finite source after expansion is handled in decl.go.
Checking for finite size is handled in validtype.go and is delayed to
the end of type checking (unless used in unsafe.Sizeof, in which case
it is computed eagerly).

We can only construct values of valid types. Thus, while a type is
pending (on the object path and thus not yet valid), it cannot construct
a value of its own type (directly or indirectly).

This change enforces the indirect case by validating each type at value
observance (and hence upholding the invariant that values of only valid
types are created). Validation is cached on Named types to avoid
duplicate work.

As an example, consider:

  type A [unsafe.Sizeof(B{})]int
  type B A

Clearly, since there are no aliases, A and B have finite source. At the
site of B{}, B will be checked for finite size, recursing down the
values of B, at which point A is seen. Since A is on the object path,
there is a cycle preventing B from being proven valid before B{},
violating our invariant.

Note that this change also works for generic types.

Fixes #75918
Fixes #76478

Change-Id: I76d493b5da9571780fed4b3c76803750ec184818
Reviewed-on: https://go-review.googlesource.com/c/go/+/726580
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Robert Griesemer <gri@google.com>
Auto-Submit: Mark Freeman <markfreeman@google.com>
src/cmd/compile/internal/types2/cycles.go
src/cmd/compile/internal/types2/expr.go
src/cmd/compile/internal/types2/named.go
src/cmd/compile/internal/types2/sizeof_test.go
src/go/types/cycles.go
src/go/types/expr.go
src/go/types/named.go
src/go/types/sizeof_test.go
src/internal/types/testdata/fixedbugs/issue52915.go
src/internal/types/testdata/fixedbugs/issue75918.go [new file with mode: 0644]
src/internal/types/testdata/fixedbugs/issue76478.go [new file with mode: 0644]