Skip to content

Commit

Permalink
Simplify handling of Failable types
Browse files Browse the repository at this point in the history
  • Loading branch information
mks-h committed Sep 18, 2024
1 parent 7f7d8cc commit 37ce904
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 7 deletions.
7 changes: 0 additions & 7 deletions src/modules/function/ret.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,6 @@ impl SyntaxModule<ParserMetadata> for Return {
syntax(meta, &mut self.expr)?;
let ret_type = meta.context.fun_ret_type.as_ref();
let expr_type = &self.expr.get_type();
// Unpacking Failable types
let (ret_type, expr_type) = match (ret_type, expr_type) {
types @ (Some(Type::Failable(_)), Type::Failable(_)) => types,
(Some(Type::Failable(ret_type)), expr_type) => (Some(ret_type.as_ref()), expr_type),
(Some(ret_type), Type::Failable(expr_type)) => (Some(ret_type), expr_type.as_ref()),
types => types
};
match ret_type {
Some(ret_type) => {
if ret_type != expr_type && !expr_type.is_subset_of(ret_type) {
Expand Down
26 changes: 26 additions & 0 deletions src/modules/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ impl Type {
(Type::Array(left), Type::Array(right)) => {
**left != Type::Generic && **right == Type::Generic
}
(left, Type::Failable(right)) if !matches!(left, Type::Failable(_)) => {
*left == **right || left.is_subset_of(right)
},
_ => false
}
}
Expand Down Expand Up @@ -160,4 +163,27 @@ mod tests {

assert!(!a.is_subset_of(&a));
}

#[test]
fn non_failable_is_a_subset_of_failable() {
let a = Type::Text;
let b = Type::Failable(Box::new(Type::Text));

assert!(a.is_subset_of(&b));
}

#[test]
fn failable_is_not_a_subset_of_non_failable() {
let a = Type::Text;
let b = Type::Failable(Box::new(Type::Text));

assert!(!b.is_subset_of(&a));
}

#[test]
fn failable_is_not_a_subset_of_itself() {
let a = Type::Failable(Box::new(Type::Text));

assert!(!a.is_subset_of(&a));
}
}
9 changes: 9 additions & 0 deletions src/tests/validity/generic_array_in_failable_return.ab
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// Output
// 1 2 3

fun test(arr: [Num]): []? {
if false { fail 1 }
return arr
}

echo unsafe test([1, 2, 3])

0 comments on commit 37ce904

Please sign in to comment.