feat(aiken-lang): anonymous functions

@MartinSchere noticed a weird error
where an unknown variable wasn't being reported
the type checker was incorrectly scoping
arguments for anonymous function definitions.
Luckily his compilation failed due to a FreeUnique
error during code gen which is good. But this may
have been the source of other mysterious FreeUnique
errors.

I also noticed that anonymous function allowed
arguments with the same name to be defined.

`fn(arg, arg)`

This now returns an error.
This commit is contained in:
rvcas
2023-04-16 16:38:43 -04:00
parent 20edce2146
commit 98c61ca151
3 changed files with 94 additions and 24 deletions

View File

@@ -154,6 +154,56 @@ fn multi_validator_warning() {
))
}
#[test]
fn anonymous_function_scoping() {
let source_code = r#"
fn reduce(list, f, i) {
todo
}
pub fn foo() {
let sum =
reduce(
[1, 2, 3],
fn(acc: Int, n: Int) { acc + n },
0,
)
sum + acc
}
"#;
assert!(matches!(
check(parse(source_code)),
Err((_, Error::UnknownVariable { name, .. })) if name == "acc"
))
}
#[test]
fn anonymous_function_dupicate_args() {
let source_code = r#"
fn reduce(list, f, i) {
todo
}
pub fn foo() {
let sum =
reduce(
[1, 2, 3],
fn(acc: Int, acc: Int) { acc + acc },
0,
)
sum
}
"#;
assert!(matches!(
check(parse(source_code)),
Err((_, Error::DuplicateArgument { label, .. })) if label == "acc"
))
}
#[test]
fn if_scoping() {
let source_code = r#"