]> Cypherpunks repositories - gostls13.git/commitdiff
[release-branch.go1.19] sync/atomic: allow linked list of atomic pointers again
authorRuss Cox <rsc@golang.org>
Tue, 15 Nov 2022 14:54:39 +0000 (09:54 -0500)
committerMichael Pratt <mpratt@google.com>
Fri, 9 Dec 2022 20:56:57 +0000 (20:56 +0000)
For #56603, CL 448275 added a _ [0]T field to atomic.Pointer,
so that different kinds of atomic.Pointer are not convertible.

Unfortunately, that breaks code like:

type List struct {
Next atomic.Pointer[List]
}

which should be valid, just as using Next *List is valid.
Instead, we get:

./atomic_test.go:2533:6: invalid recursive type List
./atomic_test.go:2533:6: List refers to
./atomic_test.go:2534:13: "sync/atomic".Pointer refers to
./atomic_test.go:2533:6: List

Fix by using _[0]*T instead.

For #56638.
Fixes #57124.

Change-Id: Icc4c83c691d35961d20cb14b824223d6c779ac5e
Reviewed-on: https://go-review.googlesource.com/c/go/+/450655
Run-TryBot: Russ Cox <rsc@golang.org>
TryBot-Result: Gopher Robot <gobot@golang.org>
Reviewed-by: Michael Knyszek <mknyszek@google.com>
(cherry picked from commit b14cf3d93ae5c477dd35f13f6ba41044f01a7f7d)
Reviewed-on: https://go-review.googlesource.com/c/go/+/452438
Reviewed-by: Cherry Mui <cherryyz@google.com>
Run-TryBot: Michael Pratt <mpratt@google.com>
Reviewed-by: Michael Pratt <mpratt@google.com>
src/sync/atomic/atomic_test.go
src/sync/atomic/type.go

index 02d55fbc1974c75a46cee18f9abdb28e8a40759d..3ab5e836e99a6451d40884a673ea027aaff21795 100644 (file)
@@ -2605,3 +2605,9 @@ func TestNilDeref(t *testing.T) {
                }()
        }
 }
+
+// Test that this compiles.
+// When atomic.Pointer used _ [0]T, it did not.
+type List struct {
+       Next Pointer[List]
+}
index 2d1e62165226903df9a1dc1136e996b14e192451..93058ca38dbc28ff5b1908ee346db5afbfc17e69 100644 (file)
@@ -37,9 +37,10 @@ func b32(b bool) uint32 {
 
 // A Pointer is an atomic pointer of type *T. The zero value is a nil *T.
 type Pointer[T any] struct {
-       // Mention T in a field to disallow conversion between Pointer types.
+       // Mention *T in a field to disallow conversion between Pointer types.
        // See go.dev/issue/56603 for more details.
-       _ [0]T
+       // Use *T, not T, to avoid spurious recursive type definition errors.
+       _ [0]*T
 
        _ noCopy
        v unsafe.Pointer