Skip to content

Commit 1a55530

Browse files
committed
chore: address PR feedback
1 parent eae0acd commit 1a55530

File tree

9 files changed

+126
-398
lines changed

9 files changed

+126
-398
lines changed

clarity/src/vm/analysis/errors.rs

Lines changed: 54 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ pub enum SyntaxBindingErrorType {
2929
Let,
3030
Eval,
3131
TupleCons,
32-
TypeDefinition,
3332
}
3433

3534
impl fmt::Display for SyntaxBindingErrorType {
@@ -44,7 +43,6 @@ impl DiagnosableError for SyntaxBindingErrorType {
4443
Self::Let => "Let-binding".to_string(),
4544
Self::Eval => "Function argument definition".to_string(),
4645
Self::TupleCons => "Tuple constructor".to_string(),
47-
Self::TypeDefinition => "Type definition".to_string(),
4846
}
4947
}
5048

@@ -57,16 +55,11 @@ impl DiagnosableError for SyntaxBindingErrorType {
5755
#[derive(Debug, PartialEq)]
5856
pub enum SyntaxBindingError {
5957
/// binding list item is not a list
60-
NotList(SyntaxBindingErrorType, usize, SymbolicExpression),
58+
NotList(SyntaxBindingErrorType, usize),
6159
/// binding list item has an invalid length (e.g. not 2)
62-
InvalidLength(SyntaxBindingErrorType, usize, SymbolicExpression),
60+
InvalidLength(SyntaxBindingErrorType, usize),
6361
/// binding name is not an atom
64-
NotAtom(SyntaxBindingErrorType, usize, SymbolicExpression),
65-
/// second binding item is a type signature, and the type signature itself is bad.
66-
/// NOTE: type signature parsing returns CheckErrors, so we cannot include a CheckErrors here
67-
/// directly without creating a recursive type. Instead, we just report the `Display`
68-
/// representation of the error here as the third item.
69-
BadTypeSignature(usize, SymbolicExpression, String),
62+
NotAtom(SyntaxBindingErrorType, usize),
7063
}
7164

7265
impl fmt::Display for SyntaxBindingError {
@@ -78,30 +71,17 @@ impl fmt::Display for SyntaxBindingError {
7871
impl DiagnosableError for SyntaxBindingError {
7972
fn message(&self) -> String {
8073
match &self {
81-
Self::NotList(err_type, item_index, item) => {
74+
Self::NotList(err_type, item_index) => {
8275
let item_no = item_index + 1;
83-
format!(
84-
"{err_type} item #{item_no} is not a list: {}",
85-
item.as_error_string()
86-
)
76+
format!("{err_type} item #{item_no} is not a list",)
8777
}
88-
Self::InvalidLength(err_type, item_index, item) => {
78+
Self::InvalidLength(err_type, item_index) => {
8979
let item_no = item_index + 1;
90-
format!(
91-
"{err_type} item #{item_no} is not a two-element list: {}",
92-
item.as_error_string()
93-
)
80+
format!("{err_type} item #{item_no} is not a two-element list",)
9481
}
95-
Self::NotAtom(err_type, item_index, item) => {
82+
Self::NotAtom(err_type, item_index) => {
9683
let item_no = item_index + 1;
97-
format!(
98-
"{err_type} item #{item_no}'s name is not an atom: {}",
99-
item.as_error_string()
100-
)
101-
}
102-
Self::BadTypeSignature(item_index, item, error_message) => {
103-
let item_no = item_index + 1;
104-
format!("Type definition item #{item_no} has an invalid type signature: {} (reason: {error_message})", item.as_error_string())
84+
format!("{err_type} item #{item_no}'s name is not an atom",)
10585
}
10686
}
10787
}
@@ -112,49 +92,55 @@ impl DiagnosableError for SyntaxBindingError {
11292
}
11393

11494
impl SyntaxBindingError {
115-
/// Helper constructor for NotList(SyntaxBindingErrorType::Let, item_no, item)
116-
pub fn let_binding_not_list(item_no: usize, item: SymbolicExpression) -> Self {
117-
Self::NotList(SyntaxBindingErrorType::Let, item_no, item)
95+
/// Helper constructor for NotList(SyntaxBindingErrorType::Let, item_no)
96+
pub fn let_binding_not_list(item_no: usize) -> Self {
97+
Self::NotList(SyntaxBindingErrorType::Let, item_no)
98+
}
99+
100+
/// Helper constructor for InvalidLength(SyntaxBindingErrorType::Let, item_no)
101+
pub fn let_binding_invalid_length(item_no: usize) -> Self {
102+
Self::InvalidLength(SyntaxBindingErrorType::Let, item_no)
118103
}
119104

120-
/// Helper constructor for InvalidLength(SyntaxBindingErrorType::Let, item_no, item)
121-
pub fn let_binding_invalid_length(item_no: usize, item: SymbolicExpression) -> Self {
122-
Self::InvalidLength(SyntaxBindingErrorType::Let, item_no, item)
105+
/// Helper constructor for NotAtom(SyntaxBindingErrorType::Let, item_no)
106+
pub fn let_binding_not_atom(item_no: usize) -> Self {
107+
Self::NotAtom(SyntaxBindingErrorType::Let, item_no)
123108
}
124109

125-
/// Helper constructor for NotAtom(SyntaxBindingErrorType::Let, item_no, item)
126-
pub fn let_binding_not_atom(item_no: usize, item: SymbolicExpression) -> Self {
127-
Self::NotAtom(SyntaxBindingErrorType::Let, item_no, item)
110+
/// Helper constructor for NotList(SyntaxBindingErrorType::Eval, item_no)
111+
pub fn eval_binding_not_list(item_no: usize) -> Self {
112+
Self::NotList(SyntaxBindingErrorType::Eval, item_no)
128113
}
129114

130-
/// Helper constructor for NotList(SyntaxBindingErrorType::Eval, item_no, item)
131-
pub fn eval_binding_not_list(item_no: usize, item: SymbolicExpression) -> Self {
132-
Self::NotList(SyntaxBindingErrorType::Eval, item_no, item)
115+
/// Helper constructor for InvalidLength(SyntaxBindingErrorType::Eval, item_no)
116+
pub fn eval_binding_invalid_length(item_no: usize) -> Self {
117+
Self::InvalidLength(SyntaxBindingErrorType::Eval, item_no)
133118
}
134119

135-
/// Helper constructor for InvalidLength(SyntaxBindingErrorType::Eval, item_no, item)
136-
pub fn eval_binding_invalid_length(item_no: usize, item: SymbolicExpression) -> Self {
137-
Self::InvalidLength(SyntaxBindingErrorType::Eval, item_no, item)
120+
/// Helper constructor for NotAtom(SyntaxBindingErrorType::Eval, item_no)
121+
pub fn eval_binding_not_atom(item_no: usize) -> Self {
122+
Self::NotAtom(SyntaxBindingErrorType::Eval, item_no)
138123
}
139124

140-
/// Helper constructor for NotAtom(SyntaxBindingErrorType::Eval, item_no, item)
141-
pub fn eval_binding_not_atom(item_no: usize, item: SymbolicExpression) -> Self {
142-
Self::NotAtom(SyntaxBindingErrorType::Eval, item_no, item)
125+
/// Helper constructor for NotList(SyntaxBindingErrorType::TupleCons, item_no)
126+
pub fn tuple_cons_not_list(item_no: usize) -> Self {
127+
Self::NotList(SyntaxBindingErrorType::TupleCons, item_no)
143128
}
144129

145-
/// Helper constructor for NotList(SyntaxBindingErrorType::TupleCons, item_no, item)
146-
pub fn tuple_cons_not_list(item_no: usize, item: SymbolicExpression) -> Self {
147-
Self::NotList(SyntaxBindingErrorType::TupleCons, item_no, item)
130+
/// Helper constructor for InvalidLength(SyntaxBindingErrorType::TupleCons, item_no)
131+
pub fn tuple_cons_invalid_length(item_no: usize) -> Self {
132+
Self::InvalidLength(SyntaxBindingErrorType::TupleCons, item_no)
148133
}
149134

150-
/// Helper constructor for InvalidLength(SyntaxBindingErrorType::TupleCons, item_no, item)
151-
pub fn tuple_cons_invalid_length(item_no: usize, item: SymbolicExpression) -> Self {
152-
Self::InvalidLength(SyntaxBindingErrorType::TupleCons, item_no, item)
135+
/// Helper constructor for NotAtom(SyntaxBindingErrorType::TupleCons, item_no)
136+
pub fn tuple_cons_not_atom(item_no: usize) -> Self {
137+
Self::NotAtom(SyntaxBindingErrorType::TupleCons, item_no)
153138
}
139+
}
154140

155-
/// Helper constructor for NotAtom(SyntaxBindingErrorType::TupleCons, item_no, item)
156-
pub fn tuple_cons_not_atom(item_no: usize, item: SymbolicExpression) -> Self {
157-
Self::NotAtom(SyntaxBindingErrorType::TupleCons, item_no, item)
141+
impl From<SyntaxBindingError> for CheckErrors {
142+
fn from(e: SyntaxBindingError) -> Self {
143+
Self::BadSyntaxBinding(e)
158144
}
159145
}
160146

@@ -348,11 +334,6 @@ impl CheckErrors {
348334
CheckErrors::SupertypeTooLarge | CheckErrors::Expects(_)
349335
)
350336
}
351-
352-
/// Is the given error message due to a BadSyntaxBinding?
353-
pub fn has_nested_bad_syntax_binding_message(msg: &str) -> bool {
354-
msg.contains("invalid syntax binding: ")
355-
}
356337
}
357338

358339
impl CheckError {
@@ -378,6 +359,18 @@ impl CheckError {
378359
self.diagnostic.spans = exprs.iter().map(|e| e.span().clone()).collect();
379360
self.expressions.replace(exprs.to_vec());
380361
}
362+
363+
pub fn with_expression(err: CheckErrors, expr: &SymbolicExpression) -> Self {
364+
let mut r = Self::new(err);
365+
r.set_expression(expr);
366+
r
367+
}
368+
}
369+
370+
impl From<(SyntaxBindingError, &SymbolicExpression)> for CheckError {
371+
fn from(e: (SyntaxBindingError, &SymbolicExpression)) -> Self {
372+
Self::with_expression(CheckErrors::BadSyntaxBinding(e.0), e.1)
373+
}
381374
}
382375

383376
impl fmt::Display for CheckErrors {

clarity/src/vm/analysis/read_only_checker/mod.rs

Lines changed: 16 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -327,19 +327,16 @@ impl<'a, 'b> ReadOnlyChecker<'a, 'b> {
327327

328328
for (i, pair) in binding_list.iter().enumerate() {
329329
let pair_expression = pair.match_list().ok_or_else(|| {
330-
CheckErrors::BadSyntaxBinding(SyntaxBindingError::let_binding_not_list(
331-
i,
332-
pair.clone(),
333-
))
330+
CheckError::with_expression(
331+
SyntaxBindingError::let_binding_not_list(i).into(),
332+
pair,
333+
)
334334
})?;
335335
if pair_expression.len() != 2 {
336-
return Err(CheckErrors::BadSyntaxBinding(
337-
SyntaxBindingError::let_binding_invalid_length(
338-
i,
339-
SymbolicExpression::list(pair_expression.to_vec()),
340-
),
341-
)
342-
.into());
336+
return Err(CheckError::with_expression(
337+
SyntaxBindingError::let_binding_invalid_length(i).into(),
338+
&SymbolicExpression::list(pair_expression.to_vec()),
339+
));
343340
}
344341

345342
if !self.check_read_only(&pair_expression[1])? {
@@ -378,19 +375,16 @@ impl<'a, 'b> ReadOnlyChecker<'a, 'b> {
378375
TupleCons => {
379376
for (i, pair) in args.iter().enumerate() {
380377
let pair_expression = pair.match_list().ok_or_else(|| {
381-
CheckErrors::BadSyntaxBinding(SyntaxBindingError::tuple_cons_not_list(
382-
i,
383-
pair.clone(),
384-
))
378+
CheckError::with_expression(
379+
SyntaxBindingError::tuple_cons_not_list(i).into(),
380+
pair,
381+
)
385382
})?;
386383
if pair_expression.len() != 2 {
387-
return Err(CheckErrors::BadSyntaxBinding(
388-
SyntaxBindingError::tuple_cons_invalid_length(
389-
i,
390-
SymbolicExpression::list(pair_expression.to_vec()),
391-
),
392-
)
393-
.into());
384+
return Err(CheckError::with_expression(
385+
SyntaxBindingError::tuple_cons_invalid_length(i).into(),
386+
&SymbolicExpression::list(pair_expression.to_vec()),
387+
));
394388
}
395389

396390
if !self.check_read_only(&pair_expression[1])? {

clarity/src/vm/analysis/tests/mod.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,8 @@ fn test_bad_tuple_construction() {
131131
fn test_tuple_expects_pairs() {
132132
let snippet = "(tuple (key 1) (key-with-missing-value))";
133133
let err = mem_type_check(snippet).unwrap_err();
134-
assert!(format!("{}", err.diagnostic).contains("invalid syntax binding: Tuple constructor item #2 is not a two-element list: ( key-with-missing-value )."));
134+
assert!(format!("{}", err.diagnostic)
135+
.contains("invalid syntax binding: Tuple constructor item #2 is not a two-element list."));
135136
}
136137

137138
#[test]

clarity/src/vm/analysis/type_checker/v2_05/tests/mod.rs

Lines changed: 7 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,13 @@ use crate::vm::analysis::mem_type_check;
2121
use crate::vm::analysis::type_checker::v2_05::TypeResult;
2222
use crate::vm::ast::build_ast;
2323
use crate::vm::ast::errors::ParseErrors;
24-
use crate::vm::diagnostic::DiagnosableError;
2524
use crate::vm::types::SequenceSubtype::*;
2625
use crate::vm::types::StringSubtype::*;
2726
use crate::vm::types::TypeSignature::{BoolType, IntType, PrincipalType, UIntType};
2827
use crate::vm::types::{
2928
FixedFunction, FunctionType, QualifiedContractIdentifier, TypeSignature, BUFF_32, BUFF_64,
3029
};
31-
use crate::vm::{ClarityVersion, SymbolicExpression, Value};
30+
use crate::vm::ClarityVersion;
3231
mod assets;
3332
mod contracts;
3433

@@ -647,16 +646,8 @@ fn test_simple_lets() {
647646
];
648647

649648
let bad_expected = [
650-
CheckErrors::BadSyntaxBinding(SyntaxBindingError::let_binding_invalid_length(
651-
0,
652-
SymbolicExpression::list(vec![
653-
SymbolicExpression::literal_value(Value::Int(1)).with_id(5)
654-
]),
655-
)),
656-
CheckErrors::BadSyntaxBinding(SyntaxBindingError::let_binding_not_atom(
657-
0,
658-
SymbolicExpression::literal_value(Value::Int(1)).with_id(5),
659-
)),
649+
CheckErrors::BadSyntaxBinding(SyntaxBindingError::let_binding_invalid_length(0)),
650+
CheckErrors::BadSyntaxBinding(SyntaxBindingError::let_binding_not_atom(0)),
660651
CheckErrors::TypeError(TypeSignature::IntType, TypeSignature::UIntType),
661652
];
662653

@@ -1245,12 +1236,7 @@ fn test_empty_tuple_should_fail() {
12451236
)
12461237
.unwrap_err()
12471238
.err,
1248-
CheckErrors::BadSyntaxBinding(SyntaxBindingError::BadTypeSignature(
1249-
0,
1250-
SymbolicExpression::list(vec![SymbolicExpression::atom("tuple".into()).with_id(8)])
1251-
.with_id(7),
1252-
CheckErrors::EmptyTuplesNotAllowed.message()
1253-
))
1239+
CheckErrors::EmptyTuplesNotAllowed
12541240
);
12551241
}
12561242

@@ -2480,18 +2466,7 @@ fn test_buff_negative_len() {
24802466
StacksEpochId::Epoch2_05,
24812467
)
24822468
.unwrap_err();
2483-
assert_eq!(
2484-
res.err,
2485-
CheckErrors::BadSyntaxBinding(SyntaxBindingError::BadTypeSignature(
2486-
0,
2487-
SymbolicExpression::list(vec![
2488-
SymbolicExpression::atom("buff".into()).with_id(8),
2489-
SymbolicExpression::literal_value(Value::Int(-12)).with_id(9),
2490-
])
2491-
.with_id(7),
2492-
CheckErrors::ValueOutOfBounds.message()
2493-
))
2494-
);
2469+
assert_eq!(res.err, CheckErrors::ValueOutOfBounds);
24952470
}
24962471

24972472
#[test]
@@ -2505,18 +2480,7 @@ fn test_string_ascii_negative_len() {
25052480
StacksEpochId::Epoch2_05,
25062481
)
25072482
.unwrap_err();
2508-
assert_eq!(
2509-
res.err,
2510-
CheckErrors::BadSyntaxBinding(SyntaxBindingError::BadTypeSignature(
2511-
0,
2512-
SymbolicExpression::list(vec![
2513-
SymbolicExpression::atom("string-ascii".into()).with_id(8),
2514-
SymbolicExpression::literal_value(Value::Int(-12)).with_id(9),
2515-
])
2516-
.with_id(7),
2517-
CheckErrors::ValueOutOfBounds.message()
2518-
))
2519-
);
2483+
assert_eq!(res.err, CheckErrors::ValueOutOfBounds);
25202484
}
25212485

25222486
#[test]
@@ -2530,16 +2494,5 @@ fn test_string_utf8_negative_len() {
25302494
StacksEpochId::Epoch2_05,
25312495
)
25322496
.unwrap_err();
2533-
assert_eq!(
2534-
res.err,
2535-
CheckErrors::BadSyntaxBinding(SyntaxBindingError::BadTypeSignature(
2536-
0,
2537-
SymbolicExpression::list(vec![
2538-
SymbolicExpression::atom("string-utf8".into()).with_id(8),
2539-
SymbolicExpression::literal_value(Value::Int(-12)).with_id(9),
2540-
])
2541-
.with_id(7),
2542-
CheckErrors::ValueOutOfBounds.message()
2543-
))
2544-
);
2497+
assert_eq!(res.err, CheckErrors::ValueOutOfBounds);
25452498
}

0 commit comments

Comments
 (0)