Skip to content

Commit c5791f4

Browse files
committed
Replace various errors with unreachable!()
1 parent 6819d89 commit c5791f4

File tree

3 files changed

+26
-134
lines changed

3 files changed

+26
-134
lines changed

src/lang/error.rs

Lines changed: 1 addition & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,12 @@
11
use thiserror::Error;
22

3-
use super::Rule;
43
use crate::{frontend, lang::parser::ParseError, middleware};
54

65
#[derive(Error, Debug)]
76
pub enum LangError {
87
#[error("Parsing failed: {0}")]
98
Parse(Box<ParseError>),
109

11-
#[error("Processing failed: {0}")]
12-
Processing(String),
13-
1410
#[error("AST processing error: {0}")]
1511
Processor(Box<ProcessorError>),
1612

@@ -24,11 +20,6 @@ pub enum LangError {
2420
/// Errors that can occur during the processing of Podlog Pest tree into middleware structures.
2521
#[derive(thiserror::Error, Debug)]
2622
pub enum ProcessorError {
27-
#[error("Semantic error: {message} at {span:?}")]
28-
Semantic {
29-
message: String,
30-
span: Option<(usize, usize)>,
31-
},
3223
#[error("Undefined identifier: '{name}' at {span:?}")]
3324
UndefinedIdentifier {
3425
name: String,
@@ -74,47 +65,16 @@ pub enum ProcessorError {
7465
name: String,
7566
span: Option<(usize, usize)>,
7667
},
77-
#[error("Pest rule mismatch: expected {expected_rule:?}, found {found_rule:?} for '{context}' at {span:?}")]
78-
RuleMismatch {
79-
expected_rule: Rule,
80-
found_rule: Rule,
81-
context: String,
82-
span: Option<(usize, usize)>,
83-
},
84-
#[error("Missing element: expected {element_type} for '{context}' at {span:?}")]
85-
MissingElement {
86-
element_type: String,
87-
context: String,
88-
span: Option<(usize, usize)>,
89-
},
9068
#[error("Invalid literal format for {kind}: '{value}' at {span:?}")]
9169
InvalidLiteralFormat {
9270
kind: String,
9371
value: String,
9472
span: Option<(usize, usize)>,
9573
},
96-
#[error(
97-
"Hex literal '0x{value}' has invalid length {len} (must be even and >= 2) at {span:?}"
98-
)]
99-
InvalidHexLength {
100-
value: String,
101-
len: usize,
102-
span: Option<(usize, usize)>,
103-
},
10474
#[error("Frontend error: {0}")]
105-
Frontend(frontend::Error),
106-
}
107-
108-
impl From<frontend::Error> for ProcessorError {
109-
fn from(err: frontend::Error) -> Self {
110-
ProcessorError::Frontend(err)
111-
}
75+
Frontend(#[from] frontend::Error),
11276
}
11377

114-
// We need to manually implement From for the boxed types because
115-
// the `?` operator needs to automatically convert ParseError to Box<ParseError> etc.
116-
// `thiserror`'s `#[from]` handles the Box<Error> -> LangError part.
117-
11878
impl From<ParseError> for LangError {
11979
fn from(err: ParseError) -> Self {
12080
LangError::Parse(Box::new(err))
@@ -132,9 +92,3 @@ impl From<middleware::Error> for LangError {
13292
LangError::Middleware(Box::new(err))
13393
}
13494
}
135-
136-
impl From<frontend::Error> for LangError {
137-
fn from(err: frontend::Error) -> Self {
138-
LangError::Frontend(Box::new(err))
139-
}
140-
}

src/lang/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -485,7 +485,7 @@ mod tests {
485485
fn test_e2e_ethdos_predicates() -> Result<(), LangError> {
486486
let params = Params {
487487
max_input_signed_pods: 3,
488-
max_input_main_pods: 3,
488+
max_input_recursive_pods: 3,
489489
max_statements: 31,
490490
max_signed_pod_values: 8,
491491
max_public_statements: 10,

src/lang/processor.rs

Lines changed: 24 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -154,12 +154,7 @@ fn first_pass<'a>(
154154
Rule::EOI => break,
155155
Rule::COMMENT | Rule::WHITESPACE => {}
156156
_ => {
157-
return Err(ProcessorError::RuleMismatch {
158-
expected_rule: Rule::custom_predicate_def,
159-
found_rule: pair.as_rule(),
160-
context: "top-level document content (expected custom_predicate_def, request_def, or EOI)".to_string(),
161-
span: Some(get_span(&pair)),
162-
});
157+
unreachable!("Unexpected rule: {:?}", pair.as_rule());
163158
}
164159
}
165160
}
@@ -206,13 +201,10 @@ fn second_pass(ctx: &mut ProcessingContext) -> Result<ProcessedOutput, Processor
206201
})
207202
}
208203

209-
fn pest_pair_to_builder_arg(
210-
arg_content_pair: &Pair<Rule>,
211-
context_stmt_name: &str,
212-
) -> Result<BuilderArg, ProcessorError> {
204+
fn pest_pair_to_builder_arg(arg_content_pair: &Pair<Rule>) -> Result<BuilderArg, ProcessorError> {
213205
match arg_content_pair.as_rule() {
214206
Rule::literal_value => {
215-
let value = process_literal_value(arg_content_pair, context_stmt_name)?;
207+
let value = process_literal_value(arg_content_pair)?;
216208
Ok(BuilderArg::Literal(value))
217209
}
218210
Rule::wildcard => {
@@ -230,15 +222,7 @@ fn pest_pair_to_builder_arg(
230222
}
231223
Rule::self_keyword => SelfOrWildcardStr::SELF,
232224
_ => {
233-
return Err(ProcessorError::RuleMismatch {
234-
expected_rule: Rule::wildcard,
235-
found_rule: pod_id_pair.as_rule(),
236-
context: format!(
237-
"pod identifier part of anchored key in {} for BuilderArg",
238-
context_stmt_name
239-
),
240-
span: Some(get_span(&pod_id_pair)),
241-
});
225+
unreachable!("Unexpected rule: {:?}", pod_id_pair.as_rule());
242226
}
243227
};
244228

@@ -254,26 +238,12 @@ fn pest_pair_to_builder_arg(
254238
KeyOrWildcardStr::Key(key_str_literal)
255239
}
256240
_ => {
257-
return Err(ProcessorError::RuleMismatch {
258-
expected_rule: Rule::wildcard,
259-
found_rule: key_part_pair.as_rule(),
260-
context: format!(
261-
"key part of anchored key {} in {} for BuilderArg",
262-
pod_id_pair.as_str(),
263-
context_stmt_name
264-
),
265-
span: Some(get_span(&key_part_pair)),
266-
});
241+
unreachable!("Unexpected rule: {:?}", key_part_pair.as_rule());
267242
}
268243
};
269244
Ok(BuilderArg::Key(pod_self_or_wc_str, key_or_wildcard_str))
270245
}
271-
_ => Err(ProcessorError::RuleMismatch {
272-
expected_rule: Rule::statement_arg,
273-
found_rule: arg_content_pair.as_rule(),
274-
context: format!("argument parsing for BuilderArg in {}", context_stmt_name),
275-
span: Some(get_span(arg_content_pair)),
276-
}),
246+
_ => unreachable!("Unexpected rule: {:?}", arg_content_pair.as_rule()),
277247
}
278248
}
279249

