authorgravatar for 14938807+xackus@users.noreply.github.comxackus <14938807+xackus@users.noreply.github.com> 2020-06-08 19:06:37+02:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2020-06-08 17:19:06-04:00
log0d40cb625564ca83f1b3d15202e02af656710c4a
treeffc35c6ac5b7892cf0083711a069aa48479fc7d4
parentc405844b0a039e6f2ba766d80644e06c1438654c

stage1: fix crash on slice byte reinterpretation


2 files changed, 27 insertions(+), 5 deletions(-)

src/ir.cpp+18-5
...@@ -29043,11 +29043,24 @@ static Error buf_read_value_bytes(IrAnalyze *ira, CodeGen *codegen, AstNode *sou...@@ -29043,11 +29043,24 @@ static Error buf_read_value_bytes(IrAnalyze *ira, CodeGen *codegen, AstNode *sou
29043 case ZigTypeIdStruct:29043 case ZigTypeIdStruct:
29044 switch (val->type->data.structure.layout) {29044 switch (val->type->data.structure.layout) {
29045 case ContainerLayoutAuto: {29045 case ContainerLayoutAuto: {
29046 ErrorMsg *msg = opt_ir_add_error_node(ira, codegen, source_node,29046 switch(val->type->data.structure.special){
29047 buf_sprintf("non-extern, non-packed struct '%s' cannot have its bytes reinterpreted",29047 case StructSpecialNone:
29048 buf_ptr(&val->type->name)));29048 case StructSpecialInferredTuple:
29049 add_error_note(codegen, msg, val->type->data.structure.decl_node,29049 case StructSpecialInferredStruct: {
29050 buf_sprintf("declared here"));29050 ErrorMsg *msg = opt_ir_add_error_node(ira, codegen, source_node,
29051 buf_sprintf("non-extern, non-packed struct '%s' cannot have its bytes reinterpreted",
29052 buf_ptr(&val->type->name)));
29053 add_error_note(codegen, msg, val->type->data.structure.decl_node,
29054 buf_sprintf("declared here"));
29055 break;
29056 }
29057 case StructSpecialSlice: {
29058 opt_ir_add_error_node(ira, codegen, source_node,
29059 buf_sprintf("slice '%s' cannot have its bytes reinterpreted",
29060 buf_ptr(&val->type->name)));
29061 break;
29062 }
29063 }
29051 return ErrorSemanticAnalyzeFail;29064 return ErrorSemanticAnalyzeFail;
29052 }29065 }
29053 case ContainerLayoutExtern: {29066 case ContainerLayoutExtern: {
test/compile_errors.zig+9
...@@ -7495,4 +7495,13 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {...@@ -7495,4 +7495,13 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
7495 ":22:12: error: cannot compare types '?[3]i32' and '[3]i32'",7495 ":22:12: error: cannot compare types '?[3]i32' and '[3]i32'",
7496 ":22:12: note: operator not supported for type '[3]i32'",7496 ":22:12: note: operator not supported for type '[3]i32'",
7497 });7497 });
7498
7499 cases.add("slice cannot have its bytes reinterpreted",
7500 \\export fn foo() void {
7501 \\ const bytes = [1]u8{ 0xfa } ** 16;
7502 \\ var value = @ptrCast(*const []const u8, &bytes).*;
7503 \\}
7504 , &[_][]const u8{
7505 ":3:52: error: slice '[]const u8' cannot have its bytes reinterpreted",
7506 });
7498}7507}