From ee4ed126e1423d7566470b31e86f4ad1021328f2 Mon Sep 17 00:00:00 2001 From: gingerBill Date: Tue, 21 Feb 2023 16:25:28 +0000 Subject: Improve error message for accidentally using a type as an expression statement --- src/check_stmt.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/check_stmt.cpp') diff --git a/src/check_stmt.cpp b/src/check_stmt.cpp index f688b7f9c..3039995b9 100644 --- a/src/check_stmt.cpp +++ b/src/check_stmt.cpp @@ -1921,7 +1921,7 @@ gb_internal void check_expr_stmt(CheckerContext *ctx, Ast *node) { case Addressing_Type: { gbString str = type_to_string(operand.type); - error(node, "'%s' is not an expression", str); + error(node, "'%s' is not an expression but a type and cannot be used as a statement", str); gb_string_free(str); break; } -- cgit v1.2.3 From a2f02b8b3218dc83a2a2783ceb79cc57abacc7bd Mon Sep 17 00:00:00 2001 From: gingerBill Date: Tue, 21 Feb 2023 16:31:22 +0000 Subject: Fix bug with for in statements and pointer intervals --- src/check_expr.cpp | 20 ++++++++++++++------ src/check_stmt.cpp | 4 ++-- 2 files changed, 16 insertions(+), 8 deletions(-) (limited to 'src/check_stmt.cpp') diff --git a/src/check_expr.cpp b/src/check_expr.cpp index 2ed77535c..8a3bb2c1b 100644 --- a/src/check_expr.cpp +++ b/src/check_expr.cpp @@ -7040,7 +7040,7 @@ gb_internal bool ternary_compare_types(Type *x, Type *y) { } -gb_internal bool check_range(CheckerContext *c, Ast *node, Operand *x, Operand *y, ExactValue *inline_for_depth_, Type *type_hint=nullptr) { +gb_internal bool check_range(CheckerContext *c, Ast *node, bool is_for_loop, Operand *x, Operand *y, ExactValue *inline_for_depth_, Type *type_hint=nullptr) { if (!is_ast_range(node)) { return false; } @@ -7089,9 +7089,17 @@ gb_internal bool check_range(CheckerContext *c, Ast *node, Operand *x, Operand * } Type *type = x->type; - if (!is_type_integer(type) && !is_type_float(type) && !is_type_pointer(type) && !is_type_enum(type)) { - error(ie->op, "Only numerical and pointer types are allowed within interval expressions"); - return false; + + if (is_for_loop) { + if (!is_type_integer(type) && !is_type_float(type) && !is_type_enum(type)) { + error(ie->op, "Only numerical types are allowed within interval expressions"); + return false; + } + } else { + if (!is_type_integer(type) && !is_type_float(type) && !is_type_pointer(type) && !is_type_enum(type)) { + error(ie->op, "Only numerical and pointer types are allowed within interval expressions"); + return false; + } } if (x->mode == Addressing_Constant && @@ -8104,7 +8112,7 @@ gb_internal ExprKind check_compound_literal(CheckerContext *c, Operand *o, Ast * Operand x = {}; Operand y = {}; - bool ok = check_range(c, fv->field, &x, &y, nullptr); + bool ok = check_range(c, fv->field, false, &x, &y, nullptr); if (!ok) { continue; } @@ -8320,7 +8328,7 @@ gb_internal ExprKind check_compound_literal(CheckerContext *c, Operand *o, Ast * Operand x = {}; Operand y = {}; - bool ok = check_range(c, fv->field, &x, &y, nullptr, index_type); + bool ok = check_range(c, fv->field, false, &x, &y, nullptr, index_type); if (!ok) { continue; } diff --git a/src/check_stmt.cpp b/src/check_stmt.cpp index 3039995b9..f300f45c7 100644 --- a/src/check_stmt.cpp +++ b/src/check_stmt.cpp @@ -725,7 +725,7 @@ gb_internal void check_inline_range_stmt(CheckerContext *ctx, Ast *node, u32 mod Operand x = {}; Operand y = {}; - bool ok = check_range(ctx, expr, &x, &y, &inline_for_depth); + bool ok = check_range(ctx, expr, true, &x, &y, &inline_for_depth); if (!ok) { goto skip_expr; } @@ -1439,7 +1439,7 @@ gb_internal void check_range_stmt(CheckerContext *ctx, Ast *node, u32 mod_flags) Operand x = {}; Operand y = {}; - bool ok = check_range(ctx, expr, &x, &y, nullptr); + bool ok = check_range(ctx, expr, true, &x, &y, nullptr); if (!ok) { goto skip_expr_range_stmt; } -- cgit v1.2.3 From ef99d03f211c5b471a25d0bcc6ac21b491369086 Mon Sep 17 00:00:00 2001 From: Tetralux Date: Wed, 22 Feb 2023 21:43:42 +0000 Subject: Remove debug print --- src/check_stmt.cpp | 1 - 1 file changed, 1 deletion(-) (limited to 'src/check_stmt.cpp') diff --git a/src/check_stmt.cpp b/src/check_stmt.cpp index f300f45c7..44d7cf59d 100644 --- a/src/check_stmt.cpp +++ b/src/check_stmt.cpp @@ -1293,7 +1293,6 @@ gb_internal void check_type_switch_stmt(CheckerContext *ctx, Ast *node, u32 mod_ for (Type *t : variants) { if (!type_ptr_set_exists(&seen, t)) { array_add(&unhandled, t); - gb_printf_err("HERE: %p %s\n", t, type_to_string(t)); } } -- cgit v1.2.3 From 6a6d7701f9892a3468c74f0c7d1e70e04f529824 Mon Sep 17 00:00:00 2001 From: gingerBill Date: Wed, 22 Feb 2023 21:50:49 +0000 Subject: Improve error bounds for `check_comparison` --- src/check_expr.cpp | 6 +++--- src/check_stmt.cpp | 8 ++++---- src/error.cpp | 8 +++++++- 3 files changed, 14 insertions(+), 8 deletions(-) (limited to 'src/check_stmt.cpp') diff --git a/src/check_expr.cpp b/src/check_expr.cpp index f42483c86..00d394966 100644 --- a/src/check_expr.cpp +++ b/src/check_expr.cpp @@ -2397,7 +2397,7 @@ gb_internal void add_comparison_procedures_for_fields(CheckerContext *c, Type *t } -gb_internal void check_comparison(CheckerContext *c, Operand *x, Operand *y, TokenKind op) { +gb_internal void check_comparison(CheckerContext *c, Ast *node, Operand *x, Operand *y, TokenKind op) { if (x->mode == Addressing_Type && y->mode == Addressing_Type) { bool comp = are_types_identical(x->type, y->type); switch (op) { @@ -2485,7 +2485,7 @@ gb_internal void check_comparison(CheckerContext *c, Operand *x, Operand *y, Tok } if (err_str != nullptr) { - error(x->expr, "Cannot compare expression, %s", err_str); + error(node, "Cannot compare expression, %s", err_str); x->type = t_untyped_bool; } else { if (x->mode == Addressing_Constant && @@ -3498,7 +3498,7 @@ gb_internal void check_binary_expr(CheckerContext *c, Operand *x, Ast *node, Typ if (token_is_comparison(op.kind)) { - check_comparison(c, x, y, op.kind); + check_comparison(c, node, x, y, op.kind); return; } diff --git a/src/check_stmt.cpp b/src/check_stmt.cpp index f300f45c7..7e3948336 100644 --- a/src/check_stmt.cpp +++ b/src/check_stmt.cpp @@ -978,19 +978,19 @@ gb_internal void check_switch_stmt(CheckerContext *ctx, Ast *node, u32 mod_flags Operand a = lhs; Operand b = rhs; - check_comparison(ctx, &a, &x, Token_LtEq); + check_comparison(ctx, expr, &a, &x, Token_LtEq); if (a.mode == Addressing_Invalid) { continue; } - check_comparison(ctx, &b, &x, upper_op); + check_comparison(ctx, expr, &b, &x, upper_op); if (b.mode == Addressing_Invalid) { continue; } Operand a1 = lhs; Operand b1 = rhs; - check_comparison(ctx, &a1, &b1, Token_LtEq); + check_comparison(ctx, expr, &a1, &b1, Token_LtEq); add_to_seen_map(ctx, &seen, upper_op, x, lhs, rhs); @@ -1029,7 +1029,7 @@ gb_internal void check_switch_stmt(CheckerContext *ctx, Ast *node, u32 mod_flags // NOTE(bill): the ordering here matters Operand z = y; - check_comparison(ctx, &z, &x, Token_CmpEq); + check_comparison(ctx, expr, &z, &x, Token_CmpEq); if (z.mode == Addressing_Invalid) { continue; } diff --git a/src/error.cpp b/src/error.cpp index 750cd147f..9279ed4d4 100644 --- a/src/error.cpp +++ b/src/error.cpp @@ -264,7 +264,7 @@ gb_internal bool show_error_on_line(TokenPos const &pos, TokenPos end) { ELLIPSIS_PADDING = 8 // `... ...` }; - error_out("\t"); + error_out("\n\t"); terminal_set_colours(TerminalStyle_Bold, TerminalColour_White); @@ -345,6 +345,9 @@ gb_internal void error_va(TokenPos const &pos, TokenPos end, char const *fmt, va } else if (global_error_collector.prev != pos) { global_error_collector.prev = pos; error_out_pos(pos); + if (has_ansi_terminal_colours()) { + error_out_coloured("Error: ", TerminalStyle_Normal, TerminalColour_Red); + } error_out_va(fmt, va); error_out("\n"); show_error_on_line(pos, end); @@ -395,6 +398,9 @@ gb_internal void error_no_newline_va(TokenPos const &pos, char const *fmt, va_li } else if (global_error_collector.prev != pos) { global_error_collector.prev = pos; error_out_pos(pos); + if (has_ansi_terminal_colours()) { + error_out_coloured("Error: ", TerminalStyle_Normal, TerminalColour_Red); + } error_out_va(fmt, va); } mutex_unlock(&global_error_collector.mutex); -- cgit v1.2.3