mirror of
https://github.com/golang/go
synced 2024-09-04 23:44:16 +00:00
![Keith Randall](/assets/img/avatar_default.png)
The problem in 51355 is that escape analysis decided that the dictionary variable was captured by reference instead of by value. We want dictionaries to always be captured by value. Escape analysis was confused because it saw what it thought was a reassignment of the dictionary variable. In fact, it was the only assignment, it just wasn't marked as the defining assignment. Fix that. Add an assert to make sure this stays true. Fixes #51355 Change-Id: Ifd9342455fa107b113f5ff521a94cdbf1b8a7733 Reviewed-on: https://go-review.googlesource.com/c/go/+/388115 Trust: Keith Randall <khr@golang.org> Run-TryBot: Keith Randall <khr@golang.org> Trust: Cuong Manh Le <cuong.manhle.vn@gmail.com> Trust: Dan Scales <danscales@google.com> Reviewed-by: Cuong Manh Le <cuong.manhle.vn@gmail.com> Reviewed-by: Dan Scales <danscales@google.com> TryBot-Result: Gopher Robot <gobot@golang.org>
32 lines
503 B
Go
32 lines
503 B
Go
// compile -G=3
|
|
|
|
// Copyright 2022 The Go Authors. All rights reserved.
|
|
// Use of this source code is governed by a BSD-style
|
|
// license that can be found in the LICENSE file.
|
|
|
|
package main
|
|
|
|
type Cache[E comparable] struct {
|
|
adder func(...E)
|
|
}
|
|
|
|
func New[E comparable]() *Cache[E] {
|
|
c := &Cache[E]{}
|
|
|
|
c.adder = func(elements ...E) {
|
|
for _, value := range elements {
|
|
value := value
|
|
go func() {
|
|
println(value)
|
|
}()
|
|
}
|
|
}
|
|
|
|
return c
|
|
}
|
|
|
|
func main() {
|
|
c := New[string]()
|
|
c.adder("test")
|
|
}
|