]> Cypherpunks repositories - gostls13.git/commitdiff
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)
committerRuss Cox <rsc@golang.org>
Tue, 15 Nov 2022 18:14:39 +0000 (18:14 +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.

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>
src/sync/atomic/atomic_test.go
src/sync/atomic/type.go

index 0cc9b06a6c910c89977e03df920da295593e8d60..c3604ef0af01d2e3ffa748f4f5f3527072012dd7 100644 (file)
@@ -2526,3 +2526,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 4d466232f16b1a1efa7678c7d0d92436661e1722..cc016833d10fa8c714e8b4e3889de1a69e224f80 100644 (file)
@@ -41,9 +41,10 @@ var _ = &Pointer[int]{}
 
 // 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