From: Matthew Dempsky Date: Wed, 12 Apr 2023 19:41:45 +0000 (-0700) Subject: [release-branch.go1.20] cmd/compile: fix reproducible build of aliased generic types X-Git-Tag: go1.20.4~7 X-Git-Url: http://www.git.cypherpunks.su/?a=commitdiff_plain;h=ee42d468f50e863f240a8b11e521feaf7f9114c9;p=gostls13.git [release-branch.go1.20] 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 #59585. Change-Id: Iaa55968cc7110b601e2f0f9b620901c2d55f7014 Reviewed-on: https://go-review.googlesource.com/c/go/+/484155 Reviewed-by: Keith Randall Auto-Submit: Matthew Dempsky TryBot-Result: Gopher Robot Run-TryBot: Matthew Dempsky Reviewed-by: Keith Randall Reviewed-by: Russ Cox (cherry picked from commit f58c6cccc44752146aabcd50a30865e34817a4b4) Reviewed-on: https://go-review.googlesource.com/c/go/+/484160 Reviewed-by: Dmitri Shuralyov Reviewed-by: Dmitri Shuralyov Auto-Submit: Dmitri Shuralyov --- diff --git a/src/cmd/compile/internal/noder/linker.go b/src/cmd/compile/internal/noder/linker.go index 0f39fdec05..5d0459d412 100644 --- a/src/cmd/compile/internal/noder/linker.go +++ b/src/cmd/compile/internal/noder/linker.go @@ -198,7 +198,7 @@ func (l *linker) relocObj(pr *pkgReader, idx pkgbits.Index) pkgbits.Index { l.exportBody(obj, local) } - if obj.Op() == ir.OTYPE { + if obj.Op() == ir.OTYPE && !obj.Alias() { if typ := obj.Type(); !typ.IsInterface() { for _, method := range typ.Methods().Slice() { l.exportBody(method.Nname.(*ir.Name), local) diff --git a/src/cmd/go/testdata/script/build_issue59571.txt b/src/cmd/go/testdata/script/build_issue59571.txt new file mode 100644 index 0000000000..2cf32594bf --- /dev/null +++ b/src/cmd/go/testdata/script/build_issue59571.txt @@ -0,0 +1,40 @@ +# Regression test for https://go.dev/issue/59571 +# Build should be reproducible, even with aliased generic types. + +go build -a -o 1.a +go build -a -o 2.a +cmp -q 1.a 2.a + +-- go.mod -- +module m + +go 1.20 +-- m.go -- +package m + +type ( + SliceFlag[T any] struct{} + + Alias1 = SliceFlag[[1]int] + Alias2 = SliceFlag[[2]int] + Alias3 = SliceFlag[[3]int] + Alias4 = SliceFlag[[4]int] + Alias5 = SliceFlag[[5]int] + Alias6 = SliceFlag[[6]int] + Alias7 = SliceFlag[[7]int] + Alias8 = SliceFlag[[8]int] + Alias9 = SliceFlag[[9]int] + Alias10 = SliceFlag[[10]int] + Alias11 = SliceFlag[[11]int] + Alias12 = SliceFlag[[12]int] + Alias13 = SliceFlag[[13]int] + Alias14 = SliceFlag[[14]int] + Alias15 = SliceFlag[[15]int] + Alias16 = SliceFlag[[16]int] + Alias17 = SliceFlag[[17]int] + Alias18 = SliceFlag[[18]int] + Alias19 = SliceFlag[[19]int] + Alias20 = SliceFlag[[20]int] +) + +func (x *SliceFlag[T]) String() string { return "zzz" }