Skip to content

Commit

Permalink
Add some negative tests for labelled args
Browse files Browse the repository at this point in the history
  • Loading branch information
mustafaquraish committed Nov 7, 2024
1 parent 2564e63 commit 5936746
Show file tree
Hide file tree
Showing 9 changed files with 1,363 additions and 1,303 deletions.
2,604 changes: 1,302 additions & 1,302 deletions bootstrap/stage0.c

Large diffs are not rendered by default.

6 changes: 5 additions & 1 deletion compiler/passes/typechecker.oc
Original file line number Diff line number Diff line change
Expand Up @@ -622,7 +622,7 @@ def TypeChecker::check_call_args_labelled(&this, node: &AST, params: &Vector<&Va
let expected_type: &Type = null
if {
not param_item? => .error(Error::new(
arg.label_span, f"Unexpected labelled argument `{arg.label}`"
arg.label_span, f"Unknown labelled argument `{arg.label}`"
))
kwarg_item? => .error(Error::new_hint(
arg.label_span, f"Duplicate argument for parameter `{arg.label}`",
Expand Down Expand Up @@ -655,6 +655,10 @@ def TypeChecker::check_call_args_labelled(&this, node: &AST, params: &Vector<&Va
new_args.push(new_arg)

} else {
// Currently... this can never be reached, since we only go to this function AFTER
// we have seen a default arg. In the future, we might want to allow all arguments to
// be unordered as long as the positional ones are provided somewhere, and in that case
// this check would be necessary.
.error(Error::new_hint(
node.u.call.close_paren_span, f"Missing required argument `{param.sym.name}` of type {param.type.str()}",
param.sym.span, "Parameter defined here"
Expand Down
8 changes: 8 additions & 0 deletions tests/bad/func/duplicate_labelled.oc
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/// fail: Duplicate argument for parameter `b`


def foo(a: u32, b: u32 = 4, c: u32 = 5): u32 => a + b + c

def main() {
foo(1, 2, b: 7)
}
8 changes: 8 additions & 0 deletions tests/bad/func/labelled_instead_of_positional.oc
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/// fail: Expected positional argument `a`, but got label `c`


def foo(a: u32, b: u32, c: u32 = 5): u32 => a + b + c

def main() {
foo(c: 7)
}
9 changes: 9 additions & 0 deletions tests/bad/func/missing_required_arg.oc
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/// fail: Missing required argument `b` of type i32

def foo(a: i32, b: i32): i32 {
return a + b
}

def main() {
foo(1)
}
8 changes: 8 additions & 0 deletions tests/bad/func/positional_after_labelled.oc
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/// fail: Can't have positional arguments after labelled arguments


def foo(a: u32, b: u32 = 4, c: u32 = 5): u32 => a + b + c

def main() {
foo(1, c: 8, 7)
}
8 changes: 8 additions & 0 deletions tests/bad/func/unexpected_labelled.oc
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/// fail: Unknown labelled argument `foo`


def foo(a: u32, b: u32 = 4, c: u32 = 5): u32 => a + b + c

def main() {
foo(1, c: 8, foo: 7)
}
3 changes: 3 additions & 0 deletions tests/bad/func/variadic_default.oc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
/// fail: Cannot have variadic parameters and default parameters

def foo(a: i32, b: i32 = 5, ...): i32 => 0
12 changes: 12 additions & 0 deletions tests/bad/value_enums/constructor_unnamed_label.oc
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/// fail: Cannot use a labelled argument for a non-labeled parameter

enum Foo {
Case0(u32)
Case1(str, i32)
Case2(str)
Case3
}

def main() {
let foo: Foo = Case0(foo: 5)
}

0 comments on commit 5936746

Please sign in to comment.