chan_named_elem.mx raw

   1  // A container's element type survives its NAME and its INSTANTIATION.
   2  //
   3  // `type Q[T cmp.Ordered] chan T`, `type S[T cmp.Ordered] []T` and
   4  // `type M[T cmp.Ordered] map[int32]T` are ordinary values. An operation on one
   5  // must see the element the instantiation binds, not the bare type parameter,
   6  // which has no layout: stage4 used to print empty values for all three while
   7  // legacy printed the numbers (a bilateral divergence in exactly the machinery
   8  // the phase-0c ownership types stand on), and the emitter's fallback turned a
   9  // channel receive into an i8 store that clang rejected.
  10  //
  11  //:wasmtest skip
  12  package main
  13  
  14  import (
  15  	"cmp"
  16  	"fmt"
  17  )
  18  
  19  type Q[T cmp.Ordered] chan T
  20  type S[T cmp.Ordered] []T
  21  type M[T cmp.Ordered] map[int32]T
  22  
  23  type Sender[T cmp.Ordered] chan<- T
  24  type Receiver[T cmp.Ordered] <-chan T
  25  
  26  func sendThrough(c Q[int32]) {
  27  	c <- 5
  28  }
  29  
  30  func main() {
  31  	select {
  32  	case <-shutdown:
  33  		return
  34  	default:
  35  	}
  36  	var q Q[int32] = Q[int32](chan int32{:1})
  37  	sendThrough(q)
  38  	if (<-q) != 5 {
  39  		panic("generic named channel lost its element")
  40  	}
  41  
  42  	var s S[int32] = S[int32]([]int32{:2})
  43  	s[0] = 7
  44  	if s[0] != 7 {
  45  		panic("generic named slice lost its element")
  46  	}
  47  
  48  	var m M[int32] = M[int32](map[int32]int32{})
  49  	m[3] = 11
  50  	if m[3] != 11 {
  51  		panic("generic named map lost its value type")
  52  	}
  53  
  54  	ch := chan int32{:1}
  55  	var ps Sender[int32] = Sender[int32](ch)
  56  	var pr Receiver[int32] = Receiver[int32](ch)
  57  	ps <- 42
  58  	if (<-pr) != 42 {
  59  		panic("directional named channel lost its element")
  60  	}
  61  	fmt.Println("named element types ok")
  62  }
  63