Commit 8fd7cc11e1

Andrew Kelley <superjoe30@gmail.com>
2018-06-18 17:12:15
disallow opaque as a return type of fn type syntax
closes #1115
1 parent d49d6f0
Changed files (3)
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");
     }
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) {
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 {