1
0
mirror of https://github.com/golang/go synced 2024-11-23 06:30:06 -07:00

[dev.typeparams] cmd/compile: add built-in name/type "comparable".

This allows exporting comparable type bounds, and importing back into
types2 for typechecking.

Fixes typeparam/mdempsky/8.go

Change-Id: I3ee12433df2ed68ac6ef4cad24be9fcdfaaca4e3
Reviewed-on: https://go-review.googlesource.com/c/go/+/333129
Run-TryBot: Dan Scales <danscales@google.com>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Robert Griesemer <gri@golang.org>
Trust: Dan Scales <danscales@google.com>
This commit is contained in:
Dan Scales 2021-07-05 20:58:56 -07:00
parent b4844c9f54
commit b614c05a15
7 changed files with 23 additions and 9 deletions

View File

@ -101,6 +101,8 @@ var predeclared = []types2.Type{
// error
types2.Universe.Lookup("error").Type(),
// comparable
types2.Universe.Lookup("comparable").Type(),
// untyped types
types2.Typ[types2.UntypedBool],

View File

@ -189,14 +189,6 @@ func (g *irgen) typ0(typ types2.Type) *types.Type {
// With Go 1.18, an embedded element can be any type, not
// just an interface.
if t := types2.AsInterface(e); t != nil {
if t.IsComparable() {
// Ignore predefined type 'comparable', since it
// doesn't resolve and it doesn't have any
// relevant methods.
continue
}
}
embeddeds[j] = types.NewField(src.NoXPos, nil, g.typ1(e))
j++
}

View File

@ -78,6 +78,8 @@ func predeclared() []*types.Type {
// error
types.ErrorType,
// comparable
types.ComparableType,
// untyped types
types.UntypedBool,

View File

@ -158,6 +158,15 @@ func InitUniverse() {
s.Def = n
types.CalcSize(types.ErrorType)
// comparable type (interface)
s = types.BuiltinPkg.Lookup("comparable")
n = ir.NewDeclNameAt(src.NoXPos, ir.OTYPE, s)
types.ComparableType = types.NewNamed(n)
types.ComparableType.SetUnderlying(makeComparableInterface())
n.SetType(types.ComparableType)
s.Def = n
types.CalcSize(types.ComparableType)
types.Types[types.TUNSAFEPTR] = defBasic(types.TUNSAFEPTR, ir.Pkgs.Unsafe, "Pointer")
// simple aliases
@ -338,6 +347,12 @@ func makeErrorInterface() *types.Type {
return types.NewInterface(types.NoPkg, []*types.Field{method})
}
func makeComparableInterface() *types.Type {
sig := types.NewSignature(types.NoPkg, fakeRecvField(), nil, nil, nil)
method := types.NewField(src.NoXPos, Lookup("=="), sig)
return types.NewInterface(types.NoPkg, []*types.Field{method})
}
// DeclareUniverse makes the universe block visible within the current package.
func DeclareUniverse() {
// Operationally, this is similar to a dot import of builtinpkg, except

View File

@ -123,6 +123,8 @@ var (
// Predeclared error interface type.
ErrorType *Type
// Predeclared comparable interface type.
ComparableType *Type
// Types to represent untyped string and boolean constants.
UntypedString = New(TSTRING)

View File

@ -104,6 +104,8 @@ var predeclared = []types.Type{
// error
types.Universe.Lookup("error").Type(),
// comparable
types.Universe.Lookup("comparable").Type(),
// untyped types
types.Typ[types.UntypedBool],

View File

@ -2210,7 +2210,6 @@ var g3Failures = setOf(
"typeparam/mdempsky/4.go",
"typeparam/mdempsky/5.go",
"typeparam/mdempsky/7.go",
"typeparam/mdempsky/8.go",
"typeparam/mdempsky/9.go",
"typeparam/mdempsky/11.go",
"typeparam/mdempsky/12.go",