Deprecate From<EvalAltResult> for Result<T, Box<EvalAltResult>> because it is clearer for code to explicitly wrap errors in Err.

This commit is contained in:
Stephen Chung
2021-10-19 23:52:58 +08:00
parent 6d31bb0d19
commit 3001e90775
23 changed files with 226 additions and 193 deletions

View File

@@ -1221,7 +1221,7 @@ impl Engine {
return if let Some(val) = this_ptr {
Ok(((*val).into(), *pos))
} else {
EvalAltResult::ErrorUnboundThis(*pos).into()
Err(EvalAltResult::ErrorUnboundThis(*pos).into())
}
}
_ if state.always_search_scope => (0, expr.position()),
@@ -1723,7 +1723,7 @@ impl Engine {
}
}
// Syntax error
_ => EvalAltResult::ErrorDotExpr("".into(), rhs.position()).into(),
_ => Err(EvalAltResult::ErrorDotExpr("".into(), rhs.position()).into()),
}
}
}
@@ -1958,12 +1958,11 @@ impl Engine {
arr_len
- index
.checked_abs()
.ok_or_else(|| {
EvalAltResult::ErrorArrayBounds(arr_len, index, idx_pos).into()
})
.ok_or_else(|| EvalAltResult::ErrorArrayBounds(arr_len, index, idx_pos))
.and_then(|n| {
if n as usize > arr_len {
EvalAltResult::ErrorArrayBounds(arr_len, index, idx_pos).into()
Err(EvalAltResult::ErrorArrayBounds(arr_len, index, idx_pos)
.into())
} else {
Ok(n as usize)
}
@@ -2014,7 +2013,9 @@ impl Engine {
let offset = index as usize;
(
if offset >= bits {
return EvalAltResult::ErrorBitFieldBounds(bits, index, idx_pos).into();
return Err(
EvalAltResult::ErrorBitFieldBounds(bits, index, idx_pos).into()
);
} else {
(*value & (1 << offset)) != 0
},
@@ -2025,14 +2026,16 @@ impl Engine {
(
// Count from end if negative
if offset > bits {
return EvalAltResult::ErrorBitFieldBounds(bits, index, idx_pos).into();
return Err(
EvalAltResult::ErrorBitFieldBounds(bits, index, idx_pos).into()
);
} else {
(*value & (1 << (bits - offset))) != 0
},
offset,
)
} else {
return EvalAltResult::ErrorBitFieldBounds(bits, index, idx_pos).into();
return Err(EvalAltResult::ErrorBitFieldBounds(bits, index, idx_pos).into());
};
Ok(Target::BitField(target, offset, bit_value.into()))
@@ -2066,7 +2069,7 @@ impl Engine {
)
} else {
let chars_len = s.chars().count();
return EvalAltResult::ErrorStringBounds(chars_len, index, idx_pos).into();
return Err(EvalAltResult::ErrorStringBounds(chars_len, index, idx_pos).into());
};
Ok(Target::StringChar(target, offset, ch.into()))
@@ -2083,7 +2086,7 @@ impl Engine {
.map(|(v, _)| v.into())
}
_ => EvalAltResult::ErrorIndexingType(
_ => Err(EvalAltResult::ErrorIndexingType(
format!(
"{} [{}]",
self.map_type_name(target.type_name()),
@@ -2091,7 +2094,7 @@ impl Engine {
),
Position::NONE,
)
.into(),
.into()),
}
}
@@ -2386,7 +2389,9 @@ impl Engine {
) -> Result<(), Box<EvalAltResult>> {
if target.is_read_only() {
// Assignment to constant variable
return EvalAltResult::ErrorAssignmentToConstant(root.0.to_string(), root.1).into();
return Err(
EvalAltResult::ErrorAssignmentToConstant(root.0.to_string(), root.1).into(),
);
}
let mut new_val = new_val;
@@ -2482,8 +2487,11 @@ impl Engine {
.expect("`lhs_ptr` is `Variable`");
if !lhs_ptr.is_ref() {
return EvalAltResult::ErrorAssignmentToConstant(var_name.to_string(), pos)
.into();
return Err(EvalAltResult::ErrorAssignmentToConstant(
var_name.to_string(),
pos,
)
.into());
}
#[cfg(not(feature = "unchecked"))]
@@ -2735,11 +2743,11 @@ impl Engine {
if let Some(c) = counter_index {
#[cfg(not(feature = "unchecked"))]
if x > INT::MAX as usize {
return EvalAltResult::ErrorArithmetic(
return Err(EvalAltResult::ErrorArithmetic(
format!("for-loop counter overflow: {}", x),
counter.as_ref().expect("`counter` is `Some`").pos,
)
.into();
.into());
}
let mut counter_var = scope
@@ -2789,13 +2797,13 @@ impl Engine {
scope.rewind(orig_scope_len);
Ok(Dynamic::UNIT)
} else {
EvalAltResult::ErrorFor(expr.position()).into()
Err(EvalAltResult::ErrorFor(expr.position()).into())
}
}
// Continue/Break statement
Stmt::BreakLoop(options, pos) => {
EvalAltResult::LoopBreak(options.contains(AST_OPTION_BREAK_OUT), *pos).into()
Err(EvalAltResult::LoopBreak(options.contains(AST_OPTION_BREAK_OUT), *pos).into())
}
// Namespace-qualified function call
@@ -2918,29 +2926,29 @@ impl Engine {
// Throw value
Stmt::Return(options, Some(expr), pos) if options.contains(AST_OPTION_BREAK_OUT) => {
EvalAltResult::ErrorRuntime(
Err(EvalAltResult::ErrorRuntime(
self.eval_expr(scope, mods, state, lib, this_ptr, expr, level)?
.flatten(),
*pos,
)
.into()
.into())
}
// Empty throw
Stmt::Return(options, None, pos) if options.contains(AST_OPTION_BREAK_OUT) => {
EvalAltResult::ErrorRuntime(Dynamic::UNIT, *pos).into()
Err(EvalAltResult::ErrorRuntime(Dynamic::UNIT, *pos).into())
}
// Return value
Stmt::Return(_, Some(expr), pos) => EvalAltResult::Return(
Stmt::Return(_, Some(expr), pos) => Err(EvalAltResult::Return(
self.eval_expr(scope, mods, state, lib, this_ptr, expr, level)?
.flatten(),
*pos,
)
.into(),
.into()),
// Empty return
Stmt::Return(_, None, pos) => EvalAltResult::Return(Dynamic::UNIT, *pos).into(),
Stmt::Return(_, None, pos) => Err(EvalAltResult::Return(Dynamic::UNIT, *pos).into()),
// Let/const statement
Stmt::Var(expr, x, options, _) => {
@@ -3003,7 +3011,7 @@ impl Engine {
// Guard against too many modules
#[cfg(not(feature = "unchecked"))]
if state.num_modules >= self.max_modules() {
return EvalAltResult::ErrorTooManyModules(*_pos).into();
return Err(EvalAltResult::ErrorTooManyModules(*_pos).into());
}
if let Some(path) = self
@@ -3032,7 +3040,10 @@ impl Engine {
.map(|r| r.resolve(self, source, &path, path_pos))
})
.unwrap_or_else(|| {
EvalAltResult::ErrorModuleNotFound(path.to_string(), path_pos).into()
Err(
EvalAltResult::ErrorModuleNotFound(path.to_string(), path_pos)
.into(),
)
})?;
if let Some(name) = export.as_ref().map(|x| x.name.clone()) {
@@ -3065,7 +3076,9 @@ impl Engine {
if rename.is_empty() { name } else { rename }.clone(),
);
} else {
return EvalAltResult::ErrorVariableNotFound(name.to_string(), *pos).into();
return Err(
EvalAltResult::ErrorVariableNotFound(name.to_string(), *pos).into()
);
}
}
Ok(Dynamic::UNIT)
@@ -3182,11 +3195,11 @@ impl Engine {
.max_string_size
.map_or(usize::MAX, NonZeroUsize::get)
{
return EvalAltResult::ErrorDataTooLarge(
return Err(EvalAltResult::ErrorDataTooLarge(
"Length of string".to_string(),
Position::NONE,
)
.into();
.into());
}
#[cfg(not(feature = "no_index"))]
@@ -3196,8 +3209,11 @@ impl Engine {
.max_array_size
.map_or(usize::MAX, NonZeroUsize::get)
{
return EvalAltResult::ErrorDataTooLarge("Size of array".to_string(), Position::NONE)
.into();
return Err(EvalAltResult::ErrorDataTooLarge(
"Size of array".to_string(),
Position::NONE,
)
.into());
}
#[cfg(not(feature = "no_object"))]
@@ -3207,11 +3223,11 @@ impl Engine {
.max_map_size
.map_or(usize::MAX, NonZeroUsize::get)
{
return EvalAltResult::ErrorDataTooLarge(
return Err(EvalAltResult::ErrorDataTooLarge(
"Size of object map".to_string(),
Position::NONE,
)
.into();
.into());
}
Ok(())
@@ -3228,14 +3244,14 @@ impl Engine {
// Guard against too many operations
if self.max_operations() > 0 && state.num_operations > self.max_operations() {
return EvalAltResult::ErrorTooManyOperations(pos).into();
return Err(EvalAltResult::ErrorTooManyOperations(pos).into());
}
// Report progress - only in steps
if let Some(ref progress) = self.progress {
if let Some(token) = progress(state.num_operations) {
// Terminate script if progress returns a termination token
return EvalAltResult::ErrorTerminated(token, pos).into();
return Err(EvalAltResult::ErrorTerminated(token, pos).into());
}
}