Skip to content

Commit

Permalink
fix(checker): reduce error noise (#285)
Browse files Browse the repository at this point in the history
  • Loading branch information
serkonda7 authored Jan 9, 2025
1 parent d1a0308 commit ab43ad2
Show file tree
Hide file tree
Showing 9 changed files with 44 additions and 16 deletions.
25 changes: 16 additions & 9 deletions lib/bait/checker/assign.bt
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,9 @@ import bait.context
import bait.token

fun (mut c Checker) decl_assign(mut node ast.AssignStmt) {
typ := c.non_void_expr(node.right) or {
return
}

// Type info must be set for BlankIdent as well due to backend limitations
node.left_type = typ
node.right_type = typ

if node.left is ast.BlankIdent {
// Note: Side types must be set for BlankIdent due to backend limitations
_ = c.decl_right_side(mut node)
return
}

Expand All @@ -32,7 +26,7 @@ fun (mut c Checker) decl_assign(mut node ast.AssignStmt) {
msg := if obj.kind == .package_ {
'cannot shadow import "${left.name}"'
} else {
'redefinition of ${left.name}'
"redefinition of `${left.name}`"
}
c.error(msg, node.pos)
return
Expand All @@ -44,6 +38,8 @@ fun (mut c Checker) decl_assign(mut node ast.AssignStmt) {
return
}

typ := c.decl_right_side(mut node)

// Register identifier
c.scope.register(left.name, context.ScopeObject{
kind = .variable
Expand All @@ -53,6 +49,17 @@ fun (mut c Checker) decl_assign(mut node ast.AssignStmt) {
_ = c.expr(node.left)
}

fun (mut c Checker) decl_right_side(mut node ast.AssignStmt) ast.Type {
typ := c.non_void_expr(node.right) or {
return ast.ERROR_TYPE
}

node.left_type = typ
node.right_type = typ

return typ
}

fun (mut c Checker) assign_stmt(mut node ast.AssignStmt) {
if node.op == .decl_assign {
c.decl_assign(mut node)
Expand Down
4 changes: 4 additions & 0 deletions lib/bait/checker/if_match.bt
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,10 @@ fun (mut c Checker) if_match(mut node ast.IfMatch) ast.Type {
last_type := c.stmts_with_return(branch.stmts)
c.close_scope()

if last_type == ast.ERROR_TYPE {
continue
}

last := branch.stmts.last()
if is_noreturn(last) {
continue
Expand Down
8 changes: 8 additions & 0 deletions lib/bait/checker/index.bt
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ fun (mut c Checker) index_expr(mut node ast.IndexExpr) ast.Type {
}

if sym.kind == .map {
if idx_type == ast.ERROR_TYPE {
return ast.ERROR_TYPE
}

info := sym.info as ast.MapInfo
if idx_type != info.key_type {
c.error("invalid map index `${c.table.type_name(idx_type)}`, expected `${c.table.type_name(info.key_type)}`", node.pos)
Expand Down Expand Up @@ -83,6 +87,10 @@ fun (mut c Checker) range_expr(node ast.RangeExpr) ast.Type {
}

fun (mut c Checker) check_int_index(typ ast.Type, pos token.Pos) {
if typ == ast.ERROR_TYPE {
return
}

if not c.check_types(typ, ast.I32_TYPE) {
c.error("non-integer index `${c.table.type_name(typ)}`", pos)
}
Expand Down
2 changes: 2 additions & 0 deletions tests/out/error/assign/twice_ident.in.bt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
a := 1
a := b
1 change: 1 addition & 0 deletions tests/out/error/assign/twice_ident.out
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
tests/out/error/assign/twice_ident.in.bt:2:1 error: redefinition of `a`
2 changes: 1 addition & 1 deletion tests/out/error/fun/param_redefine.out
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
tests/out/error/fun/param_redefine.in.bt:2:2 error: redefinition of p
tests/out/error/fun/param_redefine.in.bt:2:2 error: redefinition of `p`
tests/out/error/fun/param_redefine.in.bt:5:16 error: redefinition of a
6 changes: 3 additions & 3 deletions tests/out/error/ident/redef.out
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
tests/out/error/ident/redef.in.bt:3:2 error: redefinition of same
tests/out/error/ident/redef.in.bt:6:3 error: redefinition of same
tests/out/error/ident/redef.in.bt:11:2 error: redefinition of my_fun
tests/out/error/ident/redef.in.bt:3:2 error: redefinition of `same`
tests/out/error/ident/redef.in.bt:6:3 error: redefinition of `same`
tests/out/error/ident/redef.in.bt:11:2 error: redefinition of `my_fun`
3 changes: 3 additions & 0 deletions tests/out/error/index/invalid_index.in.bt
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,18 @@ _ = arr[0]
_ = arr[true]
_ = arr[1.0]
_ = arr["hi"]
_ = arr[badvar]

s := "hello"
_ = s[0]
_ = s[arr]
_ = s[`x`]
_ = s[badvar]

mymap := map{
"a": 1
}
_ = mymap["b"]
_ = mymap[0]
_ = mymap[s]
_ = mymap[badvar]
9 changes: 6 additions & 3 deletions tests/out/error/index/invalid_index.out
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
tests/out/error/index/invalid_index.in.bt:3:8 error: non-integer index `bool`
tests/out/error/index/invalid_index.in.bt:4:8 error: non-integer index `f64`
tests/out/error/index/invalid_index.in.bt:5:8 error: non-integer index `string`
tests/out/error/index/invalid_index.in.bt:9:6 error: non-integer index `[]i32`
tests/out/error/index/invalid_index.in.bt:10:6 error: non-integer index `u8`
tests/out/error/index/invalid_index.in.bt:16:10 error: invalid map index `i32`, expected `string`
tests/out/error/index/invalid_index.in.bt:6:9 error: undefined ident `badvar`
tests/out/error/index/invalid_index.in.bt:10:6 error: non-integer index `[]i32`
tests/out/error/index/invalid_index.in.bt:11:6 error: non-integer index `u8`
tests/out/error/index/invalid_index.in.bt:12:7 error: undefined ident `badvar`
tests/out/error/index/invalid_index.in.bt:18:10 error: invalid map index `i32`, expected `string`
tests/out/error/index/invalid_index.in.bt:20:11 error: undefined ident `badvar`

0 comments on commit ab43ad2

Please sign in to comment.