Skip to content

Commit

Permalink
disallow opaque as a return type of fn type syntax
Browse files Browse the repository at this point in the history
closes #1115
  • Loading branch information
andrewrk committed Jun 18, 2018
1 parent d49d6f0 commit 8fd7cc1
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/analyze.cpp
Expand Up @@ -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");
}
Expand Down
5 changes: 5 additions & 0 deletions src/ir.cpp
Expand Up @@ -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) {
Expand Down
10 changes: 10 additions & 0 deletions 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 {
Expand All @@ -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 {
Expand Down

0 comments on commit 8fd7cc1

Please sign in to comment.