From f59c0941f57432c08e346e0c531accf64f907988 Mon Sep 17 00:00:00 2001 From: Mark <> Date: Thu, 20 Jun 2024 15:52:57 +0200 Subject: [PATCH] add more graceful errors (w/ stacktrace) to stdlib --- mers/Cargo.toml | 4 ++-- mers_lib/Cargo.toml | 2 +- mers_lib/src/program/configs/with_base.rs | 6 +++--- mers_lib/src/program/configs/with_iters.rs | 16 +++++++-------- mers_lib/src/program/configs/with_math.rs | 20 ++++++++++--------- .../program/configs/with_multithreading.rs | 2 +- 6 files changed, 26 insertions(+), 24 deletions(-) diff --git a/mers/Cargo.toml b/mers/Cargo.toml index 25bfb4f..f333ab8 100644 --- a/mers/Cargo.toml +++ b/mers/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "mers" -version = "0.8.9" +version = "0.8.10" edition = "2021" license = "MIT OR Apache-2.0" description = "dynamically typed but type-checked programming language" @@ -11,7 +11,7 @@ repository = "https://github.com/Dummi26/mers" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] -mers_lib = "0.8.9" +mers_lib = "0.8.10" # mers_lib = { path = "../mers_lib" } clap = { version = "4.3.19", features = ["derive"] } colored = "2.1.0" diff --git a/mers_lib/Cargo.toml b/mers_lib/Cargo.toml index de20780..97f184b 100755 --- a/mers_lib/Cargo.toml +++ b/mers_lib/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "mers_lib" -version = "0.8.9" +version = "0.8.10" edition = "2021" license = "MIT OR Apache-2.0" description = "library to use the mers language in other projects" diff --git a/mers_lib/src/program/configs/with_base.rs b/mers_lib/src/program/configs/with_base.rs index 012426d..588b411 100755 --- a/mers_lib/src/program/configs/with_base.rs +++ b/mers_lib/src/program/configs/with_base.rs @@ -93,7 +93,7 @@ impl Config { } else if let Some(data::float::Float(n)) = a.as_any().downcast_ref() { Duration::from_secs_f64(*n) } else { - unreachable!("sleep called on non-int/non-float") + return Err("sleep called on non-int/non-float".into()); }); Ok(Data::empty_tuple()) }), @@ -154,7 +154,7 @@ impl Config { // -1 if more elements than isize can represent i.take(isize::MAX as usize + 1).count() as isize } else { - unreachable!("called len on {a:?}, which isn't a tuple or a string") + return Err("called len on {a:?}, which isn't a tuple or a string".into()); }))) }), inner_statements: None, @@ -394,7 +394,7 @@ impl Config { // return func.run(arg.clone()); // } // } -// unreachable!("try: no function found") +// unreacha ble!("try: no function found") // }), // inner_statements: None, // }) diff --git a/mers_lib/src/program/configs/with_iters.rs b/mers_lib/src/program/configs/with_iters.rs index c8d3a58..c0f368b 100755 --- a/mers_lib/src/program/configs/with_iters.rs +++ b/mers_lib/src/program/configs/with_iters.rs @@ -97,15 +97,15 @@ impl Config { } Ok(Data::empty_tuple()) } else { - unreachable!( - "for_each called on tuple not containing iterable and function" - ) + return Err( + "for_each called on tuple not containing iterable and function".into() + ); } } else { - unreachable!("for_each called on tuple with len < 2") + return Err("for_each called on tuple with len < 2".into()); } } else { - unreachable!("for_each called on non-tuple") + return Err("for_each called on non-tuple".into()); } }), inner_statements: None, @@ -223,13 +223,13 @@ fn genfunc_iter_and_arg( if let Some(f) = f.get().as_any().downcast_ref::() { Ok(Data::new(Iter(fd(f), v.clone()))) } else { - unreachable!("{name} called on tuple not containing function") + return Err("{name} called on tuple not containing function".into()); } } else { - unreachable!("{name} called on tuple with len < 2") + return Err("{name} called on tuple with len < 2".into()); } } else { - unreachable!("{name} called on non-tuple") + return Err("{name} called on non-tuple".into()); } }), inner_statements: None, diff --git a/mers_lib/src/program/configs/with_math.rs b/mers_lib/src/program/configs/with_math.rs index a67ef6e..20d7564 100755 --- a/mers_lib/src/program/configs/with_math.rs +++ b/mers_lib/src/program/configs/with_math.rs @@ -123,7 +123,7 @@ impl Config { -1 } else { 0 } - } else { unreachable!("called signum on non-number type")}))) + } else { return Err("called signum on non-number type".into()); }))) }), inner_statements: None, })) .add_var("div".to_string(), Data::new(data::function::Function { @@ -141,9 +141,9 @@ impl Config { (Some(data::int::Int(l)), None, None, Some(data::float::Float(r))) => Ok(Data::new(data::float::Float(*l as f64 / r))), (None, Some(data::float::Float(l)), Some(data::int::Int(r)), None) => Ok(Data::new(data::float::Float(l / *r as f64))), (None, Some(data::float::Float(l)), None, Some(data::float::Float(r))) => Ok(Data::new(data::float::Float(l / r))), - _ => unreachable!(), + _ => return Err("at least one of the arguments to div were neither an int nor a float".into()), } - } else { unreachable!() }), + } else { return Err("argument to div was not a tuple".into()); }), inner_statements: None, })).add_var("modulo".to_string(), Data::new(data::function::Function { info: Arc::new(program::run::Info::neverused()), @@ -160,9 +160,9 @@ impl Config { (Some(data::int::Int(l)), None, None, Some(data::float::Float(r))) => Ok(Data::new(data::float::Float(*l as f64 % r))), (None, Some(data::float::Float(l)), Some(data::int::Int(r)), None) => Ok(Data::new(data::float::Float(l % *r as f64))), (None, Some(data::float::Float(l)), None, Some(data::float::Float(r))) => Ok(Data::new(data::float::Float(l % r))), - _ => unreachable!(), + _ => return Err("at least one of the arguments to modulo were neither an int nor a float".into()), } - } else { unreachable!() }), + } else { return Err("argument to modulo was not a tuple".into()) }), inner_statements: None, })) .add_var( @@ -223,7 +223,7 @@ impl Config { Data::new(data::int::Int(sumi)) }) } else { - unreachable!("sum called on non-tuple") + return Err("sum called on non-tuple".into()); } }), inner_statements: None, @@ -298,7 +298,7 @@ impl Config { Data::new(data::int::Int(sumi)) }) } else { - unreachable!("sum called on non-tuple") + return Err("sum called on non-tuple".into()); } }), inner_statements: None, @@ -362,7 +362,7 @@ impl Config { Data::new(data::int::Int(prodi)) }) } else { - unreachable!("product called on non-tuple") + return Err("product called on non-tuple".into()); } }), inner_statements: None, @@ -436,7 +436,9 @@ fn ltgtoe_function( } else if let Some(data::float::Float(v)) = item.as_any().downcast_ref() { IntOrFloatOrNothing::Float(*v) } else { - unreachable!() + return Err( + "one of the (l/g)t[oe] function argument iterator elements were neither int nor float".into(), + ); }; if op(prev, new) { prev = new; diff --git a/mers_lib/src/program/configs/with_multithreading.rs b/mers_lib/src/program/configs/with_multithreading.rs index 62ac4a9..7f318e2 100755 --- a/mers_lib/src/program/configs/with_multithreading.rs +++ b/mers_lib/src/program/configs/with_multithreading.rs @@ -57,7 +57,7 @@ impl Config { move || f.run(Data::empty_tuple()), ))))))) } else { - unreachable!("thread called, but arg wasn't a function"); + return Err("thread called, but arg wasn't a function".into()); } }), inner_statements: None,