plum

#treesitter#compiler#wasm

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

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


18331bd — Peter John 2026-11-10T21:10:23+05:30
Fusion: fresh generics of a fused fn are named in a fixed order
compiler/cli/tests/cli/determinism_test.rs ADDED
@@ -0,0 +1,47 @@
1
+ //! The same source compiles to the same bytes in every process. A HashMap or HashSet is
2
+ //! seeded per process, so iteration order that reaches the module (type or function
3
+ //! order, data segments, names) only shows up across separate runs, never within one.
4
+ use crate::common::{plumBin, root, Scratch};
5
+ use std::process::Command;
6
+
7
+ const RUNS: usize = 6;
8
+
9
+ fn allSame(outs: Vec<Vec<u8>>, what: &str) {
10
+ let first = &outs[0];
11
+ let differ = outs.iter().filter(|o| *o != first).count();
12
+ assert_eq!(differ, 0, "{what}: {differ} of {RUNS} separate compiles gave other bytes than the first");
13
+ }
14
+
15
+ #[test]
16
+ fn gamesCompileToTheSameBytesInSeparateProcesses() {
17
+ let dir = Scratch::new("det_games");
18
+ for game in ["examples/games/orb_run.plum", "examples/games/kids/balloon_pop.plum"] {
19
+ let outs = (0..RUNS)
20
+ .map(|i| {
21
+ let out = dir.join(format!("g{i}.wasm"));
22
+ let st = Command::new(plumBin()).arg("compile").arg(root().join(game)).arg("-o").arg(&out).output().unwrap();
23
+ assert!(st.status.success(), "{game}: {}", String::from_utf8_lossy(&st.stderr));
24
+ std::fs::read(&out).unwrap()
25
+ })
26
+ .collect();
27
+ allSame(outs, game);
28
+ }
29
+ }
30
+
31
+ /// Test-mode modules too. BTree's tests fuse a producer into a consumer that gives the
32
+ /// fused fn fresh generics: which generic got which letter followed HashSet order, and the
33
+ /// instantiation names in the name section changed from run to run.
34
+ #[test]
35
+ fn stdTestModulesCompileToTheSameBytesInSeparateProcesses() {
36
+ let dir = Scratch::new("det_std");
37
+ for file in ["std/collections/BTree.plum", "std/Str.plum", "std/collections/Hamt.plum"] {
38
+ let outs = (0..RUNS)
39
+ .map(|i| {
40
+ let out = dir.join(format!("s{i}.wasm"));
41
+ let st = Command::new(plumBin()).arg("test").arg(root().join(file)).env("PLUM_DUMP_WASM", &out).output().unwrap();
42
+ std::fs::read(&out).unwrap_or_else(|_| panic!("{file}: no module dumped: {}", String::from_utf8_lossy(&st.stderr)))
43
+ })
44
+ .collect();
45
+ allSame(outs, file);
46
+ }
47
+ }
compiler/cli/tests/cli/main.rs CHANGED
@@ -5,6 +5,7 @@ mod args_clock_test;
5
5
  mod assert_once_test;
6
6
  mod compile_tests;
7
7
  mod crash_test;
8
+ mod determinism_test;
8
9
  mod explain_layout_test;
9
10
  mod fusion_agree_test;
10
11
  mod game_entry_test;
compiler/core/src/drive.rs CHANGED
@@ -1895,7 +1895,11 @@ impl Driver {
1895
1895
  collectGens(&sc, &pret, &mut pg);
1896
1896
  let mut used: HashSet<String> = cg.union(&pg).cloned().collect();
1897
1897
  let mut fresh: Vec<String> = Vec::new();
1898
+ // in name order: `cg` is a HashSet, and which generic gets which fresh letter shows in
1899
+ // the fused fn's instantiation names, so it must not depend on the hash seed
1900
+ let mut cgSorted: Vec<&String> = cg.iter().collect();
1901
+ cgSorted.sort();
1898
- for g in &cg {
1902
+ for g in cgSorted {
1899
1903
  if !map.contains_key(g) && pg.contains(g) {
1900
1904
  let l = ('A'..='Z').map(|c| c.to_string()).find(|l| !used.contains(l)).ok_or("generic names")?;
1901
1905
  used.insert(l.clone());