Skip to content

Commit

Permalink
fix(checker): set correct return type of array.last()
Browse files Browse the repository at this point in the history
  • Loading branch information
serkonda7 committed Jan 7, 2024
1 parent 20be021 commit c112096
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 13 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
- Properly check return type of `array.last()`

### C Backend
- builder: Fixes for using the C backend on windows
Expand Down
5 changes: 5 additions & 0 deletions lib/bait/checker/fun.bt
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,11 @@ fun (mut c Checker) method_call(mut node ast.CallExpr) ast.Type {
return left_expr_type
}

// Array methods with return type `any`
if left_sym.kind == .array and ['last'].contains(node.name) {
return (left_sym.info as ast.ArrayInfo).elem_type
}

if node.concrete_types.length > 0 {
def.concrete_types.push(node.concrete_types)

Expand Down
14 changes: 9 additions & 5 deletions tests/out/error/array/magic_methods.in.bt
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
mut a := [1, 2, 3]
a.push(true)
a.index('a')
a.contains(12 as i16)
a.concat(['b', 'c'])
mut a := [1, 2, 3]

a.push(true)
a.index('a') // FIXME should raise error
a.contains(12 as i16)
a.concat(['b', 'c'])

mut x := 'a'
x = a.last()
8 changes: 4 additions & 4 deletions tests/out/error/array/magic_methods.out
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
tests/out/error/array/magic_methods.in.bt:2:7 error: type bool not matches i32 in argument 1
tests/out/error/array/magic_methods.in.bt:3:8 error: type string not matches i32 in argument 1
tests/out/error/array/magic_methods.in.bt:4:11 error: type i16 not matches i32 in argument 1
tests/out/error/array/magic_methods.in.bt:5:9 error: type []string not matches []i32 in argument 1
tests/out/error/array/magic_methods.in.bt:3:6 error: type bool not matches i32 in argument 1
tests/out/error/array/magic_methods.in.bt:5:10 error: type i16 not matches i32 in argument 1
tests/out/error/array/magic_methods.in.bt:6:8 error: type []string not matches []i32 in argument 1
tests/out/error/array/magic_methods.in.bt:9:2 error: cannot assign type i32 to string
5 changes: 1 addition & 4 deletions tests/output_test.bt
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,7 @@
import os
import bait.util.testing

const ERR_SKIP := [
// Known fail due to regression
'tests/out/error/array/magic_methods.in.bt',
]
const ERR_SKIP := []

const DIR := os.dir($FILE)
const ENABLE_FIX := os.ARGS.contains('--fix')
Expand Down

0 comments on commit c112096

Please sign in to comment.