constraints: test against the constraints package from x/exp instead of std

Fixes golang/go#50792

Change-Id: I9abb0972c9e51c88591de1cc1ef10337f75f84d0
Reviewed-on: https://go-review.googlesource.com/c/exp/+/382834
Trust: Bryan Mills <bcmills@google.com>
Run-TryBot: Bryan Mills <bcmills@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Reviewed-by: David Chase <drchase@google.com>
diff --git a/constraints/constraints_test.go b/constraints/constraints_test.go
index a285ee6..5bc43a9 100644
--- a/constraints/constraints_test.go
+++ b/constraints/constraints_test.go
@@ -42,7 +42,7 @@
 var prolog = []byte(`
 package constrainttest
 
-import "constraints"
+import "golang.org/x/exp/constraints"
 
 type (
 	testSigned[T constraints.Signed]     struct{ f T }
@@ -71,9 +71,40 @@
 
 	tmpdir := t.TempDir()
 
-	if err := os.WriteFile(filepath.Join(tmpdir, "go.mod"), []byte("module constraintest"), 0666); err != nil {
+	cwd, err := os.Getwd()
+	if err != nil {
 		t.Fatal(err)
 	}
+	// This package is golang.org/x/exp/constraints, so the root of the x/exp
+	// module is the parent directory of the directory in which this test runs.
+	expModDir := filepath.Dir(cwd)
+
+	modFile := fmt.Sprintf(`module constraintest
+
+go 1.18
+
+replace golang.org/x/exp => %s
+`, expModDir)
+	if err := os.WriteFile(filepath.Join(tmpdir, "go.mod"), []byte(modFile), 0666); err != nil {
+		t.Fatal(err)
+	}
+
+	// Write the prolog as its own file so that 'go mod tidy' has something to inspect.
+	// This will ensure that the go.mod and go.sum files include any dependencies
+	// needed by the constraints package (which should just be some version of
+	// x/exp itself).
+	if err := os.WriteFile(filepath.Join(tmpdir, "prolog.go"), []byte(prolog), 0666); err != nil {
+		t.Fatal(err)
+	}
+
+	tidyCmd := exec.Command(gocmd, "mod", "tidy")
+	tidyCmd.Dir = tmpdir
+	tidyCmd.Env = append(os.Environ(), "PWD="+tmpdir)
+	if out, err := tidyCmd.CombinedOutput(); err != nil {
+		t.Fatalf("%v: %v\n%s", tidyCmd, err, out)
+	} else {
+		t.Logf("%v:\n%s", tidyCmd, out)
+	}
 
 	// Test for types that should not satisfy a constraint.
 	// For each pair of constraint and type, write a Go file