Skip to content

Commit 8c90e46

Browse files
sharkdpDavid Peter
authored and
David Peter
committed
Avoid poisoned Lazy instance by moving potential panic outside
1 parent 577d068 commit 8c90e46

File tree

4 files changed

+9
-11
lines changed

4 files changed

+9
-11
lines changed

numbat/src/interpreter.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use thiserror::Error;
1313

1414
pub use crate::value::Value;
1515

16-
#[derive(Debug, Error, PartialEq, Eq)]
16+
#[derive(Debug, Clone, Error, PartialEq, Eq)]
1717
pub enum RuntimeError {
1818
#[error("Division by zero")]
1919
DivisionByZero,

numbat/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ use unit_registry::UnitMetadata;
7171
use crate::prefix_parser::PrefixParserResult;
7272
use crate::unicode_input::UNICODE_INPUT;
7373

74-
#[derive(Debug, Error)]
74+
#[derive(Debug, Clone, Error)]
7575
pub enum NumbatError {
7676
#[error("{0}")]
7777
ResolverError(ResolverError),

numbat/src/typechecker.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ use num_traits::{CheckedAdd, CheckedDiv, CheckedMul, CheckedSub, FromPrimitive,
2525
use thiserror::Error;
2626
use unicode_width::UnicodeWidthStr;
2727

28-
#[derive(Debug, PartialEq, Eq)]
28+
#[derive(Debug, Clone, PartialEq, Eq)]
2929
pub struct IncompatibleDimensionsError {
3030
pub span_operation: Span,
3131
pub operation: String,
@@ -200,7 +200,7 @@ impl fmt::Display for IncompatibleDimensionsError {
200200

201201
impl Error for IncompatibleDimensionsError {}
202202

203-
#[derive(Debug, Error, PartialEq, Eq)]
203+
#[derive(Debug, Clone, Error, PartialEq, Eq)]
204204
pub enum TypeCheckError {
205205
#[error("Unknown identifier '{1}'.")]
206206
UnknownIdentifier(Span, String, Option<String>),

numbat/tests/common.rs

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use std::path::Path;
22

3-
use numbat::{module_importer::FileSystemImporter, resolver::CodeSource, Context};
3+
use numbat::{module_importer::FileSystemImporter, resolver::CodeSource, Context, NumbatError};
44
use once_cell::sync::Lazy;
55

66
pub fn get_test_context_without_prelude() -> Context {
@@ -17,14 +17,12 @@ pub fn get_test_context_without_prelude() -> Context {
1717
}
1818

1919
pub fn get_test_context() -> Context {
20-
static CONTEXT: Lazy<Context> = Lazy::new(|| {
20+
static CONTEXT: Lazy<Result<Context, NumbatError>> = Lazy::new(|| {
2121
let mut context = get_test_context_without_prelude();
2222

23-
let _ = context
24-
.interpret("use prelude", CodeSource::Internal)
25-
.expect("Error while running prelude");
26-
context
23+
let _ = context.interpret("use prelude", CodeSource::Internal)?;
24+
Ok(context)
2725
});
2826

29-
CONTEXT.clone()
27+
CONTEXT.clone().unwrap()
3028
}

0 commit comments

Comments
 (0)