Use hex consistently for .max is 64 bits or more.

This commit is contained in:
Christoffer Lerno
2025-07-09 03:09:10 +02:00
parent 83fd24faa2
commit a314e05826
4 changed files with 12 additions and 6 deletions

View File

@@ -487,7 +487,7 @@ const char *expr_const_to_error_string(const ExprConst *expr)
case CONST_BOOL:
return expr->b ? "true" : "false";
case CONST_INTEGER:
return int_to_str(expr->ixx, 10, false);
return int_to_str(expr->ixx, expr->is_hex ? 16 : 10, true);
case CONST_FLOAT:
return str_printf("%g", expr->fxx.f);
case CONST_STRING:

View File

@@ -567,10 +567,8 @@ static bool sema_error_const_int_out_of_range(CastContext *cc, Expr *expr, Expr
expr->const_expr.enum_val->var.index,
type_quoted_error_string(to_type));
}
const char *error_value = expr->const_expr.is_hex ? int_to_str(expr->const_expr.ixx, 16, true)
: expr_const_to_error_string(&expr->const_expr);
RETURN_CAST_ERROR(problem, "The value '%s' is out of range for %s, so you need an explicit cast to truncate the value.", error_value,
type_quoted_error_string(to_type));
RETURN_CAST_ERROR(problem, "The value '%s' is out of range for %s, so you need an explicit cast to truncate the value.",
expr_const_to_error_string(&expr->const_expr), type_quoted_error_string(to_type));
}

View File

@@ -4943,6 +4943,7 @@ static inline bool sema_create_const_min(Expr *expr, Type *type, Type *flat)
expr->expr_kind = EXPR_CONST;
expr->const_expr.const_kind = CONST_INTEGER;
expr->const_expr.is_character = false;
expr->const_expr.is_hex = false;
expr->type = type;
expr->resolve_status = RESOLVE_DONE;
expr->const_expr.ixx.type = flat->type_kind;
@@ -4959,9 +4960,11 @@ static inline bool sema_create_const_min(Expr *expr, Type *type, Type *flat)
break;
case TYPE_I64:
expr->const_expr.ixx.i = (Int128){ 0xFFFFFFFFFFFFFFFF, 1ULL << 63 };
expr->const_expr.is_hex = true;
break;
case TYPE_I128:
expr->const_expr.ixx.i = (Int128){ 1ULL << 63, 0 };
expr->const_expr.is_hex = true;
break;
default:
expr->const_expr.ixx.i = (Int128){ 0, 0 };
@@ -5141,6 +5144,7 @@ static inline bool sema_create_const_max(Expr *expr, Type *type, Type *flat)
expr->const_expr.const_kind = CONST_INTEGER;
expr->const_expr.is_character = false;
expr->type = type;
expr->const_expr.is_hex = false;
expr->resolve_status = RESOLVE_DONE;
expr->const_expr.ixx.type = flat->type_kind;
switch (flat->type_kind)
@@ -5156,9 +5160,11 @@ static inline bool sema_create_const_max(Expr *expr, Type *type, Type *flat)
break;
case TYPE_I64:
expr->const_expr.ixx.i = (Int128){ 0, 0x7FFFFFFFFFFFFFFFLL };
expr->const_expr.is_hex = true;
break;
case TYPE_I128:
expr->const_expr.ixx.i = (Int128){ 0x7FFFFFFFFFFFFFFFLL, 0xFFFFFFFFFFFFFFFFLL };
expr->const_expr.is_hex = true;
break;
case TYPE_U8:
expr->const_expr.ixx.i = (Int128){ 0, 0xFF };
@@ -5171,9 +5177,11 @@ static inline bool sema_create_const_max(Expr *expr, Type *type, Type *flat)
break;
case TYPE_U64:
expr->const_expr.ixx.i = (Int128){ 0, 0xFFFFFFFFFFFFFFFFLL };
expr->const_expr.is_hex = true;
break;
case TYPE_U128:
expr->const_expr.ixx.i = (Int128){ 0xFFFFFFFFFFFFFFFFLL, 0xFFFFFFFFFFFFFFFFLL };
expr->const_expr.is_hex = true;
break;
default:
UNREACHABLE

View File

@@ -1,6 +1,6 @@
fn int main()
{
ulong x = ulong.max - 1; // #error: This expression (18446744073709551615) will be implicitly converted to a signed type due to the right-hand side being signed
ulong x = ulong.max - 1; // #error: This expression (0xFFFFFFFFFFFFFFFF) will be implicitly converted to a signed type due to the right-hand side being signed
ulong x1 = -1 + ulong.max; // #error: converted to a signed type due to the left-hand side being signed, but the value does
return 0;
}