Open
Description
Go version
go version go1.22.0 linux/amd64
Output of go env
in your module/workspace:
.
What did you do?
package main
import (
"fmt"
"strings"
)
func foo() string {
for b, i := (strings.Builder{}), byte('a'); ; i++ {
b.WriteByte(i)
if i == 'z' {
return b.String()
}
}
}
func bar(callback func(*strings.Builder)) string {
for b, i := (strings.Builder{}), byte('a'); ; i++ {
b.WriteByte(i)
callback(&b) // <-- difference here
if i == 'z' {
return b.String()
}
}
}
func main() {
debugProcess := func(pb *strings.Builder) {
// do nothing
}
fmt.Println("foo:", foo())
fmt.Println("bar:", bar(debugProcess))
}
What did you see happen?
Consistent behavior between foo
and bar
with 1.22 compiler.
What did you expect to see?
Inconsistent behavior between foo
and bar
with 1.22 compiler.
Metadata
Metadata
Assignees
Labels
Type
Projects
Status
In Progress
Milestone
Relationships
Development
No branches or pull requests
Activity
[-]cmd/compile: incosistent behavior since Go 1.22[/-][+]cmd/compile: loopvar can trigger nocopy detection[/+][-]cmd/compile: loopvar can trigger nocopy detection[/-][+]cmd/compile: loopvar doesn't trigger nocopy detection[/+]zigo101 commentedon Mar 2, 2024
The problem should exist for every value which contains self-reference pointers when it is used as loop variables.(edit: looks not true)ianlancetaylor commentedon Mar 2, 2024
CC @dr2chase
zigo101 commentedon Mar 3, 2024
In the following program,
bar
function panics, butfoo
doesn't.The fact reflects that, the compiler will check whether or not a declared
function will put the references of the argument somewhere after the
function exits, but not check this for local anonymous functions.
dr2chase commentedon Mar 5, 2024
The panic I get is
Is that what you get? You could have provided this information in your bug report and it would have been helpful.
zigo101 commentedon Mar 6, 2024
Yes, we should get it for both functions (
foo
andbar
), but only get it for the second one (bar
).zigo101 commentedon Mar 6, 2024
An example to extend one of my previous comment:
zigo101 commentedon Mar 6, 2024
BTW, such cases can never happen in JS world. So this is a fundamental difference from JS.
dr2chase commentedon Mar 6, 2024
Is this causing problems in a real program? Also, in your examples, it is helpful if you provide information about what happened when you ran it. Just because I observe a problem when I run it, does not mean I observe the same problem.
zigo101 commentedon Mar 6, 2024
? The problem is just there. I just expected the behavior doesn't change when adding/removing a no-op line.
The example is real.
3 remaining items
rsc commentedon Mar 6, 2024
The original proposal explicitly acknowledged that there would be corner-case programs that might be affected in one way or another. That's why the change is keyed by the language version in go.mod and the //go:build lines, so that you can update your code gradually to be safe for the new semantics, instead of having to convert an entire program all at once.
The compiler is doing a safe transformation. The use of unsafe in strings.Builder is the unsafe part, and your example is a casualty. But in this case at least, there is no reason to write the code this way. Basically everyone who writes Go would declare b above the loop.
I don't think there's anything to do here.
zigo101 commentedon Mar 7, 2024
It would be great that Go official maintain a list of such so-called corner cases. Doing this will be much helpful to all Go programmers. BTW, I maintain an incomplete one.
This needs much more publicity. Hope the just mentioned article would help some.
But no matter how it's publicized, there will always be gophers who don't get the message.
So this is really a dangerous point for some Go projects.
I've had bad luck. :D I really didn't know that
strings.Builder
usesunsafe
.I will check the implementation when using a std function later
and avoid using it when the implantation uses
unsafe
.My reason is: the syntax allows it and I like the flexibility the old semantics provide.
Prior to Go 1.22, I can't vouch for that myself.
Since Go 1.22, I surely will, because I be fully aware of the differences between the new and old semantics.
But there will always some gophers who are not. Again, this needs much more publicity.
This issue was created mainly to make Go compiler more rigorous.
If it is not so important, just put it aside.
dr2chase commentedon Mar 7, 2024
This corner case, as a source of possible Go errors, is not a large one. I know we have a tendency to view language definition changes as a sort of "broken promise" that somehow makes those potential bug causes loom larger than all the working-as-intended foot-guns ("the programmer should have known") but at scale, what matters is the rate/cost of bugs, no matter their cause. Assume code needs maintenance, I have never seen a piece of code of any significance that lacked a bug.
I extended the vet nocopy check to cover
strings.Builder
andfor builder := BuilderTypedExpressions
and scanned a large amount of Go code, and got very few hits of any kind, and on an eyeball scan, none using the for loop. I'm going to refine it to call out specifically this case and run it again, and it will probably get turned into a regular vet check because the "false" positive rate is very low (and by "false", I mean "why are you returning astrings.Builder
instead of converting it to astring
and returning that? The only safe thing you can do with thatBuilder
is turn it into astring
.")For comparison, on this sample, I saw 152 occurrences of any kind of copying a
strings.Builder
, while the lock-oriented nocopy check fired at least 4848 times (I do not know the upper limit, I capped results at 5000, so that minus 152).I'm benchmarking an alternate fix for this problem, but even if Builders become copy-able, this same problem would affect the other nocopy types, so vet needs a modification for that (and that small change probably does not require a proposal).
gopherbot commentedon Mar 8, 2024
Change https://go.dev/cl/570137 mentions this issue:
go/analysis/passes: update copylock check for strings.Builder and 1.22 loopvar