Skip to content

Commit f58c6cc

Browse files
mdempskygopherbot
authored andcommitted
cmd/compile: fix reproducible build of aliased generic types
Due to a missing "&& !alias" check, the unified linker was treating type aliases the same as defined types for the purpose of exporting method bodies. The methods will get exported anyway alongside the aliased type, so this mistake is normally harmless. However, if multiple type aliases instantiated the same generic type but with different type arguments, this could result in the same (generic) method body being exported multiple times under different symbol names. Further, because bodies aren't expected to be exported multiple times, we were sorting them simply based on index. And consequently, the sort wasn't total and is sensitive to the map iteration order used while ranging over linker.bodies. The fix is simply to add the missing "&& !alias" check, so that we don't end up with duplicate bodies in the first place. Thanks rsc@ for providing a minimal repro case. Fixes #59571. Change-Id: Iaa55968cc7110b601e2f0f9b620901c2d55f7014 Reviewed-on: https://go-review.googlesource.com/c/go/+/484155 Reviewed-by: Keith Randall <[email protected]> Auto-Submit: Matthew Dempsky <[email protected]> TryBot-Result: Gopher Robot <[email protected]> Run-TryBot: Matthew Dempsky <[email protected]> Reviewed-by: Keith Randall <[email protected]> Reviewed-by: Russ Cox <[email protected]>
1 parent 12e65db commit f58c6cc

File tree

2 files changed

+41
-1
lines changed

2 files changed

+41
-1
lines changed

src/cmd/compile/internal/noder/linker.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ func (l *linker) relocObj(pr *pkgReader, idx pkgbits.Index) pkgbits.Index {
199199
l.exportBody(obj, local)
200200
}
201201

202-
if obj.Op() == ir.OTYPE {
202+
if obj.Op() == ir.OTYPE && !obj.Alias() {
203203
if typ := obj.Type(); !typ.IsInterface() {
204204
for _, method := range typ.Methods().Slice() {
205205
l.exportBody(method.Nname.(*ir.Name), local)
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# Regression test for https://go.dev/issue/59571
2+
# Build should be reproducible, even with aliased generic types.
3+
4+
go build -a -o 1.a
5+
go build -a -o 2.a
6+
cmp -q 1.a 2.a
7+
8+
-- go.mod --
9+
module m
10+
11+
go 1.20
12+
-- m.go --
13+
package m
14+
15+
type (
16+
SliceFlag[T any] struct{}
17+
18+
Alias1 = SliceFlag[[1]int]
19+
Alias2 = SliceFlag[[2]int]
20+
Alias3 = SliceFlag[[3]int]
21+
Alias4 = SliceFlag[[4]int]
22+
Alias5 = SliceFlag[[5]int]
23+
Alias6 = SliceFlag[[6]int]
24+
Alias7 = SliceFlag[[7]int]
25+
Alias8 = SliceFlag[[8]int]
26+
Alias9 = SliceFlag[[9]int]
27+
Alias10 = SliceFlag[[10]int]
28+
Alias11 = SliceFlag[[11]int]
29+
Alias12 = SliceFlag[[12]int]
30+
Alias13 = SliceFlag[[13]int]
31+
Alias14 = SliceFlag[[14]int]
32+
Alias15 = SliceFlag[[15]int]
33+
Alias16 = SliceFlag[[16]int]
34+
Alias17 = SliceFlag[[17]int]
35+
Alias18 = SliceFlag[[18]int]
36+
Alias19 = SliceFlag[[19]int]
37+
Alias20 = SliceFlag[[20]int]
38+
)
39+
40+
func (x *SliceFlag[T]) String() string { return "zzz" }

0 commit comments

Comments
 (0)