Skip to content

Commit

Permalink
go/types, types2: implement alternative comparable semantics
Browse files Browse the repository at this point in the history
This is an experiment to see the impact of a potential spec change:
As an exception to the rule that constraint satisfaction is the same
as interface implementation, if the flag Config.AltComparableSemantics
is set, an ordinary (non-type parameter) interface satisfies the
comparable constraint. (In go/types, the flag is not exported to
avoid changing the API.)

Disabled by default. Test files can set the flag by adding

// -altComparableSemantics

as the first line in the file.

For golang#52509.

Change-Id: Ib491b086feb5563920eaddefcebdacb2c5b72d61
Reviewed-on: https://go-review.googlesource.com/c/go/+/444635
Reviewed-by: Robert Findley <rfindley@google.com>
Run-TryBot: Robert Griesemer <gri@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Reviewed-by: David Chase <drchase@google.com>
  • Loading branch information
griesemer authored and romaindoumenc committed Nov 3, 2022
1 parent 27cde35 commit 8c2f7cd
Show file tree
Hide file tree
Showing 11 changed files with 70 additions and 16 deletions.
6 changes: 5 additions & 1 deletion src/cmd/compile/internal/types2/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,10 @@ type Config struct {
// If DisableUnusedImportCheck is set, packages are not checked
// for unused imports.
DisableUnusedImportCheck bool

// If AltComparableSemantics is set, ordinary (non-type parameter)
// interfaces satisfy the comparable constraint.
AltComparableSemantics bool
}

func srcimporter_setUsesCgo(conf *Config) {
Expand Down Expand Up @@ -480,7 +484,7 @@ func Implements(V Type, T *Interface) bool {
if V.Underlying() == Typ[Invalid] {
return false
}
return (*Checker)(nil).implements(V, T, nil)
return (*Checker)(nil).implements(V, T, false, nil)
}

// Identical reports whether x and y are identical types.
Expand Down
1 change: 1 addition & 0 deletions src/cmd/compile/internal/types2/check_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ func testFiles(t *testing.T, filenames []string, colDelta uint, manual bool) {
flags := flag.NewFlagSet("", flag.PanicOnError)
flags.StringVar(&conf.GoVersion, "lang", "", "")
flags.BoolVar(&conf.FakeImportC, "fakeImportC", false, "")
flags.BoolVar(&conf.AltComparableSemantics, "altComparableSemantics", false, "")
if err := parseFlags(filenames[0], nil, flags); err != nil {
t.Fatal(err)
}
Expand Down
13 changes: 9 additions & 4 deletions src/cmd/compile/internal/types2/instantiate.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,19 +176,20 @@ func (check *Checker) verify(pos syntax.Pos, tparams []*TypeParam, targs []Type,
// the parameterized type.
bound := check.subst(pos, tpar.bound, smap, nil, ctxt)
var cause string
if !check.implements(targs[i], bound, &cause) {
if !check.implements(targs[i], bound, true, &cause) {
return i, errors.New(cause)
}
}
return -1, nil
}

// implements checks if V implements T. The receiver may be nil if implements
// is called through an exported API call such as AssignableTo.
// is called through an exported API call such as AssignableTo. If constraint
// is set, T is a type constraint.
//
// If the provided cause is non-nil, it may be set to an error string
// explaining why V does not implement T.
func (check *Checker) implements(V, T Type, cause *string) bool {
func (check *Checker) implements(V, T Type, constraint bool, cause *string) bool {
Vu := under(V)
Tu := under(T)
if Vu == Typ[Invalid] || Tu == Typ[Invalid] {
Expand Down Expand Up @@ -245,7 +246,11 @@ func (check *Checker) implements(V, T Type, cause *string) bool {
// Only check comparability if we don't have a more specific error.
checkComparability := func() bool {
// If T is comparable, V must be comparable.
if Ti.IsComparable() && !comparable(V, false, nil, nil) {
// For constraint satisfaction, use dynamic comparability for the
// alternative comparable semantics such that ordinary, non-type
// parameter interfaces implement comparable.
dynamic := constraint && check != nil && check.conf.AltComparableSemantics
if Ti.IsComparable() && !comparable(V, dynamic, nil, nil) {
if cause != nil {
*cause = check.sprintf("%s does not implement comparable", V)
}
Expand Down
2 changes: 1 addition & 1 deletion src/cmd/compile/internal/types2/lookup.go
Original file line number Diff line number Diff line change
Expand Up @@ -472,7 +472,7 @@ func (check *Checker) newAssertableTo(V *Interface, T Type) bool {
if IsInterface(T) {
return true
}
return check.implements(T, V, nil)
return check.implements(T, V, false, nil)
}

// deref dereferences typ if it is a *Pointer and returns its base and true.
Expand Down
4 changes: 2 additions & 2 deletions src/cmd/compile/internal/types2/operand.go
Original file line number Diff line number Diff line change
Expand Up @@ -289,15 +289,15 @@ func (x *operand) assignableTo(check *Checker, T Type, cause *string) (bool, Cod
// T is an interface type and x implements T and T is not a type parameter.
// Also handle the case where T is a pointer to an interface.
if _, ok := Tu.(*Interface); ok && Tp == nil || isInterfacePtr(Tu) {
if !check.implements(V, T, cause) {
if !check.implements(V, T, false, cause) {
return false, InvalidIfaceAssign
}
return true, 0
}

// If V is an interface, check if a missing type assertion is the problem.
if Vi, _ := Vu.(*Interface); Vi != nil && Vp == nil {
if check.implements(T, V, nil) {
if check.implements(T, V, false, nil) {
// T implements V, so give hint about type assertion.
if cause != nil {
*cause = "need type assertion"
Expand Down
6 changes: 5 additions & 1 deletion src/go/types/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,10 @@ type Config struct {
// If DisableUnusedImportCheck is set, packages are not checked
// for unused imports.
DisableUnusedImportCheck bool

// If altComparableSemantics is set, ordinary (non-type parameter)
// interfaces satisfy the comparable constraint.
altComparableSemantics bool
}

func srcimporter_setUsesCgo(conf *Config) {
Expand Down Expand Up @@ -463,7 +467,7 @@ func Implements(V Type, T *Interface) bool {
if V.Underlying() == Typ[Invalid] {
return false
}
return (*Checker)(nil).implements(V, T, nil)
return (*Checker)(nil).implements(V, T, false, nil)
}

// Identical reports whether x and y are identical types.
Expand Down
7 changes: 7 additions & 0 deletions src/go/types/check_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,7 @@ func testFiles(t *testing.T, sizes Sizes, filenames []string, srcs [][]byte, man
flags := flag.NewFlagSet("", flag.PanicOnError)
flags.StringVar(&conf.GoVersion, "lang", "", "")
flags.BoolVar(&conf.FakeImportC, "fakeImportC", false, "")
flags.BoolVar(addrAltComparableSemantics(&conf), "altComparableSemantics", false, "")
if err := parseFlags(filenames[0], srcs[0], flags); err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -293,6 +294,12 @@ func readCode(err Error) int {
return int(v.FieldByName("go116code").Int())
}

// addrAltComparableSemantics(conf) returns &conf.altComparableSemantics (unexported field).
func addrAltComparableSemantics(conf *Config) *bool {
v := reflect.Indirect(reflect.ValueOf(conf))
return (*bool)(v.FieldByName("altComparableSemantics").Addr().UnsafePointer())
}

// TestManual is for manual testing of a package - either provided
// as a list of filenames belonging to the package, or a directory
// name containing the package files - after the test arguments
Expand Down
13 changes: 9 additions & 4 deletions src/go/types/instantiate.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,19 +176,20 @@ func (check *Checker) verify(pos token.Pos, tparams []*TypeParam, targs []Type,
// the parameterized type.
bound := check.subst(pos, tpar.bound, smap, nil, ctxt)
var cause string
if !check.implements(targs[i], bound, &cause) {
if !check.implements(targs[i], bound, true, &cause) {
return i, errors.New(cause)
}
}
return -1, nil
}

// implements checks if V implements T. The receiver may be nil if implements
// is called through an exported API call such as AssignableTo.
// is called through an exported API call such as AssignableTo. If constraint
// is set, T is a type constraint.
//
// If the provided cause is non-nil, it may be set to an error string
// explaining why V does not implement T.
func (check *Checker) implements(V, T Type, cause *string) bool {
func (check *Checker) implements(V, T Type, constraint bool, cause *string) bool {
Vu := under(V)
Tu := under(T)
if Vu == Typ[Invalid] || Tu == Typ[Invalid] {
Expand Down Expand Up @@ -245,7 +246,11 @@ func (check *Checker) implements(V, T Type, cause *string) bool {
// Only check comparability if we don't have a more specific error.
checkComparability := func() bool {
// If T is comparable, V must be comparable.
if Ti.IsComparable() && !comparable(V, false, nil, nil) {
// For constraint satisfaction, use dynamic comparability for the
// alternative comparable semantics such that ordinary, non-type
// parameter interfaces implement comparable.
dynamic := constraint && check != nil && check.conf.altComparableSemantics
if Ti.IsComparable() && !comparable(V, dynamic, nil, nil) {
if cause != nil {
*cause = check.sprintf("%s does not implement comparable", V)
}
Expand Down
2 changes: 1 addition & 1 deletion src/go/types/lookup.go
Original file line number Diff line number Diff line change
Expand Up @@ -471,7 +471,7 @@ func (check *Checker) newAssertableTo(V *Interface, T Type) bool {
if IsInterface(T) {
return true
}
return check.implements(T, V, nil)
return check.implements(T, V, false, nil)
}

// deref dereferences typ if it is a *Pointer and returns its base and true.
Expand Down
4 changes: 2 additions & 2 deletions src/go/types/operand.go
Original file line number Diff line number Diff line change
Expand Up @@ -278,15 +278,15 @@ func (x *operand) assignableTo(check *Checker, T Type, cause *string) (bool, Cod
// T is an interface type and x implements T and T is not a type parameter.
// Also handle the case where T is a pointer to an interface.
if _, ok := Tu.(*Interface); ok && Tp == nil || isInterfacePtr(Tu) {
if !check.implements(V, T, cause) {
if !check.implements(V, T, false, cause) {
return false, InvalidIfaceAssign
}
return true, 0
}

// If V is an interface, check if a missing type assertion is the problem.
if Vi, _ := Vu.(*Interface); Vi != nil && Vp == nil {
if check.implements(T, V, nil) {
if check.implements(T, V, false, nil) {
// T implements V, so give hint about type assertion.
if cause != nil {
*cause = "need type assertion"
Expand Down
28 changes: 28 additions & 0 deletions src/internal/types/testdata/spec/comparable.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// -altComparableSemantics

// Copyright 2022 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

func f1[_ comparable]() {}
func f2[_ interface{ comparable }]() {}

type T interface{ m() }

func _[P comparable, Q ~int, R any]() {
_ = f1[int]
_ = f1[T /* T does implement comparable */]
_ = f1[any /* any does implement comparable */]
_ = f1[P]
_ = f1[Q]
_ = f1[R /* ERROR R does not implement comparable */]

_ = f2[int]
_ = f2[T /* T does implement comparable */]
_ = f2[any /* any does implement comparable */]
_ = f2[P]
_ = f2[Q]
_ = f2[R /* ERROR R does not implement comparable */]
}

0 comments on commit 8c2f7cd

Please sign in to comment.