select_comma_ok_assign.mx raw

   1  // Regression: the comma-ok receive in an ASSIGNMENT-form select case.
   2  //
   3  // `case v, ok = <-ch:` (two names, `=`) is the pair form, exactly like
   4  // `case v, ok := <-ch:`. stage4 recognised the pair only when the statement was
   5  // a definition (token.Def), so the assignment form bound the received value to
   6  // BOTH names - giving the bool `ok` the channel element's type:
   7  //
   8  //	case incoming, ok = <-ln.incoming:   // net's fakeConnect
   9  //	  -> cannot use value of type []*net.netFD as bool in assignment
  10  //
  11  // That was the last error blocking net/http for wasm. The value must come from
  12  // this case's receive slot and ok from the select tuple's recvOk slot, and a
  13  // closed channel must report ok == false.
  14  package main
  15  
  16  func main() {
  17  	// Termination: a batch program must still be stoppable, so the entry
  18  	// handles the channel the runtime closes on SIGINT/SIGTERM.
  19  	select {
  20  	case <-shutdown:
  21  		return
  22  	default:
  23  	}
  24  
  25  	// A one-slot buffered channel with a value waiting.
  26  	ch := chan int32{1}
  27  	ch <- 7
  28  
  29  	var v int32
  30  	var ok bool
  31  	select {
  32  	case v, ok = <-ch:
  33  	default:
  34  		panic("the buffered value should have been received")
  35  	}
  36  	if !ok {
  37  		panic("ok must be true while the channel is open")
  38  	}
  39  	if v != 7 {
  40  		panic("wrong value received")
  41  	}
  42  
  43  	// A closed and drained channel reports ok == false, not a stale value.
  44  	close(ch)
  45  	v = 0
  46  	ok = true
  47  	select {
  48  	case v, ok = <-ch:
  49  	}
  50  	if ok {
  51  		panic("ok must be false after the channel is closed and drained")
  52  	}
  53  
  54  	// The definition form must keep working.
  55  	ch2 := chan int32{1}
  56  	ch2 <- 5
  57  	select {
  58  	case got, open := <-ch2:
  59  		if !open || got != 5 {
  60  			panic("definition form broke")
  61  		}
  62  	default:
  63  		panic("the buffered value should have been received")
  64  	}
  65  
  66  	println("ok")
  67  }
  68