Skip to content

Commit

Permalink
fix(checker): raise error if array type cannot be inferred
Browse files Browse the repository at this point in the history
  • Loading branch information
serkonda7 committed Jan 7, 2024
1 parent 9751e5f commit 3978c66
Show file tree
Hide file tree
Showing 4 changed files with 7 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ _unreleased_

### Type Checks
- Ensure function parameter type exists
- Raise error if array type cannot be inferred
- Properly check return type of `array.last()`

### C Backend
Expand Down
4 changes: 4 additions & 0 deletions lib/bait/checker/expr.bt
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,10 @@ fun (mut c Checker) array_init(mut node ast.ArrayInit) ast.Type {
if node.exprs.length == 0 {
// Typeless array init
if node.elem_type == ast.PLACEHOLDER_TYPE {
if c.expected_type == ast.VOID_TYPE {
c.error('cannot infer type of empty array', node.pos)
}

node.typ = c.expected_type
return node.typ
}
Expand Down
1 change: 1 addition & 0 deletions tests/out/error/array/cannot_infer.in.bt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
const FOO := []
1 change: 1 addition & 0 deletions tests/out/error/array/cannot_infer.out
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
tests/out/error/array/cannot_infer.in.bt:1:13 error: cannot infer type of empty array

0 comments on commit 3978c66

Please sign in to comment.