plum

#treesitter#compiler#wasm

git clone https://git.pyrossh.dev/plum

A statically typed, imperative programming language inspired by rust, python


b60d901 — Peter John 2026-11-10T19:24:51+05:30
FBIP fuzzer: generic types, and --no-fusion as a third mode
Files changed (2) hide show
  1. tests/fbip/fuzz/fuzz.sh +8 -6
  2. tests/fbip/fuzz/gen.pl +117 -11
tests/fbip/fuzz/fuzz.sh CHANGED
@@ -1,8 +1,9 @@
1
1
  #!/bin/bash
2
- # Differential fuzzer for the FBIP uniqueness proof.
2
+ # Differential fuzzer for the FBIP uniqueness proof and fusion.
3
- # A wrong proof writes through a value another holder can see, so a generated
3
+ # A wrong proof writes through a value another holder can see, and a wrong fusion reorders or
4
- # program must give the same result with and without --no-fbip. Any DIFF is a
4
+ # drops an effect, so a generated program must give the same result by default, with
5
+ # --no-fbip and with --no-fusion. Any DIFF is a real miscompile.
5
- # real miscompile. Usage (after a build): tests/fbip/fuzz/fuzz.sh FROM TO (seeds)
6
+ # Usage (after a build): tests/fbip/fuzz/fuzz.sh FROM TO (seeds)
6
7
  here="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
7
8
  root="$(cd "$here/../../.." && pwd)"
8
9
  cd "$root" || exit 1
@@ -16,9 +17,10 @@ for i in $(seq "$1" "$2"); do
16
17
  err=$((err+1)); echo "ERR $i: $(echo "$a" | head -1 | cut -c1-200)"; continue
17
18
  fi
18
19
  b=$(target/debug/plum test --no-fbip --lib-path . "$f" 2>&1)
20
+ c=$(target/debug/plum test --no-fusion --lib-path . "$f" 2>&1)
19
- if [ "$a" == "$b" ]; then ok=$((ok+1)); rm -f "$f"
21
+ if [ "$a" == "$b" ] && [ "$a" == "$c" ]; then ok=$((ok+1)); rm -f "$f"
20
22
  else diff=$((diff+1)); echo "DIFF $i: $f"; fi
21
23
  done
22
24
  echo "done ok=$ok diff=$diff err=$err"
23
25
  echo "$out"
24
- [ "$diff" -eq 0 ]
26
+ [ "$diff" -eq 0 ] && [ "$err" -eq 0 ]
tests/fbip/fuzz/gen.pl CHANGED
@@ -94,9 +94,9 @@ my $i = 0;
94
94
  my ($kv, $kw) = (0, 0);
95
95
  for my $v (reverse @vs) { next unless $kv++ < 2 || r(3) == 0; $dump .= " o$i := $v.show()\n"; $i++; }
96
96
  for my $w (reverse @ws) { next unless $kw++ < 2 || r(3) == 0; $dump .= " o$i := $w.show()\n"; $i++; }
97
- $dump .= " oM := m.p.show()\n oX := xs.at(0).show() + xs.at(1).show() + ys.at(0).show() + ys.at(1).show()\n";
97
+ $dump .= " oM := m.p.show()\n oX := \"{xs.at(0).show()}{xs.at(1).show()}{ys.at(0).show()}{ys.at(1).show()}\"\n";
98
- my $join = join(" + ", (map { "o$_" } 0..($i-1)), "oM", "oX");
98
+ my $join = join("", map { "{$_}" } ((map { "o$_" } 0..($i-1)), "oM", "oX"));
99
- $dump .= " assert $join == \"\"\n";
99
+ $dump .= " assert \"$join\" == \"\"\n";
100
100
  # A second test on types of its own, with a field name of its own (a read of `x.p` whose type is not
101
101
  # known counts against `p` on every type), used almost linearly. One shared use poisons a field's proof for
102
102
  # the whole program, so the updates of fields only ever run in place in programs like this; the
