From 77a5f888be664f9ef09e2c93f52338448e992e00 Mon Sep 17 00:00:00 2001 From: Andrew Kelley Date: Tue, 3 Sep 2019 22:09:47 -0400 Subject: [PATCH] emit a compile error if a test becomes async See #3117 --- src/analyze.cpp | 2 +- src/analyze.hpp | 2 ++ src/codegen.cpp | 10 ++++++++++ 3 files changed, 13 insertions(+), 1 deletion(-) diff --git a/src/analyze.cpp b/src/analyze.cpp index 2fd540a64f..e1d6b59ddf 100644 --- a/src/analyze.cpp +++ b/src/analyze.cpp @@ -4195,7 +4195,7 @@ bool fn_is_async(ZigFn *fn) { return fn->inferred_async_node != inferred_async_none; } -static void add_async_error_notes(CodeGen *g, ErrorMsg *msg, ZigFn *fn) { +void add_async_error_notes(CodeGen *g, ErrorMsg *msg, ZigFn *fn) { assert(fn->inferred_async_node != nullptr); assert(fn->inferred_async_node != inferred_async_checking); assert(fn->inferred_async_node != inferred_async_none); diff --git a/src/analyze.hpp b/src/analyze.hpp index dc702167e7..9f2c984992 100644 --- a/src/analyze.hpp +++ b/src/analyze.hpp @@ -256,4 +256,6 @@ Error type_val_resolve_zero_bits(CodeGen *g, ConstExprValue *type_val, ZigType * ZigType *resolve_union_field_type(CodeGen *g, TypeUnionField *union_field); ZigType *resolve_struct_field_type(CodeGen *g, TypeStructField *struct_field); +void add_async_error_notes(CodeGen *g, ErrorMsg *msg, ZigFn *fn); + #endif diff --git a/src/codegen.cpp b/src/codegen.cpp index 1710a3b3fd..d1499592d2 100644 --- a/src/codegen.cpp +++ b/src/codegen.cpp @@ -8905,6 +8905,15 @@ static void create_test_compile_var_and_add_test_runner(CodeGen *g) { for (size_t i = 0; i < g->test_fns.length; i += 1) { ZigFn *test_fn_entry = g->test_fns.at(i); + if (fn_is_async(test_fn_entry)) { + ErrorMsg *msg = add_node_error(g, test_fn_entry->proto_node, + buf_create_from_str("test functions cannot be async")); + add_error_note(g, msg, test_fn_entry->proto_node, + buf_sprintf("this restriction may be lifted in the future. See https://github.com/ziglang/zig/issues/3117 for more details")); + add_async_error_notes(g, msg, test_fn_entry); + continue; + } + ConstExprValue *this_val = &test_fn_array->data.x_array.data.s_none.elements[i]; this_val->special = ConstValSpecialStatic; this_val->type = struct_type; @@ -8924,6 +8933,7 @@ static void create_test_compile_var_and_add_test_runner(CodeGen *g) { fn_field->data.x_ptr.mut = ConstPtrMutComptimeConst; fn_field->data.x_ptr.data.fn.fn_entry = test_fn_entry; } + report_errors_and_maybe_exit(g); ConstExprValue *test_fn_slice = create_const_slice(g, test_fn_array, 0, g->test_fns.length, true);