github.com/jd-ly/tools@v0.5.7/go/ssa/interp/testdata/fieldprom.go (about)

     1  package main
     2  
     3  // Tests of field promotion logic.
     4  
     5  type A struct {
     6  	x int
     7  	y *int
     8  }
     9  
    10  type B struct {
    11  	p int
    12  	q *int
    13  }
    14  
    15  type C struct {
    16  	A
    17  	*B
    18  }
    19  
    20  type D struct {
    21  	a int
    22  	C
    23  }
    24  
    25  func assert(cond bool) {
    26  	if !cond {
    27  		panic("failed")
    28  	}
    29  }
    30  
    31  func f1(c C) {
    32  	assert(c.x == c.A.x)
    33  	assert(c.y == c.A.y)
    34  	assert(&c.x == &c.A.x)
    35  	assert(&c.y == &c.A.y)
    36  
    37  	assert(c.p == c.B.p)
    38  	assert(c.q == c.B.q)
    39  	assert(&c.p == &c.B.p)
    40  	assert(&c.q == &c.B.q)
    41  
    42  	c.x = 1
    43  	*c.y = 1
    44  	c.p = 1
    45  	*c.q = 1
    46  }
    47  
    48  func f2(c *C) {
    49  	assert(c.x == c.A.x)
    50  	assert(c.y == c.A.y)
    51  	assert(&c.x == &c.A.x)
    52  	assert(&c.y == &c.A.y)
    53  
    54  	assert(c.p == c.B.p)
    55  	assert(c.q == c.B.q)
    56  	assert(&c.p == &c.B.p)
    57  	assert(&c.q == &c.B.q)
    58  
    59  	c.x = 1
    60  	*c.y = 1
    61  	c.p = 1
    62  	*c.q = 1
    63  }
    64  
    65  func f3(d D) {
    66  	assert(d.x == d.C.A.x)
    67  	assert(d.y == d.C.A.y)
    68  	assert(&d.x == &d.C.A.x)
    69  	assert(&d.y == &d.C.A.y)
    70  
    71  	assert(d.p == d.C.B.p)
    72  	assert(d.q == d.C.B.q)
    73  	assert(&d.p == &d.C.B.p)
    74  	assert(&d.q == &d.C.B.q)
    75  
    76  	d.x = 1
    77  	*d.y = 1
    78  	d.p = 1
    79  	*d.q = 1
    80  }
    81  
    82  func f4(d *D) {
    83  	assert(d.x == d.C.A.x)
    84  	assert(d.y == d.C.A.y)
    85  	assert(&d.x == &d.C.A.x)
    86  	assert(&d.y == &d.C.A.y)
    87  
    88  	assert(d.p == d.C.B.p)
    89  	assert(d.q == d.C.B.q)
    90  	assert(&d.p == &d.C.B.p)
    91  	assert(&d.q == &d.C.B.q)
    92  
    93  	d.x = 1
    94  	*d.y = 1
    95  	d.p = 1
    96  	*d.q = 1
    97  }
    98  
    99  func main() {
   100  	y := 123
   101  	c := C{
   102  		A{x: 42, y: &y},
   103  		&B{p: 42, q: &y},
   104  	}
   105  
   106  	assert(&c.x == &c.A.x)
   107  
   108  	f1(c)
   109  	f2(&c)
   110  
   111  	d := D{C: c}
   112  	f3(d)
   113  	f4(&d)
   114  }