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
spec: allow range-over-func to omit iteration variables #65236
In discussion during the implementation of #61405 we changed range-over-func to require mentioning iteration variables. The idea is that if we do end up with idioms like:
for line, err := range FileLines("/etc/passwd") {
...
}
Then we want to diagnose:
for line := range FileLines("/etc/passwd") {
...
}
as an error. However, this is inconsistent with other range loops and also not what the #61405 text said. Probably we should change it. Starting a separate proposal for that discussion.
Reacted by Dominik Honnef, Andrew Ekstedt, Caleb Spare, Achille, AndrewHarrisSPU, Chanchal Kumar Ghosh, Jonathan Bluett-Duncan, Nik B, TheDiveO, Will Faught and 6 more
Reacted by jimmyfrasche, Shay Nehmad, Aliaksandr Valialkin and wlbwlbwlb
I don't think I understand what is being proposed. Doesn't the spec text of #61405 already "allow range-over-func to omit iteration variables"?
The example :
For example if f has type func(yield func(T1, T2) bool) bool any of these are valid:
for x, y := range f { ... }
for x, _ := range f { ... }
for _, y := range f { ... }
for x := range f { ... }
for range f { ... }
The proposed text:
As with range over other types, it is permitted to declare fewer iteration variables
than there are iteration values.
Anyways I am confused about what is being proposed here.
for line, err := range FileLines("/etc/passwd") {
If we think there is a subset of functions like FileSet that need to not ignore one of the values in RangeStmts, this seems like it could fits in well with the unusedresult checker in vet. I am not sure it makes sense to enforce usage at the language level.
Reacted by jimmyfrasche, TheDiveO and Komu Wairagu
We know that the design of bufio.Scanner, which requires a call to the Err method after iteration, is flawed. We have empirical evidence that people forget the call.
We can guess that range-over-func will become the dominant way to iterate over all sorts of sequences where failure is possible: file lines, DB rows, RPCs that list resources, and so on.
I'm not saying that we should require the iteration variables, but I am saying that if we don't, we need a good story for how to avoid introducing many bugs into Go code. Like a vet check for when the second return type is error that is enabled for testing.
Reacted by Branden J Brown, Leo Antunes, Hein Meling, Alessandro Arzilli, Komu Wairagu, Jonathan Bluett-Duncan, Carlana, Marco Gazerro, Blake Mizerany, Eric Lagergren and 8 more
I agree that not handling the error is a real problem. I do not think that iterators yielding errors solves that problem very well, especially if that means having to immediately buttress that in a way that forces everyone to use range-over-func differently than native iterators. Even if I'm wrong and yielding errors is a boon, then static analysis is more than capable of linting it only in the case when an error is iterated over.
The only benefit of passing an *error to the iterator as mentioned @jimmyfrasche is that you must declare and pass an error to the iterator, but you can still forget to check it after the loop. More problematic though is that the pattern prevents you from logging/handling individual errors.
Both of these are IMO better solutions than handling the error after the loop as with the bufio.Scanner example, as the error handling happens immediately after the call producing the error.
If the intent of the developer is to ignore errors, then the above could instead be written as (same semantic meaning):
forline, _:=rangeFileLines("/etc/passwd") {
...
}
The if-err-continue pattern above could be detected by a tool (gopls) and a rewrite to the simple form could be suggested (similar to some other patterns).
However, I would advice against allowing the following pattern if the iterator returns an error, because the _ is a more explicit signal that ignoring the error is intentional:
forline:=rangeFileLines("/etc/passwd") {
...
}
If you want to handle/log individual errors and continue, you would still need to use this pattern:
Handling and logging individual errors is not possible with the handling after the loop approach.
Reacted by Jonathan Bluett-Duncan, DanielDirdal, Marco Gazerro, Vojtěch Bargl, Aleksander Vedvik, Viktor Kojouharov, afocus, Vahid, Ochi Daiki, Steve Dalton and 4 more
I fully support yielding (T, error)—when the error is per iteration.
I oppose an error that signals an abnormal end to (or failure to start) the iteration being handled within the loop.
In other words, I think if you can continue on err != nil it's fine, but if you MUST break/return it's not. (Presumably in the latter case the code would still accidentally work correctly if you continued as the iteration would stop but that's not a great argument in the pattern's favor).
Reacted by DeedleFake, Eric Lagergren and Alan Donovan
What about an iterator that's using a context.Context internally to govern its fetching of additional values? If the Context is done, do you think that the iterator should yield the result of Context.Err? If so, should it respect that call to yield returning true and attempt to continue, or should it ignore yield's return value?
I've been writing such an iterator, and I've changed my mind about the answers to these questions several times now.
What about an iterator that's using a context.Context internally to govern its fetching of additional values?
This is a really interesting question - to me a practically useful solution has been to delay passing context.Context far enough that it isn't deeply internal. In other words, don't pass around an iterator with an internal context, pass around a func(context.Context) -> iter.Seq and a context.Context as far as possible, so that the base context.Context is in scope where it should be checked. If a computation using iterated values needs to branch, the context should be in scope.
In other words, don't pass around an iterator with an internal context, pass around a func(context.Context) -> iter.Seq and a context.Context as far as possible, so that the base context.Context is in scope where it should be checked.
That's already the case for my approach, but it doesn't obviate the question of whether the iterator should yield Context.Err when it detects that the Context is done.
That's already the case for my approach, but it doesn't obviate the question of whether the iterator should yield Context.Err when it detects that the Context is done.
What would be a reason for the iterator to not return the ctx.Err()?
changed the title [-]proposal: spec: allow range-over-func to omit iteration variables[/-][+]spec: allow range-over-func to omit iteration variables[/+]on Jun 24, 2024
Activity
earthboundkid commentedon Jan 23, 2024
What if it's just a vet check that looks for errors not silenced with
, _? Then you could still writefor key := range mapish.All().timothy-king commentedon Jan 23, 2024
I don't think I understand what is being proposed. Doesn't the spec text of #61405 already "allow range-over-func to omit iteration variables"?
The example :
The proposed text:
Anyways I am confused about what is being proposed here.
If we think there is a subset of functions like
FileSetthat need to not ignore one of the values in RangeStmts, this seems like it could fits in well with the unusedresult checker in vet. I am not sure it makes sense to enforce usage at the language level.jimmyfrasche commentedon Jan 23, 2024
I don't see how this buys anything other than ceremony.
jba commentedon Jan 23, 2024
We know that the design of
bufio.Scanner, which requires a call to theErrmethod after iteration, is flawed. We have empirical evidence that people forget the call.We can guess that range-over-func will become the dominant way to iterate over all sorts of sequences where failure is possible: file lines, DB rows, RPCs that list resources, and so on.
I'm not saying that we should require the iteration variables, but I am saying that if we don't, we need a good story for how to avoid introducing many bugs into Go code. Like a vet check for when the second return type is
errorthat is enabled for testing.meling commentedon Jan 23, 2024
jimmyfrasche commentedon Jan 23, 2024
I agree that not handling the error is a real problem. I do not think that iterators yielding errors solves that problem very well, especially if that means having to immediately buttress that in a way that forces everyone to use range-over-func differently than native iterators. Even if I'm wrong and yielding errors is a boon, then static analysis is more than capable of linting it only in the case when an error is iterated over.
@jba here's https://pkg.go.dev/bufio#example-Scanner.Bytes rewritten to use a hypothetical iterator that returns
([]byte, error): https://go.dev/play/p/G-Wv80AbfqF I don't think having the error handling in the loop is an improvement. One of the alternatives I saw posted was for the iterator factory to take a*error. I didn't much like the idea initially but it has grown on me: https://go.dev/play/p/DdqILPDt9B7meling commentedon Jan 24, 2024
The only benefit of passing an
*errorto the iterator as mentioned @jimmyfrasche is that you must declare and pass an error to the iterator, but you can still forget to check it after the loop. More problematic though is that the pattern prevents you from logging/handling individual errors.I see a few possible patterns related to errors:
Both of these are IMO better solutions than handling the error after the loop as with the
bufio.Scannerexample, as the error handling happens immediately after the call producing the error.The other possible pattern (
if-err-continue) is:If the intent of the developer is to ignore errors, then the above could instead be written as (same semantic meaning):
The
if-err-continuepattern above could be detected by a tool (gopls) and a rewrite to the simple form could be suggested (similar to some other patterns).However, I would advice against allowing the following pattern if the iterator returns an error, because the
_is a more explicit signal that ignoring the error is intentional:If you want to handle/log individual errors and continue, you would still need to use this pattern:
Handling and logging individual errors is not possible with the handling after the loop approach.
earthboundkid commentedon Jan 24, 2024
Looks like they finally fixed it, but for months the marketing page for Copilot showed code with a missing call to
.Err().jimmyfrasche commentedon Jan 24, 2024
@meling To clarify:
I fully support yielding
(T, error)—when the error is per iteration.I oppose an error that signals an abnormal end to (or failure to start) the iteration being handled within the loop.
In other words, I think if you can
continueonerr != nilit's fine, but if you MUSTbreak/returnit's not. (Presumably in the latter case the code would still accidentally work correctly if youcontinued as the iteration would stop but that's not a great argument in the pattern's favor).seh commentedon Jan 25, 2024
What about an iterator that's using a
context.Contextinternally to govern its fetching of additional values? If theContextis done, do you think that the iterator should yield the result ofContext.Err? If so, should it respect that call toyieldreturning true and attempt to continue, or should it ignoreyield's return value?I've been writing such an iterator, and I've changed my mind about the answers to these questions several times now.
AndrewHarrisSPU commentedon Jan 25, 2024
This is a really interesting question - to me a practically useful solution has been to delay passing
context.Contextfar enough that it isn't deeply internal. In other words, don't pass around an iterator with an internal context, pass around afunc(context.Context) -> iter.Seqand acontext.Contextas far as possible, so that the basecontext.Contextis in scope where it should be checked. If a computation using iterated values needs to branch, the context should be in scope.seh commentedon Jan 26, 2024
That's already the case for my approach, but it doesn't obviate the question of whether the iterator should yield
Context.Errwhen it detects that theContextis done.thediveo commentedon Jan 26, 2024
What would be a reason for the iterator to not return the
ctx.Err()?71 remaining items
[-]proposal: spec: allow range-over-func to omit iteration variables[/-][+]spec: allow range-over-func to omit iteration variables[/+]gopherbot commentedon Jun 24, 2024
Change https://go.dev/cl/594555 mentions this issue:
gopls/internal/analysis/simplifyrange: reenable on range-over-funcgopls/internal/analysis/simplifyrange: reenable on range-over-func
gopherbot commentedon Jul 2, 2024
Change https://go.dev/cl/596095 mentions this issue:
gopls/internal/analysis/simplifyrange: reenable on range-over-funcgopherbot commentedon Jul 2, 2024
Change https://go.dev/cl/596135 mentions this issue:
cmd/compile: verify that rangefunc assigning to no vars worksgriesemer commentedon Jul 29, 2024
This proposal has been implemented and documented. Closing.
cmd/compile: verify that rangefunc assigning to no vars works