Skip to content

Commit 0d40cb6

Browse files
xackusandrewrk
authored andcommitted
stage1: fix crash on slice byte reinterpretation
1 parent c405844 commit 0d40cb6

File tree

2 files changed

+27
-5
lines changed

2 files changed

+27
-5
lines changed

src/ir.cpp

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29043,11 +29043,24 @@ static Error buf_read_value_bytes(IrAnalyze *ira, CodeGen *codegen, AstNode *sou
2904329043
case ZigTypeIdStruct:
2904429044
switch (val->type->data.structure.layout) {
2904529045
case ContainerLayoutAuto: {
29046-
ErrorMsg *msg = opt_ir_add_error_node(ira, codegen, source_node,
29047-
buf_sprintf("non-extern, non-packed struct '%s' cannot have its bytes reinterpreted",
29048-
buf_ptr(&val->type->name)));
29049-
add_error_note(codegen, msg, val->type->data.structure.decl_node,
29050-
buf_sprintf("declared here"));
29046+
switch(val->type->data.structure.special){
29047+
case StructSpecialNone:
29048+
case StructSpecialInferredTuple:
29049+
case StructSpecialInferredStruct: {
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+
}
2905129064
return ErrorSemanticAnalyzeFail;
2905229065
}
2905329066
case ContainerLayoutExtern: {

test/compile_errors.zig

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7495,4 +7495,13 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
74957495
":22:12: error: cannot compare types '?[3]i32' and '[3]i32'",
74967496
":22:12: note: operator not supported for type '[3]i32'",
74977497
});
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+
});
74987507
}

0 commit comments

Comments
 (0)