plum

#treesitter#compiler#wasm

git clone https://git.pyrossh.dev/plum

A statically typed, imperative programming language inspired by rust, python


7131601 — Peter John 2026-11-10T11:39:15+05:30
loader: positioned duplicate and missing-import errors, duplicate tests
compiler/core/src/loader.rs CHANGED
@@ -27,10 +27,6 @@ fn load(message: String) -> Vec<Diagnostic> {
27
27
  vec![Diagnostic::bare(DiagKind::Load, message)]
28
28
  }
29
29
 
30
- fn resolution(message: String) -> Vec<Diagnostic> {
31
- vec![Diagnostic::bare(DiagKind::Resolution, message)]
32
- }
33
-
34
30
  /// Same as [`loadAndMerge`], but `overrides` (keyed by canonicalized path)
35
31
  /// supplies in-memory text for specific files instead of reading them from
36
32
  /// disk — every other file (including transitively imported ones) still
@@ -323,12 +319,24 @@ fn checkBuiltinImports(path: &Path, source: &Source, lib_path: &Path) -> Loaded_
323
319
  let import_path = format!("std/{}", name);
324
320
  let already_imported = source.imports.iter().any(|imp| imp.path == import_path);
325
321
  if !already_imported {
322
+ // At the first declaration that uses it (the file is the diagnostic's own).
323
+ let mut one = Source { items: Vec::new(), ..source.clone() };
324
+ let line = source
325
+ .items
326
+ .iter()
327
+ .find(|i| {
328
+ one.items = vec![(*i).clone()];
329
+ usedBuiltinTypeNames(&one).contains(name)
330
+ })
331
+ .map(itemLine)
332
+ .unwrap_or(0);
326
- return Err(resolution(format!(
333
+ return Err(vec![Diagnostic::new(
334
+ DiagKind::Resolution,
335
+ displayPath(path),
336
+ line,
337
+ 1,
327
- "'{}' uses type '{}' but doesn't import it: add `import {}`",
338
+ format!("uses type '{}' but doesn't import it: add `import {}`", name, import_path),
328
- path.display(),
329
- name,
330
- import_path
331
- )));
339
+ )]);
332
340
  }
333
341
  }
334
342
  Ok(())
@@ -706,11 +714,22 @@ fn mergeItems(
706
714
  .filter(|p| !BUILTIN_PRIMITIVES.contains(&p.name.as_str()) || !p.generics.is_empty())
707
715
  .cloned(),
708
716
  );
717
+ let mut test_names: HashMap<&str, usize> = HashMap::new();
709
718
  for item in &source.items {
710
719
  // `test` blocks aren't global symbols (nothing references them by
711
720
  // name), so identically-named tests in different files aren't a
712
- // real conflict — skip the collision check for them entirely.
721
+ // real conflict. Within one file the name is the test's export: two would
722
+ // be one wasm export twice.
713
- if let Item::Test(_) = item {
723
+ if let Item::Test(t) = item {
724
+ if let Some(first) = test_names.insert(t.name.as_str(), t.line) {
725
+ return Err(vec![Diagnostic::new(
726
+ DiagKind::Resolution,
727
+ displayPath(file),
728
+ t.line,
729
+ t.column,
730
+ format!("duplicate test \"{}\": a test of this name is already at line {}", t.name, first),
731
+ )]);
732
+ }
714
733
  items.push(item.clone());
715
734
  continue;
716
735
  }
@@ -735,12 +754,23 @@ fn mergeItems(
735
754
  }
736
755
  }
737
756
  if let Some(existing_file) = names.get(&key) {
757
+ let what = itemDisplayName(item);
758
+ let message = if existing_file == file {
738
- return Err(load(format!(
759
+ let first = items
760
+ .iter()
761
+ .rev()
762
+ .find(|i| !matches!(i, Item::Test(_)) && itemNameKey(i) == key)
763
+ .map(itemLine)
764
+ .unwrap_or(0);
765
+ format!("duplicate declaration of {what}: already declared at line {first}")
766
+ } else {
767
+ format!(
739
- "duplicate declaration '{}': declared in both '{}' and '{}'",
768
+ "duplicate declaration of {what}: declared in both '{}' and '{}'",
740
- key,
741
- existing_file.display(),
769
+ displayPath(existing_file),
742
- file.display()
770
+ displayPath(file)
771
+ )
743
- )));
772
+ };
773
+ return Err(vec![Diagnostic::new(DiagKind::Load, displayPath(file), itemLine(item), 1, message)]);
744
774
  }
745
775
  names.insert(key, file.to_path_buf());
746
776
  items.push(item.clone());
@@ -748,6 +778,31 @@ fn mergeItems(
748
778
  Ok(())
749
779
  }
750
780
 
781
+ /// An item's 1-based declaration line (0 when unknown).
782
+ fn itemLine(item: &Item) -> usize {
783
+ match item {
784
+ Item::Enum(e) => e.line,
785
+ Item::Trait(t) => t.line,
786
+ Item::Const(c) => c.line,
787
+ Item::Fn(f) => f.line,
788
+ Item::Test(t) => t.line,
789
+ }
790
+ }
791
+
792
+ /// How a duplicate-declaration message names an item: `function 'f'`, `method 'Box.get'`.
793
+ fn itemDisplayName(item: &Item) -> String {
794
+ match item {
795
+ Item::Enum(e) => format!("type '{}'", e.name),
796
+ Item::Trait(t) => format!("trait '{}'", t.name),
797
+ Item::Const(c) => format!("constant '{}'", c.name),
798
+ Item::Fn(f) => match &f.type_param {
799
+ Some(r) => format!("method '{}.{}'", r, f.name),
800
+ None => format!("function '{}'", f.name),
801
+ },
802
+ Item::Test(t) => format!("test \"{}\"", t.name),
803
+ }
804
+ }
805
+
751
806
  /// A collision key that mirrors `plum-checker`'s own separation of
752
807
  /// namespaces: methods are keyed by `(receiver, name)` (so `length<Cat>` and
753
808
  /// `length<Box>` never collide, exactly like `plum_checker::MethodEnv`),
compiler/core/src/modules.rs CHANGED
@@ -502,7 +502,12 @@ impl Cx<'_> {
502
502
  }
503
503
  AttrKind::Field(name) => {
504
504
  if !world.consts[target].contains(name) {
505
+ let message = if world.fns[target].contains(name) {
506
+ format!("{module}.{name} is a function, not a value: call it, or wrap it as |x| {module}.{name}(x)")
507
+ } else {
505
- self.error(line, column, format!("module '{module}' has no constant '{name}'"));
508
+ format!("module '{module}' has no constant '{name}'")
509
+ };
510
+ self.error(line, column, message);
506
511
  return;
507
512
  }
508
513
  Expr::TypeName(world.global(target, name, &world.constCollide))
compiler/core/src/tests_lower.rs CHANGED
@@ -238,6 +238,10 @@ fn desugarAssertStmt(stmt: &crate::ast::Stmt, counter: &mut usize) -> Vec<crate:
238
238
  else_ifs: vec![],
239
239
  else_: None,
240
240
  }));
241
+ // The statements made from an `assert` are its own: an error in them is at it.
242
+ for s in &mut pre {
243
+ s.span = stmt.span;
244
+ }
241
245
  pre
242
246
  }
243
247
  crate::ast::StmtKind::If(i) => vec![crate::ast::Stmt::If(crate::ast::If {
compiler/core/tests/core/loader_test.rs CHANGED
@@ -623,7 +623,7 @@ fn userTypeShadowsStdTypeOnlyWhenNotImportedDirectly() {
623
623
  assert!(types.contains(&"Pair") && types.contains(&"PairStd"), "{types:?}");
624
624
  let direct = format!("import std/Pair\n{shadow}");
625
625
  let err = plum_test_support::load("direct_pair", &direct).unwrap_err();
626
- assert!(err.contains("duplicate declaration 'enum::Pair'"), "{err}");
626
+ assert!(err.contains("duplicate declaration of type 'Pair'"), "{err}");
627
627
  }
628
628
 
629
629
  fn fnNames(merged: &plum_core::ast::Source) -> Vec<String> {