plum

#treesitter#compiler#wasm

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

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


0e76183 — Peter John 2026-11-10T23:12:53+05:30
plum run and build refuse a program without main; a missing path is named as missing
compiler/cli/src/main.rs CHANGED
@@ -407,6 +407,9 @@ fn cmdRun(
407
407
  if let Some(flag) = gameOptions.anyForRun() {
408
408
  anyhow::bail!("{flag} is only for games, and {} is not one", file.display());
409
409
  }
410
+ if !program.hasMain {
411
+ anyhow::bail!("{} has no `fun main` to run", file.display());
412
+ }
410
413
  let wasm_bytes = program.wasm;
411
414
  let resultMain = program.resultMain;
412
415
 
@@ -459,6 +462,9 @@ fn collectPlumFiles(path: &std::path::Path) -> Result<Vec<std::path::PathBuf>> {
459
462
  if path.is_file() {
460
463
  return Ok(vec![path.to_path_buf()]);
461
464
  }
465
+ if !path.exists() {
466
+ anyhow::bail!("{}: no such file or folder", path.display());
467
+ }
462
468
  let mut files = Vec::new();
463
469
  let mut dirs = vec![path.to_path_buf()];
464
470
  while let Some(dir) = dirs.pop() {
@@ -1006,6 +1012,9 @@ fn cmdBuild(
1006
1012
  if let Some(flag) = gameOptions.assets.as_ref().map(|_| "--assets") {
1007
1013
  anyhow::bail!("{flag} is only for games, and {} is not one", file.display());
1008
1014
  }
1015
+ if !program.hasMain {
1016
+ anyhow::bail!("{} has no `fun main`: an executable runs it", file.display());
1017
+ }
1009
1018
  let output = gameOptions.output;
1010
1019
  let wasm_bytes = program.wasm;
1011
1020
 
compiler/cli/src/program.rs CHANGED
@@ -16,6 +16,8 @@ pub struct Program {
16
16
  /// `main` returns a `Result`; the compiled `main` is a wrapper that crashes with the name
17
17
  /// of the `Err` variant (see `plum_core::main_result`).
18
18
  pub resultMain: bool,
19
+ /// The program has a `fun main` (a file of only declarations or tests has none).
20
+ pub hasMain: bool,
19
21
  }
20
22
 
21
23
  /// The removed game entry: `export fun init`, `update` and `draw`. Not a game any more; the
@@ -75,7 +77,8 @@ pub fn tryCompileProgram(
75
77
  if !out.isGame && hasLegacyGameExports(&ast) {
76
78
  return Err(Failure { codegen: false, text: format!("{}: the `export fun init/update/draw` game entry is gone; a game's `main` returns `Game<S, M>`, made by `Game.start(tree, state)` (std/game/Game)", file.display()) });
77
79
  }
80
+ let hasMain = ast.items.iter().any(|i| matches!(i, Item::Fn(f) if f.name == "main"));
78
- Ok(Program { isGame: out.isGame, wasm: out.wasm, resultMain })
81
+ Ok(Program { isGame: out.isGame, wasm: out.wasm, resultMain, hasMain })
79
82
  }
80
83
 
81
84
  /// The wasm of a `plum run` rebuild: the compile error text, or the same text a page would show
compiler/cli/tests/cli/inputs_test.rs ADDED
@@ -0,0 +1,60 @@
1
+ //! Every command on input that is not a program: a missing path, a folder, bytes that are
2
+ //! not UTF-8, a file with no `main`. Each fails with a message naming the input, and none
3
+ //! writes an output.
4
+ use crate::common::{plumBin, root, Scratch};
5
+ use std::process::Command;
6
+
7
+ fn plum(dir: &Scratch, args: &[&str]) -> (bool, String) {
8
+ let out = Command::new(plumBin()).current_dir(dir.path()).args(args).arg("--lib-path").arg(root()).output().unwrap();
9
+ (out.status.success(), String::from_utf8_lossy(&out.stderr).into_owned())
10
+ }
11
+
12
+ #[test]
13
+ fn eachCommandRefusesAMissingPathAFolderAndNonUtf8() {
14
+ let dir = Scratch::new("inputs");
15
+ std::fs::create_dir(dir.join("folder")).unwrap();
16
+ std::fs::write(dir.join("bytes.plum"), b"import std/Int\n\nfun main() -> Int =\n \"\xff\xfe\"\n").unwrap();
17
+ for cmd in ["compile", "run", "build", "check", "test"] {
18
+ let (ok, err) = plum(&dir, &[cmd, "missing.plum"]);
19
+ assert!(!ok && err.contains("missing.plum") && (err.contains("no such file") || err.contains("No such file")), "{cmd}: {err}");
20
+ assert!(!err.contains("directory missing.plum"), "{cmd}: {err}");
21
+ let (ok, err) = plum(&dir, &[cmd, "bytes.plum"]);
22
+ assert!(!ok && err.contains("bytes.plum") && err.contains("UTF-8"), "{cmd}: {err}");
23
+ }
24
+ for cmd in ["compile", "run", "build"] {
25
+ let (ok, err) = plum(&dir, &[cmd, "folder"]);
26
+ assert!(!ok && err.contains("folder"), "{cmd}: {err}");
27
+ }
28
+ let names: Vec<_> = std::fs::read_dir(dir.path()).unwrap().map(|e| e.unwrap().file_name()).collect();
29
+ assert_eq!(names.len(), 2, "{names:?}");
30
+ }
31
+
32
+ /// A file of only comments, or only declarations, compiles and checks, but has nothing to
33
+ /// run: `run` and `build` say so (`build` used to make an executable that panicked).
34
+ #[test]
35
+ fn aProgramWithoutMainIsNotRunOrBuilt() {
36
+ let dir = Scratch::new("inputs_nomain");
37
+ std::fs::write(dir.join("notes.plum"), "# only a comment\n").unwrap();
38
+ std::fs::write(dir.join("lib.plum"), "import std/Int\n\nfun twice(n: Int) -> Int =\n n * 2\n").unwrap();
39
+ for file in ["notes.plum", "lib.plum"] {
40
+ for cmd in ["run", "build"] {
41
+ let (ok, err) = plum(&dir, &[cmd, file]);
42
+ assert!(!ok && err.contains(&format!("{file} has no `fun main`")), "{cmd} {file}: {err}");
43
+ }
44
+ assert!(plum(&dir, &["check", file]).0, "check {file}");
45
+ }
46
+ assert!(!dir.join("notes").exists() && !dir.join("lib").exists());
47
+ }
48
+
49
+ /// A path with spaces, in a folder with spaces, compiles and runs.
50
+ #[test]
51
+ fn aPathWithSpacesWorks() {
52
+ let dir = Scratch::new("inputs_spaces");
53
+ std::fs::create_dir(dir.join("a folder")).unwrap();
54
+ std::fs::write(dir.join("a folder/my prog.plum"), "import std/Int\n\nfun main() -> Int =\n 41 + 1\n").unwrap();
55
+ let (ok, err) = plum(&dir, &["compile", "a folder/my prog.plum"]);
56
+ assert!(ok, "{err}");
57
+ assert!(dir.join("a folder/my prog.wasm").is_file());
58
+ let out = Command::new(plumBin()).current_dir(dir.path()).args(["run", "a folder/my prog.plum", "--lib-path"]).arg(root()).output().unwrap();
59
+ assert_eq!(String::from_utf8_lossy(&out.stdout).trim(), "42");
60
+ }
compiler/cli/tests/cli/main.rs CHANGED
@@ -12,6 +12,7 @@ mod fusion_agree_test;
12
12
  mod game_entry_test;
13
13
  mod game_web_test;
14
14
  mod host_stub_test;
15
+ mod inputs_test;
15
16
  mod main_result_test;
16
17
  mod no_fbip_test;
17
18
  mod old_forms_test;