plum

#treesitter#compiler#wasm

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

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


0e6b688 — Peter John 2026-11-10T20:28:50+05:30
Checker: polymorphic recursion is an error, not a hung compile
compiler/checker/src/callsites.rs CHANGED
@@ -365,6 +365,15 @@ fn calleeGenerics(f: &ast::Fn, ctx: &CheckCtx) -> (Vec<String>, Vec<String>, Vec
365
365
  (gens, owner_gens, sig_free)
366
366
  }
367
367
 
368
+ /// The number of nodes of a type.
369
+ fn typeSize(t: &PlumType) -> usize {
370
+ match t {
371
+ PlumType::TApp(_, a) => 1 + a.iter().map(typeSize).sum::<usize>(),
372
+ PlumType::TFun(ps, r) => 1 + ps.iter().map(typeSize).sum::<usize>() + typeSize(r),
373
+ _ => 1,
374
+ }
375
+ }
376
+
368
377
  /// Turns the generic calls recorded while checking one body into new
369
378
  /// instantiations (memoized by item + concrete type args).
370
379
  pub(crate) fn registerCalls(
@@ -427,6 +436,49 @@ pub(crate) fn registerCalls(
427
436
  None => cs.name.clone(),
428
437
  };
429
438
  let key = (item.clone(), args.clone());
439
+ // A generic fn that calls itself (through any chain) with a bigger type each time
440
+ // (`nest(P(a: x, b: x), n - 1)` in `nest<T>`) would need a copy per size, without
441
+ // end: the types double at each level, so the depth limit alone never ends it. A
442
+ // third copy, each bigger than the one that called it, is taken as that (one bigger
443
+ // copy may be a fixed type, `show(Some(1))` in `show<T>`, which ends there).
444
+ if !st.info.inst_index.contains_key(&key) {
445
+ let size: usize = args.iter().map(typeSize).sum();
446
+ let mut growing: Vec<usize> = Vec::new();
447
+ let mut at = parent;
448
+ while let Some(k) = at {
449
+ let anc = &st.info.instantiations[k];
450
+ if anc.item == item {
451
+ let s: usize = anc.type_args.iter().map(typeSize).sum();
452
+ if s >= *growing.last().unwrap_or(&size) {
453
+ break;
454
+ }
455
+ growing.push(k);
456
+ if growing.len() == 2 {
457
+ break;
458
+ }
459
+ }
460
+ at = anc.parent;
461
+ }
462
+ if growing.len() == 2 {
463
+ let shown = |ts: &[PlumType]| ts.iter().map(|t| t.to_string()).collect::<Vec<_>>().join(", ");
464
+ let name = plum_core::diag::sourceName(&item);
465
+ let from = &st.info.instantiations[growing[0]].type_args;
466
+ let e = CheckError::at(
467
+ cs.line,
468
+ cs.column,
469
+ format!(
470
+ "{}call '{}': {name} calls itself with a bigger type each time ({name}<{}> from {name}<{}>), so it would need a copy for every size; give the call the same type arguments it has",
471
+ parent_chain,
472
+ plum_core::diag::sourceName(&cs.name),
473
+ shown(&args),
474
+ shown(from)
475
+ ),
476
+ )
477
+ .in_fns(parent_fns.clone());
478
+ withCheck(|s| s.bound_errors.borrow_mut().push(e));
479
+ continue;
480
+ }
481
+ }
430
482
  if !st.info.inst_index.contains_key(&key) {
431
483
  let bounds: Vec<(String, Vec<PlumType>)> = {
432
484
  let traits = currentTraits();
compiler/checker/tests/checker/review_tests.rs CHANGED
@@ -209,3 +209,20 @@ fn old_type_parameters_in_brackets_are_one_error() {
209
209
  assert!(ms.iter().any(|m| m.contains("T is not a trait") && m.contains("`S<T>`")), "{ms:?}");
210
210
  assert!(!ms.iter().any(|m| m.contains("unknown type T")), "{ms:?}");
211
211
  }
212
+
213
+ /// Polymorphic recursion (`nest<T>` calling `nest<P<T, T>>`) is an error, found at once,
214
+ /// where the copies used to double in size down to the depth limit (the compile hung);
215
+ /// a generic fn that calls itself once with a fixed bigger type still compiles.
216
+ #[test]
217
+ fn a_generic_fn_calling_itself_with_a_growing_type_is_an_error() {
218
+ let es = stdErrors(
219
+ "polyrec",
220
+ "struct P<A, B>(a: A, b: B)\n\nfun nest<T>(x: T, n: Int) -> Int = if n == 0 then 0 else 1 + nest(P(a: x, b: x), n - 1)\n\nfun main() -> Int = nest(1, 3)",
221
+ );
222
+ assert!(es.iter().any(|e| e.contains("nest calls itself with a bigger type each time")), "{es:?}");
223
+ let ok = stdErrors(
224
+ "fixedrec",
225
+ "fun sh<T>(x: T, n: Int) -> Int = if n == 0 then 0 else 1 + sh(Some(1), n - 1)\n\nfun main() -> Int = sh(1, 3)",
226
+ );
227
+ assert!(ok.is_empty(), "{ok:?}");
228
+ }