]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/compile: make duplicate anonymous interface output deterministic
authorKeith Randall <keithr@alum.mit.edu>
Wed, 26 Jun 2019 02:24:34 +0000 (22:24 -0400)
committerKeith Randall <khr@golang.org>
Tue, 2 Jul 2019 13:52:21 +0000 (13:52 +0000)
Taking over CL 162240, the original CL hasn't been making progress.
I just took the parts that fix the immediate issue. I left the
signatslice changes out, I don't think they are necessary.

Fixes #30202

Change-Id: I5b347605f0841dd925d5a73150b8bf269fa82464
Reviewed-on: https://go-review.googlesource.com/c/go/+/183852
Run-TryBot: Keith Randall <khr@golang.org>
Reviewed-by: David Chase <drchase@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>

src/cmd/compile/internal/gc/reflect.go
src/cmd/compile/internal/gc/reproduciblebuilds_test.go
src/cmd/compile/internal/gc/testdata/reproducible/issue30202.go [new file with mode: 0644]

index 9b26ba16cb4a26862597077088667460b8494878..c2d9ba2fe0a6fee566af474316376762b42282fd 100644 (file)
@@ -1659,7 +1659,17 @@ func (a typesByString) Less(i, j int) bool {
        // they refer to byte or uint8, such as **byte vs **uint8,
        // the types' ShortStrings can be identical.
        // To preserve deterministic sort ordering, sort these by String().
-       return a[i].regular < a[j].regular
+       if a[i].regular != a[j].regular {
+               return a[i].regular < a[j].regular
+       }
+       // Identical anonymous interfaces defined in different locations
+       // will be equal for the above checks, but different in DWARF output.
+       // Sort by source position to ensure deterministic order.
+       // See issues 27013 and 30202.
+       if a[i].t.Etype == types.TINTER && a[i].t.Methods().Len() > 0 {
+               return a[i].t.Methods().Index(0).Pos.Before(a[j].t.Methods().Index(0).Pos)
+       }
+       return false
 }
 func (a typesByString) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
 
index 9173f80ee313322ca0b405d31cbba4395344caff..59d1edb9e8d510a409d7cdd8926b20e11e7ec819 100644 (file)
@@ -18,6 +18,7 @@ func TestReproducibleBuilds(t *testing.T) {
        tests := []string{
                "issue20272.go",
                "issue27013.go",
+               "issue30202.go",
        }
 
        testenv.MustHaveGoBuild(t)
@@ -38,7 +39,9 @@ func TestReproducibleBuilds(t *testing.T) {
                        defer os.Remove(tmp.Name())
                        defer tmp.Close()
                        for i := 0; i < iters; i++ {
-                               out, err := exec.Command(testenv.GoToolPath(t), "tool", "compile", "-o", tmp.Name(), filepath.Join("testdata", "reproducible", test)).CombinedOutput()
+                               // Note: use -c 2 to expose any nondeterminism which is the result
+                               // of the runtime scheduler.
+                               out, err := exec.Command(testenv.GoToolPath(t), "tool", "compile", "-c", "2", "-o", tmp.Name(), filepath.Join("testdata", "reproducible", test)).CombinedOutput()
                                if err != nil {
                                        t.Fatalf("failed to compile: %v\n%s", err, out)
                                }
diff --git a/src/cmd/compile/internal/gc/testdata/reproducible/issue30202.go b/src/cmd/compile/internal/gc/testdata/reproducible/issue30202.go
new file mode 100644 (file)
index 0000000..7b5de2c
--- /dev/null
@@ -0,0 +1,17 @@
+// Copyright 2019 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 A(x interface {
+       X() int
+}) int {
+       return x.X()
+}
+
+func B(x interface {
+       X() int
+}) int {
+       return x.X()
+}