authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-05-29 16:31:49-04:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-05-29 16:31:49-04:00
log1ccbd1fb67898c0691c74e65a7b9786fb5698619
tree7e4f95bffdf56633dcb47b98866b0f8804e134b1
parent9891c4f30ddfb2692ecdcc9061d0b45c9d335f6c
signature Commit is signed but in an unrecognized format.

`use` works on unions and enums in addition to structs


4 files changed, 16 insertions(+), 20 deletions(-)

src/analyze.cpp+12-11
......@@ -3453,11 +3453,12 @@ TypeEnumField *find_enum_field_by_tag(ZigType *enum_type, const BigInt *tag) {
34533453}
34543454
34553455
3456static bool is_container(ZigType *type_entry) {
3456bool is_container(ZigType *type_entry) {
34573457 switch (type_entry->id) {
34583458 case ZigTypeIdInvalid:
34593459 zig_unreachable();
34603460 case ZigTypeIdStruct:
3461 return !type_entry->data.structure.is_slice;
34613462 case ZigTypeIdEnum:
34623463 case ZigTypeIdUnion:
34633464 return true;
......@@ -3498,9 +3499,9 @@ bool is_array_ref(ZigType *type_entry) {
34983499 return array->id == ZigTypeIdArray;
34993500}
35003501
3501bool is_container_ref(ZigType *type_entry) {
3502 return is_ref(type_entry) ?
3503 is_container(type_entry->data.pointer.child_type) : is_container(type_entry);
3502bool is_container_ref(ZigType *parent_ty) {
3503 ZigType *ty = is_ref(parent_ty) ? parent_ty->data.pointer.child_type : parent_ty;
3504 return is_slice(ty) || is_container(ty);
35043505}
35053506
35063507ZigType *container_ref_type(ZigType *type_entry) {
......@@ -3765,7 +3766,7 @@ static void analyze_fn_body(CodeGen *g, ZigFn *fn_table_entry) {
37653766 analyze_fn_ir(g, fn_table_entry, return_type_node);
37663767}
37673768
3768static void add_symbols_from_struct(CodeGen *g, AstNode *src_use_node, AstNode *dst_use_node, ScopeDecls* decls_scope) {
3769static void add_symbols_from_container(CodeGen *g, AstNode *src_use_node, AstNode *dst_use_node, ScopeDecls* decls_scope) {
37693770 if (src_use_node->data.use.resolution == TldResolutionUnresolved) {
37703771 preview_use_decl(g, src_use_node, decls_scope);
37713772 }
......@@ -3784,9 +3785,9 @@ static void add_symbols_from_struct(CodeGen *g, AstNode *src_use_node, AstNode *
37843785 ZigType *src_ty = use_expr->data.x_type;
37853786 assert(src_ty);
37863787
3787 if (src_ty->id != ZigTypeIdStruct || is_slice(src_ty)) {
3788 if (!is_container(src_ty)) {
37883789 add_node_error(g, dst_use_node,
3789 buf_sprintf("expected struct, found '%s'", buf_ptr(&src_ty->name)));
3790 buf_sprintf("expected struct, enum, or union; found '%s'", buf_ptr(&src_ty->name)));
37903791 decls_scope->any_imports_failed = true;
37913792 return;
37923793 }
......@@ -3795,8 +3796,8 @@ static void add_symbols_from_struct(CodeGen *g, AstNode *src_use_node, AstNode *
37953796 ScopeDecls *src_scope = get_container_scope(src_ty);
37963797 // The top-level container where the symbols are defined, it's used in the
37973798 // loop below in order to exclude the ones coming from an import statement
3798 ZigType *src_import = get_scope_import(reinterpret_cast<Scope*>(src_scope));
3799 assert(src_import && src_import->id == ZigTypeIdStruct);
3799 ZigType *src_import = get_scope_import(&src_scope->base);
3800 assert(src_import != nullptr);
38003801
38013802 if (src_scope->any_imports_failed) {
38023803 decls_scope->any_imports_failed = true;
......@@ -3835,7 +3836,7 @@ static void add_symbols_from_struct(CodeGen *g, AstNode *src_use_node, AstNode *
38353836 for (size_t i = 0; i < src_scope->use_decls.length; i += 1) {
38363837 AstNode *use_decl_node = src_scope->use_decls.at(i);
38373838 if (use_decl_node->data.use.visib_mod != VisibModPrivate)
3838 add_symbols_from_struct(g, use_decl_node, dst_use_node, decls_scope);
3839 add_symbols_from_container(g, use_decl_node, dst_use_node, decls_scope);
38393840 }
38403841}
38413842
......@@ -3847,7 +3848,7 @@ void resolve_use_decl(CodeGen *g, AstNode *node, ScopeDecls *decls_scope) {
38473848 {
38483849 return;
38493850 }
3850 add_symbols_from_struct(g, node, node, decls_scope);
3851 add_symbols_from_container(g, node, node, decls_scope);
38513852}
38523853
38533854void preview_use_decl(CodeGen *g, AstNode *node, ScopeDecls *decls_scope) {
src/analyze.hpp+1
......@@ -250,5 +250,6 @@ ZigLLVMDIType *get_llvm_di_type(CodeGen *g, ZigType *type);
250250void add_cc_args(CodeGen *g, ZigList<const char *> &args, const char *out_dep_path, bool translate_c);
251251
252252void src_assert(bool ok, AstNode *source_node);
253bool is_container(ZigType *type_entry);
253254
254255#endif
src/ir.cpp+2-8
......@@ -10266,12 +10266,6 @@ static IrInstruction *ir_resolve_ptr_of_array_to_slice(IrAnalyze *ira, IrInstruc
1026610266 return result;
1026710267}
1026810268
10269static bool is_container(ZigType *type) {
10270 return type->id == ZigTypeIdStruct ||
10271 type->id == ZigTypeIdEnum ||
10272 type->id == ZigTypeIdUnion;
10273}
10274
1027510269static IrBasicBlock *ir_get_new_bb(IrAnalyze *ira, IrBasicBlock *old_bb, IrInstruction *ref_old_instruction) {
1027610270 assert(old_bb);
1027710271
......@@ -16179,7 +16173,7 @@ static IrInstruction *ir_analyze_instruction_field_ptr(IrAnalyze *ira, IrInstruc
1617916173
1618016174 if (type_is_invalid(container_type)) {
1618116175 return ira->codegen->invalid_instruction;
16182 } else if (is_container_ref(container_type)) {
16176 } else if (is_slice(container_type) || is_container_ref(container_type)) {
1618316177 assert(container_ptr->value.type->id == ZigTypeIdPointer);
1618416178 if (container_type->id == ZigTypeIdPointer) {
1618516179 ZigType *bare_type = container_ref_type(container_type);
......@@ -16249,7 +16243,7 @@ static IrInstruction *ir_analyze_instruction_field_ptr(IrAnalyze *ira, IrInstruc
1624916243
1625016244 if (type_is_invalid(child_type)) {
1625116245 return ira->codegen->invalid_instruction;
16252 } else if (is_container(child_type) && !is_slice(child_type)) {
16246 } else if (is_container(child_type)) {
1625316247 if (child_type->id == ZigTypeIdEnum) {
1625416248 if ((err = ensure_complete_type(ira->codegen, child_type)))
1625516249 return ira->codegen->invalid_instruction;
test/compile_errors.zig+1-1
......@@ -163,7 +163,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
163163 "usingnamespace with wrong type",
164164 \\use void;
165165 ,
166 "tmp.zig:1:1: error: expected struct, found 'void'",
166 "tmp.zig:1:1: error: expected struct, enum, or union; found 'void'",
167167 );
168168
169169 cases.add(