mirror of
https://github.com/ziglang/zig.git
synced 2026-02-12 20:37:54 +00:00
add compile error for casting negative value to...
...unsigned integer at compile-time
This commit is contained in:
parent
6237411716
commit
63f6676fee
@ -7053,6 +7053,13 @@ static IrInstruction *ir_analyze_widen_or_shorten(IrAnalyze *ira, IrInstruction
|
||||
ConstExprValue *val = ir_resolve_const(ira, target, UndefBad);
|
||||
if (!val)
|
||||
return ira->codegen->invalid_instruction;
|
||||
if (val->data.x_bignum.is_negative && wanted_type->id == TypeTableEntryIdInt &&
|
||||
!wanted_type->data.integral.is_signed)
|
||||
{
|
||||
ir_add_error(ira, source_instr,
|
||||
buf_sprintf("attempt to cast negative value to unsigned integer"));
|
||||
return ira->codegen->invalid_instruction;
|
||||
}
|
||||
IrInstruction *result = ir_create_const(&ira->new_irb, source_instr->scope,
|
||||
source_instr->source_node, wanted_type);
|
||||
result->value = *val;
|
||||
|
||||
@ -1801,4 +1801,12 @@ pub fn addCases(cases: &tests.CompileErrorContext) {
|
||||
\\}
|
||||
,
|
||||
".tmp_source.zig:2:7: error: remainder division with 'i32' and 'i32': signed integers and floats must use @rem or @mod");
|
||||
|
||||
cases.add("cast negative value to unsigned integer",
|
||||
\\comptime {
|
||||
\\ const value: i32 = -1;
|
||||
\\ const unsigned = u32(value);
|
||||
\\}
|
||||
,
|
||||
".tmp_source.zig:3:25: error: attempt to cast negative value to unsigned integer");
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user