Browse Source

Improve error message for typed constants that cannot be represented by a type

gingerBill 2 years ago
parent
commit
b9f7b2fdfa
1 changed files with 11 additions and 4 deletions
  1. 11 4
      src/check_expr.cpp

+ 11 - 4
src/check_expr.cpp

@@ -2116,16 +2116,21 @@ gb_internal bool check_is_expressible(CheckerContext *ctx, Operand *o, Type *typ
 			o->mode = Addressing_Invalid;
 			o->mode = Addressing_Invalid;
 		);
 		);
 
 
+		ERROR_BLOCK();
+
+
 		if (is_type_numeric(o->type) && is_type_numeric(type)) {
 		if (is_type_numeric(o->type) && is_type_numeric(type)) {
 			if (!is_type_integer(o->type) && is_type_integer(type)) {
 			if (!is_type_integer(o->type) && is_type_integer(type)) {
 				error(o->expr, "'%s' truncated to '%s', got %s", a, b, s);
 				error(o->expr, "'%s' truncated to '%s', got %s", a, b, s);
 			} else {
 			} else {
-				ERROR_BLOCK();
-				error(o->expr, "Cannot convert numeric value '%s' to '%s' from '%s', got %s", a, b, c, s);
+				if (are_types_identical(o->type, type)) {
+					error(o->expr, "Numeric value '%s' cannot be represented by '%s', got %s", a, c, s);
+				} else {
+					error(o->expr, "Cannot convert numeric value '%s' to '%s' from '%s', got %s", a, b, c, s);
+				}
 				check_assignment_error_suggestion(ctx, o, type);
 				check_assignment_error_suggestion(ctx, o, type);
 			}
 			}
 		} else {
 		} else {
-			ERROR_BLOCK();
 			error(o->expr, "Cannot convert '%s' to '%s' from '%s', got %s", a, b, c, s);
 			error(o->expr, "Cannot convert '%s' to '%s' from '%s', got %s", a, b, c, s);
 			check_assignment_error_suggestion(ctx, o, type);
 			check_assignment_error_suggestion(ctx, o, type);
 		}
 		}
@@ -2597,10 +2602,12 @@ gb_internal void check_shift(CheckerContext *c, Operand *x, Operand *y, Ast *nod
 				x->type = t_untyped_integer;
 				x->type = t_untyped_integer;
 			}
 			}
 
 
+			x->expr = node;
 			x->value = exact_value_shift(be->op.kind, x_val, y_val);
 			x->value = exact_value_shift(be->op.kind, x_val, y_val);
 
 
+
 			if (is_type_typed(x->type)) {
 			if (is_type_typed(x->type)) {
-				check_is_expressible(c, x, base_type(x->type));
+				check_is_expressible(c, x, x->type);
 			}
 			}
 			return;
 			return;
 		}
 		}