mirror of
https://github.com/golang/go
synced 2024-10-04 15:09:59 +00:00
cmd/compile: restore test/nested.go test cases
When handling a type declaration like: ``` type B A ``` unified IR has been writing out that B's underlying type is A, rather than the underlying type of A. This is a bit awkward to implement and adds complexity to importers, who need to handle resolving the underlying type themselves. But it was necessary to handle when A was declared like: ``` //go:notinheap type A int ``` Because we expected A's not-in-heap'ness to be conferred to B, which required knowing that A was on the path from B to its actual underlying type int. However, since #46731 was accepted, we no longer need to support this case. Instead we can write out B's actual underlying type. One stumbling point though is the existing code for exporting interfaces doesn't work for the underlying type of `comparable`, which is now needed to implement `type C comparable`. As a bit of a hack, we we instead export its underlying type as `interface{ comparable }`. Fixes #54512. Change-Id: I0fb892068d656f1e87bb8ef97da27756051126d5 Reviewed-on: https://go-review.googlesource.com/c/go/+/424854 Run-TryBot: Matthew Dempsky <mdempsky@google.com> Reviewed-by: Cuong Manh Le <cuong.manhle.vn@gmail.com> TryBot-Result: Gopher Robot <gobot@golang.org> Reviewed-by: David Chase <drchase@google.com>
This commit is contained in:
parent
c94633d2f8
commit
72a76ca1f9
|
@ -432,8 +432,9 @@ func (pw *pkgWriter) pkgIdx(pkg *types2.Package) pkgbits.Index {
|
||||||
// @@@ Types
|
// @@@ Types
|
||||||
|
|
||||||
var (
|
var (
|
||||||
anyTypeName = types2.Universe.Lookup("any").(*types2.TypeName)
|
anyTypeName = types2.Universe.Lookup("any").(*types2.TypeName)
|
||||||
runeTypeName = types2.Universe.Lookup("rune").(*types2.TypeName)
|
comparableTypeName = types2.Universe.Lookup("comparable").(*types2.TypeName)
|
||||||
|
runeTypeName = types2.Universe.Lookup("rune").(*types2.TypeName)
|
||||||
)
|
)
|
||||||
|
|
||||||
// typ writes a use of the given type into the bitstream.
|
// typ writes a use of the given type into the bitstream.
|
||||||
|
@ -485,7 +486,7 @@ func (pw *pkgWriter) typIdx(typ types2.Type, dict *writerDict) typeInfo {
|
||||||
w.Len(int(kind))
|
w.Len(int(kind))
|
||||||
|
|
||||||
default:
|
default:
|
||||||
// Handle "byte" and "rune" as references to their TypeName.
|
// Handle "byte" and "rune" as references to their TypeNames.
|
||||||
obj := types2.Universe.Lookup(typ.Name())
|
obj := types2.Universe.Lookup(typ.Name())
|
||||||
assert(obj.Type() == typ)
|
assert(obj.Type() == typ)
|
||||||
|
|
||||||
|
@ -543,6 +544,7 @@ func (pw *pkgWriter) typIdx(typ types2.Type, dict *writerDict) typeInfo {
|
||||||
w.structType(typ)
|
w.structType(typ)
|
||||||
|
|
||||||
case *types2.Interface:
|
case *types2.Interface:
|
||||||
|
// Handle "any" as reference to its TypeName.
|
||||||
if typ == anyTypeName.Type() {
|
if typ == anyTypeName.Type() {
|
||||||
w.Code(pkgbits.TypeNamed)
|
w.Code(pkgbits.TypeNamed)
|
||||||
w.obj(anyTypeName, nil)
|
w.obj(anyTypeName, nil)
|
||||||
|
@ -590,6 +592,23 @@ func (w *writer) unionType(typ *types2.Union) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (w *writer) interfaceType(typ *types2.Interface) {
|
func (w *writer) interfaceType(typ *types2.Interface) {
|
||||||
|
// If typ has no embedded types but it's not a basic interface, then
|
||||||
|
// the natural description we write out below will fail to
|
||||||
|
// reconstruct it.
|
||||||
|
if typ.NumEmbeddeds() == 0 && !typ.IsMethodSet() {
|
||||||
|
// Currently, this can only happen for the underlying Interface of
|
||||||
|
// "comparable", which is needed to handle type declarations like
|
||||||
|
// "type C comparable".
|
||||||
|
assert(typ == comparableTypeName.Type().(*types2.Named).Underlying())
|
||||||
|
|
||||||
|
// Export as "interface{ comparable }".
|
||||||
|
w.Len(0) // NumExplicitMethods
|
||||||
|
w.Len(1) // NumEmbeddeds
|
||||||
|
w.Bool(false) // IsImplicit
|
||||||
|
w.typ(comparableTypeName.Type()) // EmbeddedType(0)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
w.Len(typ.NumExplicitMethods())
|
w.Len(typ.NumExplicitMethods())
|
||||||
w.Len(typ.NumEmbeddeds())
|
w.Len(typ.NumEmbeddeds())
|
||||||
|
|
||||||
|
@ -775,9 +794,6 @@ func (w *writer) doObj(wext *writer, obj types2.Object) pkgbits.CodeObj {
|
||||||
return pkgbits.ObjFunc
|
return pkgbits.ObjFunc
|
||||||
|
|
||||||
case *types2.TypeName:
|
case *types2.TypeName:
|
||||||
decl, ok := w.p.typDecls[obj]
|
|
||||||
assert(ok)
|
|
||||||
|
|
||||||
if obj.IsAlias() {
|
if obj.IsAlias() {
|
||||||
w.pos(obj)
|
w.pos(obj)
|
||||||
w.typ(obj.Type())
|
w.typ(obj.Type())
|
||||||
|
@ -790,7 +806,7 @@ func (w *writer) doObj(wext *writer, obj types2.Object) pkgbits.CodeObj {
|
||||||
w.pos(obj)
|
w.pos(obj)
|
||||||
w.typeParamNames(named.TypeParams())
|
w.typeParamNames(named.TypeParams())
|
||||||
wext.typeExt(obj)
|
wext.typeExt(obj)
|
||||||
w.typExpr(decl.Type)
|
w.typ(named.Underlying())
|
||||||
|
|
||||||
w.Len(named.NumMethods())
|
w.Len(named.NumMethods())
|
||||||
for i := 0; i < named.NumMethods(); i++ {
|
for i := 0; i < named.NumMethods(); i++ {
|
||||||
|
@ -807,16 +823,6 @@ func (w *writer) doObj(wext *writer, obj types2.Object) pkgbits.CodeObj {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// typExpr writes the type represented by the given expression.
|
|
||||||
//
|
|
||||||
// TODO(mdempsky): Document how this differs from exprType.
|
|
||||||
func (w *writer) typExpr(expr syntax.Expr) {
|
|
||||||
tv, ok := w.p.info.Types[expr]
|
|
||||||
assert(ok)
|
|
||||||
assert(tv.IsType())
|
|
||||||
w.typ(tv.Type)
|
|
||||||
}
|
|
||||||
|
|
||||||
// objDict writes the dictionary needed for reading the given object.
|
// objDict writes the dictionary needed for reading the given object.
|
||||||
func (w *writer) objDict(obj types2.Object, dict *writerDict) {
|
func (w *writer) objDict(obj types2.Object, dict *writerDict) {
|
||||||
// TODO(mdempsky): Split objDict into multiple entries? reader.go
|
// TODO(mdempsky): Split objDict into multiple entries? reader.go
|
||||||
|
|
|
@ -104,27 +104,11 @@ func main() {
|
||||||
F[V]()
|
F[V]()
|
||||||
F[W]()
|
F[W]()
|
||||||
|
|
||||||
// TODO(go.dev/issue/54512): Restore these tests. They currently
|
type X[A any] U[X[A]]
|
||||||
// cause problems for shaping with unified IR.
|
|
||||||
//
|
F[X[int]]()
|
||||||
// For example, instantiating X[int] requires instantiating shape
|
F[X[Int]]()
|
||||||
// type X[shapify(int)] == X[go.shape.int]. In turn, this requires
|
F[X[GlobalInt]]()
|
||||||
// instantiating U[shapify(X[go.shape.int])]. But we're still in the
|
|
||||||
// process of constructing X[go.shape.int], so we don't yet know its
|
|
||||||
// underlying type.
|
|
||||||
//
|
|
||||||
// Notably, this is a consequence of unified IR writing out type
|
|
||||||
// declarations with a reference to the full RHS expression (i.e.,
|
|
||||||
// U[X[A]]) rather than its underlying type (i.e., int), which is
|
|
||||||
// necessary to support //go:notinheap. Once go.dev/issue/46731 is
|
|
||||||
// implemented and unified IR is updated, I expect this will just
|
|
||||||
// work.
|
|
||||||
//
|
|
||||||
// type X[A any] U[X[A]]
|
|
||||||
//
|
|
||||||
// F[X[int]]()
|
|
||||||
// F[X[Int]]()
|
|
||||||
// F[X[GlobalInt]]()
|
|
||||||
|
|
||||||
for j, tj := range tests {
|
for j, tj := range tests {
|
||||||
for i, ti := range tests[:j+1] {
|
for i, ti := range tests[:j+1] {
|
||||||
|
|
Loading…
Reference in a new issue