plum
git clone https://git.pyrossh.dev/plum
A statically typed, imperative programming language inspired by rust, python
eddeba1
— Peter John
2026-11-10T12:30:56+05:30
Checker: a wrong type-argument count is reported once, with no follow-up
compiler/checker/src/declchecks.rs
CHANGED
|
@@ -18,6 +18,7 @@ pub(crate) fn checkBareGenericTypes(source: &ast::Source) -> Vec<CheckError> {
|
|
|
18
18
|
})
|
|
19
19
|
.collect();
|
|
20
20
|
let mut errors = Vec::new();
|
|
21
|
+
withCheck(|s| s.bad_arity.borrow_mut().clear());
|
|
21
22
|
if generic_enums.is_empty() {
|
|
22
23
|
return errors;
|
|
23
24
|
}
|
|
@@ -37,6 +38,7 @@ pub(crate) fn checkBareGenericTypes(source: &ast::Source) -> Vec<CheckError> {
|
|
|
37
38
|
if !t.generics.is_empty() && !params.isGeneric(&t.name) {
|
|
38
39
|
if let Some(&n) = generic_enums.get(t.name.as_str()) {
|
|
39
40
|
if n != t.generics.len() {
|
|
41
|
+
withCheck(|s| s.bad_arity.borrow_mut().insert((t.name.clone(), t.generics.len())));
|
|
40
42
|
errors.push(CheckError::at(
|
|
41
43
|
line,
|
|
42
44
|
column,
|
|
@@ -61,6 +63,20 @@ pub(crate) fn checkBareGenericTypes(source: &ast::Source) -> Vec<CheckError> {
|
|
|
61
63
|
walk(g, scope, params, line, column, generic_enums, errors);
|
|
62
64
|
}
|
|
63
65
|
}
|
|
66
|
+
// `walk`'s count check alone, for a type written in a body.
|
|
67
|
+
fn arity(t: &ast::Type, params: &Scope, line: usize, column: usize, generic_enums: &BTreeMap<&str, usize>, errors: &mut Vec<CheckError>) {
|
|
68
|
+
if !t.generics.is_empty() && !params.isGeneric(&t.name) {
|
|
69
|
+
if let Some(&n) = generic_enums.get(t.name.as_str()) {
|
|
70
|
+
if n != t.generics.len() {
|
|
71
|
+
withCheck(|s| s.bad_arity.borrow_mut().insert((t.name.clone(), t.generics.len())));
|
|
72
|
+
errors.push(CheckError::at(line, column, format!("type '{}' takes {} type argument(s), got {}", t.name, n, t.generics.len())));
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
for g in &t.generics {
|
|
77
|
+
arity(g, params, line, column, generic_enums, errors);
|
|
78
|
+
}
|
|
79
|
+
}
|
|
64
80
|
for item in &source.items {
|
|
65
81
|
match item {
|
|
66
82
|
ast::Item::Fn(f) => {
|
|
@@ -82,6 +98,30 @@ pub(crate) fn checkBareGenericTypes(source: &ast::Source) -> Vec<CheckError> {
|
|
|
82
98
|
if let Some(r) = &f.returns {
|
|
83
99
|
w(r);
|
|
84
100
|
}
|
|
101
|
+
// The types written in the body (annotated locals, type arguments of
|
|
102
|
+
// constructors and calls, closure parameters): only their argument count is
|
|
103
|
+
// checked, each at its top-level statement.
|
|
104
|
+
let stmts = match &f.body {
|
|
105
|
+
ast::FnBody::Block(b) => b.stmts.clone(),
|
|
106
|
+
ast::FnBody::Expr(e) => vec![ast::Stmt::Expr(e.clone(), f.line, f.column)],
|
|
107
|
+
ast::FnBody::Extern => Vec::new(),
|
|
108
|
+
};
|
|
109
|
+
for mut s in stmts {
|
|
110
|
+
let (line, column) = if s.span.startLine != 0 { (s.span.startLine, s.span.startColumn) } else { (f.line, f.column) };
|
|
111
|
+
plum_core::rename::typesInStmts(std::slice::from_mut(&mut s), &mut |t| {
|
|
112
|
+
arity(t, ¶ms, line, column, &generic_enums, &mut errors)
|
|
113
|
+
});
|
|
114
|
+
// a constructor's own type arguments: `Box[Int, Int](value: 2)`
|
|
115
|
+
plum_core::visit::walkBlockRef(&ast::Block { stmts: vec![s] }, &mut |e| {
|
|
116
|
+
if let ast::ExprKind::ClassCall(c) = &e.kind {
|
|
117
|
+
if !c.generics.is_empty() {
|
|
118
|
+
let t = ast::Type { name: c.type_name.clone(), generics: Vec::new() };
|
|
119
|
+
let t = ast::Type { generics: c.generics.iter().map(|_| t.clone()).collect(), ..t };
|
|
120
|
+
arity(&t, ¶ms, line, column, &generic_enums, &mut errors);
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
});
|
|
124
|
+
}
|
|
85
125
|
}
|
|
86
126
|
ast::Item::Enum(e) => {
|
|
87
127
|
for ty in e
|
compiler/checker/src/state.rs
CHANGED
|
@@ -71,6 +71,10 @@ pub(crate) struct CheckState {
|
|
|
71
71
|
/// The global names of std's `ref.same` (std/ref.plum), whose argument type the checker
|
|
72
72
|
/// limits to heap types (`identity::checkSameArgs`).
|
|
73
73
|
pub(crate) same_fns: RefCell<std::collections::HashSet<String>>,
|
|
74
|
+
/// Generic types written with the wrong number of type arguments, as (name, the count
|
|
75
|
+
/// written): `declchecks::checkBareGenericTypes` reports each, and `plumTypeFromAstG`
|
|
76
|
+
/// turns such a type into a wildcard so the one mistake has no follow-up mismatches.
|
|
77
|
+
pub(crate) bad_arity: RefCell<std::collections::HashSet<(String, usize)>>,
|
|
74
78
|
/// The names of the top-level functions and constants (the keys of the global env).
|
|
75
79
|
pub(crate) globals: RefCell<std::collections::HashSet<String>>,
|
|
76
80
|
pub(crate) scope_site: RefCell<Option<(String, usize, String)>>,
|
compiler/checker/src/typeconv.rs
CHANGED
|
@@ -13,6 +13,10 @@ pub fn plumTypeFromAstG(ty: &ast::Type) -> PlumType {
|
|
|
13
13
|
);
|
|
14
14
|
}
|
|
15
15
|
if !ty.generics.is_empty() {
|
|
16
|
+
// The wrong number of type arguments, reported where it is written: any type fits.
|
|
17
|
+
if withCheck(|s| s.bad_arity.borrow().contains(&(ty.name.clone(), ty.generics.len()))) {
|
|
18
|
+
return PlumType::TVar("_".to_string());
|
|
19
|
+
}
|
|
16
20
|
return PlumType::TApp(
|
|
17
21
|
ty.name.clone(),
|
|
18
22
|
ty.generics.iter().map(plumTypeFromAstG).collect(),
|
compiler/checker/tests/checker/diagnostic_tests.rs
CHANGED
|
@@ -214,3 +214,23 @@ fn anUnpinnedGenericCallNamesItsCaller() {
|
|
|
214
214
|
let e = es.iter().find(|e| e.message.contains("can't work out")).unwrap_or_else(|| panic!("{es:?}"));
|
|
215
215
|
assert_eq!(e.inFns, vec!["main".to_string()], "{es:?}");
|
|
216
216
|
}
|
|
217
|
+
|
|
218
|
+
/// `Box[Int, Int]` for `struct Box[T]` is reported once, where it is written, with no
|
|
219
|
+
/// follow-up mismatch (`expected Box[Int, Int], found Box[Int]`, `expected Int, found T`):
|
|
220
|
+
/// in a signature, on an annotated local, and as a constructor's type arguments.
|
|
221
|
+
#[test]
|
|
222
|
+
fn wrongTypeArgumentCountIsReportedOnceWithNoFollowUp() {
|
|
223
|
+
let src = "struct Box[T](value: T)\n\nfun get(b: Box[Int, Int]) -> Int = b.value\n\nfun main() -> Int =\n b: Box[Int, Int] := Box(value: 1)\n c := Box[Int, Int](value: 2)\n b.value + get(Box(value: 3)) + c.value\n";
|
|
224
|
+
let es = errors(src);
|
|
225
|
+
assert!(es.iter().all(|e| e.message == "type 'Box' takes 1 type argument(s), got 2"), "{es:?}");
|
|
226
|
+
let at: Vec<usize> = es.iter().map(|e| e.line).collect();
|
|
227
|
+
assert_eq!(at, vec![3, 6, 7], "{es:?}");
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
/// The right count still checks its arguments.
|
|
231
|
+
#[test]
|
|
232
|
+
fn rightTypeArgumentCountStillReportsAMismatch() {
|
|
233
|
+
let es = errors("struct Box[T](value: T)\n\nfun main() -> Int =\n b: Box[Int] := Box(value: true)\n b.value\n");
|
|
234
|
+
assert_eq!(es.len(), 1, "{es:?}");
|
|
235
|
+
assert!(es[0].message.contains("mismatch"), "{es:?}");
|
|
236
|
+
}
|