Skip to content

Commit

Permalink
feat: Consider functions to be failable when marked as such
Browse files Browse the repository at this point in the history
  • Loading branch information
mks-h committed Jul 13, 2024
1 parent 13f278c commit 244345c
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 12 deletions.
16 changes: 7 additions & 9 deletions src/modules/function/declaration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,28 +147,26 @@ impl SyntaxModule<ParserMetadata> for FunctionDeclaration {
}

let mut type_tok = None;
// Semantic meaning: ValueWasSet<IsThere<aValue>>
let mut failable_mark: Option<Option<Option<Token>>> = None;
let mut marked_as_failable: Option<bool> = None;
// Optionally parse the return type
match token(meta, ":") {
Ok(_) => {
type_tok = Some(meta.get_current_token());
self.returns = parse_type(meta)?;
let mark = meta.get_current_token();
failable_mark = Some(token(meta, "?").ok().map(|_| mark));
marked_as_failable = Some(token(meta, "?").is_ok());
},
Err(_) => self.returns = Type::Generic
}


// Parse the body
token(meta, "{")?;
let (index_begin, index_end, is_failable) = skip_function_body(meta);
if let (Some(failable_mark), Some(type_tok)) = (failable_mark, type_tok) {
if is_failable && failable_mark.is_none() {
let (index_begin, index_end, mut is_failable) = skip_function_body(meta);
if let (Some(marked_as_failable), Some(type_tok)) = (marked_as_failable, type_tok) {
if is_failable && !marked_as_failable {
return error!(meta, type_tok, "Failable functions must be declared as such");
} else if !is_failable && failable_mark.is_some() {
return error!(meta, failable_mark.unwrap(), "Non-failable functions cannot be marked as failable");
} else if !is_failable && marked_as_failable {
is_failable = true;
}
}
// Create a new context with the function body
Expand Down
4 changes: 1 addition & 3 deletions src/tests/validity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -548,8 +548,7 @@ fn failable_function_with_typed_return_without_mark() {
}

#[test]
#[should_panic(expected = "ERROR: Non-failable functions cannot be marked as failable")]
fn non_failable_function_cannot_be_marked() {
fn non_failable_function_marked_as_failable() {
let code = r#"
pub fun test(): Null ? {
echo "Hello, World!"
Expand Down Expand Up @@ -1260,4 +1259,3 @@ fn optional_argument_generic(){
";
test_amber!(code, "hello")
}

0 comments on commit 244345c

Please sign in to comment.