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,
73307330 case CastOpResizeSlice:
73317331 case CastOpBytesToSlice:
73327332 // can't do it
7333 break;
7333 zig_unreachable();
73347334 case CastOpIntToFloat:
73357335 {
73367336 assert(new_type->id == TypeTableEntryIdFloat);
......@@ -7366,7 +7366,9 @@ static void eval_const_expr_implicit_cast(CastOp cast_op,
73667366static IrInstruction *ir_resolve_cast(IrAnalyze *ira, IrInstruction *source_instr, IrInstruction *value,
73677367 TypeTableEntry *wanted_type, CastOp cast_op, bool need_alloca)
73687368{
7369 if (value->value.special != ConstValSpecialRuntime) {
7369 if (value->value.special != ConstValSpecialRuntime &&
7370 cast_op != CastOpResizeSlice && cast_op != CastOpBytesToSlice)
7371 {
73707372 IrInstruction *result = ir_create_const(&ira->new_irb, source_instr->scope,
73717373 source_instr->source_node, wanted_type);
73727374 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
81668168 }
81678169 }
81688170
8169 // expliict cast from &const [N]T to []const T
8171 // explicit cast from &const [N]T to []const T
81708172 if (is_slice(wanted_type) &&
81718173 actual_type->id == TypeTableEntryIdPointer &&
81728174 actual_type->data.pointer.is_const &&
test/cases/cast.zig+9
......@@ -275,3 +275,12 @@ fn cast128Int(x: f128) -> u128 {
275275fn cast128Float(x: u128) -> f128 {
276276 @bitCast(f128, x)
277277}
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}