- Bitstruct truncated constant error escapes $defined #2515

This commit is contained in:
Christoffer Lerno
2025-10-06 20:50:56 +02:00
parent 5a1831c989
commit b4b14674b4
5 changed files with 13 additions and 5 deletions

View File

@@ -9,6 +9,7 @@
- Bug in `io::write_using_write_byte`.
- Bitstruct value cannot be used to index a const array in compile time. #2512
- Compiler fails to stop error print in recursive macro, and also prints unnecessary "inline at" #2513.
- Bitstruct truncated constant error escapes `$defined` #2515
### Stdlib changes

View File

@@ -6905,7 +6905,7 @@ static bool sema_expr_analyse_assign(SemaContext *context, Expr *expr, Expr *lef
}
if (left->expr_kind == EXPR_BITACCESS)
{
if (!sema_bit_assignment_check(context, right, left->access_resolved_expr.ref)) return false;
if (!sema_bit_assignment_check(context, right, left->access_resolved_expr.ref, failed_ref)) return false;
expr->expr_kind = EXPR_BITASSIGN;
}
return true;
@@ -12310,7 +12310,7 @@ static inline bool sema_insert_binary_overload(SemaContext *context, Expr *expr,
}
// Check if the assignment fits
bool sema_bit_assignment_check(SemaContext *context, Expr *right, Decl *member)
bool sema_bit_assignment_check(SemaContext *context, Expr *right, Decl *member, bool *failed_ref)
{
// Don't check non-consts and non integers.
if (!sema_cast_const(right) || !type_is_integer(right->type)) return true;
@@ -12322,6 +12322,7 @@ bool sema_bit_assignment_check(SemaContext *context, Expr *right, Decl *member)
if (int_bits_needed(right->const_expr.ixx) > bits)
{
if (failed_ref) return *failed_ref = true, false;
RETURN_SEMA_ERROR(right, "This constant would be truncated if stored in the "
"bitstruct, do you need a wider bit range?");
}

View File

@@ -277,7 +277,7 @@ static inline bool sema_expr_analyse_struct_plain_initializer(SemaContext *conte
if (!sema_analyse_expr_rhs(context, members[i]->type, element, true, no_match_ref, false)) return false;
if (member->decl_kind == DECL_VAR && member->var.kind == VARDECL_BITMEMBER)
{
if (!sema_bit_assignment_check(context, element, members[i])) return false;
if (!sema_bit_assignment_check(context, element, members[i], no_match_ref)) return false;
}
optional = optional || IS_OPTIONAL(element);
}
@@ -529,7 +529,7 @@ static bool sema_expr_analyse_designated_initializer(SemaContext *context, Type
if (!sema_analyse_expr_rhs(context, result, value, true, no_match_ref, false)) return false;
if (is_bitmember)
{
if (!sema_bit_assignment_check(context, value, member)) return false;
if (!sema_bit_assignment_check(context, value, member, no_match_ref)) return false;
}
optional = optional || IS_OPTIONAL(value);
expr->resolve_status = RESOLVE_DONE;

View File

@@ -113,7 +113,7 @@ Expr *sema_resolve_string_ident(SemaContext *context, Expr *inner, bool report_m
bool sema_analyse_asm(SemaContext *context, AsmInlineBlock *block, Ast *asm_stmt);
bool sema_expr_analyse_sprintf(SemaContext *context, Expr *expr, Expr *format_string, Expr **args, unsigned num_args);
bool sema_bit_assignment_check(SemaContext *context, Expr *right, Decl *member);
bool sema_bit_assignment_check(SemaContext *context, Expr *right, Decl *member, bool *failed_ref);
CondResult sema_check_comp_time_bool(SemaContext *context, Expr *expr);
bool sema_expr_check_assign(SemaContext *context, Expr *expr, bool *failed_ref);

View File

@@ -0,0 +1,6 @@
bitstruct Test : char
{
char type : 0..2;
}
$assert !$defined((Test){123});