Source file src/cmd/link/internal/ld/testdata/deadcode/structof_funcof.go

     1  // Copyright 2023 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  // Methods of reflect.rtype use StructOf and FuncOf which in turn depend on
     6  // reflect.Value.Method. StructOf and FuncOf must not disable the DCE.
     7  
     8  package main
     9  
    10  import "reflect"
    11  
    12  type S int
    13  
    14  func (s S) M() { println("S.M") }
    15  
    16  func (s S) N() { println("S.N") }
    17  
    18  type T float64
    19  
    20  func (t T) F(s S) {}
    21  
    22  func useStructOf() {
    23  	t := reflect.StructOf([]reflect.StructField{
    24  		{
    25  			Name: "X",
    26  			Type: reflect.TypeOf(int(0)),
    27  		},
    28  	})
    29  	println(t.Name())
    30  }
    31  
    32  func useFuncOf() {
    33  	t := reflect.FuncOf(
    34  		[]reflect.Type{reflect.TypeOf(int(0))},
    35  		[]reflect.Type{reflect.TypeOf(int(0))},
    36  		false,
    37  	)
    38  	println(t.Name())
    39  }
    40  
    41  func main() {
    42  	useStructOf()
    43  	useFuncOf()
    44  
    45  	var t T
    46  	meth, _ := reflect.TypeOf(t).MethodByName("F")
    47  	ft := meth.Type
    48  	at := ft.In(1)
    49  	v := reflect.New(at).Elem()
    50  	methV := v.MethodByName("M")
    51  	methV.Call([]reflect.Value{v})
    52  }
    53  

View as plain text