constraints: remove package

It has moved to golang.org/x/exp/constraints. Perhaps it will move
back to the standard library in a future release.

For golang/go#45458
Fixes golang/go#50792

Change-Id: I93aa251a7afe7b329a3d3faadc0c5d6388b1f0e9
Reviewed-on: https://go-review.googlesource.com/c/go/+/382460
Trust: Ian Lance Taylor <iant@golang.org>
Run-TryBot: Ian Lance Taylor <iant@golang.org>
Reviewed-by: Robert Griesemer <gri@golang.org>
TryBot-Result: Gopher Robot <gobot@golang.org>
This commit is contained in:
Ian Lance Taylor 2022-02-02 13:22:18 -08:00
parent 8384fe86a5
commit 070951c5dc
11 changed files with 28 additions and 203 deletions

View file

@ -1,12 +1,6 @@
pkg bufio, method (*Writer) AvailableBuffer() []uint8
pkg bufio, method (ReadWriter) AvailableBuffer() []uint8
pkg bytes, func Cut([]uint8, []uint8) ([]uint8, []uint8, bool)
pkg constraints, type Complex interface {}
pkg constraints, type Float interface {}
pkg constraints, type Integer interface {}
pkg constraints, type Ordered interface {}
pkg constraints, type Signed interface {}
pkg constraints, type Unsigned interface {}
pkg crypto/tls, method (*Conn) NetConn() net.Conn
pkg debug/buildinfo, func Read(io.ReaderAt) (*debug.BuildInfo, error)
pkg debug/buildinfo, func ReadFile(string) (*debug.BuildInfo, error)

View file

@ -467,14 +467,6 @@ Do not send CLs removing the interior tags from such phrases.
<h2 id="library">Core library</h2>
<h3 id="constraints">New <code>constraints</code> package</h3>
<p><!-- CL 349709 -->
The new <a href="/pkg/constraints/"><code>constraints</code></a> package
defines a set of useful constraints that can be used with type parameters of
generic functions.
</p>
<h3 id="debug/buildinfo">New <code>debug/buildinfo</code> package</h3>
<p><!-- golang.org/issue/39301 -->

View file

@ -8,8 +8,6 @@
package go1_17
import "constraints"
type T[P /* ERROR type parameters require go1\.18 or later */ any /* ERROR undeclared name: any \(requires version go1\.18 or later\) */ ] struct{}
// for init (and main, but we're not in package main) we should only get one error
@ -57,5 +55,3 @@ type (
_ = C1
_ = C2
)
type Ordered constraints /* ERROR using type constraint constraints\.Ordered requires go1\.18 or later */ .Ordered

View file

