You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
x/tools/gopls: modernizer for sync.OnceValues #76822
Some complex uses of sync.Once can be much simplified using OnceValues; see https://go.dev/cl/728860 for example. Let's add a modernizer that recognizes the pattern.
May I request to be assigned to this task? I’m new to gopls, but I’d like to give it a try if it’s not too complicated.
By all means! Thanks.
Before you start coding, I suggest you start by posting a brief plan here for the patterns you want to match, so that we can try to enumerate all the necessary safety conditions, reducing surprises and the need for rewrites.
May I request to be assigned to this task? I’m new to gopls, but I’d like to give it a try if it’s not too complicated.
By all means! Thanks.
Before you start coding, I suggest you start by posting a brief plan here for the patterns you want to match, so that we can try to enumerate all the necessary safety conditions, reducing surprises and the need for rewrites.
I found this rather hard to follow; Once.Do doesn't return any values, it is called only for effects (which occur once). An example may make it clearer.
The question here is how exactly we will ascertain whether the effects of the function f passed to Do are of the appropriate form that Do could be replaced by OnceValue (or Values), and, once ascertained, what parts of the code would need to change.
BTW, I would be surprised if this modernizer, correctly implemented, would have flagged the code in CL 728860 because that code had a bug (the ineffassign diagnostic that prompted the CL).
I apologize for the poor writing—the quotes around 'return' were removed by a grammar checker. I underestimated the complexity. I'll rethink this thoroughly.
Activity
gabyhelp commentedon Dec 13, 2025
Related Issues
(Emoji vote if this was helpful or unhelpful; more detailed feedback welcome in this discussion.)
linsite commentedon Dec 18, 2025
May I request to be assigned to this task? I’m new to gopls, but I’d like to give it a try if it’s not too complicated.
adonovan commentedon Dec 18, 2025
By all means! Thanks.
Before you start coding, I suggest you start by posting a brief plan here for the patterns you want to match, so that we can try to enumerate all the necessary safety conditions, reducing surprises and the need for rewrites.
linsite commentedon Dec 20, 2025
Thanks for the guidance. I'll post them later.
linsite commentedon Jan 7, 2026
The sync.Once instance can be a package
varor a member of astruct, and the returned values must be closure-captured.Patterns should be considered:
The instance is a package
var:Once.Doreturns 1 value by closure.Once.Doreturns 2 values by closure.Once.Doreturns more than 2 values, we should leave out the third and all subsequent values, assync.OnceValuescan return at most 2 values.The instance is a struct member:
Basically this is the same as a package
var, but we should update the corresponding member type fromsync.Onceto the return type ofsync.OnceValues.Return values
The returned values should be updated accordingly—typically by deleting unused ones, as in https://go.dev/cl/728860.
Patterns should not be considered:
sync.Once.Dodoes not return any values.cc @adonovan
adonovan commentedon Jan 7, 2026
I found this rather hard to follow; Once.Do doesn't return any values, it is called only for effects (which occur once). An example may make it clearer.
The question here is how exactly we will ascertain whether the effects of the function f passed to Do are of the appropriate form that Do could be replaced by OnceValue (or Values), and, once ascertained, what parts of the code would need to change.
BTW, I would be surprised if this modernizer, correctly implemented, would have flagged the code in CL 728860 because that code had a bug (the ineffassign diagnostic that prompted the CL).
linsite commentedon Jan 9, 2026
I apologize for the poor writing—the quotes around 'return' were removed by a grammar checker. I underestimated the complexity. I'll rethink this thoroughly.