plum
git clone https://git.pyrossh.dev/plum
A statically typed, imperative programming language inspired by rust, python
bb4d893
— Peter John
2026-11-10T14:07:31+05:30
Checker: two open type variables that meet are linked
compiler/checker/src/callcheck.rs
CHANGED
|
@@ -138,6 +138,10 @@ pub(crate) fn inferClassCallRaw(
|
|
|
138
138
|
// (`head` of `Cons(head: Box(..), tail: [t])` before `tail` pins the element):
|
|
139
139
|
// their recorded types keep those vars, so they are inferred again below.
|
|
140
140
|
let mut early: Vec<(&ast::FieldArg, PlumType)> = Vec::new();
|
|
141
|
+
// A value whose own type is still open (`tail: xs` with `xs := Nil`) learns it
|
|
142
|
+
// from the field, as a call's argument does from its parameter: once checked,
|
|
143
|
+
// and again when every field has had its say.
|
|
144
|
+
let mut open: Vec<(PlumType, PlumType)> = Vec::new();
|
|
141
145
|
for fa in others.into_iter().chain(closures) {
|
|
142
146
|
match info.field_names.iter().position(|n| n == &fa.name) {
|
|
143
147
|
Some(i) => {
|
|
@@ -151,6 +155,10 @@ pub(crate) fn inferClassCallRaw(
|
|
|
151
155
|
unifyArg(&substVars(&pat, &binds), &actual, &fa.value, ctx).map_err(
|
|
152
156
|
|e| format!("variant '{}' field '{}': {}", call.type_name, fa.name, e),
|
|
153
157
|
)?;
|
|
158
|
+
if hasInferVars(&actual) {
|
|
159
|
+
bindMatch(&actual, &substVars(&pat, &binds), &mut binds);
|
|
160
|
+
open.push((actual, pat));
|
|
161
|
+
}
|
|
154
162
|
}
|
|
155
163
|
None => {
|
|
156
164
|
return Err(format!(
|
|
@@ -160,6 +168,9 @@ pub(crate) fn inferClassCallRaw(
|
|
|
160
168
|
}
|
|
161
169
|
}
|
|
162
170
|
}
|
|
171
|
+
for (actual, pat) in &open {
|
|
172
|
+
bindMatch(actual, &substVars(pat, &binds), &mut binds);
|
|
173
|
+
}
|
|
163
174
|
match &call.spread {
|
|
164
175
|
Some(spread) => {
|
|
165
176
|
checkSpreadSource(spread, &call.type_name, &info.enum_name, env, ctx)?;
|
compiler/checker/src/recording.rs
CHANGED
|
@@ -17,7 +17,19 @@ pub(crate) fn resolveRecorded(ty: &PlumType) -> PlumType {
|
|
|
17
17
|
pub(crate) fn recordBind(name: &str, ty: &PlumType) {
|
|
18
18
|
withCheck(|s| { let r = &s.record_binds; {
|
|
19
19
|
if let Some(m) = r.borrow_mut().as_mut() {
|
|
20
|
+
// A variable linked to another one (`bindMatch` of two open variables) is fixed
|
|
21
|
+
// at the end of its chain, so every variable linked to it resolves to `ty`.
|
|
22
|
+
let mut at = name.to_string();
|
|
23
|
+
for _ in 0..256 {
|
|
24
|
+
match m.get(&at) {
|
|
25
|
+
Some(PlumType::TVar(next)) if crate::unification::isInferVar(next) && !matches!(ty, PlumType::TVar(_)) => at = next.clone(),
|
|
26
|
+
_ => break,
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
if crate::unification::tvarOccurs(ty, &at) {
|
|
30
|
+
return;
|
|
31
|
+
}
|
|
20
|
-
m.insert(
|
|
32
|
+
m.insert(at, ty.clone());
|
|
21
33
|
}
|
|
22
34
|
} });
|
|
23
35
|
}
|
compiler/checker/src/unification.rs
CHANGED
|
@@ -30,10 +30,13 @@ pub(crate) fn substVars(ty: &PlumType, b: &Binds) -> PlumType {
|
|
|
30
30
|
if b.is_empty() {
|
|
31
31
|
return ty.clone();
|
|
32
32
|
}
|
|
33
|
+
// A chain of variable links ends at a type or an unbound variable; `MAX_LINKS` only
|
|
34
|
+
// guards against a cycle (the binders refuse to make one).
|
|
35
|
+
const MAX_LINKS: u32 = 256;
|
|
33
36
|
fn go(ty: &PlumType, b: &Binds, depth: u32) -> PlumType {
|
|
34
37
|
use PlumType::*;
|
|
35
38
|
match ty {
|
|
36
|
-
TVar(n) if depth <
|
|
39
|
+
TVar(n) if depth < MAX_LINKS => match b.get(n) {
|
|
37
40
|
Some(t) => go(t, b, depth + 1),
|
|
38
41
|
None => ty.clone(),
|
|
39
42
|
},
|
|
@@ -108,6 +111,9 @@ pub(crate) fn instSigMap(
|
|
|
108
111
|
pub(crate) fn bindMatch(pattern: &PlumType, actual: &PlumType, b: &mut Binds) {
|
|
109
112
|
use PlumType::*;
|
|
110
113
|
match (pattern, actual) {
|
|
114
|
+
// Two open variables that must be the same type are linked: whatever later fixes
|
|
115
|
+
// one fixes the other, instead of the other being left to default to `Unit`.
|
|
116
|
+
(TVar(n), TVar(m)) if isInferVar(n) && isInferVar(m) => linkVars(n, actual, b),
|
|
111
117
|
(TVar(n), a) if isInferVar(n) => {
|
|
112
118
|
if matches!(a, TVar(_)) {
|
|
113
119
|
return;
|
|
@@ -147,6 +153,20 @@ pub(crate) fn bindMatch(pattern: &PlumType, actual: &PlumType, b: &mut Binds) {
|
|
|
147
153
|
}
|
|
148
154
|
}
|
|
149
155
|
|
|
156
|
+
/// Links the open variable `n` to the open variable `a`, unless `n` is bound already or `a`
|
|
157
|
+
/// resolves back to `n` (the occurs check: no cycle).
|
|
158
|
+
fn linkVars(n: &str, a: &PlumType, b: &mut Binds) {
|
|
159
|
+
if b.contains_key(n) || withCheck(|s| s.record_binds.borrow().as_ref().is_some_and(|r| r.contains_key(n))) {
|
|
160
|
+
return;
|
|
161
|
+
}
|
|
162
|
+
let seen = resolveRecorded(&substVars(a, b));
|
|
163
|
+
if tvarOccurs(&seen, n) {
|
|
164
|
+
return;
|
|
165
|
+
}
|
|
166
|
+
b.insert(n.to_string(), a.clone());
|
|
167
|
+
recordBind(n, a);
|
|
168
|
+
}
|
|
169
|
+
|
|
150
170
|
/// `bindMatch` of an implementer's own argument `p` from its trait object's `a`, where `a`
|
|
151
171
|
/// may itself still be open: `Holder.of(Box(id: 1))`, with `of(top: Thing[M])`, gives
|
|
152
172
|
/// `Box[_t1]` against `Thing[_t2]` before anything fixes `_t2`. The implementer's variable
|
tests/core/open_vars_link.plum
ADDED
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
# Two values whose types are both still open when they meet (`pick(a, b)` with `a := None`,
|
|
2
|
+
# `b := None`; the branches of `if c then None else None`; `Cons(head: 1, tail: xs)` with
|
|
3
|
+
# `xs := Nil`) are linked, so what later fixes one fixes the other. They used to stay apart:
|
|
4
|
+
# the one nothing else fixed became `Option[Unit]` / `List[Unit]` while its use was built for
|
|
5
|
+
# `Int`, which was invalid wasm ("expected (ref null $type), found (ref null $type)").
|
|
6
|
+
import std/Int
|
|
7
|
+
import std/Str
|
|
8
|
+
import std/List
|
|
9
|
+
import std/Option
|
|
10
|
+
import std/Pair
|
|
11
|
+
|
|
12
|
+
struct Box[T](v: Option[T])
|
|
13
|
+
|
|
14
|
+
fun pick[T](a: Option[T], b: Option[T]) -> Option[T] = if a.isSome() then a else b
|
|
15
|
+
|
|
16
|
+
fun first[T](xs: List[T], ys: List[T]) -> List[T] = match xs
|
|
17
|
+
Nil => ys
|
|
18
|
+
_ => xs
|
|
19
|
+
|
|
20
|
+
fun both[T](a: Option[T], b: Option[T]) -> Pair[Option[T], Option[T]] = Pair(first: a, second: b)
|
|
21
|
+
|
|
22
|
+
test "two open arguments of one generic are fixed together"
|
|
23
|
+
a := None
|
|
24
|
+
b := None
|
|
25
|
+
c := pick(a, b)
|
|
26
|
+
d: Option[Int] := c
|
|
27
|
+
assert d.unwrapOr(7) == 7
|
|
28
|
+
assert a.unwrapOr(8) == 8
|
|
29
|
+
assert b.unwrapOr(9) == 9
|
|
30
|
+
|
|
31
|
+
test "an open list meets a typed one"
|
|
32
|
+
e := Nil
|
|
33
|
+
f := first(e, [1, 2])
|
|
34
|
+
assert f.length() == 2
|
|
35
|
+
assert e.length() == 0
|
|
36
|
+
|
|
37
|
+
test "the branches of an inline if are both open"
|
|
38
|
+
c: Option[Int] := None
|
|
39
|
+
e := if c.isSome() then None else None
|
|
40
|
+
f: Option[Int] := e
|
|
41
|
+
assert f.unwrapOr(6) == 6
|
|
42
|
+
assert e.unwrapOr(7) == 7
|
|
43
|
+
|
|
44
|
+
test "an open value in a constructor's field"
|
|
45
|
+
xs := Nil
|
|
46
|
+
ys := Cons(head: 1, tail: xs)
|
|
47
|
+
assert ys.length() == 1
|
|
48
|
+
assert xs.length() == 0
|
|
49
|
+
|
|
50
|
+
test "open values through an alias, a pair and a generic struct"
|
|
51
|
+
a := None
|
|
52
|
+
b := a
|
|
53
|
+
c: Option[Int] := b
|
|
54
|
+
p := both(None, None)
|
|
55
|
+
q: Pair[Option[Str], Option[Str]] := p
|
|
56
|
+
bx := Box(v: None)
|
|
57
|
+
by: Box[Int] := bx
|
|
58
|
+
assert c.unwrapOr(1) == 1
|
|
59
|
+
assert a.unwrapOr(2) == 2
|
|
60
|
+
assert q.first.unwrapOr("s") == "s"
|
|
61
|
+
assert p.second.unwrapOr("t") == "t"
|
|
62
|
+
assert by.v.unwrapOr(4) == 4
|
|
63
|
+
assert bx.v.unwrapOr(5) == 5
|