@ -4,8 +4,11 @@
package p
import "constraints"
type Integer interface {
~int | ~int8 | ~int16 | ~int32 | ~int64 |
~uint | ~uint8 | ~uint16 | ~uint32 | ~uint64 | ~uintptr
}
func shl[I constraints.Integer](n int) I {
func shl[I Integer](n int) I {
return 1 << n
}

View file

@ -1,50 +0,0 @@
// 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 constraints defines a set of useful constraints to be used
// with type parameters.
package constraints
// Signed is a constraint that permits any signed integer type.
// If future releases of Go add new predeclared signed integer types,
// this constraint will be modified to include them.
type Signed interface {
~int | ~int8 | ~int16 | ~int32 | ~int64
}
// Unsigned is a constraint that permits any unsigned integer type.
// If future releases of Go add new predeclared unsigned integer types,
// this constraint will be modified to include them.
type Unsigned interface {
~uint | ~uint8 | ~uint16 | ~uint32 | ~uint64 | ~uintptr
}
// Integer is a constraint that permits any integer type.
// If future releases of Go add new predeclared integer types,
// this constraint will be modified to include them.
type Integer interface {
Signed | Unsigned
}
// Float is a constraint that permits any floating-point type.
// If future releases of Go add new predeclared floating-point types,
// this constraint will be modified to include them.
type Float interface {
~float32 | ~float64
}
// Complex is a constraint that permits any complex numeric type.
// If future releases of Go add new predeclared complex numeric types,
// this constraint will be modified to include them.
type Complex interface {
~complex64 | ~complex128
}
// Ordered is a constraint that permits any ordered type: any type
// that supports the operators < <= >= >.
// If future releases of Go add new ordered types,
// this constraint will be modified to include them.
type Ordered interface {
Integer | Float | ~string
}

View file

@ -1,117 +0,0 @@
// 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 constraints
import (
"bytes"
"fmt"
"internal/testenv"
"os"
"os/exec"
"path/filepath"
"testing"
)
type (
testSigned[T Signed] struct{ f T }
testUnsigned[T Unsigned] struct{ f T }
testInteger[T Integer] struct{ f T }
testFloat[T Float] struct{ f T }
testComplex[T Complex] struct{ f T }
testOrdered[T Ordered] struct{ f T }
)
// TestTypes passes if it compiles.
type TestTypes struct {
_ testSigned[int]
_ testSigned[int64]
_ testUnsigned[uint]
_ testUnsigned[uintptr]
_ testInteger[int8]
_ testInteger[uint8]
_ testInteger[uintptr]
_ testFloat[float32]
_ testComplex[complex64]
_ testOrdered[int]
_ testOrdered[float64]
_ testOrdered[string]
}
var prolog = []byte(`
package constrainttest
import "constraints"
type (
testSigned[T constraints.Signed] struct{ f T }
testUnsigned[T constraints.Unsigned] struct{ f T }
testInteger[T constraints.Integer] struct{ f T }
testFloat[T constraints.Float] struct{ f T }
testComplex[T constraints.Complex] struct{ f T }
testOrdered[T constraints.Ordered] struct{ f T }
)
`)
func TestFailure(t *testing.T) {
testenv.MustHaveGoBuild(t)
gocmd := testenv.GoToolPath(t)
tmpdir := t.TempDir()
if err := os.WriteFile(filepath.Join(tmpdir, "go.mod"), []byte("module constraintest"), 0666); err != nil {
t.Fatal(err)
}
// Test for types that should not satisfy a constraint.
// For each pair of constraint and type, write a Go file
// var V constraint[type]
// For example,
// var V testSigned[uint]
// This should not compile, as testSigned (above) uses
// constraints.Signed, and uint does not satisfy that constraint.
// Therefore, the build of that code should fail.
for i, test := range []struct {
constraint, typ string
}{
{"testSigned", "uint"},
{"testUnsigned", "int"},
{"testInteger", "float32"},
{"testFloat", "int8"},
{"testComplex", "float64"},
{"testOrdered", "bool"},
} {
i := i
test := test
t.Run(fmt.Sprintf("%s %d", test.constraint, i), func(t *testing.T) {
t.Parallel()
name := fmt.Sprintf("go%d.go", i)
f, err := os.Create(filepath.Join(tmpdir, name))
if err != nil {
t.Fatal(err)
}
if _, err := f.Write(prolog); err != nil {
t.Fatal(err)
}
if _, err := fmt.Fprintf(f, "var V %s[%s]\n", test.constraint, test.typ); err != nil {
t.Fatal(err)
}
if err := f.Close(); err != nil {
t.Fatal(err)
}
cmd := exec.Command(gocmd, "build", name)
cmd.Dir = tmpdir
if out, err := cmd.CombinedOutput(); err == nil {
t.Error("build succeeded, but expected to fail")
} else if len(out) > 0 {
t.Logf("%s", out)
const want = "does not implement"
if !bytes.Contains(out, []byte(want)) {
t.Errorf("output does not include %q", want)
}
} else {
t.Error("no error output, expected something")
}
})
}
}

View file

@ -8,8 +8,6 @@
package go1_17
import "constraints"
type T[P /* ERROR type parameters require go1\.18 or later */ any /* ERROR undeclared name: any \(requires version go1\.18 or later\) */ ] struct{}
// for init (and main, but we're not in package main) we should only get one error
@ -57,5 +55,3 @@ type (
_ = C1
_ = C2
)
type Ordered constraints /* ERROR using type constraint constraints\.Ordered requires go1\.18 or later */ .Ordered

View file

@ -4,8 +4,11 @@
package p
import "constraints"
type Integer interface {
~int | ~int8 | ~int16 | ~int32 | ~int64 |
~uint | ~uint8 | ~uint16 | ~uint32 | ~uint64 | ~uintptr
}
func shl[I constraints.Integer](n int) I {
func shl[I Integer](n int) I {
return 1 << n
}

View file

@ -5,11 +5,15 @@
package a
import (
"constraints"
"math/rand"
)
type Builder[T constraints.Integer] struct{}
type Integer interface {
~int | ~int8 | ~int16 | ~int32 | ~int64 |
~uint | ~uint8 | ~uint16 | ~uint32 | ~uint64 | ~uintptr
}
type Builder[T Integer] struct{}
func (r Builder[T]) New() T {
return T(rand.Int())

View file

@ -4,11 +4,12 @@
package a
import (
"constraints"
)
type Integer interface {
~int | ~int8 | ~int16 | ~int32 | ~int64 |
~uint | ~uint8 | ~uint16 | ~uint32 | ~uint64 | ~uintptr
}
type Builder[T constraints.Integer] struct{}
type Builder[T Integer] struct{}
func (r Builder[T]) New() T {
return T(42)

View file

@ -7,17 +7,20 @@
package main
import (
"constraints"
"fmt"
)
func zero[T constraints.Complex]() T {
type Complex interface {
~complex64 | ~complex128
}
func zero[T Complex]() T {
return T(0)
}
func pi[T constraints.Complex]() T {
func pi[T Complex]() T {
return T(3.14)
}
func sqrtN1[T constraints.Complex]() T {
func sqrtN1[T Complex]() T {
return T(-1i)
}