From a18f668fc402b8068d981ba33f9ac15935509b34 Mon Sep 17 00:00:00 2001 From: Christoffer Lerno Date: Wed, 14 Jul 2021 23:48:43 +0200 Subject: [PATCH] Updated error messages on failed binary arithmetic promotion. --- src/compiler/sema_expr.c | 20 +++++++++---------- src/compiler/types.c | 2 +- .../expressions/no_valid_conversion_minus.c3 | 2 +- test/test_suite/symbols/various.c3 | 2 +- test/test_suite/types/enum_errors.c3 | 2 +- 5 files changed, 14 insertions(+), 14 deletions(-) diff --git a/src/compiler/sema_expr.c b/src/compiler/sema_expr.c index 943292804..0b8748729 100644 --- a/src/compiler/sema_expr.c +++ b/src/compiler/sema_expr.c @@ -265,9 +265,9 @@ static ExprFailableStatus expr_is_failable(Expr *expr) static inline bool sema_type_error_on_binop(Expr *expr) { const char *c = token_type_to_string(binaryop_to_token(expr->binary_expr.operator)); - SEMA_ERROR(expr, "%s is not defined in the expression '%s' %s '%s'.", - c, type_to_error_string(expr->binary_expr.left->type), - c, type_to_error_string(expr->binary_expr.right->type)); + SEMA_ERROR(expr, "%s is not defined in the expression %s %s %s.", + c, type_quoted_error_string(expr->binary_expr.left->type), + c, type_quoted_error_string(expr->binary_expr.right->type)); return false; } @@ -3560,7 +3560,7 @@ static bool binary_arithmetic_promotion(Context *context, Expr *left, Expr *righ { return sema_type_error_on_binop(parent); } - SEMA_ERROR(parent, error_message, type_to_error_string(left_type), type_to_error_string(right_type)); + SEMA_ERROR(parent, error_message, type_quoted_error_string(left->type), type_quoted_error_string(right->type)); return false; } return cast_implicit(left, max) & cast_implicit(right, max); @@ -3656,7 +3656,7 @@ static bool sema_expr_analyse_sub(Context *context, Type *to, Expr *expr, Expr * } // 7. Attempt arithmetic promotion, to promote both to a common type. - if (!binary_arithmetic_promotion(context, left, right, left_type, right_type, expr, "There is no implicit conversion available for '%s' - '%s'.")) + if (!binary_arithmetic_promotion(context, left, right, left_type, right_type, expr, "The subtraction %s - %s is not possible.")) { return false; } @@ -3741,7 +3741,7 @@ static bool sema_expr_analyse_add(Context *context, Type *to, Expr *expr, Expr * } // 4. Do an binary arithmetic promotion - if (!binary_arithmetic_promotion(context, left, right, left_type, right_type, expr, "Cannot add '%s' to '%s'")) + if (!binary_arithmetic_promotion(context, left, right, left_type, right_type, expr, "Cannot do the addition %s + %s.")) { return false; } @@ -3790,7 +3790,7 @@ static bool sema_expr_analyse_mult(Context *context, Type *to, Expr *expr, Expr Type *right_type = right->type->canonical; // 2. Perform promotion to a common type. - if (!binary_arithmetic_promotion(context, left, right, left_type, right_type, expr, "Cannot multiply '%s' by '%s'")) + if (!binary_arithmetic_promotion(context, left, right, left_type, right_type, expr, "Cannot multiply %s by %s")) { return false; } @@ -3833,7 +3833,7 @@ static bool sema_expr_analyse_div(Context *context, Type *to, Expr *expr, Expr * Type *right_type = right->type->canonical; // 2. Perform promotion to a common type. - if (!binary_arithmetic_promotion(context, left, right, left_type, right_type, expr, "Cannot divide '%s' by '%s'.")) + if (!binary_arithmetic_promotion(context, left, right, left_type, right_type, expr, "Cannot divide %s by %s.")) { return false; } @@ -3894,7 +3894,7 @@ static bool sema_expr_analyse_mod(Context *context, Type *to, Expr *expr, Expr * Type *right_type = right->type->canonical; // 2. Perform promotion to a common type. - if (!binary_arithmetic_promotion(context, left, right, left_type, right_type, expr, "Cannot divide '%s' by '%s'.")) + if (!binary_arithmetic_promotion(context, left, right, left_type, right_type, expr, "Cannot divide %s by %s.")) { return false; } @@ -3902,7 +3902,7 @@ static bool sema_expr_analyse_mod(Context *context, Type *to, Expr *expr, Expr * // 3. a % 0 is not valid, so detect it. if (is_const(right) && bigint_cmp_zero(&right->const_expr.i) == CMP_EQ) { - SEMA_ERROR(expr->binary_expr.right, "Cannot perform % with a constant zero."); + SEMA_ERROR(expr->binary_expr.right, "Cannot perform %% with a constant zero."); return false; } diff --git a/src/compiler/types.c b/src/compiler/types.c index 1cec3ee8a..3f39216eb 100644 --- a/src/compiler/types.c +++ b/src/compiler/types.c @@ -142,7 +142,7 @@ const char *type_to_error_string(Type *type) asprintf(&buffer, "%s*", type_to_error_string(type->pointer)); return buffer; case TYPE_STRLIT: - return "compile time string"; + return "string literal"; case TYPE_ARRAY: asprintf(&buffer, "%s[%llu]", type_to_error_string(type->array.base), (unsigned long long)type->array.len); return buffer; diff --git a/test/test_suite/expressions/no_valid_conversion_minus.c3 b/test/test_suite/expressions/no_valid_conversion_minus.c3 index 8536f863a..653493dab 100644 --- a/test/test_suite/expressions/no_valid_conversion_minus.c3 +++ b/test/test_suite/expressions/no_valid_conversion_minus.c3 @@ -5,6 +5,6 @@ func int main() { int a = 0; Foo f; - f - a; // #error: There is no implicit conversion available for 'Foo' - 'int' + f - a; // #error: The subtraction 'Foo' - 'int' is not possible return 1; } \ No newline at end of file diff --git a/test/test_suite/symbols/various.c3 b/test/test_suite/symbols/various.c3 index 94e22bb18..2a2918a8f 100644 --- a/test/test_suite/symbols/various.c3 +++ b/test/test_suite/symbols/various.c3 @@ -125,7 +125,7 @@ func void test16() func void test17() { - int a = "test"; // #error: cast 'compile time string' to 'int' + int a = "test"; // #error: cast 'string literal' to 'int' } func void test18() diff --git a/test/test_suite/types/enum_errors.c3 b/test/test_suite/types/enum_errors.c3 index 9b9feae83..0dbf94be8 100644 --- a/test/test_suite/types/enum_errors.c3 +++ b/test/test_suite/types/enum_errors.c3 @@ -12,6 +12,6 @@ func int foo() enum State { A = foo(), // #error: Expected a constant expression for enum - B = "hello", // #error: Cannot implicitly cast 'compile time string' to 'int' + B = "hello", // #error: Cannot implicitly cast 'string literal' to 'int' C = true, // #error: Cannot implicitly cast 'bool' to 'int' }