Skip to content

Commit

Permalink
Fix optional argument type matching (#584)
Browse files Browse the repository at this point in the history
  • Loading branch information
Ph0enixKM authored Nov 15, 2024
1 parent 825f280 commit 0239da2
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/modules/function/declaration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ impl SyntaxModule<ParserMetadata> for FunctionDeclaration {
optional = true;
let mut expr = Expr::new();
syntax(meta, &mut expr)?;
if arg_type != Type::Generic && arg_type != expr.get_type() {
if !expr.get_type().is_allowed_in(&arg_type) {
return error!(meta, name_token, "Optional argument does not match annotated type");
}
self.arg_optionals.push(expr);
Expand Down
1 change: 1 addition & 0 deletions src/modules/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ pub enum Type {
impl Type {
pub fn is_subset_of(&self, other: &Type) -> bool {
match (self, other) {
(_, Type::Generic) => true,
(Type::Array(current), Type::Array(other)) => {
**current != Type::Generic && **other == Type::Generic
}
Expand Down
10 changes: 10 additions & 0 deletions src/tests/validity/function_optional_argument_generic_array.ab
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// Output
// Hello World
// 1 2 3

fun echo_var(arg: [] = [1, 2, 3]): Null {
echo arg
}

echo_var(["Hello", "World"])
echo_var()

0 comments on commit 0239da2

Please sign in to comment.