Skip to content

Commit a38b374

Browse files
committed
move the error for failable functions to show the part where it possibly fails
1 parent 948b210 commit a38b374

File tree

2 files changed

+11
-10
lines changed

2 files changed

+11
-10
lines changed

src/modules/function/declaration.rs

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -216,13 +216,9 @@ impl SyntaxModule<ParserMetadata> for FunctionDeclaration {
216216
}
217217
// Parse the body
218218
token(meta, "{")?;
219-
let (index_begin, index_end, is_failable) = skip_function_body(meta);
220-
if (!(is_failable && declared_failable)) && (is_failable || declared_failable) {
221-
if is_failable {
222-
return error!(meta, returns_tok, "Functions that can fail must have a '?' after the type name");
223-
} else {
224-
return error!(meta, returns_tok, "Infallible functions must not have a '?' after the type name");
225-
}
219+
let (index_begin, index_end, is_failable) = skip_function_body(meta, declared_failable)?;
220+
if is_failable && (!declared_failable) {
221+
return error!(meta, returns_tok, "Infallible functions must not have a '?' after the type name");
226222
}
227223
// Create a new context with the function body
228224
let expr = meta.context.expr[index_begin..index_end].to_vec();

src/modules/function/declaration_utils.rs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,23 +6,28 @@ use crate::utils::cc_flags::{CCFlags, get_ccflag_name};
66
use crate::utils::context::Context;
77
use crate::utils::function_interface::FunctionInterface;
88

9-
pub fn skip_function_body(meta: &mut ParserMetadata) -> (usize, usize, bool) {
9+
pub fn skip_function_body(meta: &mut ParserMetadata, declared_failable: bool) -> Result<(usize, usize, bool), Failure> {
1010
let index_begin = meta.get_index();
1111
let mut is_failable = false;
1212
let mut scope = 1;
1313
while let Some(tok) = meta.get_current_token() {
1414
match tok.word.as_str() {
1515
"{" => scope += 1,
1616
"}" => scope -= 1,
17-
"fail" => is_failable = true,
17+
"fail" => {
18+
if !declared_failable {
19+
return error!(meta, Some(tok), "Functions that can fail must have a '?' after the type name");
20+
}
21+
is_failable = true
22+
},
1823
"?" => is_failable = true,
1924
_ => {}
2025
}
2126
if scope == 0 { break }
2227
meta.increment_index();
2328
}
2429
let index_end = meta.get_index();
25-
(index_begin, index_end, is_failable)
30+
Ok((index_begin, index_end, is_failable))
2631
}
2732

2833
pub fn is_functions_comment_doc(meta: &mut ParserMetadata) -> bool {

0 commit comments

Comments
 (0)