github.com/goplus/igop@v0.25.0/testdata/methprom.go (about)

     1  package main
     2  
     3  // Tests of method promotion logic.
     4  
     5  type A struct{ magic int }
     6  
     7  func (a A) x() {
     8  	if a.magic != 1 {
     9  		panic(a.magic)
    10  	}
    11  }
    12  func (a *A) y() *A {
    13  	return a
    14  }
    15  
    16  type B struct{ magic int }
    17  
    18  func (b B) p() {
    19  	if b.magic != 2 {
    20  		panic(b.magic)
    21  	}
    22  }
    23  func (b *B) q() {
    24  	if b != theC.B {
    25  		panic("oops")
    26  	}
    27  }
    28  
    29  type I interface {
    30  	f()
    31  }
    32  
    33  type impl struct{ magic int }
    34  
    35  func (i impl) f() {
    36  	if i.magic != 3 {
    37  		panic("oops")
    38  	}
    39  }
    40  
    41  type C struct {
    42  	A
    43  	*B
    44  	I
    45  }
    46  
    47  func assert(cond bool) {
    48  	if !cond {
    49  		panic("failed")
    50  	}
    51  }
    52  
    53  var theC = C{
    54  	A: A{1},
    55  	B: &B{2},
    56  	I: impl{3},
    57  }
    58  
    59  func addr() *C {
    60  	return &theC
    61  }
    62  
    63  func value() C {
    64  	return theC
    65  }
    66  
    67  func main() {
    68  	// address
    69  	addr().x()
    70  	if addr().y() != &theC.A {
    71  		panic("oops")
    72  	}
    73  	addr().p()
    74  	addr().q()
    75  	addr().f()
    76  
    77  	// addressable value
    78  	var c C = value()
    79  	c.x()
    80  	if c.y() != &c.A {
    81  		panic("oops")
    82  	}
    83  	c.p()
    84  	c.q()
    85  	c.f()
    86  
    87  	// non-addressable value
    88  	value().x()
    89  	// value().y() // not in method set
    90  	value().p()
    91  	value().q()
    92  	value().f()
    93  }