multi_value_var.mx raw

   1  // Regression: `var a, b T = x, y` gives each name its own value. stage4 built
   2  // the value list once and stored one value to every name - the first in the
   3  // function-scope path, the last in the package-scope path - which is what
   4  // broke `var pow, sq uint32 = 1, PrimeRK` in bytealg.HashStrBytes: the
   5  // rolling-hash factor came out wrong and bytealg.IndexRabinKarp answered -1
   6  // for needles that are present, so bytes.Index could miss a present substring.
   7  package main
   8  
   9  import "fmt"
  10  
  11  // The package-scope form goes through the variable initializer function, a
  12  // different builder path from the function-scope form below. Only the
  13  // []byte("literal") form may carry an initializer at package scope - the loader
  14  // demotes it to a constant and both compilers allow it - so the numeric pairs
  15  // sit inside main(), which is the other path this regression covers.
  16  var gs1, gs2 []byte = []byte("x"), []byte("yz")
  17  
  18  func main() {
  19  	if len(gs1) != 1 || len(gs2) != 2 {
  20  		panic("package slice pair")
  21  	}
  22  	var g1, g2 int32 = 3, 4
  23  	var gu1, gu2 uint32 = 1, 16777619
  24  	var gf1, gf2 float64 = 1.5, 2.5
  25  	if g1 != 3 || g2 != 4 {
  26  		panic("int32 pair")
  27  	}
  28  	if gu1 != 1 || gu2 != 16777619 {
  29  		panic("uint32 pair")
  30  	}
  31  	if gf1 != 1.5 || gf2 != 2.5 {
  32  		panic("float64 pair")
  33  	}
  34  	var a, b int32 = 3, 4
  35  	if a != 3 || b != 4 {
  36  		panic("int32 pair")
  37  	}
  38  	var pow, sq uint32 = 1, 16777619
  39  	if pow != 1 || sq != 16777619 {
  40  		panic("uint32 pair")
  41  	}
  42  	var f, g float64 = 1.5, 2.5
  43  	if f != 1.5 || g != 2.5 {
  44  		panic("float64 pair")
  45  	}
  46  	var s, t []byte = []byte("x"), []byte("yz")
  47  	if len(s) != 1 || len(t) != 2 {
  48  		panic("slice pair")
  49  	}
  50  	// The same shape inside a loop body, which is where HashStrBytes uses it.
  51  	total := int32(0)
  52  	for k := int32(0); k < 3; k++ {
  53  		var p, q int32 = k, k + 10
  54  		total += p + q
  55  	}
  56  	if total != 3+33 {
  57  		panic("loop pair")
  58  	}
  59  	fmt.Println("ok")
  60  }
  61