Source file test/fixedbugs/issue54343.go

     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  
     7  package main
     8  
     9  import "runtime"
    10  
    11  func main() {
    12  	if wait() {
    13  		panic("GC'd early")
    14  	}
    15  	m = nil
    16  	if !wait() {
    17  		panic("never GC'd")
    18  	}
    19  }
    20  
    21  var m = New[int]().M
    22  
    23  func New[X any]() *T[X] {
    24  	p := new(T[X])
    25  	runtime.SetFinalizer(p, func(*T[X]) { close(done) })
    26  	return p
    27  }
    28  
    29  type T[X any] [4]int // N.B., [4]int avoids runtime's tiny object allocator
    30  
    31  func (*T[X]) M() {}
    32  
    33  var done = make(chan int)
    34  
    35  func 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  

View as plain text