mirror of
https://github.com/golang/go.git
synced 2025-12-08 06:10:04 +00:00
weak: don't panic when calling Value on a zero Pointer
Currently weak.Pointer.Value will panic if the weak.Pointer is uninitialized (zero value) which goes against it's documentation. Fix this and add a test. While we're here, also add a test to ensure weak.Make[T](nil) is equivalent to the zero value of weak.Pointer[T]. Fixes #71153. Change-Id: I4d9196026360bc42a5bfcb33ce449131ec251dba Reviewed-on: https://go-review.googlesource.com/c/go/+/641095 Reviewed-by: David Finkel <david.finkel@gmail.com> Auto-Submit: Michael Knyszek <mknyszek@google.com> LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> Reviewed-by: Carlos Amedee <carlos@golang.org>
This commit is contained in:
parent
9d0772b23e
commit
d62154db83
2 changed files with 18 additions and 0 deletions
|
|
@ -78,6 +78,9 @@ func Make[T any](ptr *T) Pointer[T] {
|
||||||
// If a weak pointer points to an object with a finalizer, then Value will
|
// If a weak pointer points to an object with a finalizer, then Value will
|
||||||
// return nil as soon as the object's finalizer is queued for execution.
|
// return nil as soon as the object's finalizer is queued for execution.
|
||||||
func (p Pointer[T]) Value() *T {
|
func (p Pointer[T]) Value() *T {
|
||||||
|
if p.u == nil {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
return (*T)(runtime_makeStrongFromWeak(p.u))
|
return (*T)(runtime_makeStrongFromWeak(p.u))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -21,6 +21,15 @@ type T struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestPointer(t *testing.T) {
|
func TestPointer(t *testing.T) {
|
||||||
|
var zero weak.Pointer[T]
|
||||||
|
if zero.Value() != nil {
|
||||||
|
t.Error("Value of zero value of weak.Pointer is not nil")
|
||||||
|
}
|
||||||
|
zeroNil := weak.Make[T](nil)
|
||||||
|
if zeroNil.Value() != nil {
|
||||||
|
t.Error("Value of weak.Make[T](nil) is not nil")
|
||||||
|
}
|
||||||
|
|
||||||
bt := new(T)
|
bt := new(T)
|
||||||
wt := weak.Make(bt)
|
wt := weak.Make(bt)
|
||||||
if st := wt.Value(); st != bt {
|
if st := wt.Value(); st != bt {
|
||||||
|
|
@ -41,6 +50,12 @@ func TestPointer(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestPointerEquality(t *testing.T) {
|
func TestPointerEquality(t *testing.T) {
|
||||||
|
var zero weak.Pointer[T]
|
||||||
|
zeroNil := weak.Make[T](nil)
|
||||||
|
if zero != zeroNil {
|
||||||
|
t.Error("weak.Make[T](nil) != zero value of weak.Pointer[T]")
|
||||||
|
}
|
||||||
|
|
||||||
bt := make([]*T, 10)
|
bt := make([]*T, 10)
|
||||||
wt := make([]weak.Pointer[T], 10)
|
wt := make([]weak.Pointer[T], 10)
|
||||||
wo := make([]weak.Pointer[int], 10)
|
wo := make([]weak.Pointer[int], 10)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue