generic_aggregate_subst.mx raw

   1  // Regression: monomorphization must substitute type parameters inside
   2  // AGGREGATES, not only bare ones.
   3  //
   4  // substTypeParams had cases for TypeParam, Pointer, Slice and Signature only,
   5  // so a field like `m map[K]V` or `s []entry[K,V]` came back still holding the
   6  // parameter. llvmType has no case for a TypeParam and its fallback is "i8", so
   7  // mapping[string,*node]'s map slot was `alloca i8` receiving a {ptr,i64,i64}
   8  // string key and entry[K,V] lowered to the layout {i8, i8} while its
   9  // substituted field stores used the real shapes. net/http's routing tree
  10  // (mapping[K,V] with entry[K,V]) failed to compile as
  11  //   '%t64' defined with type '{ ptr, i64, i64 }' but expected 'i8'
  12  //     store i8 %t64, ptr %mu18
  13  // and this file reproduces that verbatim on the pre-fix compiler.
  14  //
  15  // Substitution now walks TCMap, Array, TCChan, TCStruct and Named, and an
  16  // instantiated Named carries the underlying type with its parameters bound, so
  17  // the aggregate layout and every field access describe the same struct.
  18  package main
  19  
  20  import "moxielang.dev/pkg/mxutil"
  21  
  22  type entry[K comparable, V comparable] struct {
  23  	key   K
  24  	value V
  25  }
  26  
  27  type mapping[K comparable, V comparable] struct {
  28  	s []entry[K, V]
  29  	m map[K]V
  30  }
  31  
  32  const maxSlice = 8
  33  
  34  func (h *mapping[K, V]) add(k K, v V) {
  35  	if h.m == nil && len(h.s) < maxSlice {
  36  		h.s = mxutil.Ensure(h.s, 1)
  37  		h.s = push(h.s, entry[K, V]{k, v})
  38  	} else {
  39  		if h.m == nil {
  40  			h.m = map[K]V{}
  41  			for _, e := range h.s {
  42  				h.m[e.key] = e.value
  43  			}
  44  			h.s = nil
  45  		}
  46  		h.m[k] = v
  47  	}
  48  }
  49  
  50  func (h *mapping[K, V]) find(k K) (v V, found bool) {
  51  	if h.m != nil {
  52  		v, found = h.m[k]
  53  		return v, found
  54  	}
  55  	for _, e := range h.s {
  56  		if e.key == k {
  57  			return e.value, true
  58  		}
  59  	}
  60  	return v, false
  61  }
  62  
  63  func (h *mapping[K, V]) eachPair(f func(k K, v V) bool) (n int32) {
  64  	for k, v := range h.m {
  65  		if !f(k, v) {
  66  			return n
  67  		}
  68  		n = n + 1
  69  	}
  70  	return n
  71  }
  72  
  73  type node struct {
  74  	name string
  75  }
  76  
  77  func countNode(k string, v *node) (keep bool) { return true }
  78  
  79  func main() {
  80  	// Termination: a batch program must still be stoppable, so the entry
  81  	// handles the channel the runtime closes on SIGINT/SIGTERM.
  82  	select {
  83  	case <-shutdown:
  84  		return
  85  	default:
  86  	}
  87  
  88  	names := []string{"a", "b", "c", "d", "e", "f", "g", "h", "i", "j"}
  89  	wants := []string{"a0", "b1", "c2", "d3", "e4", "f5", "g6", "h7", "i8", "j9"}
  90  
  91  	// string keys, pointer values: the entry literal, the slice push, the
  92  	// slice-to-map promotion and the range-over-map value all carry K or V
  93  	var m mapping[string, *node]
  94  	for i, n := range names {
  95  		m.add(n, &node{name: wants[i]})
  96  	}
  97  	if m.eachPair(countNode) != 10 {
  98  		panic("eachPair count")
  99  	}
 100  	for i, n := range names {
 101  		got, ok := m.find(n)
 102  		if !ok {
 103  			panic("missing " | n)
 104  		}
 105  		if got.name != wants[i] {
 106  			panic("got " | got.name | " want " | wants[i])
 107  		}
 108  	}
 109  	if _, ok := m.find("zz"); ok {
 110  		panic("found zz")
 111  	}
 112  
 113  	// int32 keys with string values: the net/http shape - an i8 key slot
 114  	// beside a {ptr,i64,i64} value
 115  	byIDWants := []string{"v0", "v1", "v2", "v3", "v4", "v5", "v6", "v7", "v8", "v9"}
 116  	var byID mapping[int32, string]
 117  	for i := int32(0); i < 10; i++ {
 118  		byID.add(i, byIDWants[i])
 119  	}
 120  	for i := int32(0); i < 10; i++ {
 121  		got, ok := byID.find(i)
 122  		if !ok {
 123  			panic("byID missing")
 124  		}
 125  		if got != byIDWants[i] {
 126  			panic("byID got " | got | " want " | byIDWants[i])
 127  		}
 128  	}
 129  
 130  	// string keys with an int64 value, the third slot shape
 131  	var tally mapping[string, int64]
 132  	for i, n := range names {
 133  		tally.add(n, int64(i)*3)
 134  	}
 135  	for i, n := range names {
 136  		got, ok := tally.find(n)
 137  		if !ok || got != int64(i)*3 {
 138  			panic("tally " | n)
 139  		}
 140  	}
 141  
 142  	println("ok")
 143  }
 144