From 1dfd1fe0ded46f87d5457a130391866a792c2a4c Mon Sep 17 00:00:00 2001 From: tjjfvi Date: Fri, 24 May 2024 11:14:13 -0400 Subject: [PATCH 1/3] error gracefully when @main is missing --- src/ast.rs | 25 +++++++++++++------------ tests/run.rs | 2 +- 2 files changed, 14 insertions(+), 13 deletions(-) diff --git a/src/ast.rs b/src/ast.rs index 3d4e890b..f4bc7310 100644 --- a/src/ast.rs +++ b/src/ast.rs @@ -505,18 +505,19 @@ impl Book { } let mut book = hvm::Book { defs: Vec::new() }; for (fid, name) in &fid_to_name { - if let Some(ast_def) = self.defs.get(name) { - let mut def = hvm::Def { - name: name.clone(), - safe: true, - root: hvm::Port(0), - rbag: vec![], - node: vec![], - vars: 0, - }; - ast_def.build(&mut def, &name_to_fid, &mut BTreeMap::new()); - book.defs.push(def); - } + let Some(ast_def) = self.defs.get(name) else { + panic!("missing `@main` definition"); + }; + let mut def = hvm::Def { + name: name.clone(), + safe: true, + root: hvm::Port(0), + rbag: vec![], + node: vec![], + vars: 0, + }; + ast_def.build(&mut def, &name_to_fid, &mut BTreeMap::new()); + book.defs.push(def); } return book; } diff --git a/tests/run.rs b/tests/run.rs index 743c0add..baf805bc 100644 --- a/tests/run.rs +++ b/tests/run.rs @@ -60,7 +60,7 @@ fn execute_hvm(args: &[&OsStr]) -> Result> { stderr.read_to_string(&mut output)?; Ok(if !status.success() { - format!("exited with code {status}:\n{output}") + format!("{status}\n{output}") } else { parse_output(&output).unwrap_or_else(|err| { panic!("error parsing output:\n{err}\n\n{output}") From 552e79be39a9a65007502a8bd5f20c9633046961 Mon Sep 17 00:00:00 2001 From: tjjfvi Date: Fri, 24 May 2024 11:23:40 -0400 Subject: [PATCH 2/3] add test --- tests/programs/empty.hvm | 0 tests/snapshots/run__file@empty.hvm.snap | 9 +++++++++ 2 files changed, 9 insertions(+) create mode 100644 tests/programs/empty.hvm create mode 100644 tests/snapshots/run__file@empty.hvm.snap diff --git a/tests/programs/empty.hvm b/tests/programs/empty.hvm new file mode 100644 index 00000000..e69de29b diff --git a/tests/snapshots/run__file@empty.hvm.snap b/tests/snapshots/run__file@empty.hvm.snap new file mode 100644 index 00000000..c2ad3ff0 --- /dev/null +++ b/tests/snapshots/run__file@empty.hvm.snap @@ -0,0 +1,9 @@ +--- +source: tests/run.rs +expression: rust_output +input_file: tests/programs/empty.hvm +--- +exit status: 101 +thread 'main' panicked at src/ast.rs:509:9: +missing `@main` definition +note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace From 9aca3669c6f4f7ca3edbe1145092faa9c3785cb9 Mon Sep 17 00:00:00 2001 From: tjjfvi Date: Fri, 24 May 2024 14:40:47 -0400 Subject: [PATCH 3/3] use expect instead of let else --- src/ast.rs | 4 +--- tests/snapshots/run__file@empty.hvm.snap | 2 +- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/src/ast.rs b/src/ast.rs index f4bc7310..5e99c454 100644 --- a/src/ast.rs +++ b/src/ast.rs @@ -505,9 +505,7 @@ impl Book { } let mut book = hvm::Book { defs: Vec::new() }; for (fid, name) in &fid_to_name { - let Some(ast_def) = self.defs.get(name) else { - panic!("missing `@main` definition"); - }; + let ast_def = self.defs.get(name).expect("missing `@main` definition"); let mut def = hvm::Def { name: name.clone(), safe: true, diff --git a/tests/snapshots/run__file@empty.hvm.snap b/tests/snapshots/run__file@empty.hvm.snap index c2ad3ff0..b5b4735e 100644 --- a/tests/snapshots/run__file@empty.hvm.snap +++ b/tests/snapshots/run__file@empty.hvm.snap @@ -4,6 +4,6 @@ expression: rust_output input_file: tests/programs/empty.hvm --- exit status: 101 -thread 'main' panicked at src/ast.rs:509:9: +thread 'main' panicked at src/ast.rs:508:41: missing `@main` definition note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace