plum

#treesitter#compiler#wasm

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

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


7c82a31 — Peter John 2026-11-11T01:26:29+05:30
driver: share the adopted program with the pass state instead of copying it
compiler/driver/src/lib.rs CHANGED
@@ -196,7 +196,8 @@ pub struct Dropped {
196
196
 
197
197
  /// The state the passes thread through: the pre-union program and the checked result.
198
198
  struct State {
199
+ /// Shared with the checked program when that is `pre` itself (`adopt`).
199
- pre: Source,
200
+ pre: Rc<Source>,
200
201
  result: Result<(), Vec<Diagnostic>>,
201
202
  info: TypedInfo,
202
203
  /// `fip fun` violations, kept even when the rewrite that found them is dropped.
@@ -229,7 +230,7 @@ pub fn checkReport(source: &Source, opts: &PassOptions, passes: &[Pass]) -> (Res
229
230
  }
230
231
  None => preUnionSource(source),
231
232
  };
232
- let mut st = State { pre, result, info, fip: Vec::new(), dropped: Vec::new(), origins: RefCell::new(None) };
233
+ let mut st = State { pre: Rc::new(pre), result, info, fip: Vec::new(), dropped: Vec::new(), origins: RefCell::new(None) };
233
234
  if st.result.is_ok() {
234
235
  if opts.explain && !(opts.fusion && opts.fbip) && !plum_core::fip::origins(&st.pre).isEmpty() {
235
236
  eprintln!("fusion: fip fun is not enforced: it needs the uniqueness proof, which --no-fusion and --no-fbip switch off");
@@ -238,14 +239,18 @@ pub fn checkReport(source: &Source, opts: &PassOptions, passes: &[Pass]) -> (Res
238
239
  plum_core::timing::time(pass.name, || runPass(pass, source, opts, &mut st));
239
240
  }
240
241
  }
242
+ // `pre` may share the checked program (`adopt`): let go of it, so the marks below are
243
+ // set in the one copy left.
244
+ let State { pre, result, mut info, fip, dropped, .. } = st;
245
+ drop(pre);
241
246
  // Flags are set in place, so the type info, keyed by node id, stays valid.
242
- if let Some(lowered) = st.info.lowered.as_mut().and_then(Rc::get_mut) {
247
+ if let Some(lowered) = info.lowered.as_mut().and_then(Rc::get_mut) {
243
248
  plum_core::timing::time("tail marks", || plum_core::tail::markTailCalls(lowered));
244
249
  }
245
- if st.result.is_ok() && !st.fip.is_empty() {
250
+ if result.is_ok() && !fip.is_empty() {
246
- return (Err(st.fip), st.dropped);
251
+ return (Err(fip), dropped);
247
252
  }
248
- (st.result.map(|()| st.info), st.dropped)
253
+ (result.map(|()| info), dropped)
249
254
  }
250
255
 
251
256
  fn runPass(pass: &Pass, original: &Source, opts: &PassOptions, st: &mut State) {
@@ -264,7 +269,7 @@ fn runPass(pass: &Pass, original: &Source, opts: &PassOptions, st: &mut State) {
264
269
  let errors = RefCell::new(Vec::new());
265
270
  let env = Env { explain: opts.explain, fbip: opts.fbip, hints: &hints, errors: &errors, origins: &st.origins };
266
271
  let mut copy = match pass.target {
267
- Target::Pre => st.pre.clone(),
272
+ Target::Pre => (*st.pre).clone(),
268
273
  Target::Lowered => match st.info.lowered.as_ref() {
269
274
  Some(l) => (**l).clone(),
270
275
  None => return,
@@ -295,7 +300,7 @@ fn runPass(pass: &Pass, original: &Source, opts: &PassOptions, st: &mut State) {
295
300
  Err(errs) => {
296
301
  let first: Vec<String> = errs.iter().take(3).map(|e| format!("{}:{}: {}", e.line, e.column, e.message)).collect();
297
302
  let base = match pass.target {
298
- Target::Pre => Some(&st.pre),
303
+ Target::Pre => Some(&*st.pre),
299
304
  Target::Lowered => st.info.lowered.as_deref(),
300
305
  };
301
306
  let partial = match base {
@@ -333,12 +338,12 @@ fn adopt(pass: &Pass, st: &mut State, kept: Rc<Source>, mut i2: TypedInfo) {
333
338
  let keep = pass.target == Target::Lowered || i2.lowered.is_none();
334
339
  if keep {
335
340
  if pass.target == Target::Pre {
341
+ // the checked program is `pre` itself: shared, not copied
336
- st.pre = (*kept).clone();
342
+ st.pre = kept.clone();
337
343
  }
338
344
  i2.lowered = Some(kept);
339
345
  } else if pass.target == Target::Pre {
340
- // held nowhere else: moved, not copied
341
- st.pre = Rc::try_unwrap(kept).unwrap_or_else(|rc| (*rc).clone());
346
+ st.pre = kept;
342
347
  }
343
348
  st.result = Ok(());
344
349
  st.info = i2;
compiler/driver/tests/driver_tests.rs CHANGED
@@ -609,3 +609,44 @@ fn formerlyDroppingTestFilesKeepEveryPass() {
609
609
  }
610
610
  assert!(bad.is_empty(), "{}", bad.join("\n"));
611
611
  }
612
+
613
+ /// The tail calls of a fn of the checked program, by callee name.
614
+ fn tailsTo(lowered: &Source, owner: &str, callee: &str) -> Vec<plum_core::ast::TailKind> {
615
+ use plum_core::ast::{ExprKind, FnBody};
616
+ let mut out = Vec::new();
617
+ for item in &lowered.items {
618
+ let Item::Fn(f) = item else { continue };
619
+ if f.name != owner {
620
+ continue;
621
+ }
622
+ let mut see = |e: &plum_core::ast::Expr| {
623
+ if let ExprKind::FnCall(c) = &e.kind {
624
+ if c.name == callee {
625
+ out.push(c.tail);
626
+ }
627
+ }
628
+ };
629
+ match &f.body {
630
+ FnBody::Expr(e) => plum_core::visit::walkRef(e, &mut see),
631
+ FnBody::Block(b) => plum_core::visit::walkBlockRef(b, &mut see),
632
+ FnBody::Extern => {}
633
+ }
634
+ }
635
+ out
636
+ }
637
+
638
+ /// Tail calls are marked whichever pass ran last. A run that ends on a pass over the
639
+ /// pre-union program (dead code alone here: it prunes std) adopts a checked program the
640
+ /// driver's state may still share when the marks are set.
641
+ #[test]
642
+ fn tailCallsAreMarkedWhateverPassRanLast() {
643
+ let src = load(
644
+ "drv_tail",
645
+ "import std/Int\nimport std/collections/List\n\nfun count(n: Int, acc: Int) -> Int = if n == 0 then acc else count(n - 1, acc + 1)\n\nfun main() -> Int = count(10, 0)\n",
646
+ );
647
+ for passes in [&PASSES[..1], &PASSES[..2], &PASSES[..]] {
648
+ let info = check_with(&src, &PassOptions::default(), passes).expect("checks");
649
+ let tails = tailsTo(info.lowered.as_ref().expect("lowered"), "count", "count");
650
+ assert!(!tails.is_empty() && tails.iter().all(|t| *t == plum_core::ast::TailKind::SelfCall), "{} passes: {tails:?}", passes.len());
651
+ }
652
+ }