@@ -454,12 +424,7 @@ fn process_and_add_custom_predicate_to_batch(
454424
Rule::private_kw | Rule::COMMENT | Rule::WHITESPACE => {}
455425
_ if arg_part_pair.as_str() == "," => {}
456426
_ => {
457-
return Err(ProcessorError::RuleMismatch {
458-
expected_rule: Rule::public_arg_list,
459-
found_rule: arg_part_pair.as_rule(),
460-
context: format!("arguments for predicate {}", name),
461-
span: Some(get_span(&arg_part_pair)),
462-
});
427+
unreachable!("Unexpected rule: {:?}", arg_part_pair.as_rule());
463428
}
464429
}
465430
}
@@ -471,23 +436,18 @@ fn process_and_add_custom_predicate_to_batch(
471436
"AND" => true,
472437
"OR" => false,
473438
_ => {
474-
return Err(ProcessorError::Semantic {
475-
message: format!(
476-
"Invalid conjunction type: {}",
477-
conjunction_type_pair.as_str()
478-
),
479-
span: Some(get_span(&conjunction_type_pair)),
480-
})
439+
unreachable!(
440+
"Invalid conjunction type: {}",
441+
conjunction_type_pair.as_str()
442+
);
481443
}
482444
};
483445

484446
let statement_list_pair = inner_pairs
485447
.find(|p| p.as_rule() == Rule::statement_list)
486-
.ok_or_else(|| ProcessorError::MissingElement {
487-
element_type: "statement list".to_string(),
488-
context: format!("definition of predicate {}", name),
489-
span: Some(get_span(pred_def_pair)),
490-
})?;
448+
.unwrap_or_else(|| {
449+
unreachable!("statement_list rule must be present in predicate definition")
450+
});
491451

