proposal: Go2: arg tags for marking const, other properties; use in vet #33122
Labels
FrozenDueToAge
LanguageChange
Suggested changes to the Go language
Proposal
v2
An incompatible library change
Milestone
This proposal is to adopt the concept of struct field tags for function args, to allow various properties to be marked, including const, so that tools such as vet could be used to find cases where a read-only value is being written.
In the discussion of the various const-like proposals, e,g., #27975 and #22876, the consistent feedback has been that const-poison is a major problem (I certainly suffered from this many times in C++) and that const-ness is one of multiple possible properties one might want to mark.
A potential conclusion is that const-ness must be optional, and a more open-ended syntax is needed to enable various properties to be marked. A logical approach within the existing Go toolkit is to adopt the open-ended tags for struct fields to function arguments:
vet
could parse those arg tags, trace through all operations on an arg marked as const, and find any places where it is actually being written to. This would avoid const poison, have no performance overhead, enable const-ness to be asserted flexibly at any point, and support the debugging aspect of const-ness. This is analogous to how the race detector is used to find bugs, instead of somehow preventing races through the language itself.This would obviously not solve the performance advantages of const-ness, such as the no-copy conversion between []byte and string, but perhaps the debugging aspect is the most important?
This would also work well with #33088, which allows const vs. non-const to be decorated at the calling site, and
vet
could also be used to flag violations where the&
operator was used to call a non-const arg or vice-versa, or even without this proposal it could independently check whether an arg passed without the&
operator was written to.The text was updated successfully, but these errors were encountered: