Source file test/fixedbugs/issue44623.go

     1  // run
     2  
     3  // Copyright 2026 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  // An open-coded defer must not keep its closure reachable after the
     8  // deferred call has returned.
     9  
    10  package main
    11  
    12  import "runtime"
    13  
    14  var gone = make(chan bool, 1)
    15  
    16  // check runs last, after use(p) has returned.
    17  func check() {
    18  	runtime.GC()
    19  	runtime.GC()
    20  	// Deadlocks if the finalizer did not run.
    21  	<-gone
    22  }
    23  
    24  //go:noinline
    25  func use(p *[64]byte) {}
    26  
    27  func main() {
    28  	// Does not work with gccgo, which has a partially conservative GC.
    29  	if runtime.Compiler == "gccgo" {
    30  		return
    31  	}
    32  	p := new([64]byte)
    33  	runtime.SetFinalizer(p, func(*[64]byte) { gone <- true })
    34  	defer check()
    35  	defer use(p)
    36  }
    37  

View as plain text