Skip to content

Commit

Permalink
fix: Unwrap Failable type by handling failure
Browse files Browse the repository at this point in the history
  • Loading branch information
mks-h committed Jul 28, 2024
1 parent b126cdc commit 8487478
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 5 deletions.
16 changes: 11 additions & 5 deletions src/modules/function/invocation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,10 +94,19 @@ impl SyntaxModule<ParserMetadata> for FunctionInvocation {
}
}

let types = self.args.iter().map(|e| e.get_type()).collect::<Vec<Type>>();
let var_names = self.args.iter().map(|e| e.is_var()).collect::<Vec<bool>>();
self.refs.clone_from(&function_unit.arg_refs);
(self.kind, self.variant_id) = handle_function_parameters(meta, self.id, function_unit.clone(), &types, &var_names, tok.clone())?;

self.is_failable = function_unit.is_failable;
if self.is_failable {
match syntax(meta, &mut self.failed) {
Ok(_) => {},
Ok(_) => {
if let Type::Failable(t) = &self.kind {
self.kind = *t.clone();
}
},
Err(Failure::Quiet(_)) => return error!(meta, tok => {
message: "This function can fail. Please handle the failure",
comment: "You can use '?' in the end to propagate the failure"
Expand All @@ -113,10 +122,7 @@ impl SyntaxModule<ParserMetadata> for FunctionInvocation {
meta.add_message(message);
}
}
let types = self.args.iter().map(|e| e.get_type()).collect::<Vec<Type>>();
let var_names = self.args.iter().map(|e| e.is_var()).collect::<Vec<bool>>();
self.refs.clone_from(&function_unit.arg_refs);
(self.kind, self.variant_id) = handle_function_parameters(meta, self.id, function_unit, &types, &var_names, tok)?;

Ok(())
})
}
Expand Down
11 changes: 11 additions & 0 deletions src/tests/validity/failed_block_unwraps_failable_type.ab
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
fun failable(): Num? {
if 0 > 5 {
fail 1
}

return 1
}

let a = failable() failed: echo "Failed"

if a is Num: echo "Succeded"
11 changes: 11 additions & 0 deletions src/tests/validity/failed_unwraps_failable_type.ab
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
fun failable(): Num? {
if 0 > 5 {
fail 1
}

return 1
}

let a = failable() failed: echo "Failed"

if a is Num: echo "Succeded"
9 changes: 9 additions & 0 deletions src/tests/validity/unsafe_unwraps_failable_type.ab
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
fun test(): Num? {
if 0 < 5 {
fail 1
}

return 42
}

if unsafe test() is Num: echo "Succeded"

0 comments on commit 8487478

Please sign in to comment.