authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2017-08-25 19:53:29-04:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2017-08-25 19:54:20-04:00
log4d8269f69f530fe251e3132a95285c0af9d3aaab
tree9c00a12ea4176c2d2945a425825dffb0d7bc5c76
parent754f7809e38d5d44da9d63241af310e6c0bee730

fix some casts on const data causing segfault


2 files changed, 14 insertions(+), 3 deletions(-)

src/ir.cpp+5-3
...@@ -7330,7 +7330,7 @@ static void eval_const_expr_implicit_cast(CastOp cast_op,...@@ -7330,7 +7330,7 @@ static void eval_const_expr_implicit_cast(CastOp cast_op,
7330 case CastOpResizeSlice:7330 case CastOpResizeSlice:
7331 case CastOpBytesToSlice:7331 case CastOpBytesToSlice:
7332 // can't do it7332 // can't do it
7333 break;7333 zig_unreachable();
7334 case CastOpIntToFloat:7334 case CastOpIntToFloat:
7335 {7335 {
7336 assert(new_type->id == TypeTableEntryIdFloat);7336 assert(new_type->id == TypeTableEntryIdFloat);
...@@ -7366,7 +7366,9 @@ static void eval_const_expr_implicit_cast(CastOp cast_op,...@@ -7366,7 +7366,9 @@ static void eval_const_expr_implicit_cast(CastOp cast_op,
7366static IrInstruction *ir_resolve_cast(IrAnalyze *ira, IrInstruction *source_instr, IrInstruction *value,7366static IrInstruction *ir_resolve_cast(IrAnalyze *ira, IrInstruction *source_instr, IrInstruction *value,
7367 TypeTableEntry *wanted_type, CastOp cast_op, bool need_alloca)7367 TypeTableEntry *wanted_type, CastOp cast_op, bool need_alloca)
7368{7368{
7369 if (value->value.special != ConstValSpecialRuntime) {7369 if (value->value.special != ConstValSpecialRuntime &&
7370 cast_op != CastOpResizeSlice && cast_op != CastOpBytesToSlice)
7371 {
7370 IrInstruction *result = ir_create_const(&ira->new_irb, source_instr->scope,7372 IrInstruction *result = ir_create_const(&ira->new_irb, source_instr->scope,
7371 source_instr->source_node, wanted_type);7373 source_instr->source_node, wanted_type);
7372 eval_const_expr_implicit_cast(cast_op, &value->value, value->value.type,7374 eval_const_expr_implicit_cast(cast_op, &value->value, value->value.type,
...@@ -8166,7 +8168,7 @@ static IrInstruction *ir_analyze_cast(IrAnalyze *ira, IrInstruction *source_inst...@@ -8166,7 +8168,7 @@ static IrInstruction *ir_analyze_cast(IrAnalyze *ira, IrInstruction *source_inst
8166 }8168 }
8167 }8169 }
81688170
8169 // expliict cast from &const [N]T to []const T8171 // explicit cast from &const [N]T to []const T
8170 if (is_slice(wanted_type) &&8172 if (is_slice(wanted_type) &&
8171 actual_type->id == TypeTableEntryIdPointer &&8173 actual_type->id == TypeTableEntryIdPointer &&
8172 actual_type->data.pointer.is_const &&8174 actual_type->data.pointer.is_const &&
test/cases/cast.zig+9
...@@ -275,3 +275,12 @@ fn cast128Int(x: f128) -> u128 {...@@ -275,3 +275,12 @@ fn cast128Int(x: f128) -> u128 {
275fn cast128Float(x: u128) -> f128 {275fn cast128Float(x: u128) -> f128 {
276 @bitCast(f128, x)276 @bitCast(f128, x)
277}277}
278
279test "const slice widen cast" {
280 const bytes = []u8{0x12, 0x12, 0x12, 0x12};
281
282 const u32_value = ([]const u32)(bytes[0..])[0];
283 assert(u32_value == 0x12121212);
284
285 //assert(@bitCast(u32, bytes) == 0x12121212);
286}