@@ -137,7 +137,7 @@ for my $n (1..(4+r(9))) {
137
137
  my %seen; my @show = grep { !$seen{$_}++ } (@ls, @qs, @kept);
138
138
  my $linBody = join("
139
139
  ", @lin);
140
- my $linDump = join(" + ", map { "$_.show()" } @show) || '""';
140
+ my $linDump = '"' . join("", map { "{$_.show()}" } @show) . '"';
141
141
  # A third test, on a type with a `mut` field: what a copy and an in-place write may do differently (the
142
142
  # order values run in, a value writing the source), and a parameter default that holds a constant.
143
143
  my @os = ('o0');
@@ -153,7 +153,66 @@ for my $n (1..(3+r(7))) {
153
153
  $no++;
154
154
  }
155
155
  my $ordBody = join("\n ", @ord);
156
- my $ordDump = join(" + ", map { "$_.show()" } @os);
156
+ my $ordDump = join("", map { "{$_.show()}" } @os);
157
+ # A fourth test on generic types of their own (`G<T>`, `H<T>` holding a `G<T>`, `Gm<T>` with a
158
+ # `mut` field), with field names of their own: spreads that keep the type arguments may run in
159
+ # place, ones that change them (`with`) build another struct, and values reach the generic
160
+ # fns, a closure, an `Option`, a `Slice`, a trait object and `@sameRef`. Mostly linear, as
161
+ # in the second test, with the odd value kept for the dump.
162
+ my @gs = ('g0', 'g1'); my @hs = ('h0');
163
+ my ($ng, $nh) = (2, 1);
164
+ @kept = (); my @same;
165
+ sub gint {
166
+ my $k = r(5);
167
+ my @pool = (@gs, grep { /^g/ } @kept);
168
+ return r(5) if $k == 0 || (!@pool && $k <= 2);
169
+ return pick(@pool).".gn" if $k == 1;
170
+ return pick(@pool).".gv.a" if $k == 2;
171
+ return r(5) unless @hs;
172
+ return pick(@hs).".hk" if $k == 3;
173
+ return pick(@hs).".hg.gn";
174
+ }
175
+ my @gen;
176
+ for my $n (1..(5+r(12))) {
177
+ my $k = r(30);
178
+ my $x;
179
+ if (!@gs) { push @gen, "g$ng := G(gv: mk(".r(9).", ".r(9)."), gn: 0)"; push @gs, "g$ng"; $ng++; }
180
+ if (!@hs) { push @gen, "h$nh := H(hg: G(gv: mk(1, 1), gn: 0), hk: 0)"; push @hs, "h$nh"; $nh++; }
181
+ if ($k == 0) { $x = consume(\@gs).".bump()"; }
182
+ elsif ($k == 1) { my $g = consume(\@gs); $x = "G(..$g, gn: ".gint().")"; }
183
+ elsif ($k == 2) { my $g = consume(\@gs); $x = "$g.set(P(..$g.gv, a: $g.gn + 1))"; }
184
+ elsif ($k == 3) { my $g = consume(\@gs); $x = "G(..$g, gv: $g.gv.inc())"; }
185
+ elsif ($k == 4) { $x = "gbump(".consume(\@gs).")"; }
186
+ elsif ($k == 5) { my $g = consume(\@gs); my $g2 = pick(@gs, $g); $x = "gpass($g, $g2)"; }
187
+ elsif ($k == 6) { $x = "gid(".consume(\@gs).").bump()"; }
188
+ elsif ($k == 7) { my $g = consume(\@gs); $x = "$g.with($g.gn).with(P(..$g.gv, b: 9))"; }
189
+ elsif ($k == 8) { $x = "gspin(".consume(\@gs).", ".r(4).")"; }
190
+ elsif ($k == 9) { my $g = consume(\@gs); my $g2 = pick(@gs, $g); $x = "if ".gint()." > 2 then $g.bump() else $g2"; }
191
+ elsif ($k == 10) { $x = "0.until(".r(4).").fold(".consume(\@gs).", |acc, i| ".pick("acc.bump()", "G(..acc, gn: acc.gn + i)", "gbump(acc)", "acc.set(acc.gv.inc())").")"; }
192
+ elsif ($k == 11) { $x = "gapply(".consume(\@gs).", |y| ".pick("y.bump()", "gbump(y)", "G(..y, gn: 5)").")"; }
193
+ elsif ($k == 12) { $x = "Some(".consume(\@gs).").unwrapOr(g0)"; }
194
+ elsif ($k == 13) { $x = "gtk(".consume(\@gs).")"; }
195
+ elsif ($k == 14) { $x = consume(\@hs).".hg"; }
196
+ elsif ($k == 15) { $x = "gs.at(".r(2).")"; }
197
+ elsif ($k == 16) { $x = "gm.mg.bump()"; }
198
+ elsif ($k == 17) { my $g = consume(\@gs, 6); push @gen, "gs.push($g)"; next; }
199
+ elsif ($k == 18) { my $g = consume(\@gs, 6); push @gen, "gm.mg = $g"; next; }
200
+ elsif ($k == 19) { push @gen, "gm.mg = gm.mg.bump()"; next; }
201
+ elsif ($k == 20) { my @pool = (@gs, grep { /^g/ } @kept); my $a = pick(@pool); my $b = pick(@pool); push @same, "{\@sameRef($a, $b)}"; next; }
202
+ if (defined $x) { push @gen, "g$ng := $x"; push @gs, "g$ng"; $ng++; next; }
203
+ if ($k == 21) { $x = "H(hg: ".consume(\@gs, 4).", hk: ".r(4).")"; }
204
+ elsif ($k == 22) { my $h = consume(\@hs); $x = "H(..$h, hg: ".consume(\@gs, 4).")"; }
205
+ elsif ($k == 23) { my $h = consume(\@hs); $x = "H(..$h, hg: $h.hg.bump())"; }
206
+ elsif ($k == 24) { $x = consume(\@hs).".step()"; }
207
+ elsif ($k == 25) { my $h = consume(\@hs); $x = "H(..$h, hg.gn: ".gint().")"; }
208
+ elsif ($k == 26) { my $h = consume(\@hs); $x = "H(..$h, hk: $h.hg.gn, hg: gbump($h.hg))"; }
209
+ elsif ($k == 27) { $x = "hspin(".consume(\@hs).", ".r(3).")"; }
210
+ else { $x = "H(hg: gbump(".consume(\@gs, 4)."), hk: 1)"; }
211
+ push @gen, "h$nh := $x"; push @hs, "h$nh"; $nh++;
212
+ }
213
+ my %gseen; my @gshow = grep { !$gseen{$_}++ } (@gs, @hs, @kept);
214
+ my $genBody = join("\n ", @gen);
215
+ my $genDump = '"' . join("", map { "{$_.show()}" } @gshow) . '{gm.mg.show()}{gs.at(0).show()}{gs.at(1).show()}' . join("", @same) . '"';
157
216
  open(my $fh, '>', $out) or die;
158
217
  print $fh <<"EOP";
159
218
  import std/Int
@@ -163,11 +222,11 @@ import std/Slice
163
222
  struct P(a: Int, b: Int, tag: Str) =
164
223
  fun inc(self) -> P = P(..self, a: self.a + 1)
165
224
  fun dbl(self) -> P = P(..self, a: self.a * 2)
166
- fun show(self) -> Str = "P(" + self.a.toStr() + "," + self.b.toStr() + "," + self.tag + ");"
225
+ fun show(self) -> Str = "P({self.a},{self.b},{self.tag});"
167
226
 
168
227
  struct Q(p: P, n: Int) =
169
228
  fun bumped(self) -> Q = Q(..self, n: self.n + 1, p: self.p.inc())
170
- fun show(self) -> Str = "Q(" + self.p.show() + self.n.toStr() + ");"
229
+ fun show(self) -> Str = "Q({self.p.show()}{self.n});"
171
230
 
172
231
  struct M(mut p: P)
173
232
 
@@ -185,11 +244,11 @@ fun step2(p: P, i: Int) -> P = P(..p, b: p.b + i)
185
244
 
186
245
  struct Lp(a: Int, tag: Str) =
187
246
  fun up(self) -> Lp = Lp(..self, a: self.a + 1)
188
- fun show(self) -> Str = "L(" + self.a.toStr() + "," + self.tag + ");"
247
+ fun show(self) -> Str = "L({self.a},{self.tag});"
189
248
 
190
249
  struct Lq(core: Lp, n: Int) =
191
250
  fun ticked(self) -> Lq = Lq(..self, core: self.core.up(), n: self.n + 1)
192
- fun show(self) -> Str = "M(" + self.core.show() + self.n.toStr() + ");"
251
+ fun show(self) -> Str = "M({self.core.show()}{self.n});"
193
252
 
194
253
  fun lmk(a: Int) -> Lp = Lp(a: a, tag: "k")
195
254
 
@@ -230,7 +289,7 @@ struct Od(mut a: Int, b: Int, tag: Str) =
230
289
  self.a = self.a + 50
231
290
  1
232
291
  fun up(self) -> Od = Od(..self, b: self.b + 1)
233
- fun show(self) -> Str = "Od(" + self.a.toStr() + "," + self.b.toStr() + "," + self.tag + ");"
292
+ fun show(self) -> Str = "Od({self.a},{self.b},{self.tag});"
234
293
 
235
294
  fun odUp(o: Od) -> Od = Od(..o, a: o.a + 3)
236
295
 
@@ -238,6 +297,42 @@ fun odKeep(o: Od, n: Int) -> Od = if n <= 0 then o else odKeep(Od(..o, b: o.b +
238
297
 
239
298
  OdStart = Od(a: 7, b: 8, tag: "start")
240
299
 
300
+ # The generic types of the fourth test.
301
+ trait Gt =
302
+ fun gtick(self) -> Self
303
+
304
+ struct G<T>[Gt](gv: T, gn: Int) =
305
+ fun bump(self) -> G<T> = G(..self, gn: self.gn + 1)
306
+ fun set(self, v: T) -> G<T> = G(..self, gv: v)
307
+ fun with<B>(self, v: B) -> G<B> = G(..self, gv: v)
308
+ fun gtick(self) -> G<T> = G(..self, gn: self.gn + 2)
309
+ fun show(self) -> Str = "G({self.gv.show()}{self.gn});"
310
+
311
+ struct H<T>(hg: G<T>, hk: Int) =
312
+ fun step(self) -> H<T> = H(..self, hg: self.hg.bump(), hk: self.hk + 1)
313
+ fun show(self) -> Str = "H({self.hg.show()}{self.hk});"
314
+
315
+ struct Gm<T>(mut mg: G<T>)
316
+
317
+ fun gbump<T>(g: G<T>) -> G<T> = G(..g, gn: g.gn + 10)
318
+
319
+ fun gpass<T>(x: G<T>, y: G<T>) -> G<T> = if x.gn > 2 then x else G(..y, gn: 3)
320
+
321
+ fun gid<T>(x: T) -> T = x
322
+
323
+ fun gspin<T>(g: G<T>, n: Int) -> G<T> = if n == 0 then g else gspin(g.bump(), n - 1)
324
+
325
+ fun hspin<T>(h: H<T>, n: Int) -> H<T> = if n == 0 then h else hspin(h.step(), n - 1)
326
+
327
+ fun gapply<T>(g: G<T>, f: fn(G<T>) -> G<T>) -> G<T> = f(g)
328
+
329
+ fun gtk(g: G<P>) -> G<P> = match gtt(g)
330
+ G<P> as y => y
331
+ _ => g
332
+
333
+ fun gtt(t: Gt) -> Gt = t.gtick()
334
+
335
+
241
336
  struct Oh(o: Od, n: Int) =
242
337
  fun otick(self) -> Oh = Oh(..self, o: self.o.up())
243
338
 
@@ -287,6 +382,17 @@ test "order"
287
382
  c := Ctr(k: 0)
288
383
  $ordBody
289
384
  h := odDflt(2)
290
- assert h.o.show() + OdStart.show() + c.k.toStr() + $ordDump == ""
385
+ assert "{h.o.show()}{OdStart.show()}{c.k}$ordDump" == ""
386
+
387
+ test "generic"
388
+ g0 := G(gv: mk(1, 2), gn: 0)
389
+ g1 := G(gv: mk(3, 4), gn: 1)
390
+ h0 := H(hg: G(gv: mk(5, 6), gn: 2), hk: 0)
391
+ gm := Gm(mg: G(gv: mk(7, 8), gn: 3))
392
+ gs := Slice<G<P>>()
393
+ gs.push(G(gv: mk(9, 10), gn: 4))
394
+ gs.push(G(gv: mk(11, 12), gn: 5))
395
+ $genBody
396
+ assert $genDump == ""
291
397
  EOP
292
398
  close $fh;