stage1: fix crash on slice byte reinterpretation

master
xackus 2020-06-08 19:06:37 +02:00 committed by Andrew Kelley
parent c405844b0a
commit 0d40cb6255
2 changed files with 27 additions and 5 deletions

View File

@ -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: {

View File

@ -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",
});
}