ptr_recv_array_elem.mx raw

   1  // Regression: a pointer-receiver method called on an element of an array
   2  // reached through a pointer passed the element's value instead of its
   3  // address. The call's signature then disagreed with the method definition,
   4  // which the wasm validator rejected (a struct by value where wasm32 expects
   5  // an i32 pointer, "type mismatch in call"), and a method that wrote to its
   6  // receiver wrote to a copy.
   7  //
   8  // The receiver form is now chosen from the selection the type checker
   9  // recorded: a pointer-receiver method gets the operand's address.
  10  package main
  11  
  12  type cell struct {
  13  	vals [4]int32
  14  }
  15  
  16  type grid struct {
  17  	cells [2]cell
  18  }
  19  
  20  func (c *cell) bump(i int32) {
  21  	c.vals[i] = c.vals[i] + 10
  22  }
  23  
  24  func newGrid() (g *grid) {
  25  	g = &grid{}
  26  	return g
  27  }
  28  
  29  func main() {
  30  	g := newGrid()
  31  	g.cells[0].vals[1] = 1
  32  	g.cells[0].bump(1)
  33  	println("a=", int64(g.cells[0].vals[1]))
  34  
  35  	g.cells[1].vals[3] = 5
  36  	g.cells[1].bump(3)
  37  	println("b=", int64(g.cells[1].vals[3]))
  38  
  39  	// Slice elements are addressable too, and the receiver must still be the
  40  	// element itself.
  41  	cells := []cell{{}, {}}
  42  	cells[1].vals[0] = 7
  43  	cells[1].bump(0)
  44  	println("c=", int64(cells[1].vals[0]))
  45  }
  46