1// run 2 3// Copyright 2021 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 11type T [4]int // N.B., [4]int avoids runtime's tiny object allocator 12 13//go:noinline 14func g(x []*T) ([]*T, []*T) { return x, x } 15 16func main() { 17 const Jenny = 8675309 18 s := [10]*T{{Jenny}} 19 20 done := make(chan struct{}) 21 runtime.SetFinalizer(s[0], func(p *T) { close(done) }) 22 23 var h, _ interface{} = g(s[:]) 24 25 if wait(done) { 26 panic("GC'd early") 27 } 28 29 if h.([]*T)[0][0] != Jenny { 30 panic("lost Jenny's number") 31 } 32 33 if !wait(done) { 34 panic("never GC'd") 35 } 36} 37 38func wait(done <-chan struct{}) bool { 39 for i := 0; i < 10; i++ { 40 runtime.GC() 41 select { 42 case <-done: 43 return true 44 default: 45 } 46 } 47 return false 48} 49