...

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

Documentation: cmd/link/internal/ld/testdata/deadcode

     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  // Like ifacemethod2.go, this tests that a method *is* live
     6  // if the type is "indirectly" converted to an interface
     7  // using reflection with a method descriptor as intermediate.
     8  // However, it uses MethodByName() with a constant name of
     9  // a method to look up. This does not disable the DCE like
    10  // Method(0) does.
    11  
    12  package main
    13  
    14  import "reflect"
    15  
    16  type S int
    17  
    18  func (s S) M() { println("S.M") }
    19  
    20  type I interface{ M() }
    21  
    22  type T float64
    23  
    24  func (t T) F(s S) {}
    25  
    26  func main() {
    27  	var t T
    28  	meth, _ := reflect.TypeOf(t).MethodByName("F")
    29  	ft := meth.Type
    30  	at := ft.In(1)
    31  	v := reflect.New(at).Elem()
    32  	v.Interface().(I).M()
    33  }
    34  

View as plain text