plum
git clone https://git.pyrossh.dev/plum
A statically typed, imperative programming language inspired by rust, python
33b5632
— Peter John
2026-11-10T12:15:25+05:30
Fusion: a producer's written type arguments go into the fused fn
compiler/core/src/drive.rs
CHANGED
|
@@ -57,6 +57,8 @@ struct Call {
|
|
|
57
57
|
key: String,
|
|
58
58
|
f: Fn,
|
|
59
59
|
args: Vec<Expr>,
|
|
60
|
+
/// The call's explicit type arguments (`t.kidsAs[Dot](0)`): `f`'s own generics, in order.
|
|
61
|
+
targs: Vec<Type>,
|
|
60
62
|
}
|
|
61
63
|
|
|
62
64
|
struct Driver {
|
|
@@ -840,7 +842,7 @@ impl Driver {
|
|
|
840
842
|
let f = &self.fns[&c.name];
|
|
841
843
|
match positional(&c.args) {
|
|
842
844
|
Some(args) if args.len() == f.params.len() && !f.has_self => {
|
|
843
|
-
vec![Call { key: c.name.clone(), f: f.clone(), args }]
|
|
845
|
+
vec![Call { key: c.name.clone(), f: f.clone(), args, targs: c.type_args.clone() }]
|
|
844
846
|
}
|
|
845
847
|
_ => vec![],
|
|
846
848
|
}
|
|
@@ -858,12 +860,12 @@ impl Driver {
|
|
|
858
860
|
let key = self.typeNameMethodKey(t, &c.name);
|
|
859
861
|
return match self.fns.get(&key) {
|
|
860
862
|
Some(f) if !f.has_self && rest.len() == f.params.len() => {
|
|
861
|
-
vec![Call { key, f: f.clone(), args: rest }]
|
|
863
|
+
vec![Call { key, f: f.clone(), args: rest, targs: c.type_args.clone() }]
|
|
862
864
|
}
|
|
863
865
|
Some(f) if f.has_self && rest.len() == f.params.len() => {
|
|
864
866
|
let mut args = vec![a.object.clone()];
|
|
865
867
|
args.extend(rest);
|
|
866
|
-
vec![Call { key, f: f.clone(), args }]
|
|
868
|
+
vec![Call { key, f: f.clone(), args, targs: c.type_args.clone() }]
|
|
867
869
|
}
|
|
868
870
|
_ => vec![],
|
|
869
871
|
};
|
|
@@ -875,7 +877,7 @@ impl Driver {
|
|
|
875
877
|
if f.has_self && rest.len() == f.params.len() {
|
|
876
878
|
let mut args = vec![a.object.clone()];
|
|
877
879
|
args.extend(rest.iter().cloned());
|
|
878
|
-
return vec![Call { key, f: f.clone(), args }];
|
|
880
|
+
return vec![Call { key, f: f.clone(), args, targs: c.type_args.clone() }];
|
|
879
881
|
}
|
|
880
882
|
}
|
|
881
883
|
}
|
|
@@ -884,7 +886,7 @@ impl Driver {
|
|
|
884
886
|
if rest.len() == f.params.len() {
|
|
885
887
|
let mut args = vec![a.object.clone()];
|
|
886
888
|
args.extend(rest.iter().cloned());
|
|
887
|
-
out.push(Call { key: key.clone(), f: f.clone(), args });
|
|
889
|
+
out.push(Call { key: key.clone(), f: f.clone(), args, targs: c.type_args.clone() });
|
|
888
890
|
}
|
|
889
891
|
}
|
|
890
892
|
out
|
|
@@ -1111,6 +1113,14 @@ impl Driver {
|
|
|
1111
1113
|
self.note(format!("impure argument: {} of {} would reorder effects; not fused", c.key, p.key));
|
|
1112
1114
|
continue;
|
|
1113
1115
|
}
|
|
1116
|
+
// Type arguments naming this fn's own generics (`only[T](xs)` inside `inner[T]`) can
|
|
1117
|
+
// be neither built into the fused fn nor worked out again at its call.
|
|
1118
|
+
let mut written = HashSet::new();
|
|
1119
|
+
p.targs.iter().for_each(|t| tyNames(t, &mut written));
|
|
1120
|
+
if written.iter().any(|n| self.scope.isGeneric(n)) {
|
|
1121
|
+
self.note(format!("not fused: {} of {}: its type arguments name a generic of this fn", c.key, p.key));
|
|
1122
|
+
continue;
|
|
1123
|
+
}
|
|
1114
1124
|
let fixed = self.argGens(p);
|
|
1115
1125
|
let memoKey = if fixed.is_empty() {
|
|
1116
1126
|
(c.key.clone(), p.key.clone())
|
|
@@ -1799,6 +1809,11 @@ impl Driver {
|
|
|
1799
1809
|
/// (`self.stars.map(..)`: `T` is `Star`), so its body's method calls resolve.
|
|
1800
1810
|
fn argGens(&self, p: &Call) -> HashMap<String, Type> {
|
|
1801
1811
|
let mut out = HashMap::new();
|
|
1812
|
+
// written type arguments (`t.kidsAs[Dot](0)`) fix the producer's own generics: one
|
|
1813
|
+
// that only its result mentions has nothing else to be worked out from
|
|
1814
|
+
if p.targs.len() == p.f.generics.len() {
|
|
1815
|
+
out.extend(p.f.generics.iter().cloned().zip(p.targs.iter().cloned()));
|
|
1816
|
+
}
|
|
1802
1817
|
let sc = self.scopeOf(&p.f);
|
|
1803
1818
|
let Some(pt) = self.ptypes(&p.f) else { return out };
|
|
1804
1819
|
for (t, a) in pt.iter().zip(&p.args) {
|
|
@@ -1829,6 +1844,9 @@ impl Driver {
|
|
|
1829
1844
|
if c.f.has_self && k != 0 {
|
|
1830
1845
|
return Err("method consumer matching on a non-receiver argument".to_string());
|
|
1831
1846
|
}
|
|
1847
|
+
if !c.targs.is_empty() {
|
|
1848
|
+
return Err("the consumer is called with type arguments".to_string());
|
|
1849
|
+
}
|
|
1832
1850
|
for f in [&c.f, &p.f] {
|
|
1833
1851
|
if let Err(why) = self.exprBodied(f) {
|
|
1834
1852
|
return Err(format!("not expression-bodied {}: {}", f.name, why));
|
|
@@ -1933,6 +1951,13 @@ impl Driver {
|
|
|
1933
1951
|
if let Some(r) = &cret {
|
|
1934
1952
|
collectGens(&fsc, &substType(r, &map), &mut fgens);
|
|
1935
1953
|
}
|
|
1954
|
+
// A generic the fused fn's parameters don't mention (the producer's call wrote it:
|
|
1955
|
+
// `kidsAs[T](0)` inside a generic fn) could not be worked out at the fused call.
|
|
1956
|
+
let mut inParams = HashSet::new();
|
|
1957
|
+
params.iter().for_each(|p| ptGens(&fsc, &p.ty, &mut inParams));
|
|
1958
|
+
if let Some(g) = fgens.iter().find(|g| !inParams.contains(*g)) {
|
|
1959
|
+
return Err(format!("generic {g} is only in the fused result"));
|
|
1960
|
+
}
|
|
1936
1961
|
let mut fgens: Vec<String> = fgens.into_iter().collect();
|
|
1937
1962
|
fgens.sort();
|
|
1938
1963
|
let f = Fn {
|
compiler/core/src/rename.rs
CHANGED
|
@@ -134,7 +134,13 @@ pub fn typesInStmts(stmts: &mut [Stmt], f: &mut dyn FnMut(&mut Type)) {
|
|
|
134
134
|
|
|
135
135
|
fn typesInPattern(p: &mut CasePattern, f: &mut dyn FnMut(&mut Type)) {
|
|
136
136
|
match p {
|
|
137
|
+
// the matched type itself may be a generic (`T as t`): seen as one type, name and arguments
|
|
137
|
-
CasePattern::As { args, .. } =>
|
|
138
|
+
CasePattern::As { ty, args, .. } => {
|
|
139
|
+
let mut t = Type { name: std::mem::take(ty), generics: std::mem::take(args) };
|
|
140
|
+
f(&mut t);
|
|
141
|
+
*ty = t.name;
|
|
142
|
+
*args = t.generics;
|
|
143
|
+
}
|
|
138
144
|
CasePattern::Class { fields, .. } => fields.iter_mut().for_each(|x| typesInPattern(x, f)),
|
|
139
145
|
_ => {}
|
|
140
146
|
}
|
compiler/driver/tests/driver_tests.rs
CHANGED
|
@@ -418,12 +418,14 @@ fn everyGameKeepsEveryPass() {
|
|
|
418
418
|
/// Test files whose rewrites once failed their re-check (a type only the removed context gave:
|
|
419
419
|
/// a positional `Ok(5)` under a fused consumer, an inlined `Nil` argument, a closure bound to a
|
|
420
420
|
/// local, `T.Max` inlined out of its fn, a dropped parameter that typed a kept `Nil`, a closure
|
|
421
|
+
/// `|x| Ok(x + 1)` inlined into a copy, a producer called with type arguments under a consumer):
|
|
421
|
-
///
|
|
422
|
+
/// built as `plum test` builds them, nothing is dropped.
|
|
422
423
|
#[test]
|
|
423
424
|
fn formerlyDroppingTestFilesKeepEveryPass() {
|
|
424
425
|
let root = plum_test_support::repoRoot();
|
|
425
426
|
let files = [
|
|
426
427
|
"std/collections/functional/FBTree.plum",
|
|
428
|
+
"tests/core/fused_type_args.plum",
|
|
427
429
|
"tests/wasm_codegen/e2e_tail/closure_captures_iteration_value.plum",
|
|
428
430
|
"tests/wasm_codegen/e2e_tail/question_in_tail_not_tail_call.plum",
|
|
429
431
|
"tests/wasm_codegen/e2e_tail/result_mutual_million.plum",
|
tests/core/fused_type_args.plum
ADDED
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
# A producer called with type arguments (`only[Dot](xs)`) under a consumer (`.at(0)`,
|
|
2
|
+
# `.length()`): fusion once built a fused fn still generic in `T`, which nothing at its call
|
|
3
|
+
# could work out ("can't work out T for 'get$only$0'"), so the whole pass failed its re-check.
|
|
4
|
+
# The written arguments now go into the fused fn, `T as t` patterns included; a call whose
|
|
5
|
+
# type arguments name the enclosing fn's own generics is left alone.
|
|
6
|
+
import std/Int
|
|
7
|
+
import std/Str
|
|
8
|
+
import std/List
|
|
9
|
+
import std/Option
|
|
10
|
+
|
|
11
|
+
trait Thing =
|
|
12
|
+
fun id(self) -> Int
|
|
13
|
+
|
|
14
|
+
struct Dot(n: Int) is Thing =
|
|
15
|
+
fun id(self) -> Int = self.n
|
|
16
|
+
|
|
17
|
+
struct Sq(n: Int) is Thing =
|
|
18
|
+
fun id(self) -> Int = self.n * 100
|
|
19
|
+
|
|
20
|
+
fun only[T](xs: List[Thing]) -> List[T] = match xs
|
|
21
|
+
Nil => Nil
|
|
22
|
+
Cons(h, rest) => match h
|
|
23
|
+
T as t => Cons(head: t, tail: only[T](rest))
|
|
24
|
+
_ => only[T](rest)
|
|
25
|
+
|
|
26
|
+
fun countOf[T](xs: List[Thing]) -> Int = match xs
|
|
27
|
+
Nil => 0
|
|
28
|
+
Cons(h, rest) => match h
|
|
29
|
+
T as t => 1 + countOf[T](rest)
|
|
30
|
+
_ => countOf[T](rest)
|
|
31
|
+
|
|
32
|
+
fun things() -> List[Thing] =
|
|
33
|
+
a: Thing := Dot(n: 1)
|
|
34
|
+
b: Thing := Sq(n: 2)
|
|
35
|
+
c: Thing := Dot(n: 3)
|
|
36
|
+
[a, b, c]
|
|
37
|
+
|
|
38
|
+
fun firstDot(xs: List[Thing]) -> Int = only[Dot](xs).at(0).n
|
|
39
|
+
|
|
40
|
+
fun lastDot(xs: List[Thing]) -> Int = only[Dot](xs).get(1).unwrap().n
|
|
41
|
+
|
|
42
|
+
fun squares(xs: List[Thing]) -> Int = only[Sq](xs).length()
|
|
43
|
+
|
|
44
|
+
# the type argument is this fn's own generic
|
|
45
|
+
fun inner[T](xs: List[Thing]) -> Int = only[T](xs).length()
|
|
46
|
+
|
|
47
|
+
test "a chained call on a producer with type arguments"
|
|
48
|
+
assert firstDot(things()) == 1
|
|
49
|
+
assert lastDot(things()) == 3
|
|
50
|
+
assert squares(things()) == 1
|
|
51
|
+
|
|
52
|
+
test "type arguments naming the caller's generic"
|
|
53
|
+
assert inner[Dot](things()) == 2
|
|
54
|
+
assert inner[Sq](things()) == 1
|
|
55
|
+
|
|
56
|
+
test "a consumer called with type arguments"
|
|
57
|
+
a: Thing := Dot(n: 1)
|
|
58
|
+
b: Thing := Sq(n: 2)
|
|
59
|
+
assert countOf[Dot](Cons(head: a, tail: Cons(head: b, tail: Nil))) == 1
|
|
60
|
+
assert countOf[Sq](things()) == 1
|