]> Cypherpunks repositories - gostls13.git/commit
[release-branch.go1.21] cmd/compile/internal/typecheck: fix closure field naming
authorMatthew Dempsky <mdempsky@google.com>
Sat, 9 Sep 2023 01:04:31 +0000 (18:04 -0700)
committerGopher Robot <gobot@golang.org>
Fri, 13 Oct 2023 16:12:31 +0000 (16:12 +0000)
commitf9a31cda3c8a92e81989af4167c9ae5bfbb8ea5e
tree3ad83350a1862f4ca9aafd1ecfed163d502f6469
parent64b6c481075b9fce611af37baf1cce0144455784
[release-branch.go1.21] cmd/compile/internal/typecheck: fix closure field naming

When creating the struct type to hold variables captured by a function
literal, we currently reuse the captured variable names as fields.

However, there's no particular reason to do this: these struct types
aren't visible to users, and it adds extra complexity in making sure
fields belong to the correct packages.

Further, it turns out we were getting that subtly wrong. If two
function literals from different packages capture variables with
identical names starting with an uppercase letter (and in the same
order and with corresponding identical types) end up in the same
function (e.g., due to inlining), then we could end up creating
closure struct types that are "different" (i.e., not types.Identical)
yet end up with equal LinkString representations (which violates
LinkString's contract).

The easy fix is to just always use simple, exported, generated field
names in the struct. This should allow further struct reuse across
packages too, and shrink binary sizes slightly.

For #62498.
Fixes #62545.

Change-Id: I9c973f5087bf228649a8f74f7dc1522d84a26b51
Reviewed-on: https://go-review.googlesource.com/c/go/+/527135
Auto-Submit: Matthew Dempsky <mdempsky@google.com>
Reviewed-by: Cuong Manh Le <cuong.manhle.vn@gmail.com>
Reviewed-by: Keith Randall <khr@google.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
(cherry picked from commit e3ce3126212115808bc248bdc9ad92c0a46436fe)
Reviewed-on: https://go-review.googlesource.com/c/go/+/534916
Reviewed-by: Matthew Dempsky <mdempsky@google.com>
Auto-Submit: Dmitri Shuralyov <dmitshur@google.com>
Reviewed-by: Dmitri Shuralyov <dmitshur@google.com>
src/cmd/compile/internal/typecheck/func.go
src/cmd/compile/internal/types/fmt.go
test/fixedbugs/issue62498.dir/a.go [new file with mode: 0644]
test/fixedbugs/issue62498.dir/main.go [new file with mode: 0644]
test/fixedbugs/issue62498.go [new file with mode: 0644]