1// run
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
7package main
8
9import "runtime"
10
11func main() {
12	if wait() {
13		panic("GC'd early")
14	}
15	m = nil
16	if !wait() {
17		panic("never GC'd")
18	}
19}
20
21var m = New[int]().M
22
23func New[X any]() *T[X] {
24	p := new(T[X])
25	runtime.SetFinalizer(p, func(*T[X]) { close(done) })
26	return p
27}
28
29type T[X any] [4]int // N.B., [4]int avoids runtime's tiny object allocator
30
31func (*T[X]) M() {}
32
33var done = make(chan int)
34
35func wait() bool {
36	for i := 0; i < 10; i++ {
37		runtime.GC()
38		select {
39		case <-done:
40			return true
41		default:
42		}
43	}
44	return false
45}
46