-
Notifications
You must be signed in to change notification settings - Fork 18k
proposal: cmd/vet: warn when the iterator variable on 3-clause for loops where is written to in a go statement after 1.22 #66388
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Related #66156. This is inspired by case 3. |
This seems like an oddly specific version of a static race checker that looks for patterns of the form: x = f()
go func() {
x = g()
}()
use(x) What makes go1.22 for loops special is that the I think we need evidence that this is a real problem: this kind of code has always been wrong, it's just the nature of the bug has changed. We may not have such evidence for a while. |
Waiting for evidence or clearer demand instead of speculating seems reasonable to me. Another potential cause is transitioning an existing |
This is not always true. This kind of code might be correct before Go 1.22 if synchronizations are done well for all the 3 places. But since Go 1.22, it becomes impossible to do synchronization for the third implicit BTW, this is actually a continuation of #66156 |
Proposal Details
The proposal is to extend the
loopclosure
checker to additionally warn with a variables in a 3-clause for loop is written to after Go 1.22. A part of the new semantics is to copy the iterator variable at the end of the for loop. This is a READ that can race with writes from other goroutines.Example:
Criteria to use:
v
.go
statement.Updates(id)
holds whenid
refers to the variable,Updates(e.X)
holds whenUpdates(e)
,Updates(e[g])
holds whenUpdates(e)
,Updates(*e)
holds whenUpdates(e)
,Updates(e)
does not hold.Additionally:
forEachLastStmt
for the operational definition of 'last'.go
statements this would be reported forerrgroup.Go
andt.Run()
whent.Parrallel
is present. It will not be reported fordefer
statements.To discuss: The requirements give at the moment dereference pointers. This is not sufficient to ensure a race has occurred. Just that a race is overwhelmingly likely. This seems to be in the spirit of the existing loopclosure check. Curious whether others agree or think we should not report if there is indirection.
The text was updated successfully, but these errors were encountered: