stage1: disallow fields in opaque types

This commit is contained in:
Tadeo Kondrak 2020-09-25 17:14:13 -06:00
parent d5b8172a82
commit d71f339395
No known key found for this signature in database
GPG Key ID: D41E092CA43F1D8B
2 changed files with 22 additions and 4 deletions

View File

@ -3457,10 +3457,18 @@ static Error resolve_union_zero_bits(CodeGen *g, ZigType *union_type) {
}
static Error resolve_opaque_type(CodeGen *g, ZigType *opaque_type) {
opaque_type->abi_align = UINT32_MAX;
opaque_type->abi_size = SIZE_MAX;
opaque_type->size_in_bits = SIZE_MAX;
return ErrorNone;
Error err = ErrorNone;
AstNode *container_node = opaque_type->data.opaque.decl_node;
if (container_node != nullptr) {
assert(container_node->type == NodeTypeContainerDecl);
AstNodeContainerDecl *container_decl = &container_node->data.container_decl;
for (int i = 0; i < container_decl->fields.length; i++) {
AstNode *field_node = container_decl->fields.items[i];
add_node_error(g, field_node, buf_create_from_str("opaque types cannot have fields"));
err = ErrorSemanticAnalyzeFail;
}
}
return err;
}
void append_namespace_qualification(CodeGen *g, Buf *buf, ZigType *container_type) {

View File

@ -125,6 +125,16 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
"tmp.zig:15:23: error: enum field missing: 'arst'",
"tmp.zig:27:24: note: referenced here",
});
cases.add("opaque type with field",
\\const Opaque = opaque { foo: i32 };
\\export fn entry() void {
\\ const foo: ?*Opaque = null;
\\}
, &[_][]const u8{
"tmp.zig:1:25: error: opaque types cannot have fields",
});
cases.add("@Type(.Fn) with is_generic = true",
\\const Foo = @Type(.{
\\ .Fn = .{