plum

#treesitter#compiler#wasm

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

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


b999fb2 — Peter John 2026-11-10T20:01:39+05:30
Parse-time errors keep their own file
compiler/checker/src/program.rs CHANGED
@@ -232,7 +232,10 @@ pub(crate) fn checkSourceInner(source: &ast::Source) -> (CheckResult<()>, TypedI
232
232
  source
233
233
  .formErrors
234
234
  .iter()
235
+ .map(|(f, l, c, m)| {
236
+ let file = if f.is_empty() { &source.file } else { f };
235
- .map(|(l, c, m)| CheckError::at(*l, *c, m.clone())),
237
+ CheckError::new(DiagKind::Type, file.clone(), *l, *c, m.clone())
238
+ }),
236
239
  );
237
240
  let traits = std::rc::Rc::new(buildTraits(source));
238
241
  let primitive_generics = buildPrimitiveGenerics(source);
compiler/checker/tests/checker/review_tests.rs CHANGED
@@ -125,3 +125,23 @@ fn a_second_write_of_another_type_is_a_mismatch() {
125
125
  assert!(es.iter().any(|e| e.contains("assign 'c.v': type mismatch")), "{es:?}");
126
126
  }
127
127
  }
128
+
129
+ /// An error the parser leaves for the checker (here a `match` of conditions with no last
130
+ /// `| _`) names the file it is in, not the program's entry file.
131
+ #[test]
132
+ fn a_parse_time_form_error_names_its_own_file() {
133
+ let dir = plum_test_support::Scratch::new("form_file");
134
+ std::os::unix::fs::symlink(plum_test_support::repoRoot().join("std"), dir.join("std")).unwrap();
135
+ std::fs::create_dir_all(dir.join("sub")).unwrap();
136
+ std::fs::write(
137
+ dir.join("sub/m.plum"),
138
+ "import std/Int\n\nfun sign(n: Int) -> Int =\n match\n | n < 0 => 0\n | n > 0 => 1\n",
139
+ )
140
+ .unwrap();
141
+ std::fs::write(dir.join("main.plum"), "import std/Int\nimport sub/m\n\nfun main() -> Int = m.sign(1)\n").unwrap();
142
+ let source = plum_core::loader::loadAndMergeDiag(&dir.join("main.plum"), dir.path()).expect("loads");
143
+ let es = plum_checker::checkProgram(&source).0.expect_err("the form error");
144
+ let e = es.iter().find(|e| e.message.contains("last `| _ =>` arm")).unwrap_or_else(|| panic!("{es:?}"));
145
+ assert!(e.file.ends_with("m.plum"), "{e:?}");
146
+ assert_eq!(e.line, 4, "{e:?}");
147
+ }
compiler/core/src/ast.rs CHANGED
@@ -310,8 +310,9 @@ pub struct Source {
310
310
  /// other declaration and that `test` blocks form one contiguous group at
311
311
  /// the end of the file.
312
312
  pub topLevelOrder: Vec<TopLevelKind>,
313
- /// Errors the parser found that the checker reports (bad list literals).
313
+ /// Errors the parser found that the checker reports (bad list literals): the file
314
+ /// (set by `stampFile`, so a merged program keeps each one's own), line, column, message.
314
- pub formErrors: Vec<(usize, usize, String)>,
315
+ pub formErrors: Vec<(String, usize, usize, String)>,
315
316
  /// Some `for` is still to be lowered (its source's type decides how).
316
317
  pub formLoops: bool,
317
318
  /// Every `primitive <Name> =` block of this file. The loader allows one
@@ -363,6 +364,9 @@ impl Source {
363
364
  /// Records `file` on the source and on every fn it declares.
364
365
  pub fn stampFile(&mut self, file: &str) {
365
366
  self.file = file.to_string();
367
+ for e in &mut self.formErrors {
368
+ e.0 = file.to_string();
369
+ }
366
370
  for item in &mut self.items {
367
371
  match item {
368
372
  Item::Fn(f) => f.file = file.to_string(),
compiler/core/src/loader.rs CHANGED
@@ -769,7 +769,7 @@ fn lastLeaf(node: tree_sitter::Node) -> Option<tree_sitter::Node> {
769
769
  }
770
770
 
771
771
  thread_local! {
772
- static MERGED_LIT_ERRORS: std::cell::RefCell<Vec<(usize, usize, String)>> =
772
+ static MERGED_LIT_ERRORS: std::cell::RefCell<Vec<(String, usize, usize, String)>> =
773
773
  std::cell::RefCell::new(Vec::new());
774
774
  }
775
775
 
compiler/core/src/parser.rs CHANGED
@@ -200,7 +200,7 @@ impl<'a> AstParser<'a> {
200
200
  imports,
201
201
  items,
202
202
  topLevelOrder,
203
- formErrors: self.litErrors.borrow().clone(),
203
+ formErrors: self.litErrors.borrow().iter().map(|(l, c, m)| (String::new(), *l, *c, m.clone())).collect(),
204
204
  formLoops: false,
205
205
  primitives,
206
206
  };
compiler/core/tests/core/list_literal_test.rs CHANGED
@@ -87,7 +87,7 @@ fn pairsDesugarToMapSet() {
87
87
  fn mixingPairsAndValuesIsAnError() {
88
88
  let source = plum_test_support::parseRawSource(&main_("m := [1, k => 2]"));
89
89
  assert_eq!(source.formErrors.len(), 1, "{:?}", source.formErrors);
90
- let (line, column, msg) = &source.formErrors[0];
90
+ let (_, line, column, msg) = &source.formErrors[0];
91
91
  assert_eq!((*line, *column), (2, 8));
92
92
  assert!(msg.contains("cannot mix"), "{msg}");
93
93
  }
compiler/core/tests/core/loader_test.rs CHANGED
@@ -511,7 +511,7 @@ fn outOfRangeLiteralInEntryOrImportSurvivesTheMerge() {
511
511
  merged
512
512
  .formErrors
513
513
  .iter()
514
- .any(|e| e.2.contains("out of range")),
514
+ .any(|e| e.3.contains("out of range")),
515
515
  "{:?}",
516
516
  merged.formErrors
517
517
  );
compiler/core/tests/core/parser_test.rs CHANGED
@@ -234,7 +234,7 @@ fn int_literal_overflow_error() {
234
234
  source
235
235
  .formErrors
236
236
  .iter()
237
- .any(|e| e.2.contains("out of range")),
237
+ .any(|e| e.3.contains("out of range")),
238
238
  "{:?}",
239
239
  source.formErrors
240
240
  );