From ab43ad2ba27d3633c6a0a3d46117092141bc115b Mon Sep 17 00:00:00 2001 From: Lukas Neubert <40118727+serkonda7@users.noreply.github.com> Date: Thu, 9 Jan 2025 22:48:43 +0100 Subject: [PATCH] fix(checker): reduce error noise (#285) --- lib/bait/checker/assign.bt | 25 +++++++++++++++-------- lib/bait/checker/if_match.bt | 4 ++++ lib/bait/checker/index.bt | 8 ++++++++ tests/out/error/assign/twice_ident.in.bt | 2 ++ tests/out/error/assign/twice_ident.out | 1 + tests/out/error/fun/param_redefine.out | 2 +- tests/out/error/ident/redef.out | 6 +++--- tests/out/error/index/invalid_index.in.bt | 3 +++ tests/out/error/index/invalid_index.out | 9 +++++--- 9 files changed, 44 insertions(+), 16 deletions(-) create mode 100644 tests/out/error/assign/twice_ident.in.bt create mode 100644 tests/out/error/assign/twice_ident.out diff --git a/lib/bait/checker/assign.bt b/lib/bait/checker/assign.bt index 24a41b62..b2c9c5d1 100644 --- a/lib/bait/checker/assign.bt +++ b/lib/bait/checker/assign.bt @@ -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 } @@ -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 @@ -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 @@ -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) diff --git a/lib/bait/checker/if_match.bt b/lib/bait/checker/if_match.bt index eadcbb4d..c12dd098 100644 --- a/lib/bait/checker/if_match.bt +++ b/lib/bait/checker/if_match.bt @@ -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 diff --git a/lib/bait/checker/index.bt b/lib/bait/checker/index.bt index b2d54299..edfdbfaf 100644 --- a/lib/bait/checker/index.bt +++ b/lib/bait/checker/index.bt @@ -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) @@ -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) } diff --git a/tests/out/error/assign/twice_ident.in.bt b/tests/out/error/assign/twice_ident.in.bt new file mode 100644 index 00000000..cc94f922 --- /dev/null +++ b/tests/out/error/assign/twice_ident.in.bt @@ -0,0 +1,2 @@ +a := 1 +a := b diff --git a/tests/out/error/assign/twice_ident.out b/tests/out/error/assign/twice_ident.out new file mode 100644 index 00000000..636404f2 --- /dev/null +++ b/tests/out/error/assign/twice_ident.out @@ -0,0 +1 @@ +tests/out/error/assign/twice_ident.in.bt:2:1 error: redefinition of `a` diff --git a/tests/out/error/fun/param_redefine.out b/tests/out/error/fun/param_redefine.out index fb4cbbbc..e33e9838 100644 --- a/tests/out/error/fun/param_redefine.out +++ b/tests/out/error/fun/param_redefine.out @@ -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 diff --git a/tests/out/error/ident/redef.out b/tests/out/error/ident/redef.out index a1289259..859356fa 100644 --- a/tests/out/error/ident/redef.out +++ b/tests/out/error/ident/redef.out @@ -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` diff --git a/tests/out/error/index/invalid_index.in.bt b/tests/out/error/index/invalid_index.in.bt index 908c3a89..1f200f53 100644 --- a/tests/out/error/index/invalid_index.in.bt +++ b/tests/out/error/index/invalid_index.in.bt @@ -3,11 +3,13 @@ _ = 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 @@ -15,3 +17,4 @@ mymap := map{ _ = mymap["b"] _ = mymap[0] _ = mymap[s] +_ = mymap[badvar] diff --git a/tests/out/error/index/invalid_index.out b/tests/out/error/index/invalid_index.out index e2b5e225..7acc80a1 100644 --- a/tests/out/error/index/invalid_index.out +++ b/tests/out/error/index/invalid_index.out @@ -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`