authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2017-03-16 16:02:35-04:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2017-03-16 16:02:35-04:00
logaf536ac343564e5120f99cbf3b7fc9efa984eb93
tree42a937bca75d34e2d3b4fc7d44ebafa66d8bd55b
parent329457bb4f714a8392153dfecfabd6f356144688

introduce new test syntax

* remove setFnTest builtin * add test "name" { ... } syntax * remove --check-unused argument. functions are always lazy now.

58 files changed, 617 insertions(+), 880 deletions(-)

doc/langref.md+3-1
......@@ -5,7 +5,9 @@
55```
66Root = many(TopLevelItem) "EOF"
77
8TopLevelItem = ErrorValueDecl | Block | TopLevelDecl
8TopLevelItem = ErrorValueDecl | Block | TopLevelDecl | TestDecl
9
10TestDecl = "test" String Block
911
1012TopLevelDecl = option(VisibleMod) (FnDef | ExternDecl | GlobalVarDecl | TypeDecl | UseDecl)
1113
doc/vim/syntax/zig.vim+1-1
......@@ -15,7 +15,7 @@ syn keyword zigConditional if else switch try
1515syn keyword zigRepeat while for
1616
1717syn keyword zigConstant null undefined this
18syn keyword zigKeyword fn use
18syn keyword zigKeyword fn use test
1919syn keyword zigType bool f32 f64 void Unreachable type error
2020syn keyword zigType i8 u8 i16 u16 i32 u32 i64 u64 isize usize
2121syn keyword zigType c_short c_ushort c_int c_uint c_long c_ulong c_longlong c_ulonglong c_long_double
src/all_types.hpp+12-10
......@@ -311,6 +311,7 @@ enum NodeType {
311311 NodeTypeVariableDeclaration,
312312 NodeTypeTypeDecl,
313313 NodeTypeErrorValueDecl,
314 NodeTypeTestDecl,
314315 NodeTypeBinOpExpr,
315316 NodeTypeUnwrapErrorExpr,
316317 NodeTypeNumberLiteral,
......@@ -435,6 +436,14 @@ struct AstNodeErrorValueDecl {
435436 ErrorTableEntry *err;
436437};
437438
439struct AstNodeTestDecl {
440 // always invalid if it's not VisibModPrivate but can be parsed that way
441 VisibMod visib_mod;
442 Buf *name;
443
444 AstNode *body;
445};
446
438447enum BinOpType {
439448 BinOpTypeInvalid,
440449 BinOpTypeAssign,
......@@ -781,6 +790,7 @@ struct AstNode {
781790 AstNodeVariableDeclaration variable_declaration;
782791 AstNodeTypeDecl type_decl;
783792 AstNodeErrorValueDecl error_value_decl;
793 AstNodeTestDecl test_decl;
784794 AstNodeBinOpExpr bin_op_expr;
785795 AstNodeUnwrapErrorExpr unwrap_err_expr;
786796 AstNodePrefixOpExpr prefix_op_expr;
......@@ -1091,7 +1101,7 @@ enum FnInline {
10911101struct FnTableEntry {
10921102 LLVMValueRef llvm_value;
10931103 AstNode *proto_node;
1094 AstNode *fn_def_node;
1104 AstNode *body_node;
10951105 ScopeFnDef *fndef_scope; // parent should be the top level decls or container decls
10961106 Scope *child_scope; // parent is scope for last parameter
10971107 ScopeBlock *def_scope; // parent is child_scope
......@@ -1161,7 +1171,6 @@ enum BuiltinFnId {
11611171 BuiltinFnIdTruncate,
11621172 BuiltinFnIdIntType,
11631173 BuiltinFnIdUnreachable,
1164 BuiltinFnIdSetFnTest,
11651174 BuiltinFnIdSetFnVisible,
11661175 BuiltinFnIdSetDebugSafety,
11671176 BuiltinFnIdAlloca,
......@@ -1411,8 +1420,8 @@ struct CodeGen {
14111420 ZigList<const char *> lib_dirs;
14121421
14131422 uint32_t test_fn_count;
1423 TypeTableEntry *test_fn_type;
14141424
1415 bool check_unused;
14161425 bool each_lib_rpath;
14171426
14181427 ZigList<AstNode *> error_decls;
......@@ -1630,7 +1639,6 @@ enum IrInstructionId {
16301639 IrInstructionIdTypeOf,
16311640 IrInstructionIdToPtrType,
16321641 IrInstructionIdPtrTypeChild,
1633 IrInstructionIdSetFnTest,
16341642 IrInstructionIdSetFnVisible,
16351643 IrInstructionIdSetDebugSafety,
16361644 IrInstructionIdArrayType,
......@@ -1973,12 +1981,6 @@ struct IrInstructionPtrTypeChild {
19731981 IrInstruction *value;
19741982};
19751983
1976struct IrInstructionSetFnTest {
1977 IrInstruction base;
1978
1979 IrInstruction *fn_value;
1980};
1981
19821984struct IrInstructionSetFnVisible {
19831985 IrInstruction base;
19841986
src/analyze.cpp+106-50
......@@ -130,7 +130,6 @@ Scope *create_loop_scope(AstNode *node, Scope *parent) {
130130}
131131
132132ScopeFnDef *create_fndef_scope(AstNode *node, Scope *parent, FnTableEntry *fn_entry) {
133 assert(!node || node->type == NodeTypeFnDef);
134133 ScopeFnDef *scope = allocate<ScopeFnDef>(1);
135134 init_scope(&scope->base, ScopeIdFnDef, node, parent);
136135 scope->fn_entry = fn_entry;
......@@ -1756,7 +1755,8 @@ FnTableEntry *create_fn(AstNode *proto_node) {
17561755 FnTableEntry *fn_entry = create_fn_raw(inline_value, internal_linkage);
17571756
17581757 fn_entry->proto_node = proto_node;
1759 fn_entry->fn_def_node = proto_node->data.fn_proto.fn_def_node;
1758 fn_entry->body_node = (proto_node->data.fn_proto.fn_def_node == nullptr) ? nullptr :
1759 proto_node->data.fn_proto.fn_def_node->data.fn_def.body;
17601760
17611761 return fn_entry;
17621762}
......@@ -1799,76 +1799,107 @@ static void typecheck_panic_fn(CodeGen *g) {
17991799 }
18001800}
18011801
1802static TypeTableEntry *get_test_fn_type(CodeGen *g) {
1803 if (g->test_fn_type)
1804 return g->test_fn_type;
1805
1806 FnTypeId fn_type_id = {0};
1807 fn_type_id.return_type = g->builtin_types.entry_void;
1808 g->test_fn_type = get_fn_type(g, &fn_type_id);
1809 return g->test_fn_type;
1810}
1811
18021812static void resolve_decl_fn(CodeGen *g, TldFn *tld_fn) {
18031813 ImportTableEntry *import = tld_fn->base.import;
1804 AstNode *proto_node = tld_fn->base.source_node;
1805 assert(proto_node->type == NodeTypeFnProto);
1806 AstNodeFnProto *fn_proto = &proto_node->data.fn_proto;
1814 AstNode *source_node = tld_fn->base.source_node;
1815 if (source_node->type == NodeTypeFnProto) {
1816 AstNodeFnProto *fn_proto = &source_node->data.fn_proto;
18071817
1808 AstNode *fn_def_node = fn_proto->fn_def_node;
1818 AstNode *fn_def_node = fn_proto->fn_def_node;
18091819
1810 FnTableEntry *fn_table_entry = create_fn(tld_fn->base.source_node);
1811 get_fully_qualified_decl_name(&fn_table_entry->symbol_name, &tld_fn->base, '_');
1820 FnTableEntry *fn_table_entry = create_fn(source_node);
1821 get_fully_qualified_decl_name(&fn_table_entry->symbol_name, &tld_fn->base, '_');
18121822
1813 tld_fn->fn_entry = fn_table_entry;
1823 tld_fn->fn_entry = fn_table_entry;
18141824
1815 if (fn_table_entry->fn_def_node) {
1816 fn_table_entry->fndef_scope = create_fndef_scope(
1817 fn_table_entry->fn_def_node, tld_fn->base.parent_scope, fn_table_entry);
1825 if (fn_table_entry->body_node) {
1826 fn_table_entry->fndef_scope = create_fndef_scope(
1827 fn_table_entry->body_node, tld_fn->base.parent_scope, fn_table_entry);
18181828
1819 for (size_t i = 0; i < fn_proto->params.length; i += 1) {
1820 AstNode *param_node = fn_proto->params.at(i);
1821 assert(param_node->type == NodeTypeParamDecl);
1822 if (buf_len(param_node->data.param_decl.name) == 0) {
1823 add_node_error(g, param_node, buf_sprintf("missing parameter name"));
1829 for (size_t i = 0; i < fn_proto->params.length; i += 1) {
1830 AstNode *param_node = fn_proto->params.at(i);
1831 assert(param_node->type == NodeTypeParamDecl);
1832 if (buf_len(param_node->data.param_decl.name) == 0) {
1833 add_node_error(g, param_node, buf_sprintf("missing parameter name"));
1834 }
18241835 }
18251836 }
1826 }
18271837
1828 Scope *child_scope = fn_table_entry->fndef_scope ? &fn_table_entry->fndef_scope->base : tld_fn->base.parent_scope;
1829 fn_table_entry->type_entry = analyze_fn_type(g, proto_node, child_scope);
1838 Scope *child_scope = fn_table_entry->fndef_scope ? &fn_table_entry->fndef_scope->base : tld_fn->base.parent_scope;
1839 fn_table_entry->type_entry = analyze_fn_type(g, source_node, child_scope);
18301840
1831 if (fn_table_entry->type_entry->id == TypeTableEntryIdInvalid) {
1832 tld_fn->base.resolution = TldResolutionInvalid;
1833 return;
1834 }
1841 if (fn_table_entry->type_entry->id == TypeTableEntryIdInvalid) {
1842 tld_fn->base.resolution = TldResolutionInvalid;
1843 return;
1844 }
18351845
1836 if (!fn_table_entry->type_entry->data.fn.is_generic) {
1837 g->fn_protos.append(fn_table_entry);
1846 if (!fn_table_entry->type_entry->data.fn.is_generic) {
1847 g->fn_protos.append(fn_table_entry);
18381848
1839 if (fn_def_node)
1840 g->fn_defs.append(fn_table_entry);
1849 if (fn_def_node)
1850 g->fn_defs.append(fn_table_entry);
18411851
1842 if (import == g->root_import && scope_is_root_decls(tld_fn->base.parent_scope)) {
1843 if (buf_eql_str(&fn_table_entry->symbol_name, "main")) {
1844 g->main_fn = fn_table_entry;
1852 if (import == g->root_import && scope_is_root_decls(tld_fn->base.parent_scope)) {
1853 if (buf_eql_str(&fn_table_entry->symbol_name, "main")) {
1854 g->main_fn = fn_table_entry;
18451855
1846 if (!g->link_libc && tld_fn->base.visib_mod != VisibModExport) {
1847 TypeTableEntry *err_void = get_error_type(g, g->builtin_types.entry_void);
1848 TypeTableEntry *actual_return_type = fn_table_entry->type_entry->data.fn.fn_type_id.return_type;
1849 if (actual_return_type != err_void) {
1850 add_node_error(g, fn_proto->return_type,
1851 buf_sprintf("expected return type of main to be '%%void', instead is '%s'",
1852 buf_ptr(&actual_return_type->name)));
1856 if (!g->link_libc && tld_fn->base.visib_mod != VisibModExport) {
1857 TypeTableEntry *err_void = get_error_type(g, g->builtin_types.entry_void);
1858 TypeTableEntry *actual_return_type = fn_table_entry->type_entry->data.fn.fn_type_id.return_type;
1859 if (actual_return_type != err_void) {
1860 add_node_error(g, fn_proto->return_type,
1861 buf_sprintf("expected return type of main to be '%%void', instead is '%s'",
1862 buf_ptr(&actual_return_type->name)));
1863 }
18531864 }
1865 } else if (buf_eql_str(&fn_table_entry->symbol_name, "panic")) {
1866 g->panic_fn = fn_table_entry;
1867 typecheck_panic_fn(g);
1868 }
1869 } else if (import->package == g->panic_package && scope_is_root_decls(tld_fn->base.parent_scope)) {
1870 if (buf_eql_str(&fn_table_entry->symbol_name, "panic")) {
1871 g->panic_fn = fn_table_entry;
1872 typecheck_panic_fn(g);
18541873 }
1855 } else if (buf_eql_str(&fn_table_entry->symbol_name, "panic")) {
1856 g->panic_fn = fn_table_entry;
1857 typecheck_panic_fn(g);
1858 }
1859 } else if (import->package == g->panic_package && scope_is_root_decls(tld_fn->base.parent_scope)) {
1860 if (buf_eql_str(&fn_table_entry->symbol_name, "panic")) {
1861 g->panic_fn = fn_table_entry;
1862 typecheck_panic_fn(g);
18631874 }
18641875 }
1876 } else if (source_node->type == NodeTypeTestDecl) {
1877 FnTableEntry *fn_table_entry = create_fn_raw(FnInlineAuto, false);
1878
1879 get_fully_qualified_decl_name(&fn_table_entry->symbol_name, &tld_fn->base, '_');
1880
1881 tld_fn->fn_entry = fn_table_entry;
1882
1883 fn_table_entry->proto_node = source_node;
1884 fn_table_entry->fndef_scope = create_fndef_scope(source_node, tld_fn->base.parent_scope, fn_table_entry);
1885 fn_table_entry->type_entry = get_test_fn_type(g);
1886 fn_table_entry->body_node = source_node->data.test_decl.body;
1887 fn_table_entry->is_test = true;
1888 g->test_fn_count += 1;
1889
1890 g->fn_protos.append(fn_table_entry);
1891 g->fn_defs.append(fn_table_entry);
1892
1893 } else {
1894 zig_unreachable();
18651895 }
18661896}
18671897
18681898static void add_top_level_decl(CodeGen *g, ScopeDecls *decls_scope, Tld *tld) {
1869 if (g->check_unused || g->is_test_build || tld->visib_mod == VisibModExport ||
1899 if (tld->visib_mod == VisibModExport ||
18701900 (buf_eql_str(tld->name, "panic") &&
1871 (decls_scope->import->package == g->panic_package || decls_scope->import == g->root_import)))
1901 (decls_scope->import->package == g->panic_package || decls_scope->import == g->root_import)) ||
1902 (tld->id == TldIdVar && g->is_test_build))
18721903 {
18731904 g->resolve_queue.append(tld);
18741905 }
......@@ -1882,6 +1913,27 @@ static void add_top_level_decl(CodeGen *g, ScopeDecls *decls_scope, Tld *tld) {
18821913 }
18831914}
18841915
1916static void preview_test_decl(CodeGen *g, AstNode *node, ScopeDecls *decls_scope) {
1917 assert(node->type == NodeTypeTestDecl);
1918
1919 if (node->data.test_decl.visib_mod != VisibModPrivate) {
1920 add_node_error(g, node, buf_sprintf("tests require no visibility modifier"));
1921 }
1922
1923 if (!g->is_test_build)
1924 return;
1925
1926 ImportTableEntry *import = get_scope_import(&decls_scope->base);
1927 if (import->package != g->root_package)
1928 return;
1929
1930 Buf *test_name = node->data.test_decl.name;
1931
1932 TldFn *tld_fn = allocate<TldFn>(1);
1933 init_tld(&tld_fn->base, TldIdFn, test_name, VisibModPrivate, node, &decls_scope->base);
1934 g->resolve_queue.append(&tld_fn->base);
1935}
1936
18851937static void preview_error_value_decl(CodeGen *g, AstNode *node) {
18861938 assert(node->type == NodeTypeErrorValueDecl);
18871939
......@@ -1975,6 +2027,9 @@ void scan_decls(CodeGen *g, ScopeDecls *decls_scope, AstNode *node) {
19752027 // error value declarations do not depend on other top level decls
19762028 preview_error_value_decl(g, node);
19772029 break;
2030 case NodeTypeTestDecl:
2031 preview_test_decl(g, node, decls_scope);
2032 break;
19782033 case NodeTypeContainerDecl:
19792034 case NodeTypeParamDecl:
19802035 case NodeTypeFnDecl:
......@@ -2650,7 +2705,8 @@ static void analyze_fn_body(CodeGen *g, FnTableEntry *fn_table_entry) {
26502705
26512706 fn_table_entry->anal_state = FnAnalStateProbing;
26522707
2653 AstNode *return_type_node = fn_table_entry->proto_node->data.fn_proto.return_type;
2708 AstNode *return_type_node = (fn_table_entry->proto_node != nullptr) ?
2709 fn_table_entry->proto_node->data.fn_proto.return_type : fn_table_entry->fndef_scope->base.source_node;
26542710
26552711 assert(fn_table_entry->fndef_scope);
26562712 if (!fn_table_entry->child_scope)
......@@ -2674,7 +2730,7 @@ static void analyze_fn_body(CodeGen *g, FnTableEntry *fn_table_entry) {
26742730 }
26752731 if (g->verbose) {
26762732 fprintf(stderr, "\n");
2677 ast_render(stderr, fn_table_entry->fn_def_node, 4);
2733 ast_render(stderr, fn_table_entry->body_node, 4);
26782734 fprintf(stderr, "\n{ // (IR)\n");
26792735 ir_print(stderr, &fn_table_entry->ir_executable, 4);
26802736 fprintf(stderr, "}\n");
src/ast_render.cpp+3
......@@ -170,6 +170,8 @@ static const char *node_type_str(NodeType node_type) {
170170 return "TypeDecl";
171171 case NodeTypeErrorValueDecl:
172172 return "ErrorValueDecl";
173 case NodeTypeTestDecl:
174 return "TestDecl";
173175 case NodeTypeNumberLiteral:
174176 return "NumberLiteral";
175177 case NodeTypeStringLiteral:
......@@ -915,6 +917,7 @@ static void render_node_extra(AstRender *ar, AstNode *node, bool grouped) {
915917 case NodeTypeFnDecl:
916918 case NodeTypeParamDecl:
917919 case NodeTypeErrorValueDecl:
920 case NodeTypeTestDecl:
918921 case NodeTypeStructField:
919922 case NodeTypeUse:
920923 zig_panic("TODO more ast rendering");
src/codegen.cpp+1-7
......@@ -138,10 +138,6 @@ void codegen_set_verbose(CodeGen *g, bool verbose) {
138138 g->verbose = verbose;
139139}
140140
141void codegen_set_check_unused(CodeGen *g, bool check_unused) {
142 g->check_unused = check_unused;
143}
144
145141void codegen_set_each_lib_rpath(CodeGen *g, bool each_lib_rpath) {
146142 g->each_lib_rpath = each_lib_rpath;
147143}
......@@ -323,7 +319,7 @@ static ZigLLVMDIScope *get_di_scope(CodeGen *g, Scope *scope) {
323319 return get_di_scope(g, scope->parent);
324320 unsigned line_number = fn_table_entry->proto_node->line + 1;
325321 unsigned scope_line = line_number;
326 bool is_definition = fn_table_entry->fn_def_node != nullptr;
322 bool is_definition = fn_table_entry->body_node != nullptr;
327323 unsigned flags = 0;
328324 bool is_optimized = g->is_release_build;
329325 ZigLLVMDISubprogram *subprogram = ZigLLVMCreateFunction(g->dbuilder,
......@@ -2492,7 +2488,6 @@ static LLVMValueRef ir_render_instruction(CodeGen *g, IrExecutable *executable,
24922488 case IrInstructionIdToPtrType:
24932489 case IrInstructionIdPtrTypeChild:
24942490 case IrInstructionIdFieldPtr:
2495 case IrInstructionIdSetFnTest:
24962491 case IrInstructionIdSetFnVisible:
24972492 case IrInstructionIdSetDebugSafety:
24982493 case IrInstructionIdArrayType:
......@@ -4054,7 +4049,6 @@ static void define_builtin_fns(CodeGen *g) {
40544049 create_builtin_fn(g, BuiltinFnIdCompileLog, "compileLog", SIZE_MAX);
40554050 create_builtin_fn(g, BuiltinFnIdIntType, "intType", 2);
40564051 create_builtin_fn(g, BuiltinFnIdUnreachable, "unreachable", 0);
4057 create_builtin_fn(g, BuiltinFnIdSetFnTest, "setFnTest", 1);
40584052 create_builtin_fn(g, BuiltinFnIdSetFnVisible, "setFnVisible", 2);
40594053 create_builtin_fn(g, BuiltinFnIdSetDebugSafety, "setDebugSafety", 2);
40604054 create_builtin_fn(g, BuiltinFnIdAlloca, "alloca", 2);
src/codegen.hpp-1
......@@ -19,7 +19,6 @@ CodeGen *codegen_create(Buf *root_source_dir, const ZigTarget *target);
1919void codegen_set_clang_argv(CodeGen *codegen, const char **args, size_t len);
2020void codegen_set_is_release(CodeGen *codegen, bool is_release);
2121void codegen_set_is_test(CodeGen *codegen, bool is_test);
22void codegen_set_check_unused(CodeGen *codegen, bool check_unused);
2322void codegen_set_each_lib_rpath(CodeGen *codegen, bool each_lib_rpath);
2423
2524void codegen_set_is_static(CodeGen *codegen, bool is_static);
src/ir.cpp+5-60
......@@ -282,10 +282,6 @@ static constexpr IrInstructionId ir_instruction_id(IrInstructionPtrTypeChild *)
282282 return IrInstructionIdPtrTypeChild;
283283}
284284
285static constexpr IrInstructionId ir_instruction_id(IrInstructionSetFnTest *) {
286 return IrInstructionIdSetFnTest;
287}
288
289285static constexpr IrInstructionId ir_instruction_id(IrInstructionSetFnVisible *) {
290286 return IrInstructionIdSetFnVisible;
291287}
......@@ -1147,17 +1143,6 @@ static IrInstruction *ir_build_ptr_type_child(IrBuilder *irb, Scope *scope, AstN
11471143 return &instruction->base;
11481144}
11491145
1150static IrInstruction *ir_build_set_fn_test(IrBuilder *irb, Scope *scope, AstNode *source_node,
1151 IrInstruction *fn_value)
1152{
1153 IrInstructionSetFnTest *instruction = ir_build_instruction<IrInstructionSetFnTest>(irb, scope, source_node);
1154 instruction->fn_value = fn_value;
1155
1156 ir_ref_instruction(fn_value, irb->current_basic_block);
1157
1158 return &instruction->base;
1159}
1160
11611146static IrInstruction *ir_build_set_fn_visible(IrBuilder *irb, Scope *scope, AstNode *source_node, IrInstruction *fn_value,
11621147 IrInstruction *is_visible)
11631148{
......@@ -2287,13 +2272,6 @@ static IrInstruction *ir_instruction_ptrtypechild_get_dep(IrInstructionPtrTypeCh
22872272 }
22882273}
22892274
2290static IrInstruction *ir_instruction_setfntest_get_dep(IrInstructionSetFnTest *instruction, size_t index) {
2291 switch (index) {
2292 case 0: return instruction->fn_value;
2293 default: return nullptr;
2294 }
2295}
2296
22972275static IrInstruction *ir_instruction_setfnvisible_get_dep(IrInstructionSetFnVisible *instruction, size_t index) {
22982276 switch (index) {
22992277 case 0: return instruction->fn_value;
......@@ -2807,8 +2785,6 @@ static IrInstruction *ir_instruction_get_dep(IrInstruction *instruction, size_t
28072785 return ir_instruction_toptrtype_get_dep((IrInstructionToPtrType *) instruction, index);
28082786 case IrInstructionIdPtrTypeChild:
28092787 return ir_instruction_ptrtypechild_get_dep((IrInstructionPtrTypeChild *) instruction, index);
2810 case IrInstructionIdSetFnTest:
2811 return ir_instruction_setfntest_get_dep((IrInstructionSetFnTest *) instruction, index);
28122788 case IrInstructionIdSetFnVisible:
28132789 return ir_instruction_setfnvisible_get_dep((IrInstructionSetFnVisible *) instruction, index);
28142790 case IrInstructionIdSetDebugSafety:
......@@ -3810,15 +3786,6 @@ static IrInstruction *ir_gen_builtin_fn_call(IrBuilder *irb, Scope *scope, AstNo
38103786 return arg;
38113787 return ir_build_typeof(irb, scope, node, arg);
38123788 }
3813 case BuiltinFnIdSetFnTest:
3814 {
3815 AstNode *arg0_node = node->data.fn_call_expr.params.at(0);
3816 IrInstruction *arg0_value = ir_gen_node(irb, arg0_node, scope);
3817 if (arg0_value == irb->codegen->invalid_instruction)
3818 return arg0_value;
3819
3820 return ir_build_set_fn_test(irb, scope, node, arg0_value);
3821 }
38223789 case BuiltinFnIdSetFnVisible:
38233790 {
38243791 AstNode *arg0_node = node->data.fn_call_expr.params.at(0);
......@@ -5543,6 +5510,8 @@ static IrInstruction *ir_gen_node_raw(IrBuilder *irb, AstNode *node, Scope *scop
55435510 zig_panic("TODO IR gen NodeTypeErrorValueDecl");
55445511 case NodeTypeTypeDecl:
55455512 zig_panic("TODO IR gen NodeTypeTypeDecl");
5513 case NodeTypeTestDecl:
5514 zig_panic("TODO IR gen NodeTypeTestDecl");
55465515 }
55475516 zig_unreachable();
55485517}
......@@ -5633,10 +5602,7 @@ bool ir_gen_fn(CodeGen *codegen, FnTableEntry *fn_entry) {
56335602 assert(fn_entry);
56345603
56355604 IrExecutable *ir_executable = &fn_entry->ir_executable;
5636 AstNode *fn_def_node = fn_entry->fn_def_node;
5637 assert(fn_def_node->type == NodeTypeFnDef);
5638
5639 AstNode *body_node = fn_def_node->data.fn_def.body;
5605 AstNode *body_node = fn_entry->body_node;
56405606
56415607 assert(fn_entry->child_scope);
56425608
......@@ -8180,7 +8146,7 @@ static TypeTableEntry *ir_analyze_fn_call(IrAnalyze *ira, IrInstructionCall *cal
81808146 result = entry->value;
81818147 } else {
81828148 // Analyze the fn body block like any other constant expression.
8183 AstNode *body_node = fn_entry->fn_def_node->data.fn_def.body;
8149 AstNode *body_node = fn_entry->body_node;
81848150 result = ir_eval_const_value(ira->codegen, exec_scope, body_node, return_type,
81858151 ira->new_irb.exec->backward_branch_count, ira->new_irb.exec->backward_branch_quota, fn_entry,
81868152 nullptr, call_instruction->base.source_node, nullptr, ira->new_irb.exec);
......@@ -8219,7 +8185,7 @@ static TypeTableEntry *ir_analyze_fn_call(IrAnalyze *ira, IrInstructionCall *cal
82198185 FnTableEntry *impl_fn = create_fn(fn_proto_node);
82208186 impl_fn->param_source_nodes = allocate<AstNode *>(new_fn_arg_count);
82218187 buf_init_from_buf(&impl_fn->symbol_name, &fn_entry->symbol_name);
8222 impl_fn->fndef_scope = create_fndef_scope(impl_fn->fn_def_node, parent_scope, impl_fn);
8188 impl_fn->fndef_scope = create_fndef_scope(impl_fn->body_node, parent_scope, impl_fn);
82238189 impl_fn->child_scope = &impl_fn->fndef_scope->base;
82248190 FnTypeId inst_fn_type_id = {0};
82258191 init_fn_type_id(&inst_fn_type_id, fn_proto_node, new_fn_arg_count);
......@@ -9582,24 +9548,6 @@ static TypeTableEntry *ir_analyze_instruction_ptr_type_child(IrAnalyze *ira,
95829548 return ira->codegen->builtin_types.entry_type;
95839549}
95849550
9585static TypeTableEntry *ir_analyze_instruction_set_fn_test(IrAnalyze *ira,
9586 IrInstructionSetFnTest *set_fn_test_instruction)
9587{
9588 IrInstruction *fn_value = set_fn_test_instruction->fn_value->other;
9589
9590 FnTableEntry *fn_entry = ir_resolve_fn(ira, fn_value);
9591 if (!fn_entry)
9592 return ira->codegen->builtin_types.entry_invalid;
9593
9594 if (!fn_entry->is_test) {
9595 fn_entry->is_test = true;
9596 ira->codegen->test_fn_count += 1;
9597 }
9598
9599 ir_build_const_from(ira, &set_fn_test_instruction->base);
9600 return ira->codegen->builtin_types.entry_void;
9601}
9602
96039551static TypeTableEntry *ir_analyze_instruction_set_fn_visible(IrAnalyze *ira,
96049552 IrInstructionSetFnVisible *set_fn_visible_instruction)
96059553{
......@@ -12253,8 +12201,6 @@ static TypeTableEntry *ir_analyze_instruction_nocast(IrAnalyze *ira, IrInstructi
1225312201 return ir_analyze_instruction_to_ptr_type(ira, (IrInstructionToPtrType *)instruction);
1225412202 case IrInstructionIdPtrTypeChild:
1225512203 return ir_analyze_instruction_ptr_type_child(ira, (IrInstructionPtrTypeChild *)instruction);
12256 case IrInstructionIdSetFnTest:
12257 return ir_analyze_instruction_set_fn_test(ira, (IrInstructionSetFnTest *)instruction);
1225812204 case IrInstructionIdSetFnVisible:
1225912205 return ir_analyze_instruction_set_fn_visible(ira, (IrInstructionSetFnVisible *)instruction);
1226012206 case IrInstructionIdSetGlobalAlign:
......@@ -12469,7 +12415,6 @@ bool ir_has_side_effects(IrInstruction *instruction) {
1246912415 case IrInstructionIdCall:
1247012416 case IrInstructionIdReturn:
1247112417 case IrInstructionIdUnreachable:
12472 case IrInstructionIdSetFnTest:
1247312418 case IrInstructionIdSetFnVisible:
1247412419 case IrInstructionIdSetDebugSafety:
1247512420 case IrInstructionIdImport:
src/ir_print.cpp-9
......@@ -339,12 +339,6 @@ static void ir_print_enum_field_ptr(IrPrint *irp, IrInstructionEnumFieldPtr *ins
339339 fprintf(irp->f, ")");
340340}
341341
342static void ir_print_set_fn_test(IrPrint *irp, IrInstructionSetFnTest *instruction) {
343 fprintf(irp->f, "@setFnTest(");
344 ir_print_other_instruction(irp, instruction->fn_value);
345 fprintf(irp->f, ")");
346}
347
348342static void ir_print_set_fn_visible(IrPrint *irp, IrInstructionSetFnVisible *instruction) {
349343 fprintf(irp->f, "@setFnVisible(");
350344 ir_print_other_instruction(irp, instruction->fn_value);
......@@ -932,9 +926,6 @@ static void ir_print_instruction(IrPrint *irp, IrInstruction *instruction) {
932926 case IrInstructionIdEnumFieldPtr:
933927 ir_print_enum_field_ptr(irp, (IrInstructionEnumFieldPtr *)instruction);
934928 break;
935 case IrInstructionIdSetFnTest:
936 ir_print_set_fn_test(irp, (IrInstructionSetFnTest *)instruction);
937 break;
938929 case IrInstructionIdSetFnVisible:
939930 ir_print_set_fn_visible(irp, (IrInstructionSetFnVisible *)instruction);
940931 break;
src/main.cpp-5
......@@ -56,7 +56,6 @@ static int usage(const char *arg0) {
5656 " -mmacosx-version-min [ver] (darwin only) set Mac OS X deployment target\n"
5757 " -mios-version-min [ver] (darwin only) set iOS deployment target\n"
5858 " -framework [name] (darwin only) link against framework\n"
59 " --check-unused perform semantic analysis on unused declarations\n"
6059 " --linker-script [path] use a custom linker script\n"
6160 " -rpath [path] add directory to the runtime library search path\n"
6261 " --each-lib-rpath add rpath for each used dynamic library\n"
......@@ -141,7 +140,6 @@ int main(int argc, char **argv) {
141140 bool rdynamic = false;
142141 const char *mmacosx_version_min = nullptr;
143142 const char *mios_version_min = nullptr;
144 bool check_unused = false;
145143 const char *linker_script = nullptr;
146144 ZigList<const char *> rpath_list = {0};
147145 bool each_lib_rpath = false;
......@@ -166,8 +164,6 @@ int main(int argc, char **argv) {
166164 municode = true;
167165 } else if (strcmp(arg, "-rdynamic") == 0) {
168166 rdynamic = true;
169 } else if (strcmp(arg, "--check-unused") == 0) {
170 check_unused = true;
171167 } else if (strcmp(arg, "--each-lib-rpath") == 0) {
172168 each_lib_rpath = true;
173169 } else if (arg[1] == 'L' && arg[2] != 0) {
......@@ -354,7 +350,6 @@ int main(int argc, char **argv) {
354350 codegen_set_is_release(g, is_release_build);
355351 codegen_set_is_test(g, cmd == CmdTest);
356352 codegen_set_linker_script(g, linker_script);
357 codegen_set_check_unused(g, check_unused);
358353 if (each_lib_rpath)
359354 codegen_set_each_lib_rpath(g, each_lib_rpath);
360355
src/parser.cpp+31-1
......@@ -2417,6 +2417,27 @@ static AstNode *ast_parse_error_value_decl(ParseContext *pc, size_t *token_index
24172417 return node;
24182418}
24192419
2420/*
2421TestDecl = "test" String Block
2422*/
2423static AstNode *ast_parse_test_decl_node(ParseContext *pc, size_t *token_index, VisibMod visib_mod) {
2424 Token *first_token = &pc->tokens->at(*token_index);
2425
2426 if (first_token->id != TokenIdKeywordTest) {
2427 return nullptr;
2428 }
2429 *token_index += 1;
2430
2431 Token *name_tok = ast_eat_token(pc, token_index, TokenIdStringLiteral);
2432
2433 AstNode *node = ast_create_node(pc, NodeTypeTestDecl, first_token);
2434 node->data.test_decl.visib_mod = visib_mod;
2435 node->data.test_decl.name = token_buf(name_tok);
2436 node->data.test_decl.body = ast_parse_block(pc, token_index, true);
2437
2438 return node;
2439}
2440
24202441/*
24212442TypeDecl = "type" "Symbol" "=" TypeExpr ";"
24222443*/
......@@ -2443,7 +2464,7 @@ static AstNode *ast_parse_type_decl(ParseContext *pc, size_t *token_index, Visib
24432464}
24442465
24452466/*
2446TopLevelItem = ErrorValueDecl | Block | TopLevelDecl
2467TopLevelItem = ErrorValueDecl | Block | TopLevelDecl | TestDecl
24472468TopLevelDecl = option(VisibleMod) (FnDef | ExternDecl | GlobalVarDecl | TypeDecl | UseDecl)
24482469*/
24492470static void ast_parse_top_level_decls(ParseContext *pc, size_t *token_index, ZigList<AstNode *> *top_level_decls) {
......@@ -2491,6 +2512,12 @@ static void ast_parse_top_level_decls(ParseContext *pc, size_t *token_index, Zig
24912512 continue;
24922513 }
24932514
2515 AstNode *test_decl_node = ast_parse_test_decl_node(pc, token_index, visib_mod);
2516 if (test_decl_node) {
2517 top_level_decls->append(test_decl_node);
2518 continue;
2519 }
2520
24942521 AstNode *type_decl_node = ast_parse_type_decl(pc, token_index, visib_mod);
24952522 if (type_decl_node) {
24962523 top_level_decls->append(type_decl_node);
......@@ -2585,6 +2612,9 @@ void ast_visit_node_children(AstNode *node, void (*visit)(AstNode **, void *cont
25852612 case NodeTypeErrorValueDecl:
25862613 // none
25872614 break;
2615 case NodeTypeTestDecl:
2616 visit_field(&node->data.test_decl.body, visit, context);
2617 break;
25882618 case NodeTypeBinOpExpr:
25892619 visit_field(&node->data.bin_op_expr.op1, visit, context);
25902620 visit_field(&node->data.bin_op_expr.op2, visit, context);
src/tokenizer.cpp+2
......@@ -133,6 +133,7 @@ static const struct ZigKeyword zig_keywords[] = {
133133 {"return", TokenIdKeywordReturn},
134134 {"struct", TokenIdKeywordStruct},
135135 {"switch", TokenIdKeywordSwitch},
136 {"test", TokenIdKeywordTest},
136137 {"this", TokenIdKeywordThis},
137138 {"true", TokenIdKeywordTrue},
138139 {"try", TokenIdKeywordTry},
......@@ -1508,6 +1509,7 @@ const char * token_name(TokenId id) {
15081509 case TokenIdKeywordReturn: return "return";
15091510 case TokenIdKeywordStruct: return "struct";
15101511 case TokenIdKeywordSwitch: return "switch";
1512 case TokenIdKeywordTest: return "test";
15111513 case TokenIdKeywordThis: return "this";
15121514 case TokenIdKeywordTrue: return "true";
15131515 case TokenIdKeywordTry: return "try";
src/tokenizer.hpp+1
......@@ -74,6 +74,7 @@ enum TokenId {
7474 TokenIdKeywordReturn,
7575 TokenIdKeywordStruct,
7676 TokenIdKeywordSwitch,
77 TokenIdKeywordTest,
7778 TokenIdKeywordThis,
7879 TokenIdKeywordTrue,
7980 TokenIdKeywordTry,
std/compiler_rt.zig+3-9
......@@ -322,9 +322,7 @@ export fn __udivsi3(n: su_int, d: su_int) -> su_int {
322322 return q;
323323}
324324
325fn test_umoddi3() {
326 @setFnTest(this);
327
325test "test_umoddi3" {
328326 test_one_umoddi3(0, 1, 0);
329327 test_one_umoddi3(2, 1, 0);
330328 test_one_umoddi3(0x8000000000000000, 1, 0x0);
......@@ -337,9 +335,7 @@ fn test_one_umoddi3(a: du_int, b: du_int, expected_r: du_int) {
337335 assert(r == expected_r);
338336}
339337
340fn test_udivmoddi4() {
341 @setFnTest(this);
342
338test "test_udivmoddi4" {
343339 const cases = [][4]du_int {
344340 []du_int{0x0000000000000000, 0x0000000000000001, 0x0000000000000000, 0x0000000000000000},
345341 []du_int{0x0000000080000000, 0x0000000100000001, 0x0000000000000000, 0x0000000080000000},
......@@ -367,9 +363,7 @@ fn test_one_udivmoddi4(a: du_int, b: du_int, expected_q: du_int, expected_r: du_
367363 assert(r == expected_r);
368364}
369365
370fn test_udivsi3() {
371 @setFnTest(this);
372
366test "test_udivsi3" {
373367 const cases = [][3]su_int {
374368 []su_int{0x00000000, 0x00000001, 0x00000000},
375369 []su_int{0x00000000, 0x00000002, 0x00000000},
std/cstr.zig+2-6
......@@ -140,9 +140,7 @@ pub const Buffer0 = struct {
140140 }
141141};
142142
143fn testSimpleBuffer0() {
144 @setFnTest(this);
145
143test "simple Buffer0" {
146144 var buf = %%Buffer0.initEmpty(&debug.global_allocator);
147145 assert(buf.len() == 0);
148146 %%buf.appendCStr(c"hello");
......@@ -162,9 +160,7 @@ fn testSimpleBuffer0() {
162160 assert(buf.startsWithOther(&buf2));
163161}
164162
165fn testCStrFns() {
166 @setFnTest(this);
167
163test "cstr fns" {
168164 comptime testCStrFnsImpl();
169165 testCStrFnsImpl();
170166}
std/fmt.zig+3-9
......@@ -291,9 +291,7 @@ fn digitToChar(digit: u8, uppercase: bool) -> u8 {
291291 };
292292}
293293
294fn testBufPrintInt() {
295 @setFnTest(this);
296
294test "testBufPrintInt" {
297295 var buffer: [max_int_digits]u8 = undefined;
298296 const buf = buffer[0...];
299297 assert(mem.eql(u8, bufPrintIntToSlice(buf, i32(-12345678), 2, false, 0), "-101111000110000101001110"));
......@@ -315,9 +313,7 @@ fn bufPrintIntToSlice(buf: []u8, value: var, base: u8, uppercase: bool, width: u
315313 return buf[0...formatIntBuf(buf, value, base, uppercase, width)];
316314}
317315
318fn testParseU64DigitTooBig() {
319 @setFnTest(this);
320
316test "testParseU64DigitTooBig" {
321317 parseUnsigned(u64, "123a", 10) %% |err| {
322318 if (err == error.InvalidChar) return;
323319 @unreachable();
......@@ -325,9 +321,7 @@ fn testParseU64DigitTooBig() {
325321 @unreachable();
326322}
327323
328fn testParseUnsignedComptime() {
329 @setFnTest(this);
330
324test "testParseUnsignedComptime" {
331325 comptime {
332326 assert(%%parseUnsigned(usize, "2", 10) == 2);
333327 }
std/hash_map.zig+1-3
......@@ -219,9 +219,7 @@ pub fn HashMap(comptime K: type, comptime V: type,
219219 }
220220}
221221
222fn basicHashMapTest() {
223 @setFnTest(this);
224
222test "basicHashMapTest" {
225223 var map: HashMap(i32, i32, hash_i32, eql_i32) = undefined;
226224 map.init(&debug.global_allocator);
227225 defer map.deinit();
std/list.zig+1-3
......@@ -64,9 +64,7 @@ pub fn List(comptime T: type) -> type{
6464 }
6565}
6666
67fn basicListTest() {
68 @setFnTest(this);
69
67test "basicListTest" {
7068 var list = List(i32).init(&debug.global_allocator);
7169 defer list.deinit();
7270
std/math.zig+1-3
......@@ -68,9 +68,7 @@ fn getReturnTypeForAbs(comptime T: type) -> type {
6868 }
6969}
7070
71fn testMath() {
72 @setFnTest(this);
73
71test "testMath" {
7472 testMathImpl();
7573 comptime testMathImpl();
7674}
std/mem.zig+3-9
......@@ -117,17 +117,13 @@ pub fn writeInt(buf: []u8, value: var, big_endian: bool) {
117117 assert(bits == 0);
118118}
119119
120fn testStringEquality() {
121 @setFnTest(this);
122
120test "testStringEquality" {
123121 assert(eql(u8, "abcd", "abcd"));
124122 assert(!eql(u8, "abcdef", "abZdef"));
125123 assert(!eql(u8, "abcdefg", "abcdef"));
126124}
127125
128fn testReadInt() {
129 @setFnTest(this);
130
126test "testReadInt" {
131127 testReadIntImpl();
132128 comptime testReadIntImpl();
133129}
......@@ -149,9 +145,7 @@ fn testReadIntImpl() {
149145 }
150146}
151147
152fn testWriteInt() {
153 @setFnTest(this);
154
148test "testWriteInt" {
155149 testWriteIntImpl();
156150 comptime testWriteIntImpl();
157151}
std/rand.zig+3-9
......@@ -158,9 +158,7 @@ fn MersenneTwister(
158158 }
159159}
160160
161fn testFloat32() {
162 @setFnTest(this);
163
161test "testFloat32" {
164162 var r: Rand = undefined;
165163 r.init(42);
166164
......@@ -171,9 +169,7 @@ fn testFloat32() {
171169 }}
172170}
173171
174fn testMT19937_64() {
175 @setFnTest(this);
176
172test "testMT19937_64" {
177173 var rng: MT19937_64 = undefined;
178174 rng.init(rand_test.mt64_seed);
179175 for (rand_test.mt64_data) |value| {
......@@ -181,9 +177,7 @@ fn testMT19937_64() {
181177 }
182178}
183179
184fn testMT19937_32() {
185 @setFnTest(this);
186
180test "testMT19937_32" {
187181 var rng: MT19937_32 = undefined;
188182 rng.init(rand_test.mt32_seed);
189183 for (rand_test.mt32_data) |value| {
std/sort.zig+2-6
......@@ -58,9 +58,7 @@ fn reverse(was: Cmp) -> Cmp {
5858// ---------------------------------------
5959// tests
6060
61fn testSort() {
62 @setFnTest(this);
63
61test "testSort" {
6462 const u8cases = [][]const []const u8 {
6563 [][]const u8{"", ""},
6664 [][]const u8{"a", "a"},
......@@ -96,9 +94,7 @@ fn testSort() {
9694 }
9795}
9896
99fn testSortDesc() {
100 @setFnTest(this);
101
97test "testSortDesc" {
10298 const rev_cases = [][]const []const i32 {
10399 [][]const i32{[]i32{}, []i32{}},
104100 [][]const i32{[]i32{1}, []i32{1}},
test/cases/array.zig+6-18
......@@ -1,9 +1,7 @@
11const assert = @import("std").debug.assert;
22const mem = @import("std").mem;
33
4fn arrays() {
5 @setFnTest(this);
6
4test "arrays" {
75 var array : [5]u32 = undefined;
86
97 var i : u32 = 0;
......@@ -27,9 +25,7 @@ fn getArrayLen(a: []const u32) -> usize {
2725 a.len
2826}
2927
30fn voidArrays() {
31 @setFnTest(this);
32
28test "voidArrays" {
3329 var array: [4]void = undefined;
3430 array[0] = void{};
3531 array[1] = array[2];
......@@ -37,18 +33,14 @@ fn voidArrays() {
3733 assert(array.len == 4);
3834}
3935
40fn arrayLiteral() {
41 @setFnTest(this);
42
36test "arrayLiteral" {
4337 const hex_mult = []u16{4096, 256, 16, 1};
4438
4539 assert(hex_mult.len == 4);
4640 assert(hex_mult[1] == 256);
4741}
4842
49fn arrayDotLenConstExpr() {
50 @setFnTest(this);
51
43test "arrayDotLenConstExpr" {
5244 assert(comptime {some_array.len == 4});
5345}
5446
......@@ -58,9 +50,7 @@ const ArrayDotLenConstExpr = struct {
5850const some_array = []u8 {0, 1, 2, 3};
5951
6052
61fn nestedArrays() {
62 @setFnTest(this);
63
53test "nestedArrays" {
6454 const array_of_strings = [][]const u8 {"hello", "this", "is", "my", "thing"};
6555 for (array_of_strings) |s, i| {
6656 if (i == 0) assert(mem.eql(u8, s, "hello"));
......@@ -79,9 +69,7 @@ const Sub = struct {
7969const Str = struct {
8070 a: []Sub,
8171};
82fn setGlobalVarArrayViaSliceEmbeddedInStruct() {
83 @setFnTest(this);
84
72test "setGlobalVarArrayViaSliceEmbeddedInStruct" {
8573 var s = Str { .a = s_array[0...]};
8674
8775 s.a[0].b = 1;
test/cases/atomics.zig+2-6
......@@ -1,16 +1,12 @@
11const assert = @import("std").debug.assert;
22
3fn cmpxchg() {
4 @setFnTest(this);
5
3test "cmpxchg" {
64 var x: i32 = 1234;
75 while (!@cmpxchg(&x, 1234, 5678, AtomicOrder.SeqCst, AtomicOrder.SeqCst)) {}
86 assert(x == 5678);
97}
108
11fn fence() {
12 @setFnTest(this);
13
9test "fence" {
1410 var x: i32 = 1234;
1511 @fence(AtomicOrder.SeqCst);
1612 x = 5678;
test/cases/bool.zig+5-15
......@@ -1,15 +1,11 @@
11const assert = @import("std").debug.assert;
22
3fn boolLiterals() {
4 @setFnTest(this);
5
3test "boolLiterals" {
64 assert(true);
75 assert(!false);
86}
97
10fn castBoolToInt() {
11 @setFnTest(this);
12
8test "castBoolToInt" {
139 const t = true;
1410 const f = false;
1511 assert(i32(t) == i32(1));
......@@ -22,18 +18,14 @@ fn nonConstCastBoolToInt(t: bool, f: bool) {
2218 assert(i32(f) == i32(0));
2319}
2420
25fn boolCmp() {
26 @setFnTest(this);
27
21test "boolCmp" {
2822 assert(testBoolCmp(true, false) == false);
2923}
3024fn testBoolCmp(a: bool, b: bool) -> bool {
3125 a == b
3226}
3327
34fn shortCircuitAndOr() {
35 @setFnTest(this);
36
28test "shortCircuitAndOr" {
3729 var a = true;
3830 a &&= false;
3931 assert(!a);
......@@ -49,9 +41,7 @@ const global_f = false;
4941const global_t = true;
5042const not_global_f = !global_f;
5143const not_global_t = !global_t;
52fn compileTimeBoolnot() {
53 @setFnTest(this);
54
44test "compileTimeBoolnot" {
5545 assert(not_global_f);
5646 assert(!not_global_t);
5747}
test/cases/cast.zig+3-9
......@@ -1,24 +1,18 @@
11const assert = @import("std").debug.assert;
22
3fn intToPtrCast() {
4 @setFnTest(this);
5
3test "intToPtrCast" {
64 const x = isize(13);
75 const y = (&u8)(x);
86 const z = usize(y);
97 assert(z == 13);
108}
119
12fn numLitIntToPtrCast() {
13 @setFnTest(this);
14
10test "numLitIntToPtrCast" {
1511 const vga_mem = (&u16)(0xB8000);
1612 assert(usize(vga_mem) == 0xB8000);
1713}
1814
19fn pointerReinterpretConstFloatToInt() {
20 @setFnTest(this);
21
15test "pointerReinterpretConstFloatToInt" {
2216 const float: f64 = 5.99999999999994648725e-01;
2317 const float_ptr = &float;
2418 const int_ptr = (&i32)(float_ptr);
test/cases/const_slice_child.zig+1-3
......@@ -2,9 +2,7 @@ const assert = @import("std").debug.assert;
22
33var argv: &const &const u8 = undefined;
44
5fn constSliceChild() {
6 @setFnTest(this);
7
5test "constSliceChild" {
86 const strs = ([]&const u8) {
97 c"one",
108 c"two",
test/cases/defer.zig+2-6
......@@ -21,9 +21,7 @@ fn runSomeMaybeDefers(x: bool) -> ?bool {
2121 return if (x) x else null;
2222}
2323
24fn mixingNormalAndErrorDefers() {
25 @setFnTest(this);
26
24test "mixingNormalAndErrorDefers" {
2725 assert(%%runSomeErrorDefers(true));
2826 assert(result[0] == 'c');
2927 assert(result[1] == 'a');
......@@ -38,9 +36,7 @@ fn mixingNormalAndErrorDefers() {
3836 assert(result[2] == 'a');
3937}
4038
41fn mixingNormalAndMaybeDefers() {
42 @setFnTest(this);
43
39test "mixingNormalAndMaybeDefers" {
4440 assert(??runSomeMaybeDefers(true));
4541 assert(result[0] == 'c');
4642 assert(result[1] == 'a');
test/cases/enum.zig+5-15
......@@ -1,8 +1,6 @@
11const assert = @import("std").debug.assert;
22
3fn enumType() {
4 @setFnTest(this);
5
3test "enumType" {
64 const foo1 = Foo.One {13};
75 const foo2 = Foo.Two { Point { .x = 1234, .y = 5678, }};
86 const bar = Bar.B;
......@@ -15,9 +13,7 @@ fn enumType() {
1513 assert(@sizeOf(Bar) == 1);
1614}
1715
18fn enumAsReturnValue () {
19 @setFnTest(this);
20
16test "enumAsReturnValue" {
2117 switch (returnAnInt(13)) {
2218 Foo.One => |value| assert(value == 13),
2319 else => @unreachable(),
......@@ -45,9 +41,7 @@ fn returnAnInt(x: i32) -> Foo {
4541}
4642
4743
48fn constantEnumWithPayload() {
49 @setFnTest(this);
50
44test "constantEnumWithPayload" {
5145 var empty = AnEnumWithPayload.Empty;
5246 var full = AnEnumWithPayload.Full {13};
5347 shouldBeEmpty(empty);
......@@ -83,9 +77,7 @@ const Number = enum {
8377 Four,
8478};
8579
86fn enumToInt() {
87 @setFnTest(this);
88
80test "enumToInt" {
8981 shouldEqual(Number.Zero, 0);
9082 shouldEqual(Number.One, 1);
9183 shouldEqual(Number.Two, 2);
......@@ -98,9 +90,7 @@ fn shouldEqual(n: Number, expected: usize) {
9890}
9991
10092
101fn intToEnum() {
102 @setFnTest(this);
103
93test "intToEnum" {
10494 testIntToEnumEval(3);
10595}
10696fn testIntToEnumEval(x: i32) {
test/cases/enum_with_members.zig+1-3
......@@ -14,9 +14,7 @@ const ET = enum {
1414 }
1515};
1616
17fn enumWithMembers() {
18 @setFnTest(this);
19
17test "enumWithMembers" {
2018 const a = ET.SINT { -42 };
2119 const b = ET.UINT { 42 };
2220 var buf: [20]u8 = undefined;
test/cases/error.zig+7-20
......@@ -15,9 +15,7 @@ pub fn baz() -> %i32 {
1515 return y + 1;
1616}
1717
18fn errorWrapping() {
19 @setFnTest(this);
20
18test "errorWrapping" {
2119 assert(%%baz() == 15);
2220}
2321
......@@ -26,8 +24,7 @@ fn gimmeItBroke() -> []const u8 {
2624 @errorName(error.ItBroke)
2725}
2826
29fn errorName() {
30 @setFnTest(this);
27test "errorName" {
3128 assert(mem.eql(u8, @errorName(error.AnError), "AnError"));
3229 assert(mem.eql(u8, @errorName(error.ALongerErrorName), "ALongerErrorName"));
3330}
......@@ -35,9 +32,7 @@ error AnError;
3532error ALongerErrorName;
3633
3734
38fn errorValues() {
39 @setFnTest(this);
40
35test "errorValues" {
4136 const a = i32(error.err1);
4237 const b = i32(error.err2);
4338 assert(a != b);
......@@ -46,9 +41,7 @@ error err1;
4641error err2;
4742
4843
49fn redefinitionOfErrorValuesAllowed() {
50 @setFnTest(this);
51
44test "redefinitionOfErrorValuesAllowed" {
5245 shouldBeNotEqual(error.AnError, error.SecondError);
5346}
5447error AnError;
......@@ -59,9 +52,7 @@ fn shouldBeNotEqual(a: error, b: error) {
5952}
6053
6154
62fn errBinaryOperator() {
63 @setFnTest(this);
64
55test "errBinaryOperator" {
6556 const a = errBinaryOperatorG(true) %% 3;
6657 const b = errBinaryOperatorG(false) %% 3;
6758 assert(a == 3);
......@@ -77,18 +68,14 @@ fn errBinaryOperatorG(x: bool) -> %isize {
7768}
7869
7970
80fn unwrapSimpleValueFromError() {
81 @setFnTest(this);
82
71test "unwrapSimpleValueFromError" {
8372 const i = %%unwrapSimpleValueFromErrorDo();
8473 assert(i == 13);
8574}
8675fn unwrapSimpleValueFromErrorDo() -> %isize { 13 }
8776
8877
89fn errReturnInAssignment() {
90 @setFnTest(this);
91
78test "errReturnInAssignment" {
9279 %%doErrReturnInAssignment();
9380}
9481
test/cases/eval.zig+19-55
......@@ -1,8 +1,6 @@
11const assert = @import("std").debug.assert;
22
3fn compileTimeRecursion() {
4 @setFnTest(this);
5
3test "compileTimeRecursion" {
64 assert(some_data.len == 21);
75}
86var some_data: [usize(fibonacci(7))]u8 = undefined;
......@@ -17,14 +15,11 @@ fn unwrapAndAddOne(blah: ?i32) -> i32 {
1715 return ??blah + 1;
1816}
1917const should_be_1235 = unwrapAndAddOne(1234);
20fn testStaticAddOne() {
21 @setFnTest(this);
18test "testStaticAddOne" {
2219 assert(should_be_1235 == 1235);
2320}
2421
25fn inlinedLoop() {
26 @setFnTest(this);
27
22test "inlinedLoop" {
2823 comptime var i = 0;
2924 comptime var sum = 0;
3025 inline while (i <= 5; i += 1)
......@@ -38,25 +33,20 @@ fn gimme1or2(comptime a: bool) -> i32 {
3833 comptime var z: i32 = if (a) x else y;
3934 return z;
4035}
41fn inlineVariableGetsResultOfConstIf() {
42 @setFnTest(this);
36test "inlineVariableGetsResultOfConstIf" {
4337 assert(gimme1or2(true) == 1);
4438 assert(gimme1or2(false) == 2);
4539}
4640
4741
48fn staticFunctionEvaluation() {
49 @setFnTest(this);
50
42test "staticFunctionEvaluation" {
5143 assert(statically_added_number == 3);
5244}
5345const statically_added_number = staticAdd(1, 2);
5446fn staticAdd(a: i32, b: i32) -> i32 { a + b }
5547
5648
57fn constExprEvalOnSingleExprBlocks() {
58 @setFnTest(this);
59
49test "constExprEvalOnSingleExprBlocks" {
6050 assert(constExprEvalOnSingleExprBlocksFn(1, true) == 3);
6151}
6252
......@@ -75,9 +65,7 @@ fn constExprEvalOnSingleExprBlocksFn(x: i32, b: bool) -> i32 {
7565
7666
7767
78fn staticallyInitalizedList() {
79 @setFnTest(this);
80
68test "staticallyInitalizedList" {
8169 assert(static_point_list[0].x == 1);
8270 assert(static_point_list[0].y == 2);
8371 assert(static_point_list[1].x == 3);
......@@ -96,9 +84,7 @@ fn makePoint(x: i32, y: i32) -> Point {
9684}
9785
9886
99fn staticEvalListInit() {
100 @setFnTest(this);
101
87test "staticEvalListInit" {
10288 assert(static_vec3.data[2] == 1.0);
10389 assert(vec3(0.0, 0.0, 3.0).data[2] == 3.0);
10490}
......@@ -113,18 +99,14 @@ pub fn vec3(x: f32, y: f32, z: f32) -> Vec3 {
11399}
114100
115101
116fn constantExpressions() {
117 @setFnTest(this);
118
102test "constantExpressions" {
119103 var array : [array_size]u8 = undefined;
120104 assert(@sizeOf(@typeOf(array)) == 20);
121105}
122106const array_size : u8 = 20;
123107
124108
125fn constantStructWithNegation() {
126 @setFnTest(this);
127
109test "constantStructWithNegation" {
128110 assert(vertices[0].x == -0.6);
129111}
130112const Vertex = struct {
......@@ -141,9 +123,7 @@ const vertices = []Vertex {
141123};
142124
143125
144fn staticallyInitalizedStruct() {
145 @setFnTest(this);
146
126test "staticallyInitalizedStruct" {
147127 st_init_str_foo.x += 1;
148128 assert(st_init_str_foo.x == 14);
149129}
......@@ -154,18 +134,14 @@ const StInitStrFoo = struct {
154134var st_init_str_foo = StInitStrFoo { .x = 13, .y = true, };
155135
156136
157fn staticallyInitializedArrayLiteral() {
158 @setFnTest(this);
159
137test "staticallyInitializedArrayLiteral" {
160138 const y : [4]u8 = st_init_arr_lit_x;
161139 assert(y[3] == 4);
162140}
163141const st_init_arr_lit_x = []u8{1,2,3,4};
164142
165143
166fn constSlice() {
167 @setFnTest(this);
168
144test "constSlice" {
169145 comptime {
170146 const a = "1234567890";
171147 assert(a.len == 10);
......@@ -175,9 +151,7 @@ fn constSlice() {
175151 }
176152}
177153
178fn tryToTrickEvalWithRuntimeIf() {
179 @setFnTest(this);
180
154test "tryToTrickEvalWithRuntimeIf" {
181155 assert(testTryToTrickEvalWithRuntimeIf(true) == 10);
182156}
183157
......@@ -203,9 +177,7 @@ fn max(comptime T: type, a: T, b: T) -> T {
203177fn letsTryToCompareBools(a: bool, b: bool) -> bool {
204178 max(bool, a, b)
205179}
206fn inlinedBlockAndRuntimeBlockPhi() {
207 @setFnTest(this);
208
180test "inlinedBlockAndRuntimeBlockPhi" {
209181 assert(letsTryToCompareBools(true, true));
210182 assert(letsTryToCompareBools(true, false));
211183 assert(letsTryToCompareBools(false, true));
......@@ -244,17 +216,13 @@ fn performFn(comptime prefix_char: u8, start_value: i32) -> i32 {
244216 return result;
245217}
246218
247fn comptimeIterateOverFnPtrList() {
248 @setFnTest(this);
249
219test "comptimeIterateOverFnPtrList" {
250220 assert(performFn('t', 1) == 6);
251221 assert(performFn('o', 0) == 1);
252222 assert(performFn('w', 99) == 99);
253223}
254224
255fn evalSetDebugSafetyAtCompileTime() {
256 @setFnTest(this);
257
225test "evalSetDebugSafetyAtCompileTime" {
258226 const result = comptime fnWithSetDebugSafety();
259227 assert(result == 1234);
260228}
......@@ -278,17 +246,13 @@ var simple_struct = SimpleStruct{ .field = 1234, };
278246
279247const bound_fn = simple_struct.method;
280248
281fn callMethodOnBoundFnReferringToVarInstance() {
282 @setFnTest(this);
283
249test "callMethodOnBoundFnReferringToVarInstance" {
284250 assert(bound_fn() == 1237);
285251}
286252
287253
288254
289fn ptrToLocalArrayArgumentAtComptime() {
290 @setFnTest(this);
291
255test "ptrToLocalArrayArgumentAtComptime" {
292256 comptime {
293257 var bytes: [10]u8 = undefined;
294258 modifySomeBytes(bytes[0...]);
test/cases/fn.zig+13-26
......@@ -1,8 +1,6 @@
11const assert = @import("std").debug.assert;
22
3fn params() {
4 @setFnTest(this);
5
3test "params" {
64 assert(testParamsAdd(22, 11) == 33);
75}
86fn testParamsAdd(a: i32, b: i32) -> i32 {
......@@ -10,9 +8,7 @@ fn testParamsAdd(a: i32, b: i32) -> i32 {
108}
119
1210
13fn localVariables() {
14 @setFnTest(this);
15
11test "localVariables" {
1612 testLocVars(2);
1713}
1814fn testLocVars(b: i32) {
......@@ -21,9 +17,7 @@ fn testLocVars(b: i32) {
2117}
2218
2319
24fn voidParameters() {
25 @setFnTest(this);
26
20test "voidParameters" {
2721 voidFun(1, void{}, 2, {});
2822}
2923fn voidFun(a: i32, b: void, c: i32, d: void) {
......@@ -34,9 +28,7 @@ fn voidFun(a: i32, b: void, c: i32, d: void) {
3428}
3529
3630
37fn mutableLocalVariables() {
38 @setFnTest(this);
39
31test "mutableLocalVariables" {
4032 var zero : i32 = 0;
4133 assert(zero == 0);
4234
......@@ -47,9 +39,7 @@ fn mutableLocalVariables() {
4739 assert(i == 3);
4840}
4941
50fn separateBlockScopes() {
51 @setFnTest(this);
52
42test "separateBlockScopes" {
5343 {
5444 const no_conflict : i32 = 5;
5545 assert(no_conflict == 5);
......@@ -62,22 +52,21 @@ fn separateBlockScopes() {
6252 assert(c == 10);
6353}
6454
65fn callFnWithEmptyString() {
66 @setFnTest(this);
67
55test "callFnWithEmptyString" {
6856 acceptsString("");
6957}
7058
7159fn acceptsString(foo: []u8) { }
7260
7361
74fn @"weird function name"() {
75 @setFnTest(this);
62fn @"weird function name"() -> i32 {
63 return 1234;
64}
65test "weird function name" {
66 assert(@"weird function name"() == 1234);
7667}
7768
78fn implicitCastFnUnreachableReturn() {
79 @setFnTest(this);
80
69test "implicitCastFnUnreachableReturn" {
8170 wantsFnWithVoid(fnWithUnreachable);
8271}
8372
......@@ -88,9 +77,7 @@ fn fnWithUnreachable() -> unreachable {
8877}
8978
9079
91fn functionPointers() {
92 @setFnTest(this);
93
80test "functionPointers" {
9481 const fns = []@typeOf(fn1) { fn1, fn2, fn3, fn4, };
9582 for (fns) |f, i| {
9683 assert(f() == u32(i) + 5);
test/cases/for.zig+3-9
......@@ -2,9 +2,7 @@ const std = @import("std");
22const assert = std.debug.assert;
33const mem = std.mem;
44
5fn continueInForLoop() {
6 @setFnTest(this);
7
5test "continueInForLoop" {
86 const array = []i32 {1, 2, 3, 4, 5};
97 var sum : i32 = 0;
108 for (array) |x| {
......@@ -17,9 +15,7 @@ fn continueInForLoop() {
1715 if (sum != 6) @unreachable()
1816}
1917
20fn forLoopWithPointerElemVar() {
21 @setFnTest(this);
22
18test "forLoopWithPointerElemVar" {
2319 const source = "abcdefg";
2420 var target: [source.len]u8 = undefined;
2521 mem.copy(u8, target[0...], source);
......@@ -32,9 +28,7 @@ fn mangleString(s: []u8) {
3228 }
3329}
3430
35fn basicForLoop() {
36 @setFnTest(this);
37
31test "basicForLoop" {
3832 const expected_result = []u8{9, 8, 7, 6, 0, 1, 2, 3, 9, 8, 7, 6, 0, 1, 2, 3 };
3933
4034 var buffer: [expected_result.len]u8 = undefined;
test/cases/generics.zig+9-26
......@@ -1,8 +1,6 @@
11const assert = @import("std").debug.assert;
22
3fn simpleGenericFn() {
4 @setFnTest(this);
5
3test "simpleGenericFn" {
64 assert(max(i32, 3, -1) == 3);
75 assert(max(f32, 0.123, 0.456) == 0.456);
86 assert(add(2, 3) == 5);
......@@ -17,8 +15,7 @@ fn add(comptime a: i32, b: i32) -> i32 {
1715}
1816
1917const the_max = max(u32, 1234, 5678);
20fn compileTimeGenericEval() {
21 @setFnTest(this);
18test "compileTimeGenericEval" {
2219 assert(the_max == 5678);
2320}
2421
......@@ -34,18 +31,14 @@ fn sameButWithFloats(a: f64, b: f64) -> f64 {
3431 max(f64, a, b)
3532}
3633
37fn fnWithInlineArgs() {
38 @setFnTest(this);
39
34test "fnWithInlineArgs" {
4035 assert(gimmeTheBigOne(1234, 5678) == 5678);
4136 assert(shouldCallSameInstance(34, 12) == 34);
4237 assert(sameButWithFloats(0.43, 0.49) == 0.49);
4338}
4439
4540
46fn varParams() {
47 @setFnTest(this);
48
41test "varParams" {
4942 assert(max_i32(12, 34) == 34);
5043 assert(max_f64(1.2, 3.4) == 3.4);
5144}
......@@ -79,9 +72,7 @@ pub fn SmallList(comptime T: type, comptime STATIC_SIZE: usize) -> type {
7972 }
8073}
8174
82fn functionWithReturnTypeType() {
83 @setFnTest(this);
84
75test "functionWithReturnTypeType" {
8576 var list: List(i32) = undefined;
8677 var list2: List(i32) = undefined;
8778 list.length = 10;
......@@ -91,9 +82,7 @@ fn functionWithReturnTypeType() {
9182}
9283
9384
94fn genericStruct() {
95 @setFnTest(this);
96
85test "genericStruct" {
9786 var a1 = GenNode(i32) {.value = 13, .next = null,};
9887 var b1 = GenNode(bool) {.value = true, .next = null,};
9988 assert(a1.value == 13);
......@@ -108,9 +97,7 @@ fn GenNode(comptime T: type) -> type {
10897 }
10998}
11099
111fn constDeclsInStruct() {
112 @setFnTest(this);
113
100test "constDeclsInStruct" {
114101 assert(GenericDataThing(3).count_plus_one == 4);
115102}
116103fn GenericDataThing(comptime count: isize) -> type {
......@@ -120,9 +107,7 @@ fn GenericDataThing(comptime count: isize) -> type {
120107}
121108
122109
123fn useGenericParamInGenericParam() {
124 @setFnTest(this);
125
110test "useGenericParamInGenericParam" {
126111 assert(aGenericFn(i32, 3, 4) == 7);
127112}
128113fn aGenericFn(comptime T: type, comptime a: T, b: T) -> T {
......@@ -130,9 +115,7 @@ fn aGenericFn(comptime T: type, comptime a: T, b: T) -> T {
130115}
131116
132117
133fn genericFnWithImplicitCast() {
134 @setFnTest(this);
135
118test "genericFnWithImplicitCast" {
136119 assert(getFirstByte(u8, []u8 {13}) == 13);
137120 assert(getFirstByte(u16, []u16 {0, 13}) == 0);
138121}
test/cases/goto.zig+2-6
......@@ -1,8 +1,6 @@
11const assert = @import("std").debug.assert;
22
3fn gotoAndLabels() {
4 @setFnTest(this);
5
3test "gotoAndLabels" {
64 gotoLoop();
75 assert(goto_counter == 10);
86}
......@@ -21,9 +19,7 @@ var goto_counter: i32 = 0;
2119
2220
2321
24fn gotoLeaveDeferScope() {
25 @setFnTest(this);
26
22test "gotoLeaveDeferScope" {
2723 testGotoLeaveDeferScope(true);
2824}
2925fn testGotoLeaveDeferScope(b: bool) {
test/cases/if.zig+2-6
......@@ -1,8 +1,6 @@
11const assert = @import("std").debug.assert;
22
3fn ifStatements() {
4 @setFnTest(this);
5
3test "ifStatements" {
64 shouldBeEqual(1, 1);
75 firstEqlThird(2, 1, 2);
86}
......@@ -26,9 +24,7 @@ fn firstEqlThird(a: i32, b: i32, c: i32) {
2624}
2725
2826
29fn elseIfExpression() {
30 @setFnTest(this);
31
27test "elseIfExpression" {
3228 assert(elseIfExpressionF(1) == 1);
3329}
3430fn elseIfExpressionF(c: u8) -> u8 {
test/cases/import.zig+1-3
......@@ -1,8 +1,6 @@
11const assert = @import("std").debug.assert;
22const a_namespace = @import("cases/import/a_namespace.zig");
33
4fn callFnViaNamespaceLookup() {
5 @setFnTest(this);
6
4test "callFnViaNamespaceLookup" {
75 assert(a_namespace.foo() == 1234);
86}
test/cases/ir_block_deps.zig+1-3
......@@ -15,9 +15,7 @@ fn getErrInt() -> %i32 { 0 }
1515
1616error ItBroke;
1717
18fn irBlockDeps() {
19 @setFnTest(this);
20
18test "irBlockDeps" {
2119 assert(%%foo(1) == 0);
2220 assert(%%foo(2) == 0);
2321}
test/cases/math.zig+17-51
......@@ -1,60 +1,46 @@
11const assert = @import("std").debug.assert;
22
3fn exactDivision() {
4 @setFnTest(this);
5
3test "exactDivision" {
64 assert(divExact(55, 11) == 5);
75}
86fn divExact(a: u32, b: u32) -> u32 {
97 @divExact(a, b)
108}
119
12fn floatDivision() {
13 @setFnTest(this);
14
10test "floatDivision" {
1511 assert(fdiv32(12.0, 3.0) == 4.0);
1612}
1713fn fdiv32(a: f32, b: f32) -> f32 {
1814 a / b
1915}
2016
21fn overflowIntrinsics() {
22 @setFnTest(this);
23
17test "overflowIntrinsics" {
2418 var result: u8 = undefined;
2519 assert(@addWithOverflow(u8, 250, 100, &result));
2620 assert(!@addWithOverflow(u8, 100, 150, &result));
2721 assert(result == 250);
2822}
2923
30fn shlWithOverflow() {
31 @setFnTest(this);
32
24test "shlWithOverflow" {
3325 var result: u16 = undefined;
3426 assert(@shlWithOverflow(u16, 0b0010111111111111, 3, &result));
3527 assert(!@shlWithOverflow(u16, 0b0010111111111111, 2, &result));
3628 assert(result == 0b1011111111111100);
3729}
3830
39fn countLeadingZeroes() {
40 @setFnTest(this);
41
31test "countLeadingZeroes" {
4232 assert(@clz(u8(0b00001010)) == 4);
4333 assert(@clz(u8(0b10001010)) == 0);
4434 assert(@clz(u8(0b00000000)) == 8);
4535}
4636
47fn countTrailingZeroes() {
48 @setFnTest(this);
49
37test "countTrailingZeroes" {
5038 assert(@ctz(u8(0b10100000)) == 5);
5139 assert(@ctz(u8(0b10001010)) == 1);
5240 assert(@ctz(u8(0b00000000)) == 8);
5341}
5442
55fn modifyOperators() {
56 @setFnTest(this);
57
43test "modifyOperators" {
5844 var i : i32 = 0;
5945 i += 5; assert(i == 5);
6046 i -= 2; assert(i == 3);
......@@ -70,9 +56,7 @@ fn modifyOperators() {
7056 i |= 3; assert(i == 7);
7157}
7258
73fn threeExprInARow() {
74 @setFnTest(this);
75 testThreeExprInARow(false, true);
59test "threeExprInARow" {
7660}
7761fn testThreeExprInARow(f: bool, t: bool) {
7862 assertFalse(f || f || f);
......@@ -94,9 +78,7 @@ fn assertFalse(b: bool) {
9478}
9579
9680
97fn constNumberLiteral() {
98 @setFnTest(this);
99
81test "constNumberLiteral" {
10082 const one = 1;
10183 const eleven = ten + one;
10284
......@@ -106,9 +88,7 @@ const ten = 10;
10688
10789
10890
109fn unsignedWrapping() {
110 @setFnTest(this);
111
91test "unsignedWrapping" {
11292 testUnsignedWrappingEval(@maxValue(u32));
11393}
11494fn testUnsignedWrappingEval(x: u32) {
......@@ -118,9 +98,7 @@ fn testUnsignedWrappingEval(x: u32) {
11898 assert(orig == @maxValue(u32));
11999}
120100
121fn signedWrapping() {
122 @setFnTest(this);
123
101test "signedWrapping" {
124102 testSignedWrappingEval(@maxValue(i32));
125103}
126104fn testSignedWrappingEval(x: i32) {
......@@ -130,9 +108,7 @@ fn testSignedWrappingEval(x: i32) {
130108 assert(max_val == @maxValue(i32));
131109}
132110
133fn negationWrapping() {
134 @setFnTest(this);
135
111test "negationWrapping" {
136112 testNegationWrappingEval(@minValue(i16));
137113}
138114fn testNegationWrappingEval(x: i16) {
......@@ -141,9 +117,7 @@ fn testNegationWrappingEval(x: i16) {
141117 assert(neg == -32768);
142118}
143119
144fn shlWrapping() {
145 @setFnTest(this);
146
120test "shlWrapping" {
147121 testShlWrappingEval(@maxValue(u16));
148122}
149123fn testShlWrappingEval(x: u16) {
......@@ -151,9 +125,7 @@ fn testShlWrappingEval(x: u16) {
151125 assert(shifted == 65534);
152126}
153127
154fn unsigned64BitDivision() {
155 @setFnTest(this);
156
128test "unsigned64BitDivision" {
157129 const result = div(1152921504606846976, 34359738365);
158130 assert(result.quotient == 33554432);
159131 assert(result.remainder == 100663296);
......@@ -169,9 +141,7 @@ const DivResult = struct {
169141 remainder: u64,
170142};
171143
172fn binaryNot() {
173 @setFnTest(this);
174
144test "binaryNot" {
175145 assert(comptime {~u16(0b1010101010101010) == 0b0101010101010101});
176146 assert(comptime {~u64(2147483647) == 18446744071562067968});
177147 testBinaryNot(0b1010101010101010);
......@@ -181,9 +151,7 @@ fn testBinaryNot(x: u16) {
181151 assert(~x == 0b0101010101010101);
182152}
183153
184fn smallIntAddition() {
185 @setFnTest(this);
186
154test "smallIntAddition" {
187155 var x: @intType(false, 2) = 0;
188156 assert(x == 0);
189157
......@@ -202,9 +170,7 @@ fn smallIntAddition() {
202170 assert(result == 0);
203171}
204172
205fn testFloatEquality() {
206 @setFnTest(this);
207
173test "testFloatEquality" {
208174 const x: f64 = 0.012;
209175 const y: f64 = x + 1.0;
210176
test/cases/misc.zig+48-131
......@@ -5,23 +5,21 @@ const cstr = @import("std").cstr;
55// normal comment
66/// this is a documentation comment
77/// doc comment line 2
8fn emptyFunctionWithComments() {
9 @setFnTest(this);
8fn emptyFunctionWithComments() {}
9
10test "emptyFunctionWithComments" {
11 emptyFunctionWithComments();
1012}
1113
1214export fn disabledExternFn() {
1315 @setFnVisible(this, false);
1416}
1517
16fn callDisabledExternFn() {
17 @setFnTest(this);
18
18test "callDisabledExternFn" {
1919 disabledExternFn();
2020}
2121
22fn intTypeBuiltin() {
23 @setFnTest(this);
24
22test "intTypeBuiltin" {
2523 assert(@intType(true, 8) == i8);
2624 assert(@intType(true, 16) == i16);
2725 assert(@intType(true, 32) == i32);
......@@ -55,9 +53,7 @@ const u63 = @intType(false, 63);
5553const i1 = @intType(true, 1);
5654const i63 = @intType(true, 63);
5755
58fn minValueAndMaxValue() {
59 @setFnTest(this);
60
56test "minValueAndMaxValue" {
6157 assert(@maxValue(u1) == 1);
6258 assert(@maxValue(u8) == 255);
6359 assert(@maxValue(u16) == 65535);
......@@ -86,9 +82,7 @@ fn minValueAndMaxValue() {
8682 assert(@minValue(i64) == -9223372036854775808);
8783}
8884
89fn maxValueType() {
90 @setFnTest(this);
91
85test "maxValueType" {
9286 // If the type of @maxValue(i32) was i32 then this implicit cast to
9387 // u32 would not work. But since the value is a number literal,
9488 // it works fine.
......@@ -96,8 +90,7 @@ fn maxValueType() {
9690 assert(x == 2147483647);
9791}
9892
99fn shortCircuit() {
100 @setFnTest(this);
93test "shortCircuit" {
10194 testShortCircuit(false, true);
10295}
10396
......@@ -128,18 +121,14 @@ fn testShortCircuit(f: bool, t: bool) {
128121 assert(hit_4);
129122}
130123
131fn truncate() {
132 @setFnTest(this);
133
124test "truncate" {
134125 assert(testTruncate(0x10fd) == 0xfd);
135126}
136127fn testTruncate(x: u32) -> u8 {
137128 @truncate(u8, x)
138129}
139130
140fn assignToIfVarPtr() {
141 @setFnTest(this);
142
131test "assignToIfVarPtr" {
143132 var maybe_bool: ?bool = true;
144133
145134 if (const *b ?= maybe_bool) {
......@@ -153,27 +142,21 @@ fn first4KeysOfHomeRow() -> []const u8 {
153142 "aoeu"
154143}
155144
156fn ReturnStringFromFunction() {
157 @setFnTest(this);
158
145test "ReturnStringFromFunction" {
159146 assert(mem.eql(u8, first4KeysOfHomeRow(), "aoeu"));
160147}
161148
162149const g1 : i32 = 1233 + 1;
163150var g2 : i32 = 0;
164151
165fn globalVariables() {
166 @setFnTest(this);
167
152test "globalVariables" {
168153 assert(g2 == 0);
169154 g2 = g1;
170155 assert(g2 == 1234);
171156}
172157
173158
174fn memcpyAndMemsetIntrinsics() {
175 @setFnTest(this);
176
159test "memcpyAndMemsetIntrinsics" {
177160 var foo : [20]u8 = undefined;
178161 var bar : [20]u8 = undefined;
179162
......@@ -183,16 +166,12 @@ fn memcpyAndMemsetIntrinsics() {
183166 if (bar[11] != 'A') @unreachable();
184167}
185168
186fn builtinStaticEval() {
187 @setFnTest(this);
188
169test "builtinStaticEval" {
189170 const x : i32 = comptime {1 + 2 + 3};
190171 assert(x == comptime 6);
191172}
192173
193fn slicing() {
194 @setFnTest(this);
195
174test "slicing" {
196175 var array : [20]i32 = undefined;
197176
198177 array[5] = 1234;
......@@ -209,9 +188,7 @@ fn slicing() {
209188}
210189
211190
212fn constantEqualFunctionPointers() {
213 @setFnTest(this);
214
191test "constantEqualFunctionPointers" {
215192 const alias = emptyFn;
216193 assert(comptime {emptyFn == alias});
217194}
......@@ -219,27 +196,19 @@ fn constantEqualFunctionPointers() {
219196fn emptyFn() {}
220197
221198
222fn hexEscape() {
223 @setFnTest(this);
224
199test "hexEscape" {
225200 assert(mem.eql(u8, "\x68\x65\x6c\x6c\x6f", "hello"));
226201}
227202
228fn stringConcatenation() {
229 @setFnTest(this);
230
203test "stringConcatenation" {
231204 assert(mem.eql(u8, "OK" ++ " IT " ++ "WORKED", "OK IT WORKED"));
232205}
233206
234fn arrayMultOperator() {
235 @setFnTest(this);
236
207test "arrayMultOperator" {
237208 assert(mem.eql(u8, "ab" ** 5, "ababababab"));
238209}
239210
240fn stringEscapes() {
241 @setFnTest(this);
242
211test "stringEscapes" {
243212 assert(mem.eql(u8, "\"", "\x22"));
244213 assert(mem.eql(u8, "\'", "\x27"));
245214 assert(mem.eql(u8, "\n", "\x0a"));
......@@ -249,9 +218,7 @@ fn stringEscapes() {
249218 assert(mem.eql(u8, "\u1234\u0069", "\xe1\x88\xb4\x69"));
250219}
251220
252fn multilineString() {
253 @setFnTest(this);
254
221test "multilineString" {
255222 const s1 =
256223 \\one
257224 \\two)
......@@ -261,9 +228,7 @@ fn multilineString() {
261228 assert(mem.eql(u8, s1, s2));
262229}
263230
264fn multilineCString() {
265 @setFnTest(this);
266
231test "multilineCString" {
267232 const s1 =
268233 c\\one
269234 c\\two)
......@@ -274,9 +239,7 @@ fn multilineCString() {
274239}
275240
276241
277fn typeEquality() {
278 @setFnTest(this);
279
242test "typeEquality" {
280243 assert(&const u8 != &u8);
281244}
282245
......@@ -284,22 +247,17 @@ fn typeEquality() {
284247const global_a: i32 = 1234;
285248const global_b: &const i32 = &global_a;
286249const global_c: &const f32 = (&const f32)(global_b);
287fn compileTimeGlobalReinterpret() {
288 @setFnTest(this);
250test "compileTimeGlobalReinterpret" {
289251 const d = (&const i32)(global_c);
290252 assert(*d == 1234);
291253}
292254
293fn explicitCastMaybePointers() {
294 @setFnTest(this);
295
255test "explicitCastMaybePointers" {
296256 const a: ?&i32 = undefined;
297257 const b: ?&f32 = (?&f32)(a);
298258}
299259
300fn genericMallocFree() {
301 @setFnTest(this);
302
260test "genericMallocFree" {
303261 const a = %%memAlloc(u8, 10);
304262 memFree(u8, a);
305263}
......@@ -310,9 +268,7 @@ fn memAlloc(comptime T: type, n: usize) -> %[]T {
310268fn memFree(comptime T: type, memory: []T) { }
311269
312270
313fn castUndefined() {
314 @setFnTest(this);
315
271test "castUndefined" {
316272 const array: [100]u8 = undefined;
317273 const slice = ([]const u8)(array);
318274 testCastUndefined(slice);
......@@ -320,9 +276,7 @@ fn castUndefined() {
320276fn testCastUndefined(x: []const u8) {}
321277
322278
323fn castSmallUnsignedToLargerSigned() {
324 @setFnTest(this);
325
279test "castSmallUnsignedToLargerSigned" {
326280 assert(castSmallUnsignedToLargerSigned1(200) == i16(200));
327281 assert(castSmallUnsignedToLargerSigned2(9999) == i64(9999));
328282}
......@@ -330,9 +284,7 @@ fn castSmallUnsignedToLargerSigned1(x: u8) -> i16 { x }
330284fn castSmallUnsignedToLargerSigned2(x: u16) -> i64 { x }
331285
332286
333fn implicitCastAfterUnreachable() {
334 @setFnTest(this);
335
287test "implicitCastAfterUnreachable" {
336288 assert(outer() == 1234);
337289}
338290fn inner() -> i32 { 1234 }
......@@ -341,9 +293,7 @@ fn outer() -> i64 {
341293}
342294
343295
344fn pointerDereferencing() {
345 @setFnTest(this);
346
296test "pointerDereferencing" {
347297 var x = i32(3);
348298 const y = &x;
349299
......@@ -353,9 +303,7 @@ fn pointerDereferencing() {
353303 assert(*y == 4);
354304}
355305
356fn callResultOfIfElseExpression() {
357 @setFnTest(this);
358
306test "callResultOfIfElseExpression" {
359307 assert(mem.eql(u8, f2(true), "a"));
360308 assert(mem.eql(u8, f2(false), "b"));
361309}
......@@ -366,9 +314,7 @@ fn fA() -> []const u8 { "a" }
366314fn fB() -> []const u8 { "b" }
367315
368316
369fn constExpressionEvalHandlingOfVariables() {
370 @setFnTest(this);
371
317test "constExpressionEvalHandlingOfVariables" {
372318 var x = true;
373319 while (x) {
374320 x = false;
......@@ -377,9 +323,7 @@ fn constExpressionEvalHandlingOfVariables() {
377323
378324
379325
380fn constantEnumInitializationWithDifferingSizes() {
381 @setFnTest(this);
382
326test "constantEnumInitializationWithDifferingSizes" {
383327 test3_1(test3_foo);
384328 test3_2(test3_bar);
385329}
......@@ -413,18 +357,14 @@ fn test3_2(f: Test3Foo) {
413357}
414358
415359
416fn characterLiterals() {
417 @setFnTest(this);
418
360test "characterLiterals" {
419361 assert('\'' == single_quote);
420362}
421363const single_quote = '\'';
422364
423365
424366
425fn takeAddressOfParameter() {
426 @setFnTest(this);
427
367test "takeAddressOfParameter" {
428368 testTakeAddressOfParameter(12.34);
429369}
430370fn testTakeAddressOfParameter(f: f32) {
......@@ -433,9 +373,7 @@ fn testTakeAddressOfParameter(f: f32) {
433373}
434374
435375
436fn intToPtrCast() {
437 @setFnTest(this);
438
376test "intToPtrCast" {
439377 const x = isize(13);
440378 const y = (&u8)(x);
441379 const z = usize(y);
......@@ -443,9 +381,7 @@ fn intToPtrCast() {
443381}
444382
445383
446fn pointerComparison() {
447 @setFnTest(this);
448
384test "pointerComparison" {
449385 const a = ([]const u8)("a");
450386 const b = &a;
451387 assert(ptrEql(b, b));
......@@ -455,9 +391,7 @@ fn ptrEql(a: &const []const u8, b: &const []const u8) -> bool {
455391}
456392
457393
458fn cStringConcatenation() {
459 @setFnTest(this);
460
394test "cStringConcatenation" {
461395 const a = c"OK" ++ c" IT " ++ c"WORKED";
462396 const b = c"OK IT WORKED";
463397
......@@ -470,9 +404,7 @@ fn cStringConcatenation() {
470404 assert(b[len] == 0);
471405}
472406
473fn castSliceToU8Slice() {
474 @setFnTest(this);
475
407test "castSliceToU8Slice" {
476408 assert(@sizeOf(i32) == 4);
477409 var big_thing_array = []i32{1, 2, 3, 4};
478410 const big_thing_slice: []i32 = big_thing_array[0...];
......@@ -492,9 +424,7 @@ fn castSliceToU8Slice() {
492424 assert(bytes[11] == @maxValue(u8));
493425}
494426
495fn pointerToVoidReturnType() {
496 @setFnTest(this);
497
427test "pointerToVoidReturnType" {
498428 %%testPointerToVoidReturnType();
499429}
500430fn testPointerToVoidReturnType() -> %void {
......@@ -507,17 +437,14 @@ fn testPointerToVoidReturnType2() -> &const void {
507437}
508438
509439
510fn nonConstPtrToAliasedType() {
511 @setFnTest(this);
440test "nonConstPtrToAliasedType" {
512441 const int = i32;
513442 assert(?&int == ?&i32);
514443}
515444
516445
517446
518fn array2DConstDoublePtr() {
519 @setFnTest(this);
520
447test "array2DConstDoublePtr" {
521448 const rect_2d_vertexes = [][1]f32 {
522449 []f32{1.0},
523450 []f32{2.0},
......@@ -530,9 +457,7 @@ fn testArray2DConstDoublePtr(ptr: &const f32) {
530457 assert(ptr[1] == 2.0);
531458}
532459
533fn isInteger() {
534 @setFnTest(this);
535
460test "isInteger" {
536461 comptime {
537462 assert(@isInteger(i8));
538463 assert(@isInteger(u8));
......@@ -545,9 +470,7 @@ fn isInteger() {
545470 }
546471}
547472
548fn isFloat() {
549 @setFnTest(this);
550
473test "isFloat" {
551474 comptime {
552475 assert(!@isFloat(i8));
553476 assert(!@isFloat(u8));
......@@ -560,9 +483,7 @@ fn isFloat() {
560483 }
561484}
562485
563fn canImplicitCast() {
564 @setFnTest(this);
565
486test "canImplicitCast" {
566487 comptime {
567488 assert(@canImplicitCast(i64, i32(3)));
568489 assert(!@canImplicitCast(i32, f32(1.234)));
......@@ -570,18 +491,14 @@ fn canImplicitCast() {
570491 }
571492}
572493
573fn typeName() {
574 @setFnTest(this);
575
494test "typeName" {
576495 comptime {
577496 assert(mem.eql(u8, @typeName(i64), "i64"));
578497 assert(mem.eql(u8, @typeName(&usize), "&usize"));
579498 }
580499}
581500
582fn volatileLoadAndStore() {
583 @setFnTest(this);
584
501test "volatileLoadAndStore" {
585502 var number: i32 = 1234;
586503 const ptr = &volatile number;
587504 *ptr += 1;
test/cases/namespace_depends_on_compile_var/index.zig+1-3
......@@ -1,8 +1,6 @@
11const assert = @import("std").debug.assert;
22
3fn namespaceDependsOnCompileVar() {
4 @setFnTest(this);
5
3test "namespaceDependsOnCompileVar" {
64 if (some_namespace.a_bool) {
75 assert(some_namespace.a_bool);
86 } else {
test/cases/null.zig+8-24
......@@ -1,8 +1,6 @@
11const assert = @import("std").debug.assert;
22
3fn nullableType() {
4 @setFnTest(this);
5
3test "nullableType" {
64 const x : ?bool = @generatedCode(true);
75
86 if (const y ?= x) {
......@@ -28,9 +26,7 @@ fn nullableType() {
2826 assert(num == 13);
2927}
3028
31fn assignToIfVarPtr() {
32 @setFnTest(this);
33
29test "assignToIfVarPtr" {
3430 var maybe_bool: ?bool = true;
3531
3632 if (const *b ?= maybe_bool) {
......@@ -40,17 +36,13 @@ fn assignToIfVarPtr() {
4036 assert(??maybe_bool == false);
4137}
4238
43fn rhsMaybeUnwrapReturn() {
44 @setFnTest(this);
45
39test "rhsMaybeUnwrapReturn" {
4640 const x: ?bool = @generatedCode(true);
4741 const y = x ?? return;
4842}
4943
5044
51fn maybeReturn() {
52 @setFnTest(this);
53
45test "maybeReturn" {
5446 maybeReturnImpl();
5547 comptime maybeReturnImpl();
5648}
......@@ -67,9 +59,7 @@ fn foo(x: ?i32) -> ?bool {
6759}
6860
6961
70fn ifVarMaybePointer() {
71 @setFnTest(this);
72
62test "ifVarMaybePointer" {
7363 assert(shouldBeAPlus1(Particle {.a = 14, .b = 1, .c = 1, .d = 1}) == 15);
7464}
7565fn shouldBeAPlus1(p: Particle) -> u64 {
......@@ -90,9 +80,7 @@ const Particle = struct {
9080};
9181
9282
93fn nullLiteralOutsideFunction() {
94 @setFnTest(this);
95
83test "nullLiteralOutsideFunction" {
9684 const is_null = here_is_a_null_literal.context == null;
9785 assert(is_null);
9886
......@@ -107,9 +95,7 @@ const here_is_a_null_literal = SillyStruct {
10795};
10896
10997
110fn testNullRuntime() {
111 @setFnTest(this);
112
98test "testNullRuntime" {
11399 testTestNullRuntime(null);
114100}
115101fn testTestNullRuntime(x: ?i32) {
......@@ -117,9 +103,7 @@ fn testTestNullRuntime(x: ?i32) {
117103 assert(!(x != null));
118104}
119105
120fn nullableVoid() {
121 @setFnTest(this);
122
106test "nullableVoid" {
123107 nullableVoidImpl();
124108 comptime nullableVoidImpl();
125109}
test/cases/pub_enum/index.zig+2-6
......@@ -1,17 +1,13 @@
11const other = @import("cases/pub_enum/other.zig");
22const assert = @import("std").debug.assert;
33
4fn pubEnum() {
5 @setFnTest(this);
6
4test "pubEnum" {
75 pubEnumTest(other.APubEnum.Two);
86}
97fn pubEnumTest(foo: other.APubEnum) {
108 assert(foo == other.APubEnum.Two);
119}
1210
13fn castWithImportedSymbol() {
14 @setFnTest(this);
15
11test "castWithImportedSymbol" {
1612 assert(other.size_t(42) == 42);
1713}
test/cases/sizeof_and_typeof.zig+1-3
......@@ -1,8 +1,6 @@
11const assert = @import("std").debug.assert;
22
3fn sizeofAndTypeOf() {
4 @setFnTest(this);
5
3test "sizeofAndTypeOf" {
64 const y: @typeOf(x) = 120;
75 assert(@sizeOf(@typeOf(y)) == 2);
86}
test/cases/struct.zig+22-56
......@@ -5,27 +5,25 @@ const StructWithNoFields = struct {
55};
66const empty_global_instance = StructWithNoFields {};
77
8fn callStructStaticMethod() {
9 @setFnTest(this);
8test "callStructStaticMethod" {
109 const result = StructWithNoFields.add(3, 4);
1110 assert(result == 7);
1211}
1312
13test "returnEmptyStructInstance" {
14 _ = returnEmptyStructInstance();
15}
1416fn returnEmptyStructInstance() -> StructWithNoFields {
15 @setFnTest(this);
1617 return empty_global_instance;
1718}
1819
1920const should_be_11 = StructWithNoFields.add(5, 6);
2021
21fn invokeStaticMethodInGlobalScope() {
22 @setFnTest(this);
22test "invokeStaticMethodInGlobalScope" {
2323 assert(should_be_11 == 11);
2424}
2525
26fn voidStructFields() {
27 @setFnTest(this);
28
26test "voidStructFields" {
2927 const foo = VoidStructFieldsFoo {
3028 .a = void{},
3129 .b = 1,
......@@ -41,9 +39,7 @@ const VoidStructFieldsFoo = struct {
4139};
4240
4341
44pub fn structs() {
45 @setFnTest(this);
46
42test "fn" {
4743 var foo: StructFoo = undefined;
4844 @memset((&u8)(&foo), 0, @sizeOf(StructFoo));
4945 foo.a += 1;
......@@ -74,9 +70,7 @@ const Val = struct {
7470 x: i32,
7571};
7672
77fn structPointToSelf() {
78 @setFnTest(this);
79
73test "structPointToSelf" {
8074 var root : Node = undefined;
8175 root.val.x = 1;
8276
......@@ -89,9 +83,7 @@ fn structPointToSelf() {
8983 assert(node.next.next.next.val.x == 1);
9084}
9185
92fn structByvalAssign() {
93 @setFnTest(this);
94
86test "structByvalAssign" {
9587 var foo1 : StructFoo = undefined;
9688 var foo2 : StructFoo = undefined;
9789
......@@ -108,9 +100,7 @@ fn structInitializer() {
108100}
109101
110102
111fn fnCallOfStructField() {
112 @setFnTest(this);
113
103test "fnCallOfStructField" {
114104 assert(callStructField(Foo {.ptr = aFunc,}) == 13);
115105}
116106
......@@ -125,9 +115,7 @@ fn callStructField(foo: Foo) -> i32 {
125115}
126116
127117
128fn storeMemberFunctionInVariable() {
129 @setFnTest(this);
130
118test "storeMemberFunctionInVariable" {
131119 const instance = MemberFnTestFoo { .x = 1234, };
132120 const memberFn = MemberFnTestFoo.member;
133121 const result = memberFn(instance);
......@@ -139,17 +127,13 @@ const MemberFnTestFoo = struct {
139127};
140128
141129
142fn callMemberFunctionDirectly() {
143 @setFnTest(this);
144
130test "callMemberFunctionDirectly" {
145131 const instance = MemberFnTestFoo { .x = 1234, };
146132 const result = MemberFnTestFoo.member(instance);
147133 assert(result == 1234);
148134}
149135
150fn memberFunctions() {
151 @setFnTest(this);
152
136test "memberFunctions" {
153137 const r = MemberFnRand {.seed = 1234};
154138 assert(r.getSeed() == 1234);
155139}
......@@ -160,9 +144,7 @@ const MemberFnRand = struct {
160144 }
161145};
162146
163fn returnStructByvalFromFunction() {
164 @setFnTest(this);
165
147test "returnStructByvalFromFunction" {
166148 const bar = makeBar(1234, 5678);
167149 assert(bar.y == 5678);
168150}
......@@ -177,9 +159,7 @@ fn makeBar(x: i32, y: i32) -> Bar {
177159 }
178160}
179161
180fn emptyStructMethodCall() {
181 @setFnTest(this);
182
162test "emptyStructMethodCall" {
183163 const es = EmptyStruct{};
184164 assert(es.method() == 1234);
185165}
......@@ -190,9 +170,7 @@ const EmptyStruct = struct {
190170};
191171
192172
193fn returnEmptyStructFromFn() {
194 @setFnTest(this);
195
173test "returnEmptyStructFromFn" {
196174 _ = testReturnEmptyStructFromFn();
197175}
198176const EmptyStruct2 = struct {};
......@@ -200,9 +178,7 @@ fn testReturnEmptyStructFromFn() -> EmptyStruct2 {
200178 EmptyStruct2 {}
201179}
202180
203fn passSliceOfEmptyStructToFn() {
204 @setFnTest(this);
205
181test "passSliceOfEmptyStructToFn" {
206182 assert(testPassSliceOfEmptyStructToFn([]EmptyStruct2{ EmptyStruct2{} }) == 1);
207183}
208184fn testPassSliceOfEmptyStructToFn(slice: []const EmptyStruct2) -> usize {
......@@ -214,9 +190,7 @@ const APackedStruct = packed struct {
214190 y: u8,
215191};
216192
217fn packedStruct() {
218 @setFnTest(this);
219
193test "packedStruct" {
220194 var foo = APackedStruct {
221195 .x = 1,
222196 .y = 2,
......@@ -242,9 +216,7 @@ const bit_field_1 = BitField1 {
242216 .c = 3,
243217};
244218
245fn bitFieldAccess() {
246 @setFnTest(this);
247
219test "bitFieldAccess" {
248220 var data = bit_field_1;
249221 assert(getA(&data) == 1);
250222 assert(getB(&data) == 2);
......@@ -282,9 +254,7 @@ const Foo96Bits = packed struct {
282254 d: u24,
283255};
284256
285fn packedStruct24Bits() {
286 @setFnTest(this);
287
257test "packedStruct24Bits" {
288258 comptime {
289259 assert(@sizeOf(Foo24Bits) == 3);
290260 assert(@sizeOf(Foo96Bits) == 12);
......@@ -327,9 +297,7 @@ const FooArray24Bits = packed struct {
327297 c: u16,
328298};
329299
330fn packedArray24Bits() {
331 @setFnTest(this);
332
300test "packedArray24Bits" {
333301 comptime {
334302 assert(@sizeOf([9]Foo24Bits) == 9 * 3);
335303 assert(@sizeOf(FooArray24Bits) == 2 + 2 * 3 + 2);
......@@ -379,9 +347,7 @@ const FooArrayOfAligned = packed struct {
379347 a: [2]FooStructAligned,
380348};
381349
382fn alignedArrayOfPackedStruct() {
383 @setFnTest(this);
384
350test "alignedArrayOfPackedStruct" {
385351 comptime {
386352 assert(@sizeOf(FooStructAligned) == 2);
387353 assert(@sizeOf(FooArrayOfAligned) == 2 * 2);
test/cases/struct_contains_slice_of_itself.zig+1-3
......@@ -5,9 +5,7 @@ const Node = struct {
55 children: []Node,
66};
77
8fn structContainsSliceOfItself() {
9 @setFnTest(this);
10
8test "structContainsSliceOfItself" {
119 var nodes = []Node {
1210 Node {
1311 .payload = 1,
test/cases/switch.zig+8-24
......@@ -1,8 +1,6 @@
11const assert = @import("std").debug.assert;
22
3fn switchWithNumbers() {
4 @setFnTest(this);
5
3test "switchWithNumbers" {
64 testSwitchWithNumbers(13);
75}
86
......@@ -15,9 +13,7 @@ fn testSwitchWithNumbers(x: u32) {
1513 assert(result);
1614}
1715
18fn switchWithAllRanges() {
19 @setFnTest(this);
20
16test "switchWithAllRanges" {
2117 assert(testSwitchWithAllRanges(50, 3) == 1);
2218 assert(testSwitchWithAllRanges(101, 0) == 2);
2319 assert(testSwitchWithAllRanges(300, 5) == 3);
......@@ -33,9 +29,7 @@ fn testSwitchWithAllRanges(x: u32, y: u32) -> u32 {
3329 }
3430}
3531
36fn implicitComptimeSwitch() {
37 @setFnTest(this);
38
32test "implicitComptimeSwitch" {
3933 const x = 3 + 4;
4034 const result = switch (x) {
4135 3 => 10,
......@@ -50,9 +44,7 @@ fn implicitComptimeSwitch() {
5044 }
5145}
5246
53fn switchOnEnum() {
54 @setFnTest(this);
55
47test "switchOnEnum" {
5648 const fruit = Fruit.Orange;
5749 nonConstSwitchOnEnum(fruit);
5850}
......@@ -70,9 +62,7 @@ fn nonConstSwitchOnEnum(fruit: Fruit) {
7062}
7163
7264
73fn switchStatement() {
74 @setFnTest(this);
75
65test "switchStatement" {
7666 nonConstSwitch(SwitchStatmentFoo.C);
7767}
7868fn nonConstSwitch(foo: SwitchStatmentFoo) {
......@@ -92,9 +82,7 @@ const SwitchStatmentFoo = enum {
9282};
9383
9484
95fn switchProngWithVar() {
96 @setFnTest(this);
97
85test "switchProngWithVar" {
9886 switchProngWithVarFn(SwitchProngWithVarEnum.One {13});
9987 switchProngWithVarFn(SwitchProngWithVarEnum.Two {13.0});
10088 switchProngWithVarFn(SwitchProngWithVarEnum.Meh);
......@@ -119,9 +107,7 @@ fn switchProngWithVarFn(a: SwitchProngWithVarEnum) {
119107}
120108
121109
122fn switchWithMultipleExpressions() {
123 @setFnTest(this);
124
110test "switchWithMultipleExpressions" {
125111 const x = switch (returnsFive()) {
126112 1, 2, 3 => 1,
127113 4, 5, 6 => 2,
......@@ -149,8 +135,6 @@ fn returnsFalse() -> bool {
149135 Number.Three => |x| return x > 12.34,
150136 }
151137}
152fn switchOnConstEnumWithVar() {
153 @setFnTest(this);
154
138test "switchOnConstEnumWithVar" {
155139 assert(!returnsFalse());
156140}
test/cases/switch_prong_err_enum.zig+1-3
......@@ -21,9 +21,7 @@ fn doThing(form_id: u64) -> %FormValue {
2121 }
2222}
2323
24fn switchProngReturnsErrorEnum() {
25 @setFnTest(this);
26
24test "switchProngReturnsErrorEnum" {
2725 %%doThing(17);
2826 assert(read_count == 1);
2927}
test/cases/switch_prong_implicit_cast.zig+1-3
......@@ -15,9 +15,7 @@ fn foo(id: u64) -> %FormValue {
1515 }
1616}
1717
18fn switchProngImplicitCast() {
19 @setFnTest(this);
20
18test "switchProngImplicitCast" {
2119 const result = switch (%%foo(2)) {
2220 FormValue.One => false,
2321 FormValue.Two => |x| x,
test/cases/this.zig+3-9
......@@ -28,15 +28,11 @@ fn factorial(x: i32) -> i32 {
2828 }
2929}
3030
31fn thisReferToModuleCallPrivateFn() {
32 @setFnTest(this);
33
31test "thisReferToModuleCallPrivateFn" {
3432 assert(module.add(1, 2) == 3);
3533}
3634
37fn thisReferToContainer() {
38 @setFnTest(this);
39
35test "thisReferToContainer" {
4036 var pt = Point(i32) {
4137 .x = 12,
4238 .y = 34,
......@@ -46,8 +42,6 @@ fn thisReferToContainer() {
4642 assert(pt.y == 35);
4743}
4844
49fn thisReferToFn() {
50 @setFnTest(this);
51
45test "thisReferToFn" {
5246 assert(factorial(5) == 120);
5347}
test/cases/try.zig+2-6
......@@ -1,8 +1,6 @@
11const assert = @import("std").debug.assert;
22
3fn tryOnErrorUnion() {
4 @setFnTest(this);
5
3test "tryOnErrorUnion" {
64 tryOnErrorUnionImpl();
75 comptime tryOnErrorUnionImpl();
86
......@@ -25,9 +23,7 @@ fn returnsTen() -> %i32 {
2523 10
2624}
2725
28fn tryWithoutVars() {
29 @setFnTest(this);
30
26test "tryWithoutVars" {
3127 const result1 = try (failIfTrue(true)) {
3228 1
3329 } else {
test/cases/typedef.zig+1-3
......@@ -5,8 +5,6 @@ type int = u8;
55fn add(a: int, b: int) -> int {
66 a + b
77}
8fn typedef() {
9 @setFnTest(this);
10
8test "typedef" {
119 assert(add(12, 34) == 46);
1210}
test/cases/undefined.zig+3-9
......@@ -9,9 +9,7 @@ fn initStaticArray() -> [10]i32 {
99 return array;
1010}
1111const static_array = initStaticArray();
12fn initStaticArrayToUndefined() {
13 @setFnTest(this);
14
12test "initStaticArrayToUndefined" {
1513 assert(static_array[0] == 1);
1614 assert(static_array[4] == 2);
1715 assert(static_array[7] == 3);
......@@ -37,9 +35,7 @@ fn setFooX(foo: &Foo) {
3735 foo.x = 2;
3836}
3937
40fn assignUndefinedToStruct() {
41 @setFnTest(this);
42
38test "assignUndefinedToStruct" {
4339 comptime {
4440 var foo: Foo = undefined;
4541 setFooX(&foo);
......@@ -52,9 +48,7 @@ fn assignUndefinedToStruct() {
5248 }
5349}
5450
55fn assignUndefinedToStructWithMethod() {
56 @setFnTest(this);
57
51test "assignUndefinedToStructWithMethod" {
5852 comptime {
5953 var foo: Foo = undefined;
6054 foo.setFooXMethod();
test/cases/var_args.zig+3-9
......@@ -8,9 +8,7 @@ fn add(args: ...) -> i32 {
88 return sum;
99}
1010
11fn testAddArbitraryArgs() {
12 @setFnTest(this);
13
11test "testAddArbitraryArgs" {
1412 assert(add(i32(1), i32(2), i32(3), i32(4)) == 10);
1513 assert(add(i32(1234)) == 1234);
1614 assert(add() == 0);
......@@ -20,15 +18,11 @@ fn readFirstVarArg(args: ...) {
2018 const value = args[0];
2119}
2220
23fn sendVoidArgToVarArgs() {
24 @setFnTest(this);
25
21test "sendVoidArgToVarArgs" {
2622 readFirstVarArg({});
2723}
2824
29fn testPassArgsDirectly() {
30 @setFnTest(this);
31
25test "testPassArgsDirectly" {
3226 assert(addSomeStuff(i32(1), i32(2), i32(3), i32(4)) == 10);
3327 assert(addSomeStuff(i32(1234)) == 1234);
3428 assert(addSomeStuff() == 0);
test/cases/void.zig+1-3
......@@ -6,9 +6,7 @@ const Foo = struct {
66 c: void,
77};
88
9fn compareVoidWithVoidCompileTimeKnown() {
10 @setFnTest(this);
11
9test "compareVoidWithVoidCompileTimeKnown" {
1210 comptime {
1311 const foo = Foo {
1412 .a = {},
test/cases/while.zig+5-15
......@@ -1,8 +1,6 @@
11const assert = @import("std").debug.assert;
22
3fn whileLoop() {
4 @setFnTest(this);
5
3test "whileLoop" {
64 var i : i32 = 0;
75 while (i < 4) {
86 i += 1;
......@@ -18,9 +16,7 @@ fn whileLoop2() -> i32 {
1816 return 1;
1917 }
2018}
21fn staticEvalWhile() {
22 @setFnTest(this);
23
19test "staticEvalWhile" {
2420 assert(static_eval_while_number == 1);
2521}
2622const static_eval_while_number = staticWhileLoop1();
......@@ -33,9 +29,7 @@ fn staticWhileLoop2() -> i32 {
3329 }
3430}
3531
36fn continueAndBreak() {
37 @setFnTest(this);
38
32test "continueAndBreak" {
3933 runContinueAndBreakTest();
4034 assert(continue_and_break_counter == 8);
4135}
......@@ -53,9 +47,7 @@ fn runContinueAndBreakTest() {
5347 assert(i == 4);
5448}
5549
56fn returnWithImplicitCastFromWhileLoop() {
57 @setFnTest(this);
58
50test "returnWithImplicitCastFromWhileLoop" {
5951 %%returnWithImplicitCastFromWhileLoopTest();
6052}
6153fn returnWithImplicitCastFromWhileLoopTest() -> %void {
......@@ -64,9 +56,7 @@ fn returnWithImplicitCastFromWhileLoopTest() -> %void {
6456 }
6557}
6658
67fn whileWithContinueExpr() {
68 @setFnTest(this);
69
59test "whileWithContinueExpr" {
7060 var sum: i32 = 0;
7161 {var i: i32 = 0; while (i < 10; i += 1) {
7262 if (i == 5) continue;
test/run_tests.cpp+223-88
......@@ -14,6 +14,12 @@
1414#include <stdio.h>
1515#include <stdarg.h>
1616
17enum TestSpecial {
18 TestSpecialNone,
19 TestSpecialSelfHosted,
20 TestSpecialStd,
21};
22
1723struct TestSourceFile {
1824 const char *relative_path;
1925 const char *source_code;
......@@ -32,7 +38,7 @@ struct TestCase {
3238 ZigList<const char *> compiler_args;
3339 ZigList<const char *> program_args;
3440 bool is_parseh;
35 bool is_self_hosted;
41 TestSpecial special;
3642 bool is_release_mode;
3743 bool is_debug_safety;
3844 AllowWarnings allow_warnings;
......@@ -79,7 +85,6 @@ static TestCase *add_simple_case(const char *case_name, const char *source, cons
7985 test_case->compiler_args.append("--strip");
8086 test_case->compiler_args.append("--color");
8187 test_case->compiler_args.append("on");
82 test_case->compiler_args.append("--check-unused");
8388
8489 test_cases.append(test_case);
8590
......@@ -93,9 +98,10 @@ static TestCase *add_simple_case_libc(const char *case_name, const char *source,
9398 return tc;
9499}
95100
96static TestCase *add_compile_fail_case_extra(const char *case_name, const char *source, bool check_unused,
97 size_t count, va_list ap)
98{
101static TestCase *add_compile_fail_case(const char *case_name, const char *source, size_t count, ...) {
102 va_list ap;
103 va_start(ap, count);
104
99105 TestCase *test_case = allocate<TestCase>(1);
100106 test_case->case_name = case_name;
101107 test_case->source_files.resize(1);
......@@ -122,31 +128,11 @@ static TestCase *add_compile_fail_case_extra(const char *case_name, const char *
122128 test_case->compiler_args.append("--release");
123129 test_case->compiler_args.append("--strip");
124130
125 if (check_unused) {
126 test_case->compiler_args.append("--check-unused");
127 }
128
129131 test_cases.append(test_case);
130132
131133 return test_case;
132134}
133135
134static TestCase *add_compile_fail_case_no_check_unused(const char *case_name, const char *source, size_t count, ...) {
135 va_list ap;
136 va_start(ap, count);
137 TestCase *result = add_compile_fail_case_extra(case_name, source, false, count, ap);
138 va_end(ap);
139 return result;
140}
141
142static TestCase *add_compile_fail_case(const char *case_name, const char *source, size_t count, ...) {
143 va_list ap;
144 va_start(ap, count);
145 TestCase *result = add_compile_fail_case_extra(case_name, source, true, count, ap);
146 va_end(ap);
147 return result;
148}
149
150136static void add_debug_safety_case(const char *case_name, const char *source) {
151137 {
152138 TestCase *test_case = allocate<TestCase>(1);
......@@ -674,24 +660,27 @@ static void add_compile_failure_test_cases(void) {
674660 add_compile_fail_case("multiple function definitions", R"SOURCE(
675661fn a() {}
676662fn a() {}
663export fn entry() { a(); }
677664 )SOURCE", 1, ".tmp_source.zig:3:1: error: redefinition of 'a'");
678665
679666 add_compile_fail_case("unreachable with return", R"SOURCE(
680667fn a() -> unreachable {return;}
668export fn entry() { a(); }
681669 )SOURCE", 1, ".tmp_source.zig:2:24: error: expected type 'unreachable', found 'void'");
682670
683671 add_compile_fail_case("control reaches end of non-void function", R"SOURCE(
684672fn a() -> i32 {}
673export fn entry() { _ = a(); }
685674 )SOURCE", 1, ".tmp_source.zig:2:15: error: expected type 'i32', found 'void'");
686675
687676 add_compile_fail_case("undefined function call", R"SOURCE(
688fn a() {
677export fn a() {
689678 b();
690679}
691680 )SOURCE", 1, ".tmp_source.zig:3:5: error: use of undeclared identifier 'b'");
692681
693682 add_compile_fail_case("wrong number of arguments", R"SOURCE(
694fn a() {
683export fn a() {
695684 b(1);
696685}
697686fn b(a: i32, b: i32, c: i32) { }
......@@ -699,14 +688,16 @@ fn b(a: i32, b: i32, c: i32) { }
699688
700689 add_compile_fail_case("invalid type", R"SOURCE(
701690fn a() -> bogus {}
691export fn entry() { _ = a(); }
702692 )SOURCE", 1, ".tmp_source.zig:2:11: error: use of undeclared identifier 'bogus'");
703693
704694 add_compile_fail_case("pointer to unreachable", R"SOURCE(
705695fn a() -> &unreachable {}
696export fn entry() { _ = a(); }
706697 )SOURCE", 1, ".tmp_source.zig:2:12: error: pointer to unreachable not allowed");
707698
708699 add_compile_fail_case("unreachable code", R"SOURCE(
709fn a() {
700export fn a() {
710701 return;
711702 b();
712703}
......@@ -716,10 +707,11 @@ fn b() {}
716707
717708 add_compile_fail_case("bad import", R"SOURCE(
718709const bogus = @import("bogus-does-not-exist.zig");
710export fn entry() { bogus.bogo(); }
719711 )SOURCE", 1, ".tmp_source.zig:2:15: error: unable to find 'bogus-does-not-exist.zig'");
720712
721713 add_compile_fail_case("undeclared identifier", R"SOURCE(
722fn a() {
714export fn a() {
723715 b +
724716 c
725717}
......@@ -730,10 +722,11 @@ fn a() {
730722 add_compile_fail_case("parameter redeclaration", R"SOURCE(
731723fn f(a : i32, a : i32) {
732724}
725export fn entry() { f(1, 2); }
733726 )SOURCE", 1, ".tmp_source.zig:2:15: error: redeclaration of variable 'a'");
734727
735728 add_compile_fail_case("local variable redeclaration", R"SOURCE(
736fn f() {
729export fn f() {
737730 const a : i32 = 0;
738731 const a = 0;
739732}
......@@ -743,71 +736,73 @@ fn f() {
743736fn f(a : i32) {
744737 const a = 0;
745738}
739export fn entry() { f(1); }
746740 )SOURCE", 1, ".tmp_source.zig:3:5: error: redeclaration of variable 'a'");
747741
748742 add_compile_fail_case("variable has wrong type", R"SOURCE(
749fn f() -> i32 {
743export fn f() -> i32 {
750744 const a = c"a";
751745 a
752746}
753747 )SOURCE", 1, ".tmp_source.zig:4:5: error: expected type 'i32', found '&const u8'");
754748
755749 add_compile_fail_case("if condition is bool, not int", R"SOURCE(
756fn f() {
750export fn f() {
757751 if (0) {}
758752}
759753 )SOURCE", 1, ".tmp_source.zig:3:9: error: integer value 0 cannot be implicitly casted to type 'bool'");
760754
761755 add_compile_fail_case("assign unreachable", R"SOURCE(
762fn f() {
756export fn f() {
763757 const a = return;
764758}
765759 )SOURCE", 1, ".tmp_source.zig:3:5: error: unreachable code");
766760
767761 add_compile_fail_case("unreachable variable", R"SOURCE(
768fn f() {
762export fn f() {
769763 const a : unreachable = {};
770764}
771765 )SOURCE", 1, ".tmp_source.zig:3:15: error: variable of type 'unreachable' not allowed");
772766
773767 add_compile_fail_case("unreachable parameter", R"SOURCE(
774768fn f(a : unreachable) {}
769export fn entry() { f(); }
775770 )SOURCE", 1, ".tmp_source.zig:2:10: error: parameter of type 'unreachable' not allowed");
776771
777772 add_compile_fail_case("bad assignment target", R"SOURCE(
778fn f() {
773export fn f() {
779774 3 = 3;
780775}
781776 )SOURCE", 1, ".tmp_source.zig:3:7: error: cannot assign to constant");
782777
783778 add_compile_fail_case("assign to constant variable", R"SOURCE(
784fn f() {
779export fn f() {
785780 const a = 3;
786781 a = 4;
787782}
788783 )SOURCE", 1, ".tmp_source.zig:4:7: error: cannot assign to constant");
789784
790785 add_compile_fail_case("use of undeclared identifier", R"SOURCE(
791fn f() {
786export fn f() {
792787 b = 3;
793788}
794789 )SOURCE", 1, ".tmp_source.zig:3:5: error: use of undeclared identifier 'b'");
795790
796791 add_compile_fail_case("const is a statement, not an expression", R"SOURCE(
797fn f() {
792export fn f() {
798793 (const a = 0);
799794}
800795 )SOURCE", 1, ".tmp_source.zig:3:6: error: invalid token: 'const'");
801796
802797 add_compile_fail_case("array access of undeclared identifier", R"SOURCE(
803fn f() {
798export fn f() {
804799 i[i] = i[i];
805800}
806801 )SOURCE", 2, ".tmp_source.zig:3:5: error: use of undeclared identifier 'i'",
807802 ".tmp_source.zig:3:12: error: use of undeclared identifier 'i'");
808803
809804 add_compile_fail_case("array access of non array", R"SOURCE(
810fn f() {
805export fn f() {
811806 var bad : bool = undefined;
812807 bad[bad] = bad[bad];
813808}
......@@ -815,7 +810,7 @@ fn f() {
815810 ".tmp_source.zig:4:19: error: array access of non-array type 'bool'");
816811
817812 add_compile_fail_case("array access with non integer index", R"SOURCE(
818fn f() {
813export fn f() {
819814 var array = "aoeu";
820815 var bad = false;
821816 array[bad] = array[bad];
......@@ -828,6 +823,7 @@ const x : i32 = 99;
828823fn f() {
829824 x = 1;
830825}
826export fn entry() { f(); }
831827 )SOURCE", 1, ".tmp_source.zig:4:7: error: cannot assign to constant");
832828
833829
......@@ -836,22 +832,25 @@ fn f(b: bool) {
836832 const x : i32 = if (b) { 1 };
837833 const y = if (b) { i32(1) };
838834}
835export fn entry() { f(true); }
839836 )SOURCE", 2, ".tmp_source.zig:3:30: error: integer value 1 cannot be implicitly casted to type 'void'",
840837 ".tmp_source.zig:4:15: error: incompatible types: 'i32' and 'void'");
841838
842839 add_compile_fail_case("direct struct loop", R"SOURCE(
843840const A = struct { a : A, };
841export fn entry() -> usize { @sizeOf(A) }
844842 )SOURCE", 1, ".tmp_source.zig:2:11: error: struct 'A' contains itself");
845843
846844 add_compile_fail_case("indirect struct loop", R"SOURCE(
847845const A = struct { b : B, };
848846const B = struct { c : C, };
849847const C = struct { a : A, };
848export fn entry() -> usize { @sizeOf(A) }
850849 )SOURCE", 1, ".tmp_source.zig:2:11: error: struct 'A' contains itself");
851850
852851 add_compile_fail_case("invalid struct field", R"SOURCE(
853852const A = struct { x : i32, };
854fn f() {
853export fn f() {
855854 var a : A = undefined;
856855 a.foo = 1;
857856 const y = a.bar;
......@@ -873,7 +872,9 @@ const A = enum {};
873872 add_compile_fail_case("redefinition of global variables", R"SOURCE(
874873var a : i32 = 1;
875874var a : i32 = 2;
876 )SOURCE", 1, ".tmp_source.zig:3:1: error: redeclaration of variable 'a'");
875 )SOURCE", 2,
876 ".tmp_source.zig:3:1: error: redefinition of 'a'",
877 ".tmp_source.zig:2:1: note: previous definition is here");
877878
878879 add_compile_fail_case("byvalue struct parameter in exported function", R"SOURCE(
879880const A = struct { x : i32, };
......@@ -893,7 +894,7 @@ const A = struct {
893894 y : i32,
894895 z : i32,
895896};
896fn f() {
897export fn f() {
897898 const a = A {
898899 .z = 1,
899900 .y = 2,
......@@ -909,7 +910,7 @@ const A = struct {
909910 y : i32,
910911 z : i32,
911912};
912fn f() {
913export fn f() {
913914 // we want the error on the '{' not the 'A' because
914915 // the A could be a complicated expression
915916 const a = A {
......@@ -925,7 +926,7 @@ const A = struct {
925926 y : i32,
926927 z : i32,
927928};
928fn f() {
929export fn f() {
929930 const a = A {
930931 .z = 4,
931932 .y = 2,
......@@ -935,19 +936,19 @@ fn f() {
935936 )SOURCE", 1, ".tmp_source.zig:11:9: error: no member named 'foo' in 'A'");
936937
937938 add_compile_fail_case("invalid break expression", R"SOURCE(
938fn f() {
939export fn f() {
939940 break;
940941}
941942 )SOURCE", 1, ".tmp_source.zig:3:5: error: 'break' expression outside loop");
942943
943944 add_compile_fail_case("invalid continue expression", R"SOURCE(
944fn f() {
945export fn f() {
945946 continue;
946947}
947948 )SOURCE", 1, ".tmp_source.zig:3:5: error: 'continue' expression outside loop");
948949
949950 add_compile_fail_case("invalid maybe type", R"SOURCE(
950fn f() {
951export fn f() {
951952 if (const x ?= true) { }
952953}
953954 )SOURCE", 1, ".tmp_source.zig:3:20: error: expected nullable type, found 'bool'");
......@@ -956,42 +957,39 @@ fn f() {
956957fn f() -> i32 {
957958 i32(return 1)
958959}
960export fn entry() { _ = f(); }
959961 )SOURCE", 1, ".tmp_source.zig:3:8: error: unreachable code");
960962
961963 add_compile_fail_case("invalid builtin fn", R"SOURCE(
962964fn f() -> @bogus(foo) {
963965}
966export fn entry() { _ = f(); }
964967 )SOURCE", 1, ".tmp_source.zig:2:11: error: invalid builtin function: 'bogus'");
965968
966969 add_compile_fail_case("top level decl dependency loop", R"SOURCE(
967970const a : @typeOf(b) = 0;
968971const b : @typeOf(a) = 0;
972export fn entry() {
973 const c = a + b;
974}
969975 )SOURCE", 1, ".tmp_source.zig:2:1: error: 'a' depends on itself");
970976
971977 add_compile_fail_case("noalias on non pointer param", R"SOURCE(
972978fn f(noalias x: i32) {}
979export fn entry() { f(1234); }
973980 )SOURCE", 1, ".tmp_source.zig:2:6: error: noalias on non-pointer parameter");
974981
975982 add_compile_fail_case("struct init syntax for array", R"SOURCE(
976983const foo = []u16{.x = 1024,};
984export fn entry() -> usize { @sizeOf(@typeOf(foo)) }
977985 )SOURCE", 1, ".tmp_source.zig:2:18: error: type '[]u16' does not support struct initialization syntax");
978986
979987 add_compile_fail_case("type variables must be constant", R"SOURCE(
980988var foo = u8;
981 )SOURCE", 1, ".tmp_source.zig:2:1: error: variable of type 'type' must be constant");
982
983 add_compile_fail_case("variables shadowing types", R"SOURCE(
984const Foo = struct {};
985const Bar = struct {};
986
987fn f(Foo: i32) {
988 var Bar : i32 = undefined;
989export fn entry() -> foo {
990 return 1;
989991}
990 )SOURCE", 4,
991 ".tmp_source.zig:5:6: error: redeclaration of variable 'Foo'",
992 ".tmp_source.zig:2:1: note: previous declaration is here",
993 ".tmp_source.zig:6:5: error: redeclaration of variable 'Bar'",
994 ".tmp_source.zig:3:1: note: previous declaration is here");
992 )SOURCE", 1, ".tmp_source.zig:2:1: error: variable of type 'type' must be constant");
995993
996994 add_compile_fail_case("multiple else prongs in a switch", R"SOURCE(
997995fn f(x: u32) {
......@@ -1000,28 +998,36 @@ fn f(x: u32) {
1000998 else => true,
1001999 else => true,
10021000 };
1001}
1002export fn entry() {
1003 f(1234);
10031004}
10041005 )SOURCE", 1, ".tmp_source.zig:6:9: error: multiple else prongs in switch expression");
10051006
10061007 add_compile_fail_case("global variable initializer must be constant expression", R"SOURCE(
10071008extern fn foo() -> i32;
10081009const x = foo();
1010export fn entry() -> i32 { x }
10091011 )SOURCE", 1, ".tmp_source.zig:3:11: error: unable to evaluate constant expression");
10101012
10111013 add_compile_fail_case("array concatenation with wrong type", R"SOURCE(
10121014const src = "aoeu";
10131015const derp = usize(1234);
10141016const a = derp ++ "foo";
1017
1018export fn entry() -> usize { @sizeOf(@typeOf(a)) }
10151019 )SOURCE", 1, ".tmp_source.zig:4:11: error: expected array or C string literal, found 'usize'");
10161020
10171021 add_compile_fail_case("non compile time array concatenation", R"SOURCE(
10181022fn f(s: [10]u8) -> []u8 {
10191023 s ++ "foo"
10201024}
1025export fn entry() -> usize { @sizeOf(@typeOf(f)) }
10211026 )SOURCE", 1, ".tmp_source.zig:3:5: error: unable to evaluate constant expression");
10221027
10231028 add_compile_fail_case("@cImport with bogus include", R"SOURCE(
10241029const c = @cImport(@cInclude("bogus.h"));
1030export fn entry() -> usize { @sizeOf(@typeOf(c.bogo)) }
10251031 )SOURCE", 2, ".tmp_source.zig:2:11: error: C import failed",
10261032 ".h:1:10: note: 'bogus.h' file not found");
10271033
......@@ -1029,14 +1035,17 @@ const c = @cImport(@cInclude("bogus.h"));
10291035const x = 3;
10301036const y = &x;
10311037fn foo() -> &const i32 { y }
1038export fn entry() -> usize { @sizeOf(@typeOf(foo)) }
10321039 )SOURCE", 1, ".tmp_source.zig:4:26: error: expected type '&const i32', found '&const (integer literal)'");
10331040
10341041 add_compile_fail_case("integer overflow error", R"SOURCE(
10351042const x : u8 = 300;
1043export fn entry() -> usize { @sizeOf(@typeOf(x)) }
10361044 )SOURCE", 1, ".tmp_source.zig:2:16: error: integer value 300 cannot be implicitly casted to type 'u8'");
10371045
10381046 add_compile_fail_case("incompatible number literals", R"SOURCE(
10391047const x = 2 == 2.0;
1048export fn entry() -> usize { @sizeOf(@typeOf(x)) }
10401049 )SOURCE", 1, ".tmp_source.zig:2:11: error: integer value 2 cannot be implicitly casted to type '(float literal)'");
10411050
10421051 add_compile_fail_case("missing function call param", R"SOURCE(
......@@ -1061,11 +1070,14 @@ const members = []member_fn_type {
10611070fn f(foo: Foo, index: usize) {
10621071 const result = members[index]();
10631072}
1073
1074export fn entry() -> usize { @sizeOf(@typeOf(f)) }
10641075 )SOURCE", 1, ".tmp_source.zig:21:34: error: expected 1 arguments, found 0");
10651076
10661077 add_compile_fail_case("missing function name and param name", R"SOURCE(
10671078fn () {}
10681079fn f(i32) {}
1080export fn entry() -> usize { @sizeOf(@typeOf(f)) }
10691081 )SOURCE", 2,
10701082 ".tmp_source.zig:2:1: error: missing function name",
10711083 ".tmp_source.zig:3:6: error: missing parameter name");
......@@ -1075,6 +1087,7 @@ const fns = []fn(){ a, b, c };
10751087fn a() -> i32 {0}
10761088fn b() -> i32 {1}
10771089fn c() -> i32 {2}
1090export fn entry() -> usize { @sizeOf(@typeOf(fns)) }
10781091 )SOURCE", 1, ".tmp_source.zig:2:21: error: expected type 'fn()', found 'fn() -> i32'");
10791092
10801093 add_compile_fail_case("extern function pointer mismatch", R"SOURCE(
......@@ -1082,18 +1095,23 @@ const fns = [](fn(i32)->i32){ a, b, c };
10821095pub fn a(x: i32) -> i32 {x + 0}
10831096pub fn b(x: i32) -> i32 {x + 1}
10841097export fn c(x: i32) -> i32 {x + 2}
1098
1099export fn entry() -> usize { @sizeOf(@typeOf(fns)) }
10851100 )SOURCE", 1, ".tmp_source.zig:2:37: error: expected type 'fn(i32) -> i32', found 'extern fn(i32) -> i32'");
10861101
10871102
10881103 add_compile_fail_case("implicit cast from f64 to f32", R"SOURCE(
10891104const x : f64 = 1.0;
10901105const y : f32 = x;
1106
1107export fn entry() -> usize { @sizeOf(@typeOf(y)) }
10911108 )SOURCE", 1, ".tmp_source.zig:3:17: error: expected type 'f32', found 'f64'");
10921109
10931110
10941111 add_compile_fail_case("colliding invalid top level functions", R"SOURCE(
10951112fn func() -> bogus {}
10961113fn func() -> bogus {}
1114export fn entry() -> usize { @sizeOf(@typeOf(func)) }
10971115 )SOURCE", 2,
10981116 ".tmp_source.zig:3:1: error: redefinition of 'func'",
10991117 ".tmp_source.zig:2:14: error: use of undeclared identifier 'bogus'");
......@@ -1101,6 +1119,7 @@ fn func() -> bogus {}
11011119
11021120 add_compile_fail_case("bogus compile var", R"SOURCE(
11031121const x = @compileVar("bogus");
1122export fn entry() -> usize { @sizeOf(@typeOf(x)) }
11041123 )SOURCE", 1, ".tmp_source.zig:2:23: error: unrecognized compile variable: 'bogus'");
11051124
11061125
......@@ -1110,6 +1129,8 @@ const Foo = struct {
11101129};
11111130var global_var: usize = 1;
11121131fn get() -> usize { global_var }
1132
1133export fn entry() -> usize { @sizeOf(@typeOf(Foo)) }
11131134 )SOURCE", 3,
11141135 ".tmp_source.zig:6:21: error: unable to evaluate constant expression",
11151136 ".tmp_source.zig:3:12: note: called from here",
......@@ -1121,6 +1142,8 @@ const Foo = struct {
11211142 field: i32,
11221143};
11231144const x = Foo {.field = 1} + Foo {.field = 2};
1145
1146export fn entry() -> usize { @sizeOf(@typeOf(x)) }
11241147 )SOURCE", 1, ".tmp_source.zig:5:28: error: invalid operands to binary expression: 'Foo' and 'Foo'");
11251148
11261149
......@@ -1129,6 +1152,11 @@ const lit_int_x = 1 / 0;
11291152const lit_float_x = 1.0 / 0.0;
11301153const int_x = i32(1) / i32(0);
11311154const float_x = f32(1.0) / f32(0.0);
1155
1156export fn entry1() -> usize { @sizeOf(@typeOf(lit_int_x)) }
1157export fn entry2() -> usize { @sizeOf(@typeOf(lit_float_x)) }
1158export fn entry3() -> usize { @sizeOf(@typeOf(int_x)) }
1159export fn entry4() -> usize { @sizeOf(@typeOf(float_x)) }
11321160 )SOURCE", 4,
11331161 ".tmp_source.zig:2:21: error: division by zero is undefined",
11341162 ".tmp_source.zig:3:25: error: division by zero is undefined",
......@@ -1150,16 +1178,22 @@ fn f(n: Number) -> i32 {
11501178 Number.Three => i32(3),
11511179 }
11521180}
1181
1182export fn entry() -> usize { @sizeOf(@typeOf(f)) }
11531183 )SOURCE", 1, ".tmp_source.zig:9:5: error: enumeration value 'Number.Four' not handled in switch");
11541184
11551185 add_compile_fail_case("normal string with newline", R"SOURCE(
11561186const foo = "a
11571187b";
1188
1189export fn entry() -> usize { @sizeOf(@typeOf(foo)) }
11581190 )SOURCE", 1, ".tmp_source.zig:2:13: error: newline not allowed in string literal");
11591191
11601192 add_compile_fail_case("invalid comparison for function pointers", R"SOURCE(
11611193fn foo() {}
11621194const invalid = foo > foo;
1195
1196export fn entry() -> usize { @sizeOf(@typeOf(invalid)) }
11631197 )SOURCE", 1, ".tmp_source.zig:3:21: error: operator not allowed for type 'fn()'");
11641198
11651199 add_compile_fail_case("generic function instance with non-constant expression", R"SOURCE(
......@@ -1167,10 +1201,12 @@ fn foo(comptime x: i32, y: i32) -> i32 { return x + y; }
11671201fn test1(a: i32, b: i32) -> i32 {
11681202 return foo(a, b);
11691203}
1204
1205export fn entry() -> usize { @sizeOf(@typeOf(test1)) }
11701206 )SOURCE", 1, ".tmp_source.zig:4:16: error: unable to evaluate constant expression");
11711207
11721208 add_compile_fail_case("goto jumping into block", R"SOURCE(
1173fn f() {
1209export fn f() {
11741210 {
11751211a_label:
11761212 }
......@@ -1185,15 +1221,19 @@ fn f(b: bool) {
11851221label:
11861222}
11871223fn derp(){}
1224
1225export fn entry() -> usize { @sizeOf(@typeOf(f)) }
11881226 )SOURCE", 1, ".tmp_source.zig:3:12: error: no label in scope named 'label'");
11891227
11901228 add_compile_fail_case("assign null to non-nullable pointer", R"SOURCE(
11911229const a: &u8 = null;
1230
1231export fn entry() -> usize { @sizeOf(@typeOf(a)) }
11921232 )SOURCE", 1, ".tmp_source.zig:2:16: error: expected type '&u8', found '(null)'");
11931233
11941234 add_compile_fail_case("indexing an array of size zero", R"SOURCE(
11951235const array = []u8{};
1196fn foo() {
1236export fn foo() {
11971237 const pointer = &array[0];
11981238}
11991239 )SOURCE", 1, ".tmp_source.zig:4:27: error: index 0 outside array of size 0");
......@@ -1203,12 +1243,16 @@ const y = foo(0);
12031243fn foo(x: i32) -> i32 {
12041244 1 / x
12051245}
1246
1247export fn entry() -> usize { @sizeOf(@typeOf(y)) }
12061248 )SOURCE", 2,
12071249 ".tmp_source.zig:4:7: error: division by zero is undefined",
12081250 ".tmp_source.zig:2:14: note: called from here");
12091251
12101252 add_compile_fail_case("branch on undefined value", R"SOURCE(
12111253const x = if (undefined) true else false;
1254
1255export fn entry() -> usize { @sizeOf(@typeOf(x)) }
12121256 )SOURCE", 1, ".tmp_source.zig:2:15: error: use of undefined value");
12131257
12141258
......@@ -1217,12 +1261,16 @@ const seventh_fib_number = fibbonaci(7);
12171261fn fibbonaci(x: i32) -> i32 {
12181262 return fibbonaci(x - 1) + fibbonaci(x - 2);
12191263}
1264
1265export fn entry() -> usize { @sizeOf(@typeOf(seventh_fib_number)) }
12201266 )SOURCE", 2,
12211267 ".tmp_source.zig:4:21: error: evaluation exceeded 1000 backwards branches",
12221268 ".tmp_source.zig:4:21: note: called from here");
12231269
12241270 add_compile_fail_case("@embedFile with bogus file", R"SOURCE(
12251271const resource = @embedFile("bogus.txt");
1272
1273export fn entry() -> usize { @sizeOf(@typeOf(resource)) }
12261274 )SOURCE", 1, ".tmp_source.zig:2:29: error: unable to find './bogus.txt'");
12271275
12281276
......@@ -1232,6 +1280,8 @@ const Foo = struct {
12321280};
12331281const a = Foo {.x = get_it()};
12341282extern fn get_it() -> i32;
1283
1284export fn entry() -> usize { @sizeOf(@typeOf(a)) }
12351285 )SOURCE", 1, ".tmp_source.zig:5:21: error: unable to evaluate constant expression");
12361286
12371287 add_compile_fail_case("non-const expression function call with struct return value outside function", R"SOURCE(
......@@ -1245,12 +1295,13 @@ fn get_it() -> Foo {
12451295}
12461296var global_side_effect = false;
12471297
1298export fn entry() -> usize { @sizeOf(@typeOf(a)) }
12481299 )SOURCE", 2,
12491300 ".tmp_source.zig:7:24: error: unable to evaluate constant expression",
12501301 ".tmp_source.zig:5:17: note: called from here");
12511302
12521303 add_compile_fail_case("undeclared identifier error should mark fn as impure", R"SOURCE(
1253fn foo() {
1304export fn foo() {
12541305 test_a_thing();
12551306}
12561307fn test_a_thing() {
......@@ -1269,12 +1320,15 @@ const EnumWithData = enum {
12691320fn bad_eql_2(a: EnumWithData, b: EnumWithData) -> bool {
12701321 a == b
12711322}
1323
1324export fn entry1() -> usize { @sizeOf(@typeOf(bad_eql_1)) }
1325export fn entry2() -> usize { @sizeOf(@typeOf(bad_eql_2)) }
12721326 )SOURCE", 2,
12731327 ".tmp_source.zig:3:7: error: operator not allowed for type '[]u8'",
12741328 ".tmp_source.zig:10:7: error: operator not allowed for type 'EnumWithData'");
12751329
12761330 add_compile_fail_case("non-const switch number literal", R"SOURCE(
1277fn foo() {
1331export fn foo() {
12781332 const x = switch (bar()) {
12791333 1, 2 => 1,
12801334 3, 4 => 2,
......@@ -1284,18 +1338,17 @@ fn foo() {
12841338fn bar() -> i32 {
12851339 2
12861340}
1287
12881341 )SOURCE", 1, ".tmp_source.zig:3:15: error: unable to infer expression type");
12891342
12901343 add_compile_fail_case("atomic orderings of cmpxchg - failure stricter than success", R"SOURCE(
1291fn f() {
1344export fn f() {
12921345 var x: i32 = 1234;
12931346 while (!@cmpxchg(&x, 1234, 5678, AtomicOrder.Monotonic, AtomicOrder.SeqCst)) {}
12941347}
12951348 )SOURCE", 1, ".tmp_source.zig:4:72: error: failure atomic ordering must be no stricter than success");
12961349
12971350 add_compile_fail_case("atomic orderings of cmpxchg - success Monotonic or stricter", R"SOURCE(
1298fn f() {
1351export fn f() {
12991352 var x: i32 = 1234;
13001353 while (!@cmpxchg(&x, 1234, 5678, AtomicOrder.Unordered, AtomicOrder.Unordered)) {}
13011354}
......@@ -1306,6 +1359,8 @@ const y = neg(-128);
13061359fn neg(x: i8) -> i8 {
13071360 -x
13081361}
1362
1363export fn entry() -> usize { @sizeOf(@typeOf(y)) }
13091364 )SOURCE", 2,
13101365 ".tmp_source.zig:4:5: error: negation caused overflow",
13111366 ".tmp_source.zig:2:14: note: called from here");
......@@ -1315,6 +1370,8 @@ const y = add(65530, 10);
13151370fn add(a: u16, b: u16) -> u16 {
13161371 a + b
13171372}
1373
1374export fn entry() -> usize { @sizeOf(@typeOf(y)) }
13181375 )SOURCE", 2,
13191376 ".tmp_source.zig:4:7: error: operation caused overflow",
13201377 ".tmp_source.zig:2:14: note: called from here");
......@@ -1325,6 +1382,8 @@ const y = sub(10, 20);
13251382fn sub(a: u16, b: u16) -> u16 {
13261383 a - b
13271384}
1385
1386export fn entry() -> usize { @sizeOf(@typeOf(y)) }
13281387 )SOURCE", 2,
13291388 ".tmp_source.zig:4:7: error: operation caused overflow",
13301389 ".tmp_source.zig:2:14: note: called from here");
......@@ -1334,6 +1393,8 @@ const y = mul(300, 6000);
13341393fn mul(a: u16, b: u16) -> u16 {
13351394 a * b
13361395}
1396
1397export fn entry() -> usize { @sizeOf(@typeOf(y)) }
13371398 )SOURCE", 2,
13381399 ".tmp_source.zig:4:7: error: operation caused overflow",
13391400 ".tmp_source.zig:2:14: note: called from here");
......@@ -1343,10 +1404,12 @@ fn f() -> i8 {
13431404 const x: u32 = 10;
13441405 @truncate(i8, x)
13451406}
1407
1408export fn entry() -> usize { @sizeOf(@typeOf(f)) }
13461409 )SOURCE", 1, ".tmp_source.zig:4:19: error: expected signed integer type, found 'u32'");
13471410
13481411 add_compile_fail_case("%return in function with non error return type", R"SOURCE(
1349fn f() {
1412export fn f() {
13501413 %return something();
13511414}
13521415fn something() -> %void { }
......@@ -1361,7 +1424,7 @@ pub fn main(args: [][]u8) { }
13611424 add_compile_fail_case("invalid pointer for var type", R"SOURCE(
13621425extern fn ext() -> usize;
13631426var bytes: [ext()]u8 = undefined;
1364fn f() {
1427export fn f() {
13651428 for (bytes) |*b, i| {
13661429 *b = u8(i);
13671430 }
......@@ -1379,10 +1442,11 @@ extern fn foo(comptime x: i32, y: i32) -> i32;
13791442fn f() -> i32 {
13801443 foo(1, 2)
13811444}
1445export fn entry() -> usize { @sizeOf(@typeOf(f)) }
13821446 )SOURCE", 1, ".tmp_source.zig:2:15: error: comptime parameter not allowed in extern function");
13831447
13841448 add_compile_fail_case("convert fixed size array to slice with invalid size", R"SOURCE(
1385fn f() {
1449export fn f() {
13861450 var array: [5]u8 = undefined;
13871451 var foo = ([]const u32)(array)[0];
13881452}
......@@ -1403,7 +1467,7 @@ pub fn SmallList(comptime T: type, comptime STATIC_SIZE: usize) -> type {
14031467 }
14041468}
14051469
1406fn function_with_return_type_type() {
1470export fn function_with_return_type_type() {
14071471 var list: List(i32) = undefined;
14081472 list.length = 10;
14091473}
......@@ -1417,6 +1481,7 @@ var self = "aoeu";
14171481fn f(m: []const u8) {
14181482 m.copy(u8, self[0...], m);
14191483}
1484export fn entry() -> usize { @sizeOf(@typeOf(f)) }
14201485 )SOURCE", 1, ".tmp_source.zig:4:6: error: no member named 'copy' in '[]const u8'");
14211486
14221487 add_compile_fail_case("wrong number of arguments for method fn call", R"SOURCE(
......@@ -1427,17 +1492,18 @@ fn f(foo: &const Foo) {
14271492
14281493 foo.method(1, 2);
14291494}
1495export fn entry() -> usize { @sizeOf(@typeOf(f)) }
14301496 )SOURCE", 1, ".tmp_source.zig:7:15: error: expected 2 arguments, found 3");
14311497
14321498 add_compile_fail_case("assign through constant pointer", R"SOURCE(
1433fn f() {
1499export fn f() {
14341500 var cstr = c"Hat";
14351501 cstr[0] = 'W';
14361502}
14371503 )SOURCE", 1, ".tmp_source.zig:4:11: error: cannot assign to constant");
14381504
14391505 add_compile_fail_case("assign through constant slice", R"SOURCE(
1440pub fn f() {
1506export fn f() {
14411507 var cstr: []const u8 = "Hat";
14421508 cstr[0] = 'W';
14431509}
......@@ -1451,6 +1517,7 @@ pub fn main(args: [][]bogus) -> %void {}
14511517fn foo(blah: []u8) {
14521518 for (blah) { }
14531519}
1520export fn entry() -> usize { @sizeOf(@typeOf(foo)) }
14541521 )SOURCE", 1, ".tmp_source.zig:3:5: error: for loop expression missing element parameter");
14551522
14561523 add_compile_fail_case("misspelled type with pointer only reference", R"SOURCE(
......@@ -1482,6 +1549,8 @@ fn foo() {
14821549 jll.init(1234);
14831550 var jd = JsonNode {.kind = JsonType.JSONArray , .jobject = JsonOA.JSONArray {jll} };
14841551}
1552
1553export fn entry() -> usize { @sizeOf(@typeOf(foo)) }
14851554 )SOURCE", 1, ".tmp_source.zig:6:16: error: use of undeclared identifier 'JsonList'");
14861555
14871556 add_compile_fail_case("method call with first arg type primitive", R"SOURCE(
......@@ -1495,7 +1564,7 @@ const Foo = struct {
14951564 }
14961565};
14971566
1498fn f() {
1567export fn f() {
14991568 const derp = Foo.init(3);
15001569
15011570 derp.init();
......@@ -1523,7 +1592,7 @@ pub const Allocator = struct {
15231592 field: i32,
15241593};
15251594
1526fn foo() {
1595export fn foo() {
15271596 var x = List.init(&global_allocator);
15281597 x.init();
15291598}
......@@ -1533,13 +1602,15 @@ fn foo() {
15331602const TINY_QUANTUM_SHIFT = 4;
15341603const TINY_QUANTUM_SIZE = 1 << TINY_QUANTUM_SHIFT;
15351604var block_aligned_stuff: usize = (4 + TINY_QUANTUM_SIZE) & ~(TINY_QUANTUM_SIZE - 1);
1605
1606export fn entry() -> usize { @sizeOf(@typeOf(block_aligned_stuff)) }
15361607 )SOURCE", 1, ".tmp_source.zig:4:60: error: unable to perform binary not operation on type '(integer literal)'");
15371608
15381609 {
15391610 TestCase *tc = add_compile_fail_case("multiple files with private function error", R"SOURCE(
15401611const foo = @import("foo.zig");
15411612
1542fn callPrivFunction() {
1613export fn callPrivFunction() {
15431614 foo.privateFunction();
15441615}
15451616 )SOURCE", 2,
......@@ -1554,13 +1625,15 @@ fn privateFunction() { }
15541625 add_compile_fail_case("container init with non-type", R"SOURCE(
15551626const zero: i32 = 0;
15561627const a = zero{1};
1628
1629export fn entry() -> usize { @sizeOf(@typeOf(a)) }
15571630 )SOURCE", 1, ".tmp_source.zig:3:11: error: expected type, found 'i32'");
15581631
15591632 add_compile_fail_case("assign to constant field", R"SOURCE(
15601633const Foo = struct {
15611634 field: i32,
15621635};
1563fn derp() {
1636export fn derp() {
15641637 const f = Foo {.field = 1234,};
15651638 f.field = 0;
15661639}
......@@ -1580,6 +1653,8 @@ fn canFail() -> %void { }
15801653pub fn maybeInt() -> ?i32 {
15811654 return 0;
15821655}
1656
1657export fn entry() -> usize { @sizeOf(@typeOf(testTrickyDefer)) }
15831658 )SOURCE", 1, ".tmp_source.zig:5:11: error: cannot return from defer expression");
15841659
15851660 add_compile_fail_case("attempt to access var args out of bounds", R"SOURCE(
......@@ -1590,6 +1665,8 @@ fn add(args: ...) -> i32 {
15901665fn foo() -> i32 {
15911666 add(i32(1234))
15921667}
1668
1669export fn entry() -> usize { @sizeOf(@typeOf(foo)) }
15931670 )SOURCE", 2,
15941671 ".tmp_source.zig:3:19: error: index 1 outside argument list of size 1",
15951672 ".tmp_source.zig:7:8: note: called from here");
......@@ -1606,10 +1683,12 @@ fn add(args: ...) -> i32 {
16061683fn bar() -> i32 {
16071684 add(1, 2, 3, 4)
16081685}
1686
1687export fn entry() -> usize { @sizeOf(@typeOf(bar)) }
16091688 )SOURCE", 1, ".tmp_source.zig:11:9: error: parameter of type '(integer literal)' requires comptime");
16101689
16111690 add_compile_fail_case("assign too big number to u16", R"SOURCE(
1612fn foo() {
1691export fn foo() {
16131692 var vga_mem: u16 = 0xB8000;
16141693}
16151694 )SOURCE", 1, ".tmp_source.zig:3:24: error: integer value 753664 cannot be implicitly casted to type 'u16'");
......@@ -1619,10 +1698,11 @@ const some_data: [100]u8 = {
16191698 @setGlobalAlign(some_data, 3);
16201699 undefined
16211700};
1701export fn entry() -> usize { @sizeOf(@typeOf(some_data)) }
16221702 )SOURCE", 1, ".tmp_source.zig:3:32: error: alignment value must be power of 2");
16231703
16241704 add_compile_fail_case("compile log", R"SOURCE(
1625fn foo() {
1705export fn foo() {
16261706 comptime bar(12, "hi");
16271707}
16281708fn bar(a: i32, b: []const u8) {
......@@ -1660,9 +1740,11 @@ fn foo(bit_field: &const BitField) -> u3 {
16601740fn bar(x: &const u3) -> u3 {
16611741 return *x;
16621742}
1743
1744export fn entry() -> usize { @sizeOf(@typeOf(foo)) }
16631745 )SOURCE", 1, ".tmp_source.zig:12:26: error: expected type '&const u3', found '&:3:6 const u3'");
16641746
1665 add_compile_fail_case_no_check_unused("referring to a struct that is invalid without --check-unused", R"SOURCE(
1747 add_compile_fail_case("referring to a struct that is invalid", R"SOURCE(
16661748const UsbDeviceRequest = struct {
16671749 Type: u8,
16681750};
......@@ -1679,7 +1761,7 @@ fn assert(ok: bool) {
16791761 ".tmp_source.zig:7:20: note: called from here");
16801762
16811763 add_compile_fail_case("control flow uses comptime var at runtime", R"SOURCE(
1682fn foo() {
1764export fn foo() {
16831765 comptime var i = 0;
16841766 while (i < 5; i += 1) {
16851767 bar();
......@@ -1692,14 +1774,14 @@ fn bar() { }
16921774 ".tmp_source.zig:4:21: note: compile-time variable assigned here");
16931775
16941776 add_compile_fail_case("ignored return value", R"SOURCE(
1695fn foo() {
1777export fn foo() {
16961778 bar();
16971779}
16981780fn bar() -> i32 { 0 }
16991781 )SOURCE", 1, ".tmp_source.zig:3:8: error: return value ignored");
17001782
17011783 add_compile_fail_case("integer literal on a non-comptime var", R"SOURCE(
1702fn foo() {
1784export fn foo() {
17031785 var i = 0;
17041786 while (i < 10; i += 1) { }
17051787}
......@@ -1712,6 +1794,8 @@ pub fn pass(in: []u8) -> []u8 {
17121794 *out[0] = in[0];
17131795 return (*out)[0...1];
17141796}
1797
1798export fn entry() -> usize { @sizeOf(@typeOf(pass)) }
17151799 )SOURCE", 1, ".tmp_source.zig:5:5: error: attempt to dereference non pointer type '[10]u8'");
17161800
17171801 add_compile_fail_case("pass const ptr to mutable ptr fn", R"SOURCE(
......@@ -1723,6 +1807,8 @@ fn foo() -> bool {
17231807fn ptrEql(a: &[]const u8, b: &[]const u8) -> bool {
17241808 return true;
17251809}
1810
1811export fn entry() -> usize { @sizeOf(@typeOf(foo)) }
17261812 )SOURCE", 1, ".tmp_source.zig:5:19: error: expected type '&[]const u8', found '&const []const u8'");
17271813}
17281814
......@@ -2159,23 +2245,69 @@ static void run_self_hosted_test(bool is_release_mode) {
21592245 }
21602246}
21612247
2248static void run_std_lib_test(bool is_release_mode) {
2249 Buf std_index_file = BUF_INIT;
2250 os_path_join(buf_create_from_str(ZIG_STD_DIR),
2251 buf_create_from_str("index.zig"), &std_index_file);
2252
2253 Buf zig_stderr = BUF_INIT;
2254 Buf zig_stdout = BUF_INIT;
2255 ZigList<const char *> args = {0};
2256 args.append("test");
2257 args.append(buf_ptr(&std_index_file));
2258 if (is_release_mode) {
2259 args.append("--release");
2260 }
2261 Termination term;
2262 os_exec_process(zig_exe, args, &term, &zig_stderr, &zig_stdout);
2263
2264 if (term.how != TerminationIdClean || term.code != 0) {
2265 printf("\nstd lib tests failed:\n");
2266 printf("./zig");
2267 for (size_t i = 0; i < args.length; i += 1) {
2268 printf(" %s", args.at(i));
2269 }
2270 printf("\n%s\n", buf_ptr(&zig_stderr));
2271 exit(1);
2272 }
2273}
2274
2275
21622276static void add_self_hosted_tests(void) {
21632277 {
21642278 TestCase *test_case = allocate<TestCase>(1);
21652279 test_case->case_name = "self hosted tests (debug)";
2166 test_case->is_self_hosted = true;
2280 test_case->special = TestSpecialSelfHosted;
21672281 test_case->is_release_mode = false;
21682282 test_cases.append(test_case);
21692283 }
21702284 {
21712285 TestCase *test_case = allocate<TestCase>(1);
21722286 test_case->case_name = "self hosted tests (release)";
2173 test_case->is_self_hosted = true;
2287 test_case->special = TestSpecialSelfHosted;
21742288 test_case->is_release_mode = true;
21752289 test_cases.append(test_case);
21762290 }
21772291}
21782292
2293static void add_std_lib_tests(void) {
2294 {
2295 TestCase *test_case = allocate<TestCase>(1);
2296 test_case->case_name = "std (debug)";
2297 test_case->special = TestSpecialStd;
2298 test_case->is_release_mode = false;
2299 test_cases.append(test_case);
2300 }
2301 {
2302 TestCase *test_case = allocate<TestCase>(1);
2303 test_case->case_name = "std (release)";
2304 test_case->special = TestSpecialStd;
2305 test_case->is_release_mode = true;
2306 test_cases.append(test_case);
2307 }
2308}
2309
2310
21792311static void print_compiler_invocation(TestCase *test_case) {
21802312 printf("%s", zig_exe);
21812313 for (size_t i = 0; i < test_case->compiler_args.length; i += 1) {
......@@ -2193,8 +2325,10 @@ static void print_exe_invocation(TestCase *test_case) {
21932325}
21942326
21952327static void run_test(TestCase *test_case) {
2196 if (test_case->is_self_hosted) {
2328 if (test_case->special == TestSpecialSelfHosted) {
21972329 return run_self_hosted_test(test_case->is_release_mode);
2330 } else if (test_case->special == TestSpecialStd) {
2331 return run_std_lib_test(test_case->is_release_mode);
21982332 }
21992333
22002334 for (size_t i = 0; i < test_case->source_files.length; i += 1) {
......@@ -2366,6 +2500,7 @@ int main(int argc, char **argv) {
23662500 add_compile_failure_test_cases();
23672501 add_parseh_test_cases();
23682502 add_self_hosted_tests();
2503 add_std_lib_tests();
23692504 run_all_tests(reverse);
23702505 cleanup();
23712506}