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) {...@@ -2893,7 +2893,7 @@ static Error resolve_struct_zero_bits(CodeGen *g, ZigType *struct_type) {
2893 return ErrorSemanticAnalyzeFail;2893 return ErrorSemanticAnalyzeFail;
2894 }2894 }
2895 if (field_is_opaque_type) {2895 if (field_is_opaque_type) {
2896 add_node_error(g, field_node->data.struct_field.type,2896 add_node_error(g, field_node,
2897 buf_sprintf("opaque types have unknown size and therefore cannot be directly embedded in structs"));2897 buf_sprintf("opaque types have unknown size and therefore cannot be directly embedded in structs"));
2898 struct_type->data.structure.resolve_status = ResolveStatusInvalid;2898 struct_type->data.structure.resolve_status = ResolveStatusInvalid;
2899 return ErrorSemanticAnalyzeFail;2899 return ErrorSemanticAnalyzeFail;
...@@ -3185,7 +3185,7 @@ static Error resolve_union_zero_bits(CodeGen *g, ZigType *union_type) {...@@ -3185,7 +3185,7 @@ static Error resolve_union_zero_bits(CodeGen *g, ZigType *union_type) {
3185 return ErrorSemanticAnalyzeFail;3185 return ErrorSemanticAnalyzeFail;
3186 }3186 }
3187 if (field_is_opaque_type) {3187 if (field_is_opaque_type) {
3188 add_node_error(g, field_node->data.struct_field.type,3188 add_node_error(g, field_node,
3189 buf_create_from_str(3189 buf_create_from_str(
3190 "opaque types have unknown size and therefore cannot be directly embedded in unions"));3190 "opaque types have unknown size and therefore cannot be directly embedded in unions"));
3191 union_type->data.unionation.resolve_status = ResolveStatusInvalid;3191 union_type->data.unionation.resolve_status = ResolveStatusInvalid;
test/compile_errors.zig+7-2
...@@ -1610,9 +1610,14 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {...@@ -1610,9 +1610,14 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
1610 \\export fn b() void {1610 \\export fn b() void {
1611 \\ var bar: Bar = undefined;1611 \\ var bar: Bar = undefined;
1612 \\}1612 \\}
1613 \\export fn c() void {
1614 \\ var baz: *@OpaqueType() = undefined;
1615 \\ const qux = .{baz.*};
1616 \\}
1613 , &[_][]const u8{1617 , &[_][]const u8{
1614 "tmp.zig:3:8: error: opaque types have unknown size and therefore cannot be directly embedded in structs",1618 "tmp.zig:3:5: 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",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",
1616 });1621 });
16171622
1618 cases.add("implicit cast between C pointer and Zig pointer - bad const/align/child",1623 cases.add("implicit cast between C pointer and Zig pointer - bad const/align/child",