main.mx raw

   1  // len()/cap() on a channel must report the ring, not zero.
   2  //
   3  // stage4's len/cap lowering had no *TCChan arm, so it fell through to the
   4  // "return zero" default while legacy called runtime.chanLen/chanCap. Every
   5  // len(chan)/cap(chan) therefore read 0 under stage4 and disagreed with
   6  // legacy. The channel needs both a sender and a listener or the compiler
   7  // rejects it, so the body sends and receives.
   8  package main
   9  
  10  func main() {
  11  	ch := chan int32{4}
  12  	if cap(ch) != 4 {
  13  		panic("cap(chan) is not the ring capacity")
  14  	}
  15  	if len(ch) != 0 {
  16  		panic("len(chan) of a new channel is not 0")
  17  	}
  18  	ch <- 7
  19  	if len(ch) != 1 {
  20  		panic("len(chan) after one send is not 1")
  21  	}
  22  	if <-ch != 7 {
  23  		panic("receive returned the wrong value")
  24  	}
  25  	if len(ch) != 0 {
  26  		panic("len(chan) after receive is not 0")
  27  	}
  28  	println("ok")
  29  }
  30