From 0d40cb625564ca83f1b3d15202e02af656710c4a Mon Sep 17 00:00:00 2001 From: xackus <14938807+xackus@users.noreply.github.com> Date: Mon, 8 Jun 2020 19:06:37 +0200 Subject: [PATCH] stage1: fix crash on slice byte reinterpretation --- src/ir.cpp | 23 ++++++++++++++++++----- test/compile_errors.zig | 9 +++++++++ 2 files changed, 27 insertions(+), 5 deletions(-) diff --git a/src/ir.cpp b/src/ir.cpp index 54442861c..3583f6ff0 100644 --- a/src/ir.cpp +++ b/src/ir.cpp @@ -29043,11 +29043,24 @@ static Error buf_read_value_bytes(IrAnalyze *ira, CodeGen *codegen, AstNode *sou case ZigTypeIdStruct: switch (val->type->data.structure.layout) { case ContainerLayoutAuto: { - ErrorMsg *msg = opt_ir_add_error_node(ira, codegen, source_node, - buf_sprintf("non-extern, non-packed struct '%s' cannot have its bytes reinterpreted", - buf_ptr(&val->type->name))); - add_error_note(codegen, msg, val->type->data.structure.decl_node, - buf_sprintf("declared here")); + switch(val->type->data.structure.special){ + case StructSpecialNone: + case StructSpecialInferredTuple: + case StructSpecialInferredStruct: { + ErrorMsg *msg = opt_ir_add_error_node(ira, codegen, source_node, + buf_sprintf("non-extern, non-packed struct '%s' cannot have its bytes reinterpreted", + buf_ptr(&val->type->name))); + add_error_note(codegen, msg, val->type->data.structure.decl_node, + buf_sprintf("declared here")); + break; + } + case StructSpecialSlice: { + opt_ir_add_error_node(ira, codegen, source_node, + buf_sprintf("slice '%s' cannot have its bytes reinterpreted", + buf_ptr(&val->type->name))); + break; + } + } return ErrorSemanticAnalyzeFail; } case ContainerLayoutExtern: { diff --git a/test/compile_errors.zig b/test/compile_errors.zig index 7fbe3f284..17a9e0663 100644 --- a/test/compile_errors.zig +++ b/test/compile_errors.zig @@ -7495,4 +7495,13 @@ pub fn addCases(cases: *tests.CompileErrorContext) void { ":22:12: error: cannot compare types '?[3]i32' and '[3]i32'", ":22:12: note: operator not supported for type '[3]i32'", }); + + cases.add("slice cannot have its bytes reinterpreted", + \\export fn foo() void { + \\ const bytes = [1]u8{ 0xfa } ** 16; + \\ var value = @ptrCast(*const []const u8, &bytes).*; + \\} + , &[_][]const u8{ + ":3:52: error: slice '[]const u8' cannot have its bytes reinterpreted", + }); }