]> Cypherpunks repositories - gostls13.git/commit
cmd/compile: support field access for typeparam with structural constraint
authorDan Scales <danscales@google.com>
Fri, 7 Jan 2022 00:51:10 +0000 (16:51 -0800)
committerDan Scales <danscales@google.com>
Tue, 18 Jan 2022 18:16:14 +0000 (18:16 +0000)
commitef4be98abd699c6f059dcac6dc6dfe5774cbf257
tree4a3803d85e00b298735735598db566a8aaa1c9e4
parentd54f6630703900948d9757487a1bffd87b7d36e2
cmd/compile: support field access for typeparam with structural constraint

In the compiler, we need to distinguish field and method access on a
type param. For field access, we avoid the dictionary access (to create
an interface bound) and just do the normal transformDot() (which will
create the field access on the shape type).

This field access works fine for non-pointer types, since the shape type
preserves the underlying type of all types in the shape. But we
generally merge all pointer types into a single shape, which means the
field will not be accessible via the shape type. So, we need to change
Shapify() so that a type which is a pointer type is mapped to its
underlying type, rather than being merged with other pointers.

Because we don't want to change the export format at this point in the
release, we need to compute StructuralType() directly in types1, rather
than relying on types2. That implementation is in types/type.go, along
with the helper specificTypes().

I enabled the compiler-related tests in issue50417.go, added an extra
test for unnamed pointer types, and added a bunch more tests for
interesting cases involving StructuralType(). I added a test
issue50417b.go similar to the original example, but also tests access to
an embedded field.

I also added a unit test in
cmd/compile/internal/types/structuraltype_test.go that tests a bunch of
unusual cases directly (some of which have no structural type).

Updates #50417

Change-Id: I77c55cbad98a2b95efbd4a02a026c07dfbb46caa
Reviewed-on: https://go-review.googlesource.com/c/go/+/376194
Reviewed-by: Keith Randall <khr@golang.org>
Reviewed-by: Robert Griesemer <gri@golang.org>
Trust: Dan Scales <danscales@google.com>
Run-TryBot: Dan Scales <danscales@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
src/cmd/compile/internal/noder/stencil.go
src/cmd/compile/internal/reflectdata/reflect.go
src/cmd/compile/internal/typecheck/crawler.go
src/cmd/compile/internal/typecheck/subr.go
src/cmd/compile/internal/types/structuraltype.go [new file with mode: 0644]
src/cmd/compile/internal/types/structuraltype_test.go [new file with mode: 0644]
src/go/internal/gcimporter/gcimporter_test.go
test/run.go
test/typeparam/issue50417.go
test/typeparam/issue50417b.go [new file with mode: 0644]