aboutsummaryrefslogtreecommitdiff
path: root/src/check_type.cpp
diff options
context:
space:
mode:
authorgingerBill <bill@gingerbill.org>2024-03-23 16:51:04 +0000
committergingerBill <bill@gingerbill.org>2024-03-23 16:51:04 +0000
commit1d46adb598328b4977d5b4cfad2a1dc679b05a21 (patch)
treec74cbdb96967efbb05b58e6f4a059f7d5123bb8f /src/check_type.cpp
parent61aa4558dc864019192072ff9a7f1f1ee23096bf (diff)
Treat `*x` as an unary operator to improve error messages for common C-programmer mistakes
Diffstat (limited to 'src/check_type.cpp')
-rw-r--r--src/check_type.cpp16
1 files changed, 15 insertions, 1 deletions
diff --git a/src/check_type.cpp b/src/check_type.cpp
index 38bfa56ef..0b9042905 100644
--- a/src/check_type.cpp
+++ b/src/check_type.cpp
@@ -3375,7 +3375,9 @@ gb_internal Type *check_type_expr(CheckerContext *ctx, Ast *e, Type *named_type)
type = t_invalid;
- // NOTE(bill): Check for common mistakes from C programmers e.g. T[] and T[N]
+ // NOTE(bill): Check for common mistakes from C programmers
+ // e.g. T[] and T[N]
+ // e.g. *T
Ast *node = unparen_expr(e);
if (node && node->kind == Ast_IndexExpr) {
gbString index_str = nullptr;
@@ -3395,6 +3397,18 @@ gb_internal Type *check_type_expr(CheckerContext *ctx, Ast *e, Type *named_type)
Ast *pseudo_array_expr = ast_array_type(e->file(), ast_token(node->IndexExpr.expr), node->IndexExpr.index, node->IndexExpr.expr);
check_array_type_internal(ctx, pseudo_array_expr, &type, nullptr);
}
+ } else if (node && node->kind == Ast_UnaryExpr && node->UnaryExpr.op.kind == Token_Mul) {
+ gbString type_str = expr_to_string(node->UnaryExpr.expr);
+ defer (gb_string_free(type_str));
+
+ error_line("\tSuggestion: Did you mean '^%s'?\n", type_str);
+ end_error_block();
+
+ // NOTE(bill): Minimize error propagation of bad array syntax by treating this like a type
+ if (node->UnaryExpr.expr != nullptr) {
+ Ast *pseudo_pointer_expr = ast_pointer_type(e->file(), ast_token(node->UnaryExpr.expr), node->UnaryExpr.expr);
+ return check_type_expr(ctx, pseudo_pointer_expr, named_type);
+ }
} else {
end_error_block();
}