Skip to content

Commit 0907d57

Browse files
committed
cmd/compile: emit types of constants which are instantiated generic types
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 <[email protected]> Run-TryBot: Keith Randall <[email protected]> TryBot-Result: Gopher Robot <[email protected]> Reviewed-by: Cherry Mui <[email protected]>
1 parent 9c4a862 commit 0907d57

File tree

2 files changed

+20
-0
lines changed

2 files changed

+20
-0
lines changed

src/cmd/compile/internal/gc/obj.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,10 @@ func dumpGlobalConst(n ir.Node) {
217217
if ir.ConstOverflow(v, t) {
218218
return
219219
}
220+
} else {
221+
// If the type of the constant is an instantiated generic, we need to emit
222+
// that type so the linker knows about it. See issue 51245.
223+
_ = reflectdata.TypeLinksym(t)
220224
}
221225
base.Ctxt.DwarfIntConst(base.Ctxt.Pkgpath, n.Sym().Name, types.TypeSymName(t), ir.IntVal(t, v))
222226
}

test/typeparam/issue51245.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// build -gcflags=-G=3
2+
3+
// Copyright 2022 The Go Authors. All rights reserved.
4+
// Use of this source code is governed by a BSD-style
5+
// license that can be found in the LICENSE file.
6+
7+
package main
8+
9+
type T[P any] int
10+
const C T[int] = 3
11+
12+
type T2 int
13+
const C2 T2 = 9
14+
15+
func main() {
16+
}

0 commit comments

Comments
 (0)