Skip to content

Commit 2783191

Browse files
authored
Merge pull request #6502 from jferrant/chore/aac-rename-short-return-type
Chore/aac rename short return type
2 parents acb1694 + 7bced7f commit 2783191

File tree

8 files changed

+42
-35
lines changed

8 files changed

+42
-35
lines changed

clarity-types/src/errors/mod.rs

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ pub enum Error {
4646
Unchecked(CheckErrors),
4747
Interpreter(InterpreterError),
4848
Runtime(RuntimeErrorType, Option<StackTrace>),
49-
ShortReturn(ShortReturnType),
49+
EarlyReturn(EarlyReturnError),
5050
}
5151

5252
/// InterpreterErrors are errors that *should never* occur.
@@ -104,8 +104,15 @@ pub enum RuntimeErrorType {
104104
}
105105

106106
#[derive(Debug, PartialEq)]
107-
pub enum ShortReturnType {
108-
ExpectedValue(Box<Value>),
107+
/// Errors triggered during Clarity contract evaluation that cause early termination.
108+
/// These errors halt evaluation and fail the transaction.
109+
pub enum EarlyReturnError {
110+
/// Failed to unwrap an `Optional` (`none`) or `Response` (`err` or `ok`) Clarity value.
111+
/// The `Box<Value>` holds the original or thrown value. Triggered by `try!`, `unwrap-or`, or
112+
/// `unwrap-err-or`.
113+
UnwrapFailed(Box<Value>),
114+
/// An 'asserts!' expression evaluated to false.
115+
/// The `Box<Value>` holds the value provided as the second argument to `asserts!`.
109116
AssertionFailed(Box<Value>),
110117
}
111118

@@ -122,7 +129,7 @@ impl PartialEq<Error> for Error {
122129
match (self, other) {
123130
(Error::Runtime(x, _), Error::Runtime(y, _)) => x == y,
124131
(Error::Unchecked(x), Error::Unchecked(y)) => x == y,
125-
(Error::ShortReturn(x), Error::ShortReturn(y)) => x == y,
132+
(Error::EarlyReturn(x), Error::EarlyReturn(y)) => x == y,
126133
(Error::Interpreter(x), Error::Interpreter(y)) => x == y,
127134
_ => false,
128135
}
@@ -208,9 +215,9 @@ impl From<(CheckErrors, &SymbolicExpression)> for Error {
208215
}
209216
}
210217

211-
impl From<ShortReturnType> for Error {
212-
fn from(err: ShortReturnType) -> Self {
213-
Error::ShortReturn(err)
218+
impl From<EarlyReturnError> for Error {
219+
fn from(err: EarlyReturnError) -> Self {
220+
Error::EarlyReturn(err)
214221
}
215222
}
216223

@@ -225,11 +232,11 @@ impl From<Error> for () {
225232
fn from(_err: Error) -> Self {}
226233
}
227234

228-
impl From<ShortReturnType> for Value {
229-
fn from(val: ShortReturnType) -> Self {
235+
impl From<EarlyReturnError> for Value {
236+
fn from(val: EarlyReturnError) -> Self {
230237
match val {
231-
ShortReturnType::ExpectedValue(v) => *v,
232-
ShortReturnType::AssertionFailed(v) => *v,
238+
EarlyReturnError::UnwrapFailed(v) => *v,
239+
EarlyReturnError::AssertionFailed(v) => *v,
233240
}
234241
}
235242
}
@@ -241,15 +248,15 @@ mod test {
241248
#[test]
242249
fn equality() {
243250
assert_eq!(
244-
Error::ShortReturn(ShortReturnType::ExpectedValue(Box::new(Value::Bool(true)))),
245-
Error::ShortReturn(ShortReturnType::ExpectedValue(Box::new(Value::Bool(true))))
251+
Error::EarlyReturn(EarlyReturnError::UnwrapFailed(Box::new(Value::Bool(true)))),
252+
Error::EarlyReturn(EarlyReturnError::UnwrapFailed(Box::new(Value::Bool(true))))
246253
);
247254
assert_eq!(
248255
Error::Interpreter(InterpreterError::InterpreterError("".to_string())),
249256
Error::Interpreter(InterpreterError::InterpreterError("".to_string()))
250257
);
251258
assert!(
252-
Error::ShortReturn(ShortReturnType::ExpectedValue(Box::new(Value::Bool(true))))
259+
Error::EarlyReturn(EarlyReturnError::UnwrapFailed(Box::new(Value::Bool(true))))
253260
!= Error::Interpreter(InterpreterError::InterpreterError("".to_string()))
254261
);
255262
}

clarity/src/vm/callables.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -294,7 +294,7 @@ impl DefinedFunction {
294294
match result {
295295
Ok(r) => Ok(r),
296296
Err(e) => match e {
297-
Error::ShortReturn(v) => Ok(v.into()),
297+
Error::EarlyReturn(v) => Ok(v.into()),
298298
_ => Err(e),
299299
},
300300
}

clarity/src/vm/errors.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@
1515
// along with this program. If not, see <http://www.gnu.org/licenses/>.
1616

1717
pub use clarity_types::errors::{
18-
Error, IncomparableError, InterpreterError, InterpreterResult, RuntimeErrorType,
19-
ShortReturnType,
18+
EarlyReturnError, Error, IncomparableError, InterpreterError, InterpreterResult,
19+
RuntimeErrorType,
2020
};
2121

2222
pub use crate::vm::analysis::errors::{

clarity/src/vm/functions/mod.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ use crate::vm::callables::{cost_input_sized_vararg, CallableType, NativeHandle};
2020
use crate::vm::costs::cost_functions::ClarityCostFunction;
2121
use crate::vm::costs::{constants as cost_constants, runtime_cost, CostTracker, MemoryConsumer};
2222
use crate::vm::errors::{
23-
check_argument_count, check_arguments_at_least, CheckErrors, Error,
24-
InterpreterResult as Result, ShortReturnType, SyntaxBindingError, SyntaxBindingErrorType,
23+
check_argument_count, check_arguments_at_least, CheckErrors, EarlyReturnError, Error,
24+
InterpreterResult as Result, SyntaxBindingError, SyntaxBindingErrorType,
2525
};
2626
pub use crate::vm::functions::assets::stx_transfer_consolidated;
2727
use crate::vm::representations::{ClarityName, SymbolicExpression, SymbolicExpressionType};
@@ -667,7 +667,7 @@ fn special_asserts(
667667
Ok(conditional)
668668
} else {
669669
let thrown = eval(&args[1], env, context)?;
670-
Err(ShortReturnType::AssertionFailed(Box::new(thrown)).into())
670+
Err(EarlyReturnError::AssertionFailed(Box::new(thrown)).into())
671671
}
672672
}
673673
_ => Err(CheckErrors::TypeValueError(

clarity/src/vm/functions/options.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ use crate::vm::contexts::{Environment, LocalContext};
1818
use crate::vm::costs::cost_functions::ClarityCostFunction;
1919
use crate::vm::costs::{runtime_cost, CostTracker, MemoryConsumer};
2020
use crate::vm::errors::{
21-
check_arguments_at_least, CheckErrors, InterpreterError, InterpreterResult as Result,
22-
RuntimeErrorType, ShortReturnType,
21+
check_arguments_at_least, CheckErrors, EarlyReturnError, InterpreterError,
22+
InterpreterResult as Result, RuntimeErrorType,
2323
};
2424
use crate::vm::types::{CallableData, OptionalData, ResponseData, TypeSignature, Value};
2525
use crate::vm::Value::CallableContract;
@@ -66,7 +66,7 @@ pub fn native_unwrap(input: Value) -> Result<Value> {
6666
pub fn native_unwrap_or_ret(input: Value, thrown: Value) -> Result<Value> {
6767
inner_unwrap(input).and_then(|opt_value| match opt_value {
6868
Some(v) => Ok(v),
69-
None => Err(ShortReturnType::ExpectedValue(Box::new(thrown)).into()),
69+
None => Err(EarlyReturnError::UnwrapFailed(Box::new(thrown)).into()),
7070
})
7171
}
7272

@@ -80,15 +80,15 @@ pub fn native_unwrap_err(input: Value) -> Result<Value> {
8080
pub fn native_unwrap_err_or_ret(input: Value, thrown: Value) -> Result<Value> {
8181
inner_unwrap_err(input).and_then(|opt_value| match opt_value {
8282
Some(v) => Ok(v),
83-
None => Err(ShortReturnType::ExpectedValue(Box::new(thrown)).into()),
83+
None => Err(EarlyReturnError::UnwrapFailed(Box::new(thrown)).into()),
8484
})
8585
}
8686

8787
pub fn native_try_ret(input: Value) -> Result<Value> {
8888
match input {
8989
Value::Optional(data) => match data.data {
9090
Some(data) => Ok(*data),
91-
None => Err(ShortReturnType::ExpectedValue(Box::new(Value::none())).into()),
91+
None => Err(EarlyReturnError::UnwrapFailed(Box::new(Value::none())).into()),
9292
},
9393
Value::Response(data) => {
9494
if data.committed {
@@ -99,7 +99,7 @@ pub fn native_try_ret(input: Value) -> Result<Value> {
9999
"BUG: Failed to construct new response type from old response type".into(),
100100
)
101101
})?;
102-
Err(ShortReturnType::ExpectedValue(Box::new(short_return_val)).into())
102+
Err(EarlyReturnError::UnwrapFailed(Box::new(short_return_val)).into())
103103
}
104104
}
105105
_ => Err(CheckErrors::ExpectedOptionalOrResponseValue(Box::new(input)).into()),

clarity/src/vm/tests/datamaps.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
use crate::vm::types::{TupleData, Value};
1717
#[cfg(test)]
1818
use crate::vm::{
19-
errors::{CheckErrors, ShortReturnType, SyntaxBindingError},
19+
errors::{CheckErrors, EarlyReturnError, SyntaxBindingError},
2020
types::{ListData, SequenceData, TupleTypeSignature, TypeSignature},
2121
};
2222
use crate::vm::{execute, ClarityName, Error};
@@ -296,7 +296,7 @@ fn test_set_response_variable() {
296296
"#;
297297
let contract_src = contract_src.to_string();
298298
assert_eq!(
299-
Err(ShortReturnType::ExpectedValue(Box::new(Value::Int(5))).into()),
299+
Err(EarlyReturnError::UnwrapFailed(Box::new(Value::Int(5))).into()),
300300
execute(&contract_src)
301301
);
302302
}

clarity/src/vm/tests/simple_apply_eval.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ use crate::vm::callables::DefinedFunction;
3131
use crate::vm::contexts::OwnedEnvironment;
3232
use crate::vm::costs::LimitedCostTracker;
3333
use crate::vm::database::MemoryBackingStore;
34-
use crate::vm::errors::{CheckErrors, Error, RuntimeErrorType, ShortReturnType};
34+
use crate::vm::errors::{CheckErrors, EarlyReturnError, Error, RuntimeErrorType};
3535
use crate::vm::tests::{execute, test_clarity_versions};
3636
use crate::vm::types::signatures::*;
3737
use crate::vm::types::{
@@ -1431,7 +1431,7 @@ fn test_option_destructs() {
14311431
.into(),
14321432
),
14331433
Ok(Value::Int(3)),
1434-
Err(ShortReturnType::ExpectedValue(Box::new(Value::Int(2))).into()),
1434+
Err(EarlyReturnError::UnwrapFailed(Box::new(Value::Int(2))).into()),
14351435
Ok(Value::Int(3)),
14361436
Ok(Value::Int(3)),
14371437
Ok(Value::Int(3)),
@@ -1444,9 +1444,9 @@ fn test_option_destructs() {
14441444
Ok(Value::Int(8)),
14451445
Err(CheckErrors::BadMatchInput(Box::new(TypeSignature::IntType)).into()),
14461446
Err(CheckErrors::BadMatchInput(Box::new(TypeSignature::IntType)).into()),
1447-
Err(ShortReturnType::ExpectedValue(Box::new(Value::error(Value::UInt(1)).unwrap())).into()),
1447+
Err(EarlyReturnError::UnwrapFailed(Box::new(Value::error(Value::UInt(1)).unwrap())).into()),
14481448
Ok(Value::Int(3)),
1449-
Err(ShortReturnType::ExpectedValue(Box::new(Value::none())).into()),
1449+
Err(EarlyReturnError::UnwrapFailed(Box::new(Value::none())).into()),
14501450
Ok(Value::Bool(true)),
14511451
Err(CheckErrors::IncorrectArgumentCount(1, 2).into()),
14521452
Err(CheckErrors::ExpectedOptionalOrResponseValue(Box::new(Value::Int(1))).into()),
@@ -1675,10 +1675,10 @@ fn test_asserts_short_circuit() {
16751675
];
16761676

16771677
let expectations: &[Error] = &[
1678-
Error::ShortReturn(ShortReturnType::AssertionFailed(Box::new(
1678+
Error::EarlyReturn(EarlyReturnError::AssertionFailed(Box::new(
16791679
Value::error(Value::Int(0)).unwrap(),
16801680
))),
1681-
Error::ShortReturn(ShortReturnType::AssertionFailed(Box::new(
1681+
Error::EarlyReturn(EarlyReturnError::AssertionFailed(Box::new(
16821682
Value::error(Value::Int(1)).unwrap(),
16831683
))),
16841684
];

stackslib/src/chainstate/stacks/db/transactions.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -382,7 +382,7 @@ pub fn handle_clarity_runtime_error(error: clarity_error) -> ClarityRuntimeTxErr
382382
err_type: "runtime error",
383383
}
384384
}
385-
clarity_error::Interpreter(InterpreterError::ShortReturn(_)) => {
385+
clarity_error::Interpreter(InterpreterError::EarlyReturn(_)) => {
386386
ClarityRuntimeTxError::Acceptable {
387387
error,
388388
err_type: "short return/panic",

0 commit comments

Comments
 (0)