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 11, 2024
1 parent 8de6884 commit 6fcebe8
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 8 deletions.
9 changes: 1 addition & 8 deletions src/modules/function/ret.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,16 +37,9 @@ 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 {
if ret_type != expr_type && !expr_type.is_subset(ret_type) {
return error!(meta, tok => {
message: "Return type does not match function return type",
comment: format!("Given type: {}, expected type: {}", expr_type, ret_type)
Expand Down
24 changes: 24 additions & 0 deletions src/modules/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ impl Type {
(Type::Array(l), Type::Array(r)) => {
**l != Type::Generic && **r == Type::Generic
}
(l, Type::Failable(r)) => !matches!(l, Type::Failable(_)) && *l == **r,
_ => false
}
}
Expand Down Expand Up @@ -156,4 +157,27 @@ mod tests {

assert!(!a.is_subset(&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(&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(&a));
}

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

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

0 comments on commit 6fcebe8

Please sign in to comment.