authorgravatar for thatlemon@gmail.comLemonBoy <thatlemon@gmail.com> 2020-03-08 19:48:35+01:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2020-03-08 18:12:50-04:00
log06d0dac0fb58c2d20036e34f5c1a1bc9c386c189
treebe7a91b228dc4f385a9bce111857f905d7506af8
parentf90fe1f8f2be6ffa1c19997d123e12310d9e04b5

ir: Prevent crash in compiler error

Anonymous containers have no struct_field->type AstNode set, let's always use the field node itself to make the error messages consistent. Closes #4691

2 files changed, 9 insertions(+), 4 deletions(-)

src/analyze.cpp+2-2
......@@ -2893,7 +2893,7 @@ static Error resolve_struct_zero_bits(CodeGen *g, ZigType *struct_type) {
28932893 return ErrorSemanticAnalyzeFail;
28942894 }
28952895 if (field_is_opaque_type) {
2896 add_node_error(g, field_node->data.struct_field.type,
2896 add_node_error(g, field_node,
28972897 buf_sprintf("opaque types have unknown size and therefore cannot be directly embedded in structs"));
28982898 struct_type->data.structure.resolve_status = ResolveStatusInvalid;
28992899 return ErrorSemanticAnalyzeFail;
......@@ -3185,7 +3185,7 @@ static Error resolve_union_zero_bits(CodeGen *g, ZigType *union_type) {
31853185 return ErrorSemanticAnalyzeFail;
31863186 }
31873187 if (field_is_opaque_type) {
3188 add_node_error(g, field_node->data.struct_field.type,
3188 add_node_error(g, field_node,
31893189 buf_create_from_str(
31903190 "opaque types have unknown size and therefore cannot be directly embedded in unions"));
31913191 union_type->data.unionation.resolve_status = ResolveStatusInvalid;
test/compile_errors.zig+7-2
......@@ -1610,9 +1610,14 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
16101610 \\export fn b() void {
16111611 \\ var bar: Bar = undefined;
16121612 \\}
1613 \\export fn c() void {
1614 \\ var baz: *@OpaqueType() = undefined;
1615 \\ const qux = .{baz.*};
1616 \\}
16131617 , &[_][]const u8{
1614 "tmp.zig:3:8: error: opaque types have unknown size and therefore cannot be directly embedded in structs",
1615 "tmp.zig:7:10: error: opaque types have unknown size and therefore cannot be directly embedded in unions",
1618 "tmp.zig:3:5: error: opaque types have unknown size and therefore cannot be directly embedded in structs",
1619 "tmp.zig:7:5: error: opaque types have unknown size and therefore cannot be directly embedded in unions",
1620 "tmp.zig:17:22: error: opaque types have unknown size and therefore cannot be directly embedded in structs",
16161621 });
16171622
16181623 cases.add("implicit cast between C pointer and Zig pointer - bad const/align/child",