mirror of
https://github.com/odin-lang/Odin.git
synced 2026-04-19 21:10:30 +00:00
Treat *x as an unary operator to improve error messages for common C-programmer mistakes
This commit is contained in:
@@ -1800,6 +1800,19 @@ gb_internal bool check_unary_op(CheckerContext *c, Operand *o, Token op) {
|
||||
}
|
||||
break;
|
||||
|
||||
case Token_Mul:
|
||||
{
|
||||
ERROR_BLOCK();
|
||||
error(op, "Operator '%.*s' is not a valid unary operator in Odin", LIT(op.string));
|
||||
if (is_type_pointer(o->type)) {
|
||||
str = expr_to_string(o->expr);
|
||||
error_line("\tSuggestion: Did you mean '%s^'?\n", str);
|
||||
} else if (is_type_multi_pointer(o->type)) {
|
||||
str = expr_to_string(o->expr);
|
||||
error_line("\tSuggestion: The value is a multi-pointer, did you mean '%s[0]'?\n", str);
|
||||
}
|
||||
}
|
||||
break;
|
||||
default:
|
||||
error(op, "Unknown operator '%.*s'", LIT(op.string));
|
||||
return false;
|
||||
|
||||
@@ -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();
|
||||
}
|
||||
|
||||
@@ -2541,6 +2541,9 @@ gb_internal Ast *parse_operand(AstFile *f, bool lhs) {
|
||||
return ast_pointer_type(f, token, elem);
|
||||
} break;
|
||||
|
||||
case Token_Mul:
|
||||
return parse_unary_expr(f, true);
|
||||
|
||||
case Token_OpenBracket: {
|
||||
Token token = expect_token(f, Token_OpenBracket);
|
||||
Ast *count_expr = nullptr;
|
||||
@@ -3274,7 +3277,9 @@ gb_internal Ast *parse_unary_expr(AstFile *f, bool lhs) {
|
||||
case Token_Sub:
|
||||
case Token_Xor:
|
||||
case Token_And:
|
||||
case Token_Not: {
|
||||
case Token_Not:
|
||||
case Token_Mul: // Used for error handling when people do C-like things
|
||||
{
|
||||
Token token = advance_token(f);
|
||||
Ast *expr = parse_unary_expr(f, lhs);
|
||||
return ast_unary_expr(f, token, expr);
|
||||
|
||||
Reference in New Issue
Block a user