Source file test/fixedbugs/issue25897a.go

     1  // run
     2  
     3  // Copyright 2019 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  // Make sure the runtime can scan args of an unstarted goroutine
     8  // which starts with a reflect-generated function.
     9  
    10  package main
    11  
    12  import (
    13  	"reflect"
    14  	"runtime"
    15  )
    16  
    17  const N = 100
    18  
    19  func main() {
    20  	runtime.GOMAXPROCS(1)
    21  	// Run GC in a loop. This makes it more likely GC will catch
    22  	// an unstarted goroutine then if we were to GC after kicking
    23  	// everything off.
    24  	go func() {
    25  		for {
    26  			runtime.GC()
    27  		}
    28  	}()
    29  	c := make(chan bool, N)
    30  	for i := 0; i < N; i++ {
    31  		// Test both with an argument and without because this
    32  		// affects whether the compiler needs to generate a
    33  		// wrapper closure for the "go" statement.
    34  		f := reflect.MakeFunc(reflect.TypeOf(((func(*int))(nil))),
    35  			func(args []reflect.Value) []reflect.Value {
    36  				c <- true
    37  				return nil
    38  			}).Interface().(func(*int))
    39  		go f(nil)
    40  
    41  		g := reflect.MakeFunc(reflect.TypeOf(((func())(nil))),
    42  			func(args []reflect.Value) []reflect.Value {
    43  				c <- true
    44  				return nil
    45  			}).Interface().(func())
    46  		go g()
    47  	}
    48  	for i := 0; i < N*2; i++ {
    49  		<-c
    50  	}
    51  }
    52  

View as plain text