News for hackers. Stuff that matters.
Re:Fixing for Loops in Go 1.22 by campbel on Tuesday September 19, 2023 @03:59PM (#37575630)
Won't this end up breaking programs that depend on the current behavior?
Re:Fixing for Loops in Go 1.22 by infogulch on Tuesday September 19, 2023 @04:01PM (#37575673)
> To ensure backwards compatibility with existing code, the new semantics will only apply in packages contained in modules that declare go 1.22 or later in their go.mod files. ... It is also possible to use //go:build lines to control the decision on a per-file basis.
Re:Fixing for Loops in Go 1.22 by sixstringtheory on Tuesday September 19, 2023 @05:07PM (#37576677)
Doesn't that mean that all code written so far can't take up newer versions of the Go compiler for any other reason like new features/bugfixes/optimizations/etc without a full audit of codepaths involving for loops?
Re:Fixing for Loops in Go 1.22 by ericpauley on Tuesday September 19, 2023 @05:47PM (#37577282)
No, it does not. Packages can compile using 1.22 and gain other benefits without opting into this change.
Re:Fixing for Loops in Go 1.22 by sixstringtheory on Tuesday September 19, 2023 @06:54PM (#37578191)
Ah, I didn't see the part about //go:build
Re:Fixing for Loops in Go 1.22 by acheong08 on Wednesday September 20, 2023 @02:17AM (#37580953)
Without /:go:build tags, you can just define 1.21 as your Go version in go.mod to opt out of new features while getting other benefits of the new compiler
Re:Fixing for Loops in Go 1.22 by kevincox on Tuesday September 19, 2023 @08:52PM (#37579129)
No, the version declared in go.mod is different than the version of the toolchain used to compile the project. If you declare an older version even new toolchains will act like the previous versions.
Re:Fixing for Loops in Go 1.22 by s17n on Wednesday September 20, 2023 @02:53AM (#37581186)
There isn’t really anything that you could actually use the old behavior for, any code that depends on it is probably wrong anyway.
Re:Fixing for Loops in Go 1.22 by Cthulhu_ on Wednesday September 20, 2023 @04:00AM (#37581639)
No I don't think so; any old working code will be using the x := x workaround, which will keep working when going to this version with the changed loop mechanics. What may happen is a form of... some adage, I forgot the name, where code accidentally relies on the old behaviour and breaks when that old behaviour is no longer there.

(that same adage applies to e.g. browser manufacturers having to implement bugs to not break certain websites)

Re:Fixing for Loops in Go 1.22 by Quekid5 on Wednesday September 20, 2023 @07:04AM (#37582714)
Are you thinking of Hyrum's Law?
Re:Fixing for Loops in Go 1.22 by sgerenser on Wednesday September 20, 2023 @10:10PM (#37592493)
Whatever, just give me an option to enable spacebar heating. https://xkcd.com/1172/
Re:Fixing for Loops in Go 1.22 by Quekid5 on Sunday September 24, 2023 @05:34PM (#37637424)
I enjoyed that reference :)
Re:Fixing for Loops in Go 1.22 by omeid2 on Tuesday September 19, 2023 @04:08PM (#37575783)
I don't know why you're being down voted, but it is actually breaking the Go1 compat promise. Which says:

    It is intended that programs written to the Go 1 specification will continue to compile and run correctly, unchanged, over the lifetime of that specification. At some indefinite point, a Go 2 specification may arise, but until that time, Go programs that work today should continue to work even as future "point" releases of Go 1 arise (Go 1.1, Go 1.2, etc.).
Re:Fixing for Loops in Go 1.22 by freedomben on Tuesday September 19, 2023 @04:15PM (#37575882)
I upvoted the question to offset one of the downs because I agree it's a fair question. However I would guess the downvotes are because TFA addressed this issue directly and comprehensively, so it's a clear "I didn't read the article" indicator :-) Possibly also because the downvoters can't imagine a scenario where this would be desirable behavior (i.e. it's always a bug)
Re:Fixing for Loops in Go 1.22 by campbel on Tuesday September 19, 2023 @04:18PM (#37575928)
Yeah its fair, I didn't closely read that section. Although, I'm not entirely convinced the approach is safe, maybe its worth it to fix such a common pitfall.
Re:Fixing for Loops in Go 1.22 by colejohnson66 on Tuesday September 19, 2023 @04:43PM (#37576246)
But if it’s a bug, then the logic to not compile future versions is wrong, IMO. If it’s a feature change, then such logic would make sense.
Re:Fixing for Loops in Go 1.22 by wrs on Tuesday September 19, 2023 @04:14PM (#37575863)
Note the word “programs”, not “files”. If your program doesn’t declare go 1.22 in its go.mod, it will continue to work (or not work!), unchanged.
Re:Fixing for Loops in Go 1.22 by robertlagrant on Tuesday September 19, 2023 @04:24PM (#37576003)
Isn't that "working with future point releases", though? If I don't declare 1.22, am I not excluded from that point release?
Re:Fixing for Loops in Go 1.22 by ben0x539 on Tuesday September 19, 2023 @04:41PM (#37576216)
I assume that if compiling with 1.22 or later, you still get all the benefits from that version like other new features, bug fixes or perf improvements, just not this particular change.
Re:Fixing for Loops in Go 1.22 by mrkstu on Tuesday September 19, 2023 @04:46PM (#37576287)
No, the compiler will revert to the original behavior, it only adopts the new behavior with the declaration.
Re:Fixing for Loops in Go 1.22 by skywhopper on Tuesday September 19, 2023 @08:22PM (#37578918)
This has all been addressed in the proposal. The research was done and this change will impact so few projects that it’s worth making a technical exception to the compatibility promise to fix a real design flaw.
Re:Fixing for Loops in Go 1.22 by campbel on Tuesday September 19, 2023 @04:17PM (#37575903)
I don't mind.

Yeah, I thought this kind of change wouldn't happen because of this promise.

Re:Fixing for Loops in Go 1.22 by jacquesm on Tuesday September 19, 2023 @04:24PM (#37576002)
And they do. You can specify the precise logic to use on a per-file basis.
Re:Fixing for Loops in Go 1.22 by tgv on Tuesday September 19, 2023 @04:19PM (#37575934)
I think it was downvoted precisely because of that. It's a bit of a contentious issue.
Re:Fixing for Loops in Go 1.22 by efuquen on Tuesday September 19, 2023 @10:47PM (#37579881)
In a previous blog post they basically said they will never make a Go 2, and also addressed a lot of things about compatibility:

https://go.dev/blog/compat

In particular they said:

> The end of the document warns, “[It] is impossible to guarantee that no future change will break any program.” Then it lays out a number of reasons why programs might still break.

> For example, it makes sense that if your program depends on a buggy behavior and we fix the bug, your program will break. But we try very hard to break as little as possible and keep Go boring.

Re:Fixing for Loops in Go 1.22 by Cthulhu_ on Wednesday September 20, 2023 @04:05AM (#37581672)
> In a previous blog post they basically said they will never make a Go 2

No, they didn't say that, they said it wouldn't be backwards-incompatible with Go 1. Relevant quote:

> [...] when should we expect the Go 2 specification that breaks old Go 1 programs?

> The answer is never. Go 2, in the sense of breaking with the past and no longer compiling old programs, is never going to happen. Go 2 in the sense of being the major revision of Go 1 we started toward in 2017 has already happened.

Re:Fixing for Loops in Go 1.22 by Cthulhu_ on Wednesday September 20, 2023 @04:04AM (#37581659)
No, it doesn't break the promise; "Go programs that work today should continue to work even as future "point" releases of Go 1 arise (Go 1.1, Go 1.2, etc.)."

You can install Go 1.22 and your program will compile and run as-is. That's the promise. If however you opt-in to the changed for loop behaviour by adjusting your go.mod, the onus is on you to update your program accordingly.

It's only a backwards incompatible change if the developer makes a backwards incompatible change by updating the configured target version.

(I'm aware I'm probably being pedantic here, I understand the language used seems to imply you can just set it to v1.22 and it works but it's a bit more specific)

Re:Fixing for Loops in Go 1.22 by matthewmueller on Tuesday September 19, 2023 @04:01PM (#37575656)
It's only enabled for modules that run Go 1.22 and higher
Re:Fixing for Loops in Go 1.22 by badrequest on Tuesday September 19, 2023 @04:04PM (#37575725)
I also can't imagine a case where it is useful or even truly intended to rely on this behavior.
Re:Fixing for Loops in Go 1.22 by omginternets on Tuesday September 19, 2023 @04:17PM (#37575904)
Yeah it’s def a code smell …
Re:Fixing for Loops in Go 1.22 by ben0x539 on Tuesday September 19, 2023 @04:45PM (#37576274)
Yeah I don't think it's so much "we explicitly rely on this behavior, how dare you change this" as "somewhere in our mountains of maintenance-mode code that haven't seen the sun shine through an editor window in years, this behavior cancels out another bug that we never noticed". Tooling should be able to detect when code relies on this, but it's still gonna cost some non-zero amount of developer effort to touch ancient code and safely roll out a new version if it needs to be actively addressed.
Re:Fixing for Loops in Go 1.22 by rsc on Tuesday September 19, 2023 @05:55PM (#37577405)
If you have tests and they break with GOEXPERIMENT=loopvar, then there is a new tool that will tell you exactly which loop is causing the breakage. That's a post for a few weeks from now.
Re:Fixing for Loops in Go 1.22 by kuchenbecker on Tuesday September 19, 2023 @09:34PM (#37579419)
If
Re:Fixing for Loops in Go 1.22 by Cthulhu_ on Wednesday September 20, 2023 @04:06AM (#37581681)
Neither can I, but there may be cases of code accidentally relying on it - there's an adage that I forgot the name of that says just that, and I think compiler manufacturers are the most aware of that adage.
Re:Fixing for Loops in Go 1.22 by minroot on Tuesday September 19, 2023 @04:04PM (#37575727)
Some time spent with Go gives a strong indication that Go team always has backwards compatibility in mind
Re:Fixing for Loops in Go 1.22 by jacquesm on Tuesday September 19, 2023 @04:26PM (#37576029)
They do. Go has avoided most of the pitfalls that other language eco-systems have fallen for over the years (backwards compatibility issues, soft forks masquerading as language improvements, re-booting the whole language under the same name, aggressively pushing down on other languages etc). They've done remarkably well in those respects, and should deserve huge credit for it.
Re:Fixing for Loops in Go 1.22 by omginternets on Tuesday September 19, 2023 @04:16PM (#37575892)
Seems like yes, though hopefully that should be rare.
Re:Fixing for Loops in Go 1.22 by nerdponx on Tuesday September 19, 2023 @04:59PM (#37576526)
Python has the same problem (to the extent that it's actually a problem, which you might or might not agree with), and this is the #1 reason they won't change it.
Re:Fixing for Loops in Go 1.22 by doctor_eval on Tuesday September 19, 2023 @05:18PM (#37576861)
To add to the other comments, in the run-up to go1.21 they talked about how they’d analysed a very large corpus of Go code to see what would be affected, and it was a very very small number.

I remember thinking that the number of people who have created inadvertent bugs due to this design (myself included) would be significantly greater than the number of people affected by the fix.

Re:Fixing for Loops in Go 1.22 by skywhopper on Tuesday September 19, 2023 @08:20PM (#37578894)
The original proposal for this change went into great detail about the research they did into existing uses of this syntax. In my memory, they found vanishingly few cases in the Google codebase or GitHub code where the change would violate the expected behavior. The decision to break the backwards compatibility here came only after determining how few codebases would be affected and developing a mechanism in Go itself (the version specification in go.mod) to require actively modifying the code to build with the new behavior.
Re:Fixing for Loops in Go 1.22 by beltsazar on Wednesday September 20, 2023 @03:05AM (#37581242)
Yes, the loopvar change will break some programs, and hence the compatibility promise. But the Go team argues that the change will fix much more programs than it will break [1].

This makes me wonder, though, what guarantees that a similar breaking change won't ever happen again in the future? If any change with #(programs fixed) >> #(programs broken) is accepted, we might as well remove the compatibility promise page [2].

---

[1] https://github.com/golang/go/issues/60078

[2] https://go.dev/doc/go1compat

Re:Fixing for Loops in Go 1.22 by baq on Wednesday September 20, 2023 @05:05AM (#37582048)
You may also wonder how many would it fix if it wasn't hidden behind a flag?