From 59b3dc8907f76b93caa689732e878a5bfa2f65c2 Mon Sep 17 00:00:00 2001 From: Andrew Kelley Date: Wed, 13 Jun 2018 22:40:38 -0400 Subject: allow passing by non-copying value closes #733 --- src/analyze.cpp | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) (limited to 'src/analyze.cpp') diff --git a/src/analyze.cpp b/src/analyze.cpp index cbeac7bc21..758bc1a045 100644 --- a/src/analyze.cpp +++ b/src/analyze.cpp @@ -1135,7 +1135,10 @@ TypeTableEntry *get_fn_type(CodeGen *g, FnTypeId *fn_type_id) { gen_param_info->src_index = i; gen_param_info->gen_index = SIZE_MAX; - type_ensure_zero_bits_known(g, type_entry); + ensure_complete_type(g, type_entry); + if (type_is_invalid(type_entry)) + return g->builtin_types.entry_invalid; + if (type_has_bits(type_entry)) { TypeTableEntry *gen_type; if (handle_is_ptr(type_entry)) { @@ -1546,12 +1549,6 @@ static TypeTableEntry *analyze_fn_type(CodeGen *g, AstNode *proto_node, Scope *c case TypeTableEntryIdUnion: case TypeTableEntryIdFn: case TypeTableEntryIdPromise: - ensure_complete_type(g, type_entry); - if (calling_convention_allows_zig_types(fn_type_id.cc) && !type_is_copyable(g, type_entry)) { - add_node_error(g, param_node->data.param_decl.type, - buf_sprintf("type '%s' is not copyable; cannot pass by value", buf_ptr(&type_entry->name))); - return g->builtin_types.entry_invalid; - } break; } FnTypeParamInfo *param_info = &fn_type_id.param_info[fn_type_id.next_param_index]; -- cgit v1.2.3 From 8fd7cc11e167c0b23892d6f22841bb6856d0f499 Mon Sep 17 00:00:00 2001 From: Andrew Kelley Date: Mon, 18 Jun 2018 11:12:15 -0400 Subject: disallow opaque as a return type of fn type syntax closes #1115 --- src/analyze.cpp | 1 + src/ir.cpp | 5 +++++ test/compile_errors.zig | 10 ++++++++++ 3 files changed, 16 insertions(+) (limited to 'src/analyze.cpp') diff --git a/src/analyze.cpp b/src/analyze.cpp index 758bc1a045..10cdb0af6f 100644 --- a/src/analyze.cpp +++ b/src/analyze.cpp @@ -1022,6 +1022,7 @@ TypeTableEntry *get_fn_type(CodeGen *g, FnTypeId *fn_type_id) { ensure_complete_type(g, fn_type_id->return_type); if (type_is_invalid(fn_type_id->return_type)) return g->builtin_types.entry_invalid; + assert(fn_type_id->return_type->id != TypeTableEntryIdOpaque); } else { zig_panic("TODO implement inferred return types https://github.com/ziglang/zig/issues/447"); } diff --git a/src/ir.cpp b/src/ir.cpp index a312b501ab..c75a3ae7c1 100644 --- a/src/ir.cpp +++ b/src/ir.cpp @@ -18676,6 +18676,11 @@ static TypeTableEntry *ir_analyze_instruction_fn_proto(IrAnalyze *ira, IrInstruc fn_type_id.return_type = ir_resolve_type(ira, return_type_value); if (type_is_invalid(fn_type_id.return_type)) return ira->codegen->builtin_types.entry_invalid; + if (fn_type_id.return_type->id == TypeTableEntryIdOpaque) { + ir_add_error(ira, instruction->return_type, + buf_sprintf("return type cannot be opaque")); + return ira->codegen->builtin_types.entry_invalid; + } if (fn_type_id.cc == CallingConventionAsync) { if (instruction->async_allocator_type_value == nullptr) { diff --git a/test/compile_errors.zig b/test/compile_errors.zig index 23337ca479..8c5abaaccc 100644 --- a/test/compile_errors.zig +++ b/test/compile_errors.zig @@ -1,6 +1,15 @@ const tests = @import("tests.zig"); pub fn addCases(cases: *tests.CompileErrorContext) void { + cases.add( + "use c_void as return type of fn ptr", + \\export fn entry() void { + \\ const a: fn () c_void = undefined; + \\} + , + ".tmp_source.zig:2:20: error: return type cannot be opaque", + ); + cases.add( "non int passed to @intToFloat", \\export fn entry() void { @@ -9,6 +18,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void { , ".tmp_source.zig:2:32: error: expected int type, found 'comptime_float'", ); + cases.add( "use implicit casts to assign null to non-nullable pointer", \\export fn entry() void { -- cgit v1.2.3