plum
git clone https://git.pyrossh.dev/plum
A statically typed, imperative programming language inspired by rust, python
89b61f7
— Peter John
2026-11-10T20:09:47+05:30
Checker: an enum's name or an unknown name with fields is no constructor
compiler/checker/src/callcheck.rs
CHANGED
|
@@ -229,6 +229,23 @@ pub(crate) fn inferClassCallRaw(
|
|
|
229
229
|
markResolution();
|
|
230
230
|
Err("a list literal `[..]` builds a std List: import std/collections/List".to_string())
|
|
231
231
|
}
|
|
232
|
+
// An enum's own name with fields (`E(..e, n: 3)`): it has no fields, only its variants.
|
|
233
|
+
None if ctx.enum_variants.isEnum(&call.type_name) => {
|
|
234
|
+
let variants: Vec<&str> = {
|
|
235
|
+
let mut vs: Vec<(&String, &EnumVariantInfo)> = ctx.enum_variants.variantsOf(&call.type_name).collect();
|
|
236
|
+
vs.sort_by_key(|(_, i)| i.tag);
|
|
237
|
+
vs.into_iter().map(|(n, _)| n.as_str()).collect()
|
|
238
|
+
};
|
|
239
|
+
Err(format!(
|
|
240
|
+
"'{}' is an enum: build one of its variants ({}), not the enum itself",
|
|
241
|
+
call.type_name,
|
|
242
|
+
variants.join(", ")
|
|
243
|
+
))
|
|
244
|
+
}
|
|
245
|
+
None if !crate::declchecks::isKnownType(&call.type_name, env, ctx) => {
|
|
246
|
+
markResolution();
|
|
247
|
+
Err(format!("unknown type or variant '{}'", call.type_name))
|
|
248
|
+
}
|
|
232
249
|
_ => Ok(PlumType::TNamed(call.type_name.clone())),
|
|
233
250
|
}
|
|
234
251
|
}
|
compiler/checker/src/declchecks.rs
CHANGED
|
@@ -247,8 +247,10 @@ pub(crate) fn checkUnknownTypes(source: &ast::Source) -> Vec<CheckError> {
|
|
|
247
247
|
for v in &e.variants {
|
|
248
248
|
v.fields.iter().for_each(|t| walk(t, &sc, &known, &mut bad));
|
|
249
249
|
}
|
|
250
|
+
// An entry that names no trait is reported as that (`struct S[T](..)`, with
|
|
251
|
+
// the hint for type parameters); only its arguments are types here.
|
|
250
252
|
for t in &e.implements {
|
|
251
|
-
walk(
|
|
253
|
+
t.generics.iter().for_each(|g| walk(g, &sc, &known, &mut bad));
|
|
252
254
|
}
|
|
253
255
|
for n in bad.into_iter().filter(|n| n != ast::SELF_TYPE) {
|
|
254
256
|
errors.push(CheckError::at(e.line, e.column, format!("{}: unknown type {}", e.name, n)));
|
compiler/checker/tests/checker/review_tests.rs
CHANGED
|
@@ -178,3 +178,9 @@ fn a_lowered_tests_errors_name_the_test() {
|
|
|
178
178
|
assert!(m.contains("test \"adds up\""), "{m}");
|
|
179
179
|
assert!(!m.contains("__"), "{m}");
|
|
180
180
|
}
|
|
181
|
+
|
|
182
|
+
#[test]
|
|
183
|
+
fn an_enum_or_unknown_name_with_fields_is_no_constructor() {
|
|
184
|
+
errorWith("enum E =\n | A(n: Int)\n | B(s: Int)\n\nfun k(e: E) -> E = E(..e, n: 3)", "'E' is an enum: build one of its variants (A, B)");
|
|
185
|
+
errorWith("fun k() -> Int =\n x := Foo(a: 1)\n 1", "unknown type or variant 'Foo'");
|
|
186
|
+
}
|