]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/compile: emit types of constants which are instantiated generic types
authorKeith Randall <khr@golang.org>
Sat, 26 Feb 2022 00:06:53 +0000 (16:06 -0800)
committerKeith Randall <khr@golang.org>
Mon, 28 Feb 2022 15:50:51 +0000 (15:50 +0000)
Normally types of constants are emitted when the type is defined (an
ODCLTYPE). However, the types of constants where the type is an
instantiated generic type made inside the constant declaration, do not
normally get emitted. But the DWARF processor in the linker wants
to see those types. So we emit them during stenciling.

Fixes #51245

Change-Id: I59f20f1d7b91501c9ac760cf839a354356331fc6
Reviewed-on: https://go-review.googlesource.com/c/go/+/388117
Trust: Keith Randall <khr@golang.org>
Run-TryBot: Keith Randall <khr@golang.org>
TryBot-Result: Gopher Robot <gobot@golang.org>
Reviewed-by: Cherry Mui <cherryyz@google.com>
src/cmd/compile/internal/gc/obj.go
test/typeparam/issue51245.go [new file with mode: 0644]

index 5353435ed1174c538a2a502b9b9d7e13c67df93a..74e4c0a89005d4fdf1369dffbe61be745b1b1dd8 100644 (file)
@@ -217,6 +217,10 @@ func dumpGlobalConst(n ir.Node) {
                if ir.ConstOverflow(v, t) {
                        return
                }
+       } else {
+               // If the type of the constant is an instantiated generic, we need to emit
+               // that type so the linker knows about it. See issue 51245.
+               _ = reflectdata.TypeLinksym(t)
        }
        base.Ctxt.DwarfIntConst(base.Ctxt.Pkgpath, n.Sym().Name, types.TypeSymName(t), ir.IntVal(t, v))
 }
diff --git a/test/typeparam/issue51245.go b/test/typeparam/issue51245.go
new file mode 100644 (file)
index 0000000..bd4f7c5
--- /dev/null
@@ -0,0 +1,16 @@
+// build -gcflags=-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 T[P any] int
+const C T[int] = 3
+
+type T2 int
+const C2 T2 = 9
+
+func main() {
+}