Skip to content

Commit a314e05

Browse files
committed
Use hex consistently for .max is 64 bits or more.
1 parent 83fd24f commit a314e05

File tree

4 files changed

+12
-6
lines changed

4 files changed

+12
-6
lines changed

src/compiler/number.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -487,7 +487,7 @@ const char *expr_const_to_error_string(const ExprConst *expr)
487487
case CONST_BOOL:
488488
return expr->b ? "true" : "false";
489489
case CONST_INTEGER:
490-
return int_to_str(expr->ixx, 10, false);
490+
return int_to_str(expr->ixx, expr->is_hex ? 16 : 10, true);
491491
case CONST_FLOAT:
492492
return str_printf("%g", expr->fxx.f);
493493
case CONST_STRING:

src/compiler/sema_casts.c

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -567,10 +567,8 @@ static bool sema_error_const_int_out_of_range(CastContext *cc, Expr *expr, Expr
567567
expr->const_expr.enum_val->var.index,
568568
type_quoted_error_string(to_type));
569569
}
570-
const char *error_value = expr->const_expr.is_hex ? int_to_str(expr->const_expr.ixx, 16, true)
571-
: expr_const_to_error_string(&expr->const_expr);
572-
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,
573-
type_quoted_error_string(to_type));
570+
RETURN_CAST_ERROR(problem, "The value '%s' is out of range for %s, so you need an explicit cast to truncate the value.",
571+
expr_const_to_error_string(&expr->const_expr), type_quoted_error_string(to_type));
574572
}
575573

576574

src/compiler/sema_expr.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4943,6 +4943,7 @@ static inline bool sema_create_const_min(Expr *expr, Type *type, Type *flat)
49434943
expr->expr_kind = EXPR_CONST;
49444944
expr->const_expr.const_kind = CONST_INTEGER;
49454945
expr->const_expr.is_character = false;
4946+
expr->const_expr.is_hex = false;
49464947
expr->type = type;
49474948
expr->resolve_status = RESOLVE_DONE;
49484949
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)
49594960
break;
49604961
case TYPE_I64:
49614962
expr->const_expr.ixx.i = (Int128){ 0xFFFFFFFFFFFFFFFF, 1ULL << 63 };
4963+
expr->const_expr.is_hex = true;
49624964
break;
49634965
case TYPE_I128:
49644966
expr->const_expr.ixx.i = (Int128){ 1ULL << 63, 0 };
4967+
expr->const_expr.is_hex = true;
49654968
break;
49664969
default:
49674970
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)
51415144
expr->const_expr.const_kind = CONST_INTEGER;
51425145
expr->const_expr.is_character = false;
51435146
expr->type = type;
5147+
expr->const_expr.is_hex = false;
51445148
expr->resolve_status = RESOLVE_DONE;
51455149
expr->const_expr.ixx.type = flat->type_kind;
51465150
switch (flat->type_kind)
@@ -5156,9 +5160,11 @@ static inline bool sema_create_const_max(Expr *expr, Type *type, Type *flat)
51565160
break;
51575161
case TYPE_I64:
51585162
expr->const_expr.ixx.i = (Int128){ 0, 0x7FFFFFFFFFFFFFFFLL };
5163+
expr->const_expr.is_hex = true;
51595164
break;
51605165
case TYPE_I128:
51615166
expr->const_expr.ixx.i = (Int128){ 0x7FFFFFFFFFFFFFFFLL, 0xFFFFFFFFFFFFFFFFLL };
5167+
expr->const_expr.is_hex = true;
51625168
break;
51635169
case TYPE_U8:
51645170
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)
51715177
break;
51725178
case TYPE_U64:
51735179
expr->const_expr.ixx.i = (Int128){ 0, 0xFFFFFFFFFFFFFFFFLL };
5180+
expr->const_expr.is_hex = true;
51745181
break;
51755182
case TYPE_U128:
51765183
expr->const_expr.ixx.i = (Int128){ 0xFFFFFFFFFFFFFFFFLL, 0xFFFFFFFFFFFFFFFFLL };
5184+
expr->const_expr.is_hex = true;
51775185
break;
51785186
default:
51795187
UNREACHABLE
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
fn int main()
22
{
3-
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
3+
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
44
ulong x1 = -1 + ulong.max; // #error: converted to a signed type due to the left-hand side being signed, but the value does
55
return 0;
66
}

0 commit comments

Comments
 (0)