plum

#treesitter#compiler#wasm

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

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


d38eae1 — Peter John 2026-11-10T12:33:55+05:30
Tests: scratch folders are deleted by a drop guard
compiler/checker/tests/checker/features/review_fixes.rs CHANGED
@@ -7,7 +7,8 @@ const PRE: &str = "import std/Int\nimport std/Option\n";
7
7
  /// Loads `src` (after `PRE`) with std and checks it; the errors, or the load error.
8
8
  fn diags(src: &str) -> Vec<Diagnostic> {
9
9
  let text = format!("{PRE}{src}");
10
+ let (_dir, path) = write(&text);
10
- let source = match plum_core::loader::loadAndMergeDiag(&write(&text), &plum_test_support::repoRoot()) {
11
+ let source = match plum_core::loader::loadAndMergeDiag(&path, &plum_test_support::repoRoot()) {
11
12
  Ok(s) => s,
12
13
  Err(ds) => return ds,
13
14
  };
@@ -17,14 +18,12 @@ fn diags(src: &str) -> Vec<Diagnostic> {
17
18
  }
18
19
  }
19
20
 
21
+ /// `text` as `main.plum` in a scratch folder that is deleted when the guard is dropped.
20
- fn write(text: &str) -> std::path::PathBuf {
22
+ fn write(text: &str) -> (plum_test_support::Scratch, std::path::PathBuf) {
21
- use std::sync::atomic::{AtomicUsize, Ordering};
22
- static N: AtomicUsize = AtomicUsize::new(0);
23
+ let dir = plum_test_support::Scratch::new("review");
23
- let dir = std::env::temp_dir().join(format!("plum_review_{}_{}", std::process::id(), N.fetch_add(1, Ordering::SeqCst)));
24
- std::fs::create_dir_all(&dir).unwrap();
25
24
  let path = dir.join("main.plum");
26
25
  std::fs::write(&path, text).unwrap();
27
- path
26
+ (dir, path)
28
27
  }
29
28
 
30
29
  /// The one error whose message contains `needle`, at `(line, column)` of the user's source
@@ -200,7 +199,8 @@ fn nestedClosureCallingTheOuterOneIsAnError() {
200
199
  fn failingCompareInATestIsAtTheAssert() {
201
200
  // `plum test` checks the program with its tests lowered to functions.
202
201
  let text = format!("{PRE}import std/Str\ntest \"x\"\n y := 1\n assert y == \"a\"\n");
202
+ let (_dir, path) = write(&text);
203
- let source = plum_core::loader::loadAndMergeDiag(&write(&text), &plum_test_support::repoRoot()).expect("loads");
203
+ let source = plum_core::loader::loadAndMergeDiag(&path, &plum_test_support::repoRoot()).expect("loads");
204
204
  let lowered = plum_core::tests_lower::lowerTests(&source).source;
205
205
  let es = plum_driver::check(&lowered, &plum_driver::PassOptions::default()).expect_err("mismatch");
206
206
  let e = es.iter().find(|e| e.message.contains("compare op")).expect("compare error");
compiler/cli/tests/cli/std_bounds_test.rs CHANGED
@@ -15,8 +15,7 @@ fn plumBin() -> std::path::PathBuf {
15
15
 
16
16
  /// Runs `body` as `main`'s body and returns (success, stderr).
17
17
  fn run(name: &str, body: &str) -> (bool, String) {
18
- let dir = std::env::temp_dir().join(format!("plum_bounds_{}_{}", name, std::process::id()));
18
+ let dir = crate::common::Scratch::new(&format!("bounds_{name}"));
19
- std::fs::create_dir_all(&dir).unwrap();
20
19
  let src = dir.join(format!("{name}.plum"));
21
20
  let text = format!(
22
21
  "import std/Int\nimport std/Str\nimport std/Io\nimport std/Option\nimport std/Slice\nimport std/Deque\n\nfun main(io: Io) =\n{body}"
compiler/test-support/src/lib.rs CHANGED
@@ -53,15 +53,58 @@ pub fn parseRawSource(src: &str) -> Source {
53
53
 
54
54
  static COUNTER: AtomicUsize = AtomicUsize::new(0);
55
55
 
56
+ /// A fresh scratch folder, deleted when dropped (also when the test panics).
57
+ pub struct Scratch(PathBuf);
58
+
59
+ impl Scratch {
60
+ /// A new folder under the system temp folder: `plum_<name>_<process id>_<n>`.
61
+ pub fn new(name: &str) -> Scratch {
62
+ Scratch::at(std::env::temp_dir().join(format!("plum_{name}_{}", std::process::id())))
63
+ }
64
+
65
+ /// A new folder named `dir` plus a number no other `Scratch` of this process has, so two
66
+ /// tests asking for the same name never share (or delete) each other's folder.
67
+ pub fn at(dir: PathBuf) -> Scratch {
68
+ let n = COUNTER.fetch_add(1, Ordering::SeqCst);
69
+ let mut name = dir.file_name().unwrap_or_default().to_os_string();
70
+ name.push(format!("_{n}"));
71
+ let dir = dir.with_file_name(name);
72
+ let _ = std::fs::remove_dir_all(&dir);
73
+ std::fs::create_dir_all(&dir).unwrap();
74
+ Scratch(dir)
75
+ }
76
+
77
+ pub fn path(&self) -> &std::path::Path {
78
+ &self.0
79
+ }
80
+ }
81
+
82
+ impl std::ops::Deref for Scratch {
83
+ type Target = std::path::Path;
84
+ fn deref(&self) -> &std::path::Path {
85
+ &self.0
86
+ }
87
+ }
88
+
89
+ impl AsRef<std::path::Path> for Scratch {
90
+ fn as_ref(&self) -> &std::path::Path {
91
+ &self.0
92
+ }
93
+ }
94
+
95
+ impl Drop for Scratch {
96
+ fn drop(&mut self) {
97
+ let _ = std::fs::remove_dir_all(&self.0);
98
+ }
99
+ }
100
+
56
101
  /// Loads `src` as `main.plum` through the real loader (imports resolve against the repo root).
57
102
  pub fn load(name: &str, src: &str) -> Result<Source, String> {
58
- let n = COUNTER.fetch_add(1, Ordering::SeqCst);
59
- let dir = std::env::temp_dir().join(format!("plum_ts_{}_{}_{}", name, std::process::id(), n));
103
+ let dir = Scratch::new(&format!("ts_{name}"));
60
- std::fs::create_dir_all(&dir).unwrap();
61
104
  let path = dir.join("main.plum");
62
105
  std::fs::write(&path, src).unwrap();
63
106
  let r = plum_core::loader::loadAndMergeDiag(&path, &repoRoot());
64
- let _ = std::fs::remove_dir_all(&dir);
107
+ drop(dir);
65
108
  r.map_err(|ds| ds.iter().map(|d| d.to_string()).collect::<Vec<_>>().join("\n"))
66
109
  }
67
110
 
compiler/wasm-codegen/src/context.rs CHANGED
@@ -98,10 +98,10 @@ pub struct CompileCtx<'a> {
98
98
  /// Shared runtime helper `(n: i64) -> ref Str`: allocates a new `array<i8>`
99
99
  /// holding `n`'s decimal representation, for interpolating an `Int`.
100
100
  /// wasm-gc type-section indices for this program's enums/Str/closures —
101
- /// every value's real representation (see `docs/superpowers/plans/2026-07-25-wasm-gc-migration.md`).
101
+ /// every value's real representation.
102
102
  pub gc_types: GcTypeRegistry,
103
103
  /// Payload-free variant name (End/...) -> the global index holding
104
- /// its one pre-allocated instance (see this migration plan's Decision 2).
104
+ /// its one pre-allocated instance.
105
105
  pub singleton_globals: HashMap<(PlumType, String), u32>,
106
106
  /// The checker's types of plain fns and of each instantiation.
107
107
  pub(crate) base_types: UnitTypes,
compiler/wasm-codegen/src/lib.rs CHANGED
@@ -177,10 +177,8 @@ mod compileEntryTests {
177
177
  }
178
178
 
179
179
  fn loadSrc(src: &str) -> ast::Source {
180
- static NEXT: std::sync::atomic::AtomicUsize = std::sync::atomic::AtomicUsize::new(0);
180
+ // deleted when `dir` goes out of scope: the loader has read the file by then
181
- let n = NEXT.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
182
- let dir = std::env::temp_dir().join(format!("plum_compile_entry_{}_{}", std::process::id(), n));
181
+ let dir = plum_test_support::Scratch::new("compile_entry");
183
- std::fs::create_dir_all(&dir).unwrap();
184
182
  let path = dir.join("main.plum");
185
183
  std::fs::write(&path, src).unwrap();
186
184
  plum_core::loadAndMergeDiag(&path, &plum_test_support::repoRoot()).expect("load")
compiler/wasm-codegen/tests/wasm_codegen/codegen_tests.rs CHANGED
@@ -80,8 +80,7 @@ test \"one fails, rest still runs\"
80
80
  test \"hard assert traps\"
81
81
  assert hardAssertTrap() == 0
82
82
  ";
83
- let dir = std::env::temp_dir().join(format!("plum_t3_testblocks_{}", std::process::id()));
83
+ let dir = plum_test_support::Scratch::at(std::env::temp_dir().join(format!("plum_t3_testblocks_{}", std::process::id())));
84
- std::fs::create_dir_all(&dir).unwrap();
85
84
  let path = dir.join("main.plum");
86
85
  std::fs::write(&path, src).unwrap();
87
86
  let source = plum_core::loadAndMergeDiag(&path, &repoRoot()).expect("load");
@@ -168,8 +167,7 @@ test \"fb enum\"
168
167
  test \"fb int\"
169
168
  assert 1 + 1 == 3
170
169
  ";
171
- let dir = std::env::temp_dir().join(format!("plum_assert_fallback_{}", std::process::id()));
170
+ let dir = plum_test_support::Scratch::at(std::env::temp_dir().join(format!("plum_assert_fallback_{}", std::process::id())));
172
- std::fs::create_dir_all(&dir).unwrap();
173
171
  let path = dir.join("main.plum");
174
172
  std::fs::write(&path, src).unwrap();
175
173
  let source = plum_core::loadAndMergeDiag(&path, &repoRoot()).expect("load");
@@ -1091,8 +1089,7 @@ fun main() -> Int =
1091
1089
  /// Compiles `src` as a real file (so `import std/...` resolves against the repo's
1092
1090
  /// `std`) and returns the wasm instance's `main` export plus its store.
1093
1091
  fn runMerged<R: wasmtime::WasmResults>(name: &str, src: &str) -> R {
1094
- let dir = std::env::temp_dir().join(format!("plum_t3_{}_{}", name, std::process::id()));
1092
+ let dir = plum_test_support::Scratch::at(std::env::temp_dir().join(format!("plum_t3_{}_{}", name, std::process::id())));
1095
- std::fs::create_dir_all(&dir).unwrap();
1096
1093
  let path = dir.join("main.plum");
1097
1094
  std::fs::write(&path, src).unwrap();
1098
1095
  let source = plum_core::loadAndMergeDiag(&path, &repoRoot()).expect("load");
@@ -1125,8 +1122,7 @@ fn runMerged<R: wasmtime::WasmResults>(name: &str, src: &str) -> R {
1125
1122
 
1126
1123
  /// Compiles `src` (merged with the real std) and returns every compiled function key/name.
1127
1124
  fn compiledKeys(name: &str, src: &str) -> (Vec<u8>, Vec<String>) {
1128
- let dir = std::env::temp_dir().join(format!("plum_t3k_{}_{}", name, std::process::id()));
1125
+ let dir = plum_test_support::Scratch::at(std::env::temp_dir().join(format!("plum_t3k_{}_{}", name, std::process::id())));
1129
- std::fs::create_dir_all(&dir).unwrap();
1130
1126
  let path = dir.join("main.plum");
1131
1127
  std::fs::write(&path, src).unwrap();
1132
1128
  let source = plum_core::loadAndMergeDiag(&path, &repoRoot()).expect("load");
@@ -1580,8 +1576,7 @@ fun main() -> Int =
1580
1576
  else
1581
1577
  0
1582
1578
  ";
1583
- let dir = std::env::temp_dir().join(format!("plum_t3_bool_noimport_{}", std::process::id()));
1579
+ let dir = plum_test_support::Scratch::at(std::env::temp_dir().join(format!("plum_t3_bool_noimport_{}", std::process::id())));
1584
- std::fs::create_dir_all(&dir).unwrap();
1585
1580
  let path = dir.join("main.plum");
1586
1581
  std::fs::write(&path, src).unwrap();
1587
1582
  let source = plum_core::loadAndMergeDiag(&path, &repoRoot()).expect("load");
@@ -1969,8 +1964,7 @@ fun main() -> Int =
1969
1964
 
1970
1965
  /// The checker's error messages for `src` compiled as a real file ("" when it checks).
1971
1966
  fn checkErrorsMerged(name: &str, src: &str) -> String {
1972
- let dir = std::env::temp_dir().join(format!("plum_seq_{}_{}", name, std::process::id()));
1967
+ let dir = plum_test_support::Scratch::at(std::env::temp_dir().join(format!("plum_seq_{}_{}", name, std::process::id())));
1973
- std::fs::create_dir_all(&dir).unwrap();
1974
1968
  let path = dir.join("main.plum");
1975
1969
  std::fs::write(&path, src).unwrap();
1976
1970
  let source = plum_core::loadAndMergeDiag(&path, &repoRoot()).expect("load");
@@ -2021,8 +2015,7 @@ fn runInt(src: &str) -> i64 {
2021
2015
 
2022
2016
  /// Runs a std-importing program; returns (result or trap, crash messages seen).
2023
2017
  fn runWithCrash(name: &str, src: &str) -> (Result<i64, String>, Vec<String>) {
2024
- let dir = std::env::temp_dir().join(format!("plum_e3_{}_{}", name, std::process::id()));
2018
+ let dir = plum_test_support::Scratch::at(std::env::temp_dir().join(format!("plum_e3_{}_{}", name, std::process::id())));
2025
- std::fs::create_dir_all(&dir).unwrap();
2026
2019
  let path = dir.join("main.plum");
2027
2020
  std::fs::write(&path, src).unwrap();
2028
2021
  let source = plum_core::loadAndMergeDiag(&path, &repoRoot()).expect("load");
@@ -2185,8 +2178,7 @@ fn short_function_names_get_a_prefix() {
2185
2178
  let src = "import std/Int\n\nfun g(n: Int) -> Int =\n if n == 0 then 1 else g(n - 1) + 1\n\nfun main() -> Int =\n g(5)\n";
2186
2179
  let (r, _) = runWithCrash("shortname", src);
2187
2180
  assert_eq!(r, Ok(6));
2188
- let dir = std::env::temp_dir().join(format!("plum_shortname_{}", std::process::id()));
2181
+ let dir = plum_test_support::Scratch::at(std::env::temp_dir().join(format!("plum_shortname_{}", std::process::id())));
2189
- std::fs::create_dir_all(&dir).unwrap();
2190
2182
  let path = dir.join("main.plum");
2191
2183
  std::fs::write(&path, src).unwrap();
2192
2184
  let source = plum_core::loadAndMergeDiag(&path, &repoRoot()).expect("load");
@@ -2348,8 +2340,7 @@ fun main() -> Int =
2348
2340
  #[test]
2349
2341
  fn strIsAViewOfAPackedByteArray() {
2350
2342
  let src = "import std/Str\nimport std/Int\nimport std/Float\n\nfun main() -> Int =\n \"hello\".length()\n";
2351
- let dir = std::env::temp_dir().join(format!("plum_strshape_{}", std::process::id()));
2343
+ let dir = plum_test_support::Scratch::at(std::env::temp_dir().join(format!("plum_strshape_{}", std::process::id())));
2352
- std::fs::create_dir_all(&dir).unwrap();
2353
2344
  let path = dir.join("main.plum");
2354
2345
  std::fs::write(&path, src).unwrap();
2355
2346
  let source = plum_core::loadAndMergeDiag(&path, &repoRoot()).expect("load");
@@ -2425,8 +2416,7 @@ fun main() -> Float =
2425
2416
  b := a.resize(5)
2426
2417
  a.get(0).x + b.get(1).y
2427
2418
  ";
2428
- let dir = std::env::temp_dir().join(format!("plum_soa_{}", std::process::id()));
2419
+ let dir = plum_test_support::Scratch::at(std::env::temp_dir().join(format!("plum_soa_{}", std::process::id())));
2429
- std::fs::create_dir_all(&dir).unwrap();
2430
2420
  let path = dir.join("main.plum");
2431
2421
  std::fs::write(&path, src).unwrap();
2432
2422
  let source = plum_core::loadAndMergeDiag(&path, &repoRoot()).expect("load");
@@ -2505,8 +2495,7 @@ fn exportRepoRoot() -> std::path::PathBuf {
2505
2495
  /// Compiles `src` through the real loader (so `import std/Str` resolves) and
2506
2496
  /// instantiates it with every host import defaulted.
2507
2497
  fn instantiateExports(name: &str, src: &str) -> (wasmtime::Store<()>, wasmtime::Instance, Vec<u8>) {
2508
- let dir = std::env::temp_dir().join(format!("plum_export_{}_{}", name, std::process::id()));
2498
+ let dir = plum_test_support::Scratch::at(std::env::temp_dir().join(format!("plum_export_{}_{}", name, std::process::id())));
2509
- std::fs::create_dir_all(&dir).unwrap();
2510
2499
  let path = dir.join("main.plum");
2511
2500
  let full = format!("import std/Int\nimport std/Float\nimport std/Bool\nimport std/Str\n{src}");
2512
2501
  std::fs::write(&path, full).unwrap();
@@ -2834,8 +2823,7 @@ import std/mem
2834
2823
  fun main() -> Int =
2835
2824
  mem.loadI32(16 * 65536)
2836
2825
  ";
2837
- let dir = std::env::temp_dir().join(format!("plum_mem_oob_{}", std::process::id()));
2826
+ let dir = plum_test_support::Scratch::at(std::env::temp_dir().join(format!("plum_mem_oob_{}", std::process::id())));
2838
- std::fs::create_dir_all(&dir).unwrap();
2839
2827
  let path = dir.join("main.plum");
2840
2828
  std::fs::write(&path, src).unwrap();
2841
2829
  let source = plum_core::loadAndMergeDiag(&path, &repoRoot()).expect("load");
compiler/wasm-codegen/tests/wasm_codegen/examples_test.rs CHANGED
@@ -171,8 +171,7 @@ fn const_table_1000_entries() {
171
171
  src.push_str(&format!(" out.push({})\n", i * i));
172
172
  }
173
173
  src.push_str(" out\n\nfun probe() -> Int =\n E999 + Double\n");
174
- let dir = std::env::temp_dir().join(format!("plum_const_table_{}", std::process::id()));
174
+ let dir = plum_test_support::Scratch::at(std::env::temp_dir().join(format!("plum_const_table_{}", std::process::id())));
175
- std::fs::create_dir_all(&dir).unwrap();
176
175
  let path = dir.join("t.plum");
177
176
  std::fs::write(&path, src).unwrap();
178
177
  let source = plum_core::loadAndMergeDiag(&path, &repoRoot()).expect("1000-const file should load");