main.mx raw

   1  // Regression: the missing-return check must accept every terminating shape.
   2  //
   3  // The check mirrors go/types' isTerminating, so an over-strict version would
   4  // reject valid code - and for the stdlib that means the compiler cannot build
   5  // itself. Each function below is a terminating statement go/types accepts; the
   6  // loop forms are never called, they only have to compile and be seen as
   7  // complete. The nested-switch case is the subtle one: the unlabelled break
   8  // belongs to the switch, not to the surrounding for, so the for still
   9  // terminates.
  10  package main
  11  
  12  func ifElse(b bool) (r int32) {
  13  	if b {
  14  		return 1
  15  	} else {
  16  		return 2
  17  	}
  18  }
  19  
  20  func infiniteFor() (r int32) {
  21  	n := int32(0)
  22  	for {
  23  		n++
  24  		if n > 2 {
  25  			return n
  26  		}
  27  	}
  28  }
  29  
  30  func switchDefault(x int32) (r int32) {
  31  	switch x {
  32  	case 1:
  33  		return 1
  34  	default:
  35  		return 2
  36  	}
  37  }
  38  
  39  func nestedSwitchBreak() (r int32) {
  40  	for {
  41  		switch 1 {
  42  		case 1:
  43  			break
  44  		}
  45  	}
  46  }
  47  
  48  func panicOnly() (r int32) {
  49  	panic("unreachable")
  50  }
  51  
  52  func main() {
  53  	if ifElse(true) != 1 {
  54  		panic("if/else")
  55  	}
  56  	if infiniteFor() != 3 {
  57  		panic("infinite for")
  58  	}
  59  	if switchDefault(9) != 2 {
  60  		panic("switch default")
  61  	}
  62  	println("ok")
  63  }
  64