From ac78501b9c4f7458c6b4352b1590db058d9ac27c Mon Sep 17 00:00:00 2001 From: Dan Scales Date: Thu, 5 Aug 2021 23:26:21 -0700 Subject: [dev.typeparams] cmd/compile: make sure closures inside generic funcs are not compiled Closures inside generic functions were being added to the g.target.Decls list during noding, just like other closures. We remove generic functions/methods from g.target.Decls, so they don't get compiled (they're only available for export and stenciling). Most closures inside generic functions/methods were similarly being removed from g.target.Decls, because they have a generic parameter. But we need to ensure no closures in generic function/methods are left remaining in g.target.Decls, since we don't want them transformed and compiled. So, we set a flag in (*irgen) that records when we are noding a top-level generic function/method, and don't add any closures to g.target.Decls when the flag is true. Updates #47514 Change-Id: Id66b4c41d307ffa8f54cab6ce3646ade81606862 Reviewed-on: https://go-review.googlesource.com/c/go/+/340258 Trust: Dan Scales Reviewed-by: Keith Randall --- test/typeparam/issue47514.go | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 test/typeparam/issue47514.go (limited to 'test') diff --git a/test/typeparam/issue47514.go b/test/typeparam/issue47514.go new file mode 100644 index 0000000000..947f254003 --- /dev/null +++ b/test/typeparam/issue47514.go @@ -0,0 +1,20 @@ +// run -gcflags=-G=3 + +// Copyright 2021 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. + +// Test that closures inside a generic function are not exported, +// even though not themselves generic. + +package main + +func Do[T any]() { + _ = func() string { + return "" + } +} + +func main() { + Do[int]() +} -- cgit v1.2.3-54-g00ecf