Repository navigation
Description
Proposal Details
I propose that the zero value of a unique.Handle[T] be explicitly defined as being a globally unique identity for the zero value of T. Specifically, I propose that:
unique.Make[T](T{})should return the zero value ofunique.Handle[T]unique.Make[T]{}.Value()should return the zero value ofTinstead of panicking
This would have the effect of defining away the potential "invalid" state of a nil interior pointer.
Failing that, I propose that the documentation be updated to indicate that a zero value Handle is distinct from a Handle of the zero value, and clarify that it is potentially unsafe to use.
Motivation
The unique.Handle type is defined to be "a globally unique identity for some value of type T" and states that "Two handles compare equal exactly if the two values used to create the handles would have also compared equal." However, unique.Handle[T] actually has one more possible value than does T because it is possible for unique.Handle to be uninitialized.
This can create trouble when trying to intern a value of type T for types T with a useful zero value because it creates the potential for two distinct "zero" values: the zero value Handle[T] and the zero value T. At best, it is surprising to begin with a type T with a non-nil zero value and inadvertently introduce a hidden pointer after switching to unique.Handle[T]; at worst, it makes Value() potentially unsafe since it dereferences a hidden pointer. Issue #73266 makes reference to these difficulties.
Is there a strong use case for separating the two zero values that I am not aware of that justifies the sharp edge and makes it important that they be distinguishable?
Backwards Compatibility
I'm unsure whether this is a legal backwards-compatible change.
On one hand, the documentation of unique.Handle makes no mention of the behavior of its zero value. It also does not divulge that it has an interior pointer that can be nil and makes no reference to or commitments about invalid handles.
On the other hand, this would result in an observable behavioral change in programs. Consider:
invalid := unique.Handle[int]{}
zero := unique.Make(int(0))
fmt.Println(invalid == zero)
// Outputs "false" today, would change to "true" with this proposalThere is at least some precedent for making such potentially-observable behavior changes by leveraging the Forward Compatibility mechanism (#60078). I'm not familiar enough with it to know if that's an option here.
Activity
gabyhelp commentedon Apr 12, 2025
Related Issues
(Emoji vote if this was helpful or unhelpful; more detailed feedback welcome in this discussion.)
[-]proposal: unique: Define Handle zero value as zero value of underlying type[/-][+]proposal: unique: Define Handle[T] zero value as zero value of T[/+][-]proposal: unique: Define Handle[T] zero value as zero value of T[/-][+]proposal: unique: Define Handle[T] zero value as handle to zero value of T[/+]prattmic commentedon Apr 14, 2025
cc @golang/runtime @mknyszek
[-]proposal: unique: Define Handle[T] zero value as handle to zero value of T[/-][+]proposal: unique: define Handle[T] zero value as handle to zero value of T[/+]mknyszek commentedon Apr 14, 2025
I'm not in favor of this proposal due to backwards compatibility concerns.
The first issue is that I'm not sure how to implement this proposal efficiently. When do we install the zero value for a particular type? Installing it conservatively at program initialization time would add startup time and add a potentially large amount of unnecessary memory costs. The list of values that are installed could be culled with compiler help, but this change doesn't seem worth the extra compiler complexity.EDIT: Oh, 'doh,
Valuefor a zeroHandle[T]could simply return the zero value ofT.Unfortunately, I don't think it is backwards-compatible. I believe this could break existing programs. As you point out, there is a difference in observable behavior. (It may not matter in practice, but we'd have to essentially prove that on an ecosystem level. Sometimes such analyses are practical, but this depends on run-time values, so static analysis is likely too weak to come to a satisfactory conclusion.)
mknyszek commentedon Apr 14, 2025
To clarify my concerns: anyone already working with the 'uninitialized' state of the
Handle[T]in order to signal something specific might run into problems if they do actually callunique.Makewith the type's zero value.tylerchr commentedon Apr 14, 2025
I'm certainly not motivated enough to attempt to produce an ecosystem-level proof, and if we think there are legitimate reasons to be able to discriminate between the "invalid" and "zero" states then I agree that changing equality semantics is probably off the table.
Yeah, so perhaps this leads to a stripped-down solution wherein the first of my two bullet points is dropped but the second is retained. Specifically: the "invalid" and "zero" handles would remain distinct, but calling
Valuewould return the zero T for both.That'd be enough to take the sharp edge off of
unique.Handle(can't panic) and resolve the problem that initially motivated me (makingunique.Handle[T]a semantically safe substitute for allT).