cmd/compile/internal/types: change structuraltype_test.go to external test

This test can run against package types's exported API just fine.

Change-Id: I74184eedc9ca9159b05d893c5f7c615c3dd1884d
Reviewed-on: https://go-review.googlesource.com/c/go/+/405655
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:
Matthew Dempsky 2022-05-10 16:26:58 -07:00
parent e99275241c
commit 53db708a1d

View File

@ -5,9 +5,11 @@
// Test that StructuralType() calculates the correct value of structural type for // Test that StructuralType() calculates the correct value of structural type for
// unusual cases. // unusual cases.
package types package types_test
import ( import (
"cmd/compile/internal/ir"
. "cmd/compile/internal/types"
"cmd/internal/src" "cmd/internal/src"
"testing" "testing"
) )
@ -25,32 +27,33 @@ func TestStructuralType(t *testing.T) {
RegSize = 8 RegSize = 8
MaxWidth = 1 << 50 MaxWidth = 1 << 50
InitTypes(func(sym *Sym, typ *Type) Object {
obj := ir.NewDeclNameAt(src.NoXPos, ir.OTYPE, sym)
obj.SetType(typ)
sym.Def = obj
return obj
})
// type intType = int // type intType = int
intType := newType(TINT) intType := Types[TINT]
// type structf = struct { f int } // type structf = struct { f int }
structf := NewStruct(nil, []*Field{ structf := NewStruct(nil, []*Field{
NewField(src.NoXPos, LocalPkg.Lookup("f"), intType), NewField(src.NoXPos, LocalPkg.Lookup("f"), intType),
}) })
// type Sf structf defNamed := func(name string, underlying *Type) *Type {
Sf := newType(TFORW) sym := LocalPkg.Lookup(name)
Sf.sym = LocalPkg.Lookup("Sf") obj := ir.NewDeclNameAt(src.NoXPos, ir.OTYPE, sym)
Sf.SetUnderlying(structf) typ := NewNamed(obj)
typ.SetUnderlying(underlying)
return typ
}
// type A int Sf := defNamed("Sf", structf) // type Sf structf
A := newType(TFORW) A := defNamed("A", intType) // type A int
A.sym = LocalPkg.Lookup("A") B := defNamed("B", intType) // type B int
A.SetUnderlying(intType)
// type B int any := AnyType
B := newType(TFORW)
B.sym = LocalPkg.Lookup("B")
B.SetUnderlying(intType)
emptyInterface := NewInterface(BuiltinPkg, []*Field{}, false)
any := newType(TFORW)
any.sym = LocalPkg.Lookup("any")
any.SetUnderlying(emptyInterface)
// The tests marked NONE have no structural type; all the others have a // The tests marked NONE have no structural type; all the others have a
// structural type of structf - "struct { f int }" // structural type of structf - "struct { f int }"
@ -71,7 +74,7 @@ func TestStructuralType(t *testing.T) {
structf, structf,
}, },
{ {
// interface { any | Sf } // interface { any; Sf }
embed(any, Sf), embed(any, Sf),
structf, structf,
}, },
@ -118,10 +121,10 @@ func TestStructuralType(t *testing.T) {
structf, structf,
}, },
} }
for _, tst := range tests { for i, tst := range tests {
if got, want := tst.typ.StructuralType(), tst.structuralType; got != want { if got, want := tst.typ.StructuralType(), tst.structuralType; got != want {
t.Errorf("StructuralType(%v) = %v, wanted %v", t.Errorf("#%v: StructuralType(%v) = %v, wanted %v",
tst.typ, got, want) i, tst.typ, got, want)
} }
} }
} }