492452
let mut statement_builders = Vec::new();
493453
for stmt_pair in statement_list_pair
@@ -497,14 +457,10 @@ fn process_and_add_custom_predicate_to_batch(
497457
let mut inner_stmt_pairs = stmt_pair.clone().into_inner();
498458
let stmt_name_pair = inner_stmt_pairs
499459
.find(|p| p.as_rule() == Rule::identifier)
500-
.ok_or_else(|| ProcessorError::MissingElement {
501-
element_type: "statement name".to_string(),
502-
context: "statement parsing".to_string(),
503-
span: Some(get_span(&stmt_pair)),
504-
})?;
460+
.unwrap_or_else(|| unreachable!("statement name must be present in statement"));
505461
let stmt_name_str = stmt_name_pair.as_str();
506462

507-
let builder_args = parse_statement_args(&stmt_pair, stmt_name_str)?;
463+
let builder_args = parse_statement_args(&stmt_pair)?;
508464

509465
let middleware_predicate_type =
510466
if let Some(native_pred) = native_predicate_from_string(stmt_name_str) {
@@ -583,13 +539,6 @@ fn process_request_def(
583539
request_templates.push(tmpl);
584540
}
585541

586-
if request_templates.len() > processing_ctx.params.max_statements {
587-
return Err(ProcessorError::Middleware(middleware::Error::max_length(
588-
"request statements".to_string(),
589-
request_templates.len(),
590-
processing_ctx.params.max_statements,
591-
)));
592-
}
593542
Ok(request_templates)
594543
}
595544

@@ -606,7 +555,7 @@ fn process_proof_request_statement_template(
606555
.unwrap();
607556
let stmt_name_str = name_pair.as_str();
608557

609-
let builder_args = parse_statement_args(stmt_pair, stmt_name_str)?;
558+
let builder_args = parse_statement_args(stmt_pair)?;
610559
let mut temp_stmt_wildcard_names: Vec<String> = Vec::new();
611560

612561
for arg in &builder_args {
@@ -664,10 +613,7 @@ fn process_proof_request_statement_template(
664613
Ok(stb.desugar())
665614
}
666615

667-
fn process_literal_value(
668-
lit_val_pair: &Pair<Rule>,
669-
context_stmt_name: &str,
670-
) -> Result<Value, ProcessorError> {
616+
fn process_literal_value(lit_val_pair: &Pair<Rule>) -> Result<Value, ProcessorError> {
671617
let inner_lit = lit_val_pair.clone().into_inner().next().unwrap();
672618

673619
match inner_lit.as_rule() {
@@ -711,7 +657,7 @@ fn process_literal_value(
711657
Rule::literal_array => {
712658
let elements: Result<Vec<Value>, ProcessorError> = inner_lit
713659
.into_inner()
714-
.map(|elem_pair| process_literal_value(&elem_pair, context_stmt_name))
660+
.map(|elem_pair| process_literal_value(&elem_pair))
715661
.collect();
716662
let middleware_array = middleware::containers::Array::new(elements?)
717663
.map_err(|e| ProcessorError::Internal(format!("Failed to create Array: {}", e)))?;
@@ -720,7 +666,7 @@ fn process_literal_value(
720666
Rule::literal_set => {
721667
let elements: Result<HashSet<Value>, ProcessorError> = inner_lit
722668
.into_inner()
723-
.map(|elem_pair| process_literal_value(&elem_pair, context_stmt_name))
669+
.map(|elem_pair| process_literal_value(&elem_pair))
724670
.collect();
725671
let middleware_set = middleware::containers::Set::new(elements?)
726672
.map_err(|e| ProcessorError::Internal(format!("Failed to create Set: {}", e)))?;
@@ -734,7 +680,7 @@ fn process_literal_value(
734680
let key_pair = entry_inner.next().unwrap();
735681
let val_pair = entry_inner.next().unwrap();
736682
let key_str = parse_pest_string_literal(&key_pair)?;
737-
let val = process_literal_value(&val_pair, context_stmt_name)?;
683+
let val = process_literal_value(&val_pair)?;
738684
Ok((Key::new(key_str), val))
739685
})
740686
.collect();
@@ -743,12 +689,7 @@ fn process_literal_value(
743689
})?;
744690
Ok(Value::from(middleware_dict))
745691
}
746-
_ => Err(ProcessorError::RuleMismatch {
747-
expected_rule: Rule::literal_value,
748-
found_rule: inner_lit.as_rule(),
749-
context: format!("literal parsing for {}", context_stmt_name),
750-
span: Some(get_span(&inner_lit)),
751-
}),
692+
_ => unreachable!("Unexpected rule: {:?}", inner_lit.as_rule()),
752693
}
753694
}
754695

@@ -905,10 +846,7 @@ fn resolve_request_statement_builder(
905846
})
906847
}
907848

908-
fn parse_statement_args(
909-
stmt_pair: &Pair<Rule>,
910-
context_stmt_name: &str,
911-
) -> Result<Vec<BuilderArg>, ProcessorError> {
849+
fn parse_statement_args(stmt_pair: &Pair<Rule>) -> Result<Vec<BuilderArg>, ProcessorError> {
912850
let mut builder_args = Vec::new();
913851
let mut inner_stmt_pairs = stmt_pair.clone().into_inner();
914852

@@ -919,7 +857,7 @@ fn parse_statement_args(
919857
.filter(|p| p.as_rule() == Rule::statement_arg)
920858
{
921859
let arg_content_pair = arg_pair.into_inner().next().unwrap();
922-
let builder_arg = pest_pair_to_builder_arg(&arg_content_pair, context_stmt_name)?;
860+
let builder_arg = pest_pair_to_builder_arg(&arg_content_pair)?;
923861
builder_args.push(builder_arg);
924862
}
925863
}

0 commit comments

Comments
 (0)