Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Failable types -> failable functions #642

Open
wants to merge 10 commits into
base: main
Choose a base branch
from
8 changes: 0 additions & 8 deletions src/modules/expression/typeop/cast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,14 +56,6 @@ impl SyntaxModule<ParserMetadata> for Cast {
let message = Message::new_warn_at_position(meta, pos)
.message(format!("Casting a value of type '{l_type}' value to a '{r_type}' is not recommended"))
.comment(format!("To suppress this warning, use '{flag_name}' compiler flag"));
let (l_type, r_type) = match (l_type, r_type) {
(Type::Failable(l), Type::Failable(r)) => (*l, *r),
(Type::Failable(_), _) | (_, Type::Failable(_)) => {
meta.add_message(message);
return Ok(());
},
types => types
};
match (l_type, r_type) {
(Type::Array(left), Type::Array(right)) => {
if *left != *right && !matches!(*left, Type::Bool | Type::Num) && !matches!(*right, Type::Bool | Type::Num) {
Expand Down
17 changes: 8 additions & 9 deletions src/modules/function/declaration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -177,9 +177,6 @@ impl SyntaxModule<ParserMetadata> for FunctionDeclaration {
self.arg_types.push(Type::Generic);
}
}
if let Type::Failable(_) = arg_type {
return error!(meta, name_token, "Failable types cannot be used as arguments");
}
match token(meta, "=") {
Ok(_) => {
if is_ref {
Expand All @@ -205,21 +202,23 @@ impl SyntaxModule<ParserMetadata> for FunctionDeclaration {
};
}
let mut returns_tok = None;
let mut declared_failable = false;
// Optionally parse the return type
match token(meta, ":") {
Ok(_) => {
returns_tok = meta.get_current_token();
self.returns = parse_type(meta)?
self.returns = parse_type(meta)?;
if token(meta, "?").is_ok() {
declared_failable = true;
}
},
Err(_) => self.returns = Type::Generic
}
// Parse the body
token(meta, "{")?;
let (index_begin, index_end, is_failable) = skip_function_body(meta);
if is_failable && !matches!(self.returns, Type::Failable(_) | Type::Generic) {
return error!(meta, returns_tok, "Failable functions must return a Failable type");
} else if !is_failable && matches!(self.returns, Type::Failable(_)) {
return error!(meta, returns_tok, "Non-failable functions cannot return a Failable type");
let (index_begin, index_end, is_failable) = skip_function_body(meta, declared_failable, &returns_tok)?;
if !is_failable && declared_failable {
return error!(meta, returns_tok, "Infallible functions must not have a '?' after the type name");
b1ek marked this conversation as resolved.
Show resolved Hide resolved
}
// Create a new context with the function body
let expr = meta.context.expr[index_begin..index_end].to_vec();
Expand Down
15 changes: 12 additions & 3 deletions src/modules/function/declaration_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,32 @@ use crate::utils::cc_flags::{CCFlags, get_ccflag_name};
use crate::utils::context::Context;
use crate::utils::function_interface::FunctionInterface;

pub fn skip_function_body(meta: &mut ParserMetadata) -> (usize, usize, bool) {
pub fn skip_function_body(
meta: &mut ParserMetadata,
declared_failable: bool,
returns_tok: &Option<Token>
b1ek marked this conversation as resolved.
Show resolved Hide resolved
) -> Result<(usize, usize, bool), Failure> {
let index_begin = meta.get_index();
let mut is_failable = false;
let mut scope = 1;
while let Some(tok) = meta.get_current_token() {
match tok.word.as_str() {
"{" => scope += 1,
"}" => scope -= 1,
"fail" => is_failable = true,
"fail" => {
if !declared_failable && returns_tok.is_some() {
return error!(meta, Some(tok), "Functions that can fail must have a '?' after the type name");
b1ek marked this conversation as resolved.
Show resolved Hide resolved
}
is_failable = true
},
"?" => is_failable = true,
_ => {}
}
if scope == 0 { break }
meta.increment_index();
}
let index_end = meta.get_index();
(index_begin, index_end, is_failable)
Ok((index_begin, index_end, is_failable))
}

pub fn is_functions_comment_doc(meta: &mut ParserMetadata) -> bool {
Expand Down
4 changes: 1 addition & 3 deletions src/modules/function/invocation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,9 +101,7 @@ impl SyntaxModule<ParserMetadata> for FunctionInvocation {
self.is_failable = function_unit.is_failable;
if self.is_failable {
match syntax(meta, &mut self.failed) {
Ok(_) => if let Type::Failable(t) = &self.kind {
self.kind = *t.clone();
},
Ok(_) => (),
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 Down
38 changes: 1 addition & 37 deletions src/modules/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ pub enum Type {
Bool,
Num,
Array(Box<Type>),
Failable(Box<Type>),
Generic
}

Expand All @@ -20,9 +19,6 @@ impl Type {
(_, Type::Generic) => true,
(Type::Array(current), Type::Array(other)) => {
**current != Type::Generic && **other == Type::Generic
}
(current, Type::Failable(other)) if !matches!(current, Type::Failable(_)) => {
current.is_allowed_in(other)
},
_ => false
}
Expand All @@ -33,11 +29,7 @@ impl Type {
}

pub fn is_array(&self) -> bool {
match self {
Type::Array(_) => true,
Type::Failable(inner) => inner.is_array(),
_ => false,
}
matches!(self, Type::Array(_))
}
}

Expand All @@ -53,7 +45,6 @@ impl Display for Type {
} else {
write!(f, "[{}]", t)
},
Type::Failable(t) => write!(f, "{}?", t),
Type::Generic => write!(f, "Generic")
}
}
Expand Down Expand Up @@ -136,10 +127,6 @@ pub fn try_parse_type(meta: &mut ParserMetadata) -> Result<Type, Failure> {
}
};

if token(meta, "?").is_ok() {
return res.map(|t| Type::Failable(Box::new(t)))
}

res
}

Expand Down Expand Up @@ -176,27 +163,4 @@ 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));
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Output
// Failable functions must return a Failable type
// Functions that can fail must have a '?' after the type name
b1ek marked this conversation as resolved.
Show resolved Hide resolved

pub fun test(): Null {
fail 1
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Output
// Non-failable functions cannot return a Failable type
// Infallible functions must not have a '?' after the type name
b1ek marked this conversation as resolved.
Show resolved Hide resolved

pub fun test(): Null? {
echo "Hello, World!"
Expand Down
7 changes: 0 additions & 7 deletions src/tests/erroring/function_with_failable_typed_arg.ab

This file was deleted.

Loading