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

Fix optional argument type matching #584

Merged
merged 4 commits into from
Nov 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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()