modernc.org/gc@v1.0.1-0.20240304020402-f0dba7c97c2b/testdata/errchk/test/method7.go (about)

     1  // run
     2  
     3  // Copyright 2017 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  // Test forms of method expressions T.m where T is
     8  // a literal type.
     9  
    10  package main
    11  
    12  var got, want string
    13  
    14  type I interface {
    15  	m()
    16  }
    17  
    18  type S struct {
    19  }
    20  
    21  func (S) m()          { got += " m()" }
    22  func (S) m1(s string) { got += " m1(" + s + ")" }
    23  
    24  type T int
    25  
    26  func (T) m2() { got += " m2()" }
    27  
    28  func main() {
    29  	// method expressions with named receiver types
    30  	I.m(S{})
    31  	want += " m()"
    32  
    33  	S.m1(S{}, "a")
    34  	want += " m1(a)"
    35  
    36  	// method expressions with literal receiver types
    37  	f := interface{ m1(string) }.m1
    38  	f(S{}, "b")
    39  	want += " m1(b)"
    40  
    41  	interface{ m1(string) }.m1(S{}, "c")
    42  	want += " m1(c)"
    43  
    44  	x := S{}
    45  	interface{ m1(string) }.m1(x, "d")
    46  	want += " m1(d)"
    47  
    48  	// cannot link the call below - see #22444
    49  	// g := struct{ T }.m2
    50  	// g(struct{T}{})
    51  	// want += " m2()"
    52  
    53  	if got != want {
    54  		panic("got" + got + ", want" + want)
    55  	}
    56  }