aboutsummaryrefslogtreecommitdiff
path: root/src/stage1/ir.cpp
diff options
context:
space:
mode:
authorMatthew Borkowski <matthew.h.borkowski@gmail.com>2021-05-28 23:32:19 -0400
committerVeikka Tuominen <git@vexu.eu>2021-06-08 20:58:30 +0300
commit9ac6d2861466e03407dddebf47354034be9a136c (patch)
treeb615983d7ea908439044f8e242d940b45ce107bc /src/stage1/ir.cpp
parentff0a15bb7a57370091898fde7bd53364c90252df (diff)
downloadzig-9ac6d2861466e03407dddebf47354034be9a136c.tar.gz
zig-9ac6d2861466e03407dddebf47354034be9a136c.zip
stage1: make `@truncate` to an integer type of different sign an error at comptime too
Diffstat (limited to 'src/stage1/ir.cpp')
-rw-r--r--src/stage1/ir.cpp22
1 files changed, 12 insertions, 10 deletions
diff --git a/src/stage1/ir.cpp b/src/stage1/ir.cpp
index 19242a5c42..fd44e11721 100644
--- a/src/stage1/ir.cpp
+++ b/src/stage1/ir.cpp
@@ -19563,6 +19563,18 @@ static IrInstGen *ir_analyze_instruction_truncate(IrAnalyze *ira, IrInstSrcTrunc
return ir_implicit_cast2(ira, &instruction->target->base, target, dest_type);
}
+ if (src_type->id != ZigTypeIdComptimeInt) {
+ if (src_type->data.integral.is_signed != dest_type->data.integral.is_signed) {
+ const char *sign_str = dest_type->data.integral.is_signed ? "signed" : "unsigned";
+ ir_add_error(ira, &target->base, buf_sprintf("expected %s integer type, found '%s'", sign_str, buf_ptr(&src_type->name)));
+ return ira->codegen->invalid_inst_gen;
+ } else if (src_type->data.integral.bit_count > 0 && src_type->data.integral.bit_count < dest_type->data.integral.bit_count) {
+ ir_add_error(ira, &target->base, buf_sprintf("type '%s' has fewer bits than destination type '%s'",
+ buf_ptr(&src_type->name), buf_ptr(&dest_type->name)));
+ return ira->codegen->invalid_inst_gen;
+ }
+ }
+
if (instr_is_comptime(target)) {
ZigValue *val = ir_resolve_const(ira, target, UndefBad);
if (val == nullptr)
@@ -19580,16 +19592,6 @@ static IrInstGen *ir_analyze_instruction_truncate(IrAnalyze *ira, IrInstSrcTrunc
return result;
}
- if (src_type->data.integral.is_signed != dest_type->data.integral.is_signed) {
- const char *sign_str = dest_type->data.integral.is_signed ? "signed" : "unsigned";
- ir_add_error(ira, &target->base, buf_sprintf("expected %s integer type, found '%s'", sign_str, buf_ptr(&src_type->name)));
- return ira->codegen->invalid_inst_gen;
- } else if (src_type->data.integral.bit_count < dest_type->data.integral.bit_count) {
- ir_add_error(ira, &target->base, buf_sprintf("type '%s' has fewer bits than destination type '%s'",
- buf_ptr(&src_type->name), buf_ptr(&dest_type->name)));
- return ira->codegen->invalid_inst_gen;
- }
-
return ir_build_truncate_gen(ira, &instruction->base.base, dest_type, target);
}