From d7a43e89124044f0f468e656ec17a3cc86916b33 Mon Sep 17 00:00:00 2001 From: Dan Scales Date: Wed, 25 Aug 2021 17:12:27 -0700 Subject: [PATCH] cmd/compile: support type C comparable 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 Trust: Robert Griesemer Run-TryBot: Dan Scales TryBot-Result: Go Bot Reviewed-by: Robert Griesemer --- src/cmd/compile/internal/typecheck/bexport.go | 3 +++ src/cmd/compile/internal/typecheck/iexport.go | 8 ++++++++ src/cmd/compile/internal/types/type.go | 2 ++ src/cmd/compile/internal/types/universe.go | 8 ++++++++ test/typeparam/issue47966.go | 9 +++++++++ test/typeparam/subdict.go | 4 +++- 6 files changed, 33 insertions(+), 1 deletion(-) create mode 100644 test/typeparam/issue47966.go diff --git a/src/cmd/compile/internal/typecheck/bexport.go b/src/cmd/compile/internal/typecheck/bexport.go index cc7f91f9372..352f7a96ad3 100644 --- a/src/cmd/compile/internal/typecheck/bexport.go +++ b/src/cmd/compile/internal/typecheck/bexport.go @@ -99,6 +99,9 @@ func predeclared() []*types.Type { // comparable types.ComparableType, + + // any + types.AnyType, } } return predecl diff --git a/src/cmd/compile/internal/typecheck/iexport.go b/src/cmd/compile/internal/typecheck/iexport.go index dbdf8eda358..89eab4df162 100644 --- a/src/cmd/compile/internal/typecheck/iexport.go +++ b/src/cmd/compile/internal/typecheck/iexport.go @@ -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() diff --git a/src/cmd/compile/internal/types/type.go b/src/cmd/compile/internal/types/type.go index f1fb93ad1be..60bb7b46fa0 100644 --- a/src/cmd/compile/internal/types/type.go +++ b/src/cmd/compile/internal/types/type.go @@ -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) diff --git a/src/cmd/compile/internal/types/universe.go b/src/cmd/compile/internal/types/universe.go index 1291b0e0fa3..8fa4b7cd20e 100644 --- a/src/cmd/compile/internal/types/universe.go +++ b/src/cmd/compile/internal/types/universe.go @@ -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 index 00000000000..f431f7fc74b --- /dev/null +++ b/test/typeparam/issue47966.go @@ -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 diff --git a/test/typeparam/subdict.go b/test/typeparam/subdict.go index b4e84baf8a7..c519b4f51c3 100644 --- a/test/typeparam/subdict.go +++ b/test/typeparam/subdict.go @@ -14,7 +14,9 @@ import ( "fmt" ) -type value[T comparable] struct { +type C comparable + +type value[T C] struct { val T }