Skip to content

weak: docs don't guarantee Pointer compares not equal for different values #71381

@FiloSottile

Description

@FiloSottile

weak.Pointer docs say this about when they compare equal

// Two Pointer values always compare equal if the pointers from which they were
// created compare equal. This property is retained even after the
// object referenced by the pointer used to create a weak reference is
// reclaimed.
// If multiple weak pointers are made to different offsets within the same object
// (for example, pointers to different fields of the same struct), those pointers
// will not compare equal.
// If a weak pointer is created from an object that becomes unreachable, but is
// then resurrected due to a finalizer, that weak pointer will not compare equal
// with weak pointers created after the resurrection.

However, to use safely as a cache key, two things need to be true

  • always compare equal if the pointers from which they were created compare equal (yes)
  • never compare equal if the pointers from which they were created don't compare equal (unclear?)

In particular, I am worried about using weak.Make(x) as a cache key, then x is garbage collected, a new value y is allocated at the same address, and now weak.Make(y) compares equal to my weak.Pointer map key.

I think this can't be the case, because at that point Value would end up returning y instead of x, but that's me reading into the implementation details, rather than the docs telling me what I am doing is safe.

Metadata

Metadata

Assignees

Labels

BugReportIssues describing a possible bug in the Go implementation.DocumentationIssues describing a change to documentation.NeedsFixThe path to resolution is known, but the work has not been done.

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions