plum
git clone https://git.pyrossh.dev/plum
A statically typed, imperative programming language inspired by rust, python
a107655
— Peter John
2026-11-10T19:50:25+05:30
Checker: an open variable keeps the type its first use fixes
compiler/checker/src/recording.rs
CHANGED
|
@@ -29,6 +29,12 @@ pub(crate) fn recordBind(name: &str, ty: &PlumType) {
|
|
|
29
29
|
if crate::unification::tvarOccurs(ty, &at) {
|
|
30
30
|
return;
|
|
31
31
|
}
|
|
32
|
+
// A variable an earlier use fixed keeps that type: a later use with another type
|
|
33
|
+
// (`c.v = Some(1)` then `c.v = Some("a")`) is a mismatch `unify` reports, not a
|
|
34
|
+
// new binding that would compile the first use as the second's type.
|
|
35
|
+
if m.get(&at).is_some_and(|old| !crate::callsites::hasInferVars(old) && !matches!(old, PlumType::TVar(_))) {
|
|
36
|
+
return;
|
|
37
|
+
}
|
|
32
38
|
m.insert(at, ty.clone());
|
|
33
39
|
}
|
|
34
40
|
} });
|
compiler/checker/src/unification.rs
CHANGED
|
@@ -193,6 +193,18 @@ fn bindOpenArg(p: &PlumType, a: &PlumType, b: &mut Binds) {
|
|
|
193
193
|
}
|
|
194
194
|
|
|
195
195
|
pub fn unify(t1: &PlumType, t2: &PlumType, ctx: &CheckCtx) -> Result<(), String> {
|
|
196
|
+
// A variable an earlier use fixed is that type here: `x := None`, given to an
|
|
197
|
+
// `Option<Int>` and then to an `Option<Str>`, is a mismatch, not a wildcard twice.
|
|
198
|
+
if hasInferVars(t1) || hasInferVars(t2) {
|
|
199
|
+
let (a, b) = (resolveRecorded(t1), resolveRecorded(t2));
|
|
200
|
+
if a != *t1 || b != *t2 {
|
|
201
|
+
return unifyResolved(&a, &b, ctx);
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
unifyResolved(t1, t2, ctx)
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
fn unifyResolved(t1: &PlumType, t2: &PlumType, ctx: &CheckCtx) -> Result<(), String> {
|
|
196
208
|
match (t1, t2) {
|
|
197
209
|
(PlumType::TVar(_), _) | (_, PlumType::TVar(_)) => Ok(()),
|
|
198
210
|
(PlumType::TInt, PlumType::TInt) => Ok(()),
|
|
@@ -325,6 +337,12 @@ pub(crate) fn unifyArg(
|
|
|
325
337
|
ctx: &CheckCtx,
|
|
326
338
|
) -> Result<(), String> {
|
|
327
339
|
unify(expected, actual, ctx).map_err(|e| format!("{}{}", e, wrapHint(expected, actual, ctx)))?;
|
|
340
|
+
// The value is that type from here on: what one side leaves open, the other fixes
|
|
341
|
+
// (`x := None` then `a: Option<Int> := x`, or `cell.v = Some(1)`), instead of the open
|
|
342
|
+
// side being left to default to `Unit` while the other is compiled as written.
|
|
343
|
+
if hasInferVars(expected) || hasInferVars(actual) {
|
|
344
|
+
bindAcross(expected, actual);
|
|
345
|
+
}
|
|
328
346
|
if let (PlumType::TNamed(variant_name), PlumType::TNamed(actual_name)) = (expected, actual) {
|
|
329
347
|
if variant_name != actual_name
|
|
330
348
|
&& ctx.enum_variants.contains_key(variant_name)
|
compiler/checker/tests/checker/main.rs
CHANGED
|
@@ -11,6 +11,7 @@ mod fip_test;
|
|
|
11
11
|
mod fusion_pipeline_test;
|
|
12
12
|
mod inline_variants_tests;
|
|
13
13
|
mod question_tests;
|
|
14
|
+
mod review_tests;
|
|
14
15
|
mod str_plus_tests;
|
|
15
16
|
mod syntax_tests;
|
|
16
17
|
mod traits_tests;
|
compiler/checker/tests/checker/review_tests.rs
ADDED
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
#![allow(non_snake_case)]
|
|
2
|
+
#![allow(dead_code)]
|
|
3
|
+
//! Programs the checker used to accept and leave to codegen (or to run wrongly).
|
|
4
|
+
use plum_checker::checkSource;
|
|
5
|
+
|
|
6
|
+
fn run(body: &str) -> Result<(), Vec<plum_checker::types::CheckError>> {
|
|
7
|
+
let mut source = plum_test_support::parse(&format!("{body}\n"));
|
|
8
|
+
plum_core::lower::frontEndDiag(&mut source).expect("front end");
|
|
9
|
+
checkSource(&source)
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
fn errorWith(body: &str, needle: &str) -> plum_checker::types::CheckError {
|
|
13
|
+
let es = run(body).expect_err(&format!("expected an error:\n{body}"));
|
|
14
|
+
es.iter()
|
|
15
|
+
.find(|e| e.message.contains(needle))
|
|
16
|
+
.cloned()
|
|
17
|
+
.unwrap_or_else(|| panic!("{body}\nno '{needle}' error in {:?}", es.iter().map(|x| &x.message).collect::<Vec<_>>()))
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
fn ok(body: &str) {
|
|
21
|
+
if let Err(es) = run(body) {
|
|
22
|
+
panic!("{body}\nexpected no error, got {:?}", es.iter().map(|x| &x.message).collect::<Vec<_>>());
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
fn stdErrors(name: &str, body: &str) -> Vec<String> {
|
|
27
|
+
plum_test_support::checkErrors(name, &format!("import std/Int\nimport std/Str\nimport std/Option\nimport std/collections/List\n\n{body}\n"))
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/// `x := None` fixed as an `Option<Int>` by its first use is not an `Option<Str>` later.
|
|
31
|
+
#[test]
|
|
32
|
+
fn an_open_local_is_fixed_by_its_first_use() {
|
|
33
|
+
let es = stdErrors(
|
|
34
|
+
"openlocal",
|
|
35
|
+
"fun a(o: Option<Int>) -> Int = o.unwrapOr(1)\nfun b(o: Option<Str>) -> Int = o.unwrapOr(\"s\").length()\n\nfun k() -> Int =\n x := None\n a(x) + b(x)",
|
|
36
|
+
);
|
|
37
|
+
assert!(es.iter().any(|e| e.contains("expected Option<Str>, found Option<Int>")), "{es:?}");
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/// What one side of an argument leaves open, the other fixes: still a valid program.
|
|
41
|
+
#[test]
|
|
42
|
+
fn an_open_local_given_one_type_checks() {
|
|
43
|
+
let es = stdErrors("openlocal_ok", "fun k() -> Int =\n x := None\n a: Option<Int> := x\n a.unwrapOr(3)");
|
|
44
|
+
assert!(es.is_empty(), "{es:?}");
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/// A field whose type the first write fixes is not rebound by a second write of another
|
|
48
|
+
/// type: that compiled `Some(1)` into an `Option<Str>` slot, invalid wasm.
|
|
49
|
+
#[test]
|
|
50
|
+
fn a_second_write_of_another_type_is_a_mismatch() {
|
|
51
|
+
for (a, b) in [("Some(1)", "Some(\"a\")"), ("Some(\"a\")", "Some(1)")] {
|
|
52
|
+
let es = stdErrors(
|
|
53
|
+
"rebind",
|
|
54
|
+
&format!("struct Cell<T>(mut v: Option<T>)\n\nfun k() -> Int =\n c := Cell(v: None)\n c.v = {a}\n c.v = {b}\n 1"),
|
|
55
|
+
);
|
|
56
|
+
assert!(es.iter().any(|e| e.contains("assign 'c.v': type mismatch")), "{es:?}");
|
|
57
|
+
}
|
|
58
|
+
}
|