From d71f339395f8a3a3c09bbb2d51e6f35cb5295d14 Mon Sep 17 00:00:00 2001 From: Tadeo Kondrak Date: Fri, 25 Sep 2020 17:14:13 -0600 Subject: [PATCH] stage1: disallow fields in opaque types --- src/stage1/analyze.cpp | 16 ++++++++++++---- test/compile_errors.zig | 10 ++++++++++ 2 files changed, 22 insertions(+), 4 deletions(-) diff --git a/src/stage1/analyze.cpp b/src/stage1/analyze.cpp index 6008496fe2..b22f0ef393 100644 --- a/src/stage1/analyze.cpp +++ b/src/stage1/analyze.cpp @@ -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) { diff --git a/test/compile_errors.zig b/test/compile_errors.zig index f19e3c44c1..6c2e96fc5e 100644 --- a/test/compile_errors.zig +++ b/test/compile_errors.zig @@ -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 = .{