]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/compile: support type C comparable
authorDan Scales <danscales@google.com>
Thu, 26 Aug 2021 00:12:27 +0000 (17:12 -0700)
committerDan Scales <danscales@google.com>
Sat, 28 Aug 2021 16:00:51 +0000 (16:00 +0000)
Support 'type C comparable' properly by using the same logic as for
'type T error', since ErrorType and ComparableType are entirely
analogous.

Added support for 'any' type as well, as requested by Robert. (For the
future - we can't currently have 'any' anywhere other than in a
constraint.)

Fixes #47966

Change-Id: I68bd284ced9a8bfca7d2339cd576f3cb909b1b83
Reviewed-on: https://go-review.googlesource.com/c/go/+/345174
Trust: Dan Scales <danscales@google.com>
Trust: Robert Griesemer <gri@golang.org>
Run-TryBot: Dan Scales <danscales@google.com>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Robert Griesemer <gri@golang.org>
src/cmd/compile/internal/typecheck/bexport.go
src/cmd/compile/internal/typecheck/iexport.go
src/cmd/compile/internal/types/type.go
src/cmd/compile/internal/types/universe.go
test/typeparam/issue47966.go [new file with mode: 0644]
test/typeparam/subdict.go

index cc7f91f9372af4665131f801bf459cde47dd0aac..352f7a96ad3eac4ec468bfb6d4baf4e276a2920b 100644 (file)
@@ -99,6 +99,9 @@ func predeclared() []*types.Type {
 
                        // comparable
                        types.ComparableType,
+
+                       // any
+                       types.AnyType,
                }
        }
        return predecl
index dbdf8eda358e9888ac2c82ad4c9055bc6e4fe8ce..89eab4df1625c1d40fc71f90ecae10810c6e1abe 100644 (file)
@@ -566,6 +566,14 @@ func (p *iexporter) doDecl(n *ir.Name) {
                        // for predeclared objects).
                        underlying = types.ErrorType
                }
+               if underlying == types.ComparableType.Underlying() {
+                       // Do same for ComparableType as for ErrorType.
+                       underlying = types.ComparableType
+               }
+               if base.Flag.G > 0 && underlying == types.AnyType.Underlying() {
+                       // Do same for AnyType as for ErrorType.
+                       underlying = types.AnyType
+               }
                w.typ(underlying)
 
                t := n.Type()
index f1fb93ad1be6d5f66df38142185d5130b2e05b18..60bb7b46fa0e50806ba4e160af8ab663bf1e18da 100644 (file)
@@ -119,6 +119,8 @@ var (
        ErrorType *Type
        // Predeclared comparable interface type.
        ComparableType *Type
+       // Predeclared any interface type.
+       AnyType *Type
 
        // Types to represent untyped string and boolean constants.
        UntypedString = newType(TSTRING)
index 1291b0e0fa3499634ac0e907b180188b73f04eea..8fa4b7cd20eebc3e36b55b50f1ae475f9e149783 100644 (file)
@@ -107,6 +107,14 @@ func InitTypes(defTypeName func(sym *Sym, typ *Type) Object) {
        ComparableType.SetUnderlying(makeComparableInterface())
        ResumeCheckSize()
 
+       // any type (interface)
+       if base.Flag.G > 0 {
+               DeferCheckSize()
+               AnyType = defBasic(TFORW, BuiltinPkg, "any")
+               AnyType.SetUnderlying(NewInterface(NoPkg, []*Field{}))
+               ResumeCheckSize()
+       }
+
        Types[TUNSAFEPTR] = defBasic(TUNSAFEPTR, UnsafePkg, "Pointer")
 
        Types[TBLANK] = newType(TBLANK)
diff --git a/test/typeparam/issue47966.go b/test/typeparam/issue47966.go
new file mode 100644 (file)
index 0000000..f431f7f
--- /dev/null
@@ -0,0 +1,9 @@
+// compile -G=3
+
+// Copyright 2021 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+package p
+
+type C comparable
index b4e84baf8a7fb3e4a6b57b6fce273bad22ef8eb4..c519b4f51c3d701d17df5eca36060cdbe2737b64 100644 (file)
@@ -14,7 +14,9 @@ import (
        "fmt"
 )
 
-type value[T comparable] struct {
+type C comparable
+
+type value[T C] struct {
        val T
 }