From 8fd2875c3e9455df722dd3c930332591eebbb3c2 Mon Sep 17 00:00:00 2001 From: Robert Griesemer Date: Thu, 1 Dec 2022 09:39:45 -0800 Subject: go/types, types2: make the new comparable semantics the default Ordinary interface types now satisfy comparable constraints. This is a fully backward-compatible change: it simply permits additional code to be valid that wasn't valid before. This change makes the new comparable semantics the default behavior, depending on the Go -lang version. It also renames the flag types2.Config.AltComparableSemantics to types2.Config.OldComparableSemantics and inverts its meaning (or types.Config.oldComparableSemantics respectively). Add new predicate Satisfies (matching the predicate Implements but for constraint satisfaction), per the proposal description. Adjust some existing tests by setting -oldComparableSemantics and add some new tests that verify version-dependent behavior. The compiler flag -oldcomparable may be used to temporarily switch back to the Go 1.18/1.19 behavior should this change cause problems, or to identify that a problem is unrelated to this change. The flag will be removed for Go 1.21. For #52509. For #56548. For #57011. Change-Id: I8b3b3d9d492fc24b0693567055f0053ccb5aeb42 Reviewed-on: https://go-review.googlesource.com/c/go/+/454575 TryBot-Result: Gopher Robot Reviewed-by: Robert Griesemer Run-TryBot: Robert Griesemer Reviewed-by: Robert Findley --- src/internal/types/testdata/check/issues1.go | 2 ++ .../types/testdata/fixedbugs/issue50646.go | 2 ++ .../types/testdata/fixedbugs/issue51257.go | 2 ++ src/internal/types/testdata/spec/comparable.go | 2 -- src/internal/types/testdata/spec/comparable1.19.go | 28 ++++++++++++++++++++++ src/internal/types/testdata/spec/oldcomparable.go | 28 ++++++++++++++++++++++ 6 files changed, 62 insertions(+), 2 deletions(-) create mode 100644 src/internal/types/testdata/spec/comparable1.19.go create mode 100644 src/internal/types/testdata/spec/oldcomparable.go (limited to 'src/internal') diff --git a/src/internal/types/testdata/check/issues1.go b/src/internal/types/testdata/check/issues1.go index b986023cc1..02ad822e0f 100644 --- a/src/internal/types/testdata/check/issues1.go +++ b/src/internal/types/testdata/check/issues1.go @@ -1,3 +1,5 @@ +// -oldComparableSemantics + // Copyright 2020 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. diff --git a/src/internal/types/testdata/fixedbugs/issue50646.go b/src/internal/types/testdata/fixedbugs/issue50646.go index 3bdba1113a..bc53700704 100644 --- a/src/internal/types/testdata/fixedbugs/issue50646.go +++ b/src/internal/types/testdata/fixedbugs/issue50646.go @@ -1,3 +1,5 @@ +// -oldComparableSemantics + // 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. diff --git a/src/internal/types/testdata/fixedbugs/issue51257.go b/src/internal/types/testdata/fixedbugs/issue51257.go index 8a3eb3278d..4730c98e2f 100644 --- a/src/internal/types/testdata/fixedbugs/issue51257.go +++ b/src/internal/types/testdata/fixedbugs/issue51257.go @@ -1,3 +1,5 @@ +// -oldComparableSemantics + // 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. diff --git a/src/internal/types/testdata/spec/comparable.go b/src/internal/types/testdata/spec/comparable.go index 8dbbb4e337..03c8471393 100644 --- a/src/internal/types/testdata/spec/comparable.go +++ b/src/internal/types/testdata/spec/comparable.go @@ -1,5 +1,3 @@ -// -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. diff --git a/src/internal/types/testdata/spec/comparable1.19.go b/src/internal/types/testdata/spec/comparable1.19.go new file mode 100644 index 0000000000..c9c87e4f77 --- /dev/null +++ b/src/internal/types/testdata/spec/comparable1.19.go @@ -0,0 +1,28 @@ +// -lang=go1.19 + +// 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 /* ERROR T to implement comparable requires go1\.20 or later */] + _ = f1[any /* ERROR any to implement comparable requires go1\.20 or later */] + _ = f1[P] + _ = f1[Q] + _ = f1[R /* ERROR R does not implement comparable */] + + _ = f2[int] + _ = f2[T /* ERROR T to implement comparable requires go1\.20 or later */] + _ = f2[any /* ERROR any to implement comparable requires go1\.20 or later */] + _ = f2[P] + _ = f2[Q] + _ = f2[R /* ERROR R does not implement comparable */] +} diff --git a/src/internal/types/testdata/spec/oldcomparable.go b/src/internal/types/testdata/spec/oldcomparable.go new file mode 100644 index 0000000000..9f6cf749f0 --- /dev/null +++ b/src/internal/types/testdata/spec/oldcomparable.go @@ -0,0 +1,28 @@ +// -oldComparableSemantics + +// 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 /* ERROR T does not implement comparable */] + _ = f1[any /* ERROR any does not implement comparable */] + _ = f1[P] + _ = f1[Q] + _ = f1[R /* ERROR R does not implement comparable */] + + _ = f2[int] + _ = f2[T /* ERROR T does not implement comparable */] + _ = f2[any /* ERROR any does not implement comparable */] + _ = f2[P] + _ = f2[Q] + _ = f2[R /* ERROR R does not implement comparable */] +} -- cgit v1.3