const_fold_exact.mx raw

   1  /*
   2  Untyped integer constants wider than 64 bits must fold exactly whether they are
   3  written inline or named.
   4  
   5  The named form used to lose the high bits. A const declaration stores its value
   6  by converting it to the declared type, and ConvertConstLocal rebuilt the ConstInt
   7  with only its 64-bit slot - dropping the Exact *big.Int that the folder had
   8  produced - so `const hi = (1<<26-1) << 'A'` came back 0 at `hi >> 64` while the
   9  same expression written inline was exact. A silently wrong constant is the
  10  deception case, not a clean failure, so the two spellings are asserted EQUAL
  11  here: that oracle needs no hand-computed constant, and the four values below are
  12  small enough to check by hand ('A' is 65, so hi>>64 is (2^26-1)<<1).
  13  */
  14  package main
  15  
  16  const hi = (1 << 26 - 1) << 'A'
  17  const big = 1 << 70
  18  const mask = (1 << 64 - 1) << 8
  19  
  20  // The textproto bitmap shape that first exposed this: a mask literal wider than
  21  // 64 bits, whose halves both came back zero so every MIME header failed to parse.
  22  const bitmap = ((1 << 26 - 1) << 'A') | ((1 << 26 - 1) << 'a')
  23  
  24  func main() {
  25  	// Termination: a batch program must still be stoppable, so the entry
  26  	// handles the channel the runtime closes on SIGINT/SIGTERM.
  27  	select {
  28  	case <-shutdown:
  29  		return
  30  	default:
  31  	}
  32  
  33  	if got, want := int64(hi>>64), int64(134217726); got != want {
  34  		panic("named hi>>64 = " | got.String() | ", want " | want.String())
  35  	}
  36  	if got, want := int64(hi>>64), int64((((1 << 26 - 1) << 'A') >> 64)); got != want {
  37  		panic("named hi>>64 = " | got.String() | " but the same expression inline = " | want.String())
  38  	}
  39  	if got, want := int64(big>>64), int64(64); got != want {
  40  		panic("named big>>64 = " | got.String() | ", want 64")
  41  	}
  42  	if got, want := int64(big>>64), int64((1 << 70) >> 64); got != want {
  43  		panic("named big>>64 = " | got.String() | " but inline = " | want.String())
  44  	}
  45  	if got, want := int64(mask>>64), int64(255); got != want {
  46  		panic("named mask>>64 = " | got.String() | ", want 255")
  47  	}
  48  	if got := int64(bitmap >> 64); got == 0 {
  49  		panic("the high half of the >64-bit bitmap folded to zero")
  50  	}
  51  	if got, want := int64(bitmap>>64), int64((((1 << 26 - 1) << 'A') | ((1 << 26 - 1) << 'a')) >> 64); got != want {
  52  		panic("named bitmap>>64 = " | got.String() | " but inline = " | want.String())
  53  	}
  54  	println("untyped wide-constant folding ok")
  55  }
  56