authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2021-06-30 11:27:39-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2021-07-02 13:27:28-07:00
log8ce880ca753ce95138bf03d956cf363ea2dfde5a
treecb9b26f3b186e3ea8da52974ef2cb4c1ac416dc1
parent22b20f20b66a0225fe1d57ab8773ac066c205bdd

avoid calling into stage1 backend when AstGen fails

The motivation for this commit is that there exists source files which produce ast-check errors, but crash stage1 or otherwise trigger stage1 bugs. Previously to this commit, Zig would run AstGen, collect the compile errors, run stage1, report stage1 compile errors and exit if any, and then report AstGen compile errors. The main change in this commit is to report AstGen errors prior to invoking stage1, and in fact if any AstGen errors occur, do not invoke stage1 at all. This caused most of the compile error tests to fail due to things such as unused local variables and mismatched stage1/stage2 error messages. It was taking a long time to update the test cases one-by-one, so I took this opportunity to unify the stage1 and stage2 testing harness, specifically with regards to compile errors. In this way we can start keeping track of which tests pass for 1, 2, or both. `zig build test-compile-errors` no longer works; it is now integrated into `zig build test-stage2`. This is one step closer to executing compile error tests in parallel; in fact the ThreadPool object is already in scope. There are some cases where the stage1 compile errors were actually better; those are left failing in this commit, to be addressed in a follow-up commit. Other changes in this commit: * build.zig: improve support for -Dstage1 used with the test step. * AstGen: minor cosmetic changes to error messages. * stage2: add -fstage1 and -fno-stage1 flags. This now allows one to download a binary of the zig compiler and use the llvm backend of self-hosted. This was also needed for hooking up the test harness. However, I realized that stage1 calls exit() and also has memory leaks, so had to complicate the test harness by not using this flag after all and instead invoking as a child process. - These CLI flags will disappear once we start shipping the self-hosted compiler as the main compiler. Until then, they can be used to try out the work-in-progress stage2. * stage2: select the LLVM backend by default for release modes, as long as the target architecture is supported by LLVM. * test harness: support setting the optimize mode

15 files changed, 3329 insertions(+), 3033 deletions(-)

build.zig+20-12
......@@ -40,7 +40,7 @@ pub fn build(b: *Builder) !void {
4040
4141 var test_stage2 = b.addTest("src/test.zig");
4242 test_stage2.setBuildMode(mode);
43 test_stage2.addPackagePath("stage2_tests", "test/stage2/test.zig");
43 test_stage2.addPackagePath("test_cases", "test/cases.zig");
4444
4545 const fmt_build_zig = b.addFmt(&[_][]const u8{"build.zig"});
4646
......@@ -113,11 +113,15 @@ pub fn build(b: *Builder) !void {
113113 if (is_stage1) {
114114 exe.addIncludeDir("src");
115115 exe.addIncludeDir("deps/SoftFloat-3e/source/include");
116
117 test_stage2.addIncludeDir("src");
118 test_stage2.addIncludeDir("deps/SoftFloat-3e/source/include");
116119 // This is intentionally a dummy path. stage1.zig tries to @import("compiler_rt") in case
117120 // of being built by cmake. But when built by zig it's gonna get a compiler_rt so that
118121 // is pointless.
119122 exe.addPackagePath("compiler_rt", "src/empty.zig");
120123 exe.defineCMacro("ZIG_LINK_MODE", "Static");
124 test_stage2.defineCMacro("ZIG_LINK_MODE", "Static");
121125
122126 const softfloat = b.addStaticLibrary("softfloat", null);
123127 softfloat.setBuildMode(.ReleaseFast);
......@@ -126,10 +130,15 @@ pub fn build(b: *Builder) !void {
126130 softfloat.addIncludeDir("deps/SoftFloat-3e/source/8086");
127131 softfloat.addIncludeDir("deps/SoftFloat-3e/source/include");
128132 softfloat.addCSourceFiles(&softfloat_sources, &[_][]const u8{ "-std=c99", "-O3" });
133
129134 exe.linkLibrary(softfloat);
135 test_stage2.linkLibrary(softfloat);
130136
131137 exe.addCSourceFiles(&stage1_sources, &exe_cflags);
132138 exe.addCSourceFiles(&optimized_c_sources, &[_][]const u8{ "-std=c99", "-O3" });
139
140 test_stage2.addCSourceFiles(&stage1_sources, &exe_cflags);
141 test_stage2.addCSourceFiles(&optimized_c_sources, &[_][]const u8{ "-std=c99", "-O3" });
133142 }
134143 if (cmake_cfg) |cfg| {
135144 // Inside this code path, we have to coordinate with system packaged LLVM, Clang, and LLD.
......@@ -139,8 +148,8 @@ pub fn build(b: *Builder) !void {
139148 b.addSearchPrefix(cfg.cmake_prefix_path);
140149 }
141150
142 try addCmakeCfgOptionsToExe(b, cfg, tracy, exe);
143 try addCmakeCfgOptionsToExe(b, cfg, tracy, test_stage2);
151 try addCmakeCfgOptionsToExe(b, cfg, exe);
152 try addCmakeCfgOptionsToExe(b, cfg, test_stage2);
144153 } else {
145154 // Here we are -Denable-llvm but no cmake integration.
146155 try addStaticLlvmOptionsToExe(exe);
......@@ -233,7 +242,9 @@ pub fn build(b: *Builder) !void {
233242 const is_darling_enabled = b.option(bool, "enable-darling", "[Experimental] Use Darling to run cross compiled macOS tests") orelse false;
234243 const glibc_multi_dir = b.option([]const u8, "enable-foreign-glibc", "Provide directory with glibc installations to run cross compiled tests that link glibc");
235244
245 test_stage2.addBuildOption(bool, "enable_logging", enable_logging);
236246 test_stage2.addBuildOption(bool, "skip_non_native", skip_non_native);
247 test_stage2.addBuildOption(bool, "skip_compile_errors", skip_compile_errors);
237248 test_stage2.addBuildOption(bool, "is_stage1", is_stage1);
238249 test_stage2.addBuildOption(bool, "omit_stage2", omit_stage2);
239250 test_stage2.addBuildOption(bool, "have_llvm", enable_llvm);
......@@ -243,7 +254,8 @@ pub fn build(b: *Builder) !void {
243254 test_stage2.addBuildOption(u32, "mem_leak_frames", mem_leak_frames * 2);
244255 test_stage2.addBuildOption(bool, "enable_darling", is_darling_enabled);
245256 test_stage2.addBuildOption(?[]const u8, "glibc_multi_install_dir", glibc_multi_dir);
246 test_stage2.addBuildOption([]const u8, "version", version);
257 test_stage2.addBuildOption([:0]const u8, "version", try b.allocator.dupeZ(u8, version));
258 test_stage2.addBuildOption(std.SemanticVersion, "semver", semver);
247259
248260 const test_stage2_step = b.step("test-stage2", "Run the stage2 compiler tests");
249261 test_stage2_step.dependOn(&test_stage2.step);
......@@ -339,9 +351,6 @@ pub fn build(b: *Builder) !void {
339351 }
340352 // tests for this feature are disabled until we have the self-hosted compiler available
341353 // toolchain_step.dependOn(tests.addGenHTests(b, test_filter));
342 if (!skip_compile_errors) {
343 toolchain_step.dependOn(tests.addCompileErrorTests(b, test_filter, modes));
344 }
345354
346355 const std_step = tests.addPkgTests(
347356 b,
......@@ -383,7 +392,6 @@ const exe_cflags = [_][]const u8{
383392fn addCmakeCfgOptionsToExe(
384393 b: *Builder,
385394 cfg: CMakeConfig,
386 tracy: ?[]const u8,
387395 exe: *std.build.LibExeObjStep,
388396) !void {
389397 exe.addObjectFile(fs.path.join(b.allocator, &[_][]const u8{
......@@ -397,7 +405,7 @@ fn addCmakeCfgOptionsToExe(
397405 addCMakeLibraryList(exe, cfg.lld_libraries);
398406 addCMakeLibraryList(exe, cfg.llvm_libraries);
399407
400 const need_cpp_includes = tracy != null;
408 const need_cpp_includes = true;
401409
402410 // System -lc++ must be used because in this code path we are attempting to link
403411 // against system-provided LLVM, Clang, LLD.
......@@ -486,9 +494,9 @@ fn addCxxKnownPath(
486494 if (need_cpp_includes) {
487495 // I used these temporarily for testing something but we obviously need a
488496 // more general purpose solution here.
489 //exe.addIncludeDir("/nix/store/b3zsk4ihlpiimv3vff86bb5bxghgdzb9-gcc-9.2.0/lib/gcc/x86_64-unknown-linux-gnu/9.2.0/../../../../include/c++/9.2.0");
490 //exe.addIncludeDir("/nix/store/b3zsk4ihlpiimv3vff86bb5bxghgdzb9-gcc-9.2.0/lib/gcc/x86_64-unknown-linux-gnu/9.2.0/../../../../include/c++/9.2.0/x86_64-unknown-linux-gnu");
491 //exe.addIncludeDir("/nix/store/b3zsk4ihlpiimv3vff86bb5bxghgdzb9-gcc-9.2.0/lib/gcc/x86_64-unknown-linux-gnu/9.2.0/../../../../include/c++/9.2.0/backward");
497 //exe.addIncludeDir("/nix/store/fvf3qjqa5qpcjjkq37pb6ypnk1mzhf5h-gcc-9.3.0/lib/gcc/x86_64-unknown-linux-gnu/9.3.0/../../../../include/c++/9.3.0");
498 //exe.addIncludeDir("/nix/store/fvf3qjqa5qpcjjkq37pb6ypnk1mzhf5h-gcc-9.3.0/lib/gcc/x86_64-unknown-linux-gnu/9.3.0/../../../../include/c++/9.3.0/x86_64-unknown-linux-gnu");
499 //exe.addIncludeDir("/nix/store/fvf3qjqa5qpcjjkq37pb6ypnk1mzhf5h-gcc-9.3.0/lib/gcc/x86_64-unknown-linux-gnu/9.3.0/../../../../include/c++/9.3.0/backward");
492500 }
493501}
494502
lib/std/zig/ast.zig+1-1
......@@ -284,7 +284,7 @@ pub const Tree = struct {
284284 return stream.writeAll("bit range not allowed on slices and arrays");
285285 },
286286 .invalid_token => {
287 return stream.print("invalid token '{s}'", .{
287 return stream.print("invalid token: '{s}'", .{
288288 token_tags[parse_error.token].symbol(),
289289 });
290290 },
src/AstGen.zig+3-3
......@@ -4570,7 +4570,7 @@ fn tryExpr(
45704570 return astgen.failNode(node, "invalid 'try' outside function scope", .{});
45714571 };
45724572
4573 if (parent_gz.in_defer) return astgen.failNode(node, "try is not allowed inside defer expression", .{});
4573 if (parent_gz.in_defer) return astgen.failNode(node, "'try' is not allowed inside defer expression", .{});
45744574
45754575 var block_scope = parent_gz.makeSubBlock(scope);
45764576 block_scope.setBreakResultLoc(rl);
......@@ -6199,7 +6199,7 @@ fn identifier(
61996199 const ident_token = main_tokens[ident];
62006200 const ident_name = try astgen.identifierTokenString(ident_token);
62016201 if (mem.eql(u8, ident_name, "_")) {
6202 return astgen.failNode(ident, "'_' may not be used as an identifier", .{});
6202 return astgen.failNode(ident, "'_' used as an identifier without @\"_\" syntax", .{});
62036203 }
62046204
62056205 if (simple_types.get(ident_name)) |zir_const_ref| {
......@@ -6827,7 +6827,7 @@ fn builtinCall(
68276827 if (info.param_count) |expected| {
68286828 if (expected != params.len) {
68296829 const s = if (expected == 1) "" else "s";
6830 return astgen.failNode(node, "expected {d} parameter{s}, found {d}", .{
6830 return astgen.failNode(node, "expected {d} argument{s}, found {d}", .{
68316831 expected, s, params.len,
68326832 });
68336833 }
src/Compilation.zig+43-18
......@@ -670,6 +670,7 @@ pub const InitOptions = struct {
670670 use_llvm: ?bool = null,
671671 use_lld: ?bool = null,
672672 use_clang: ?bool = null,
673 use_stage1: ?bool = null,
673674 rdynamic: bool = false,
674675 strip: bool = false,
675676 single_threaded: bool = false,
......@@ -807,8 +808,22 @@ pub fn create(gpa: *Allocator, options: InitOptions) !*Compilation {
807808
808809 const ofmt = options.object_format orelse options.target.getObjectFormat();
809810
811 const use_stage1 = options.use_stage1 orelse blk: {
812 if (build_options.omit_stage2)
813 break :blk true;
814 if (options.use_llvm) |use_llvm| {
815 if (!use_llvm) {
816 break :blk false;
817 }
818 }
819 break :blk build_options.is_stage1;
820 };
821
810822 // Make a decision on whether to use LLVM or our own backend.
811 const use_llvm = if (options.use_llvm) |explicit| explicit else blk: {
823 const use_llvm = build_options.have_llvm and blk: {
824 if (options.use_llvm) |explicit|
825 break :blk explicit;
826
812827 // If we have no zig code to compile, no need for LLVM.
813828 if (options.root_pkg == null)
814829 break :blk false;
......@@ -817,18 +832,24 @@ pub fn create(gpa: *Allocator, options: InitOptions) !*Compilation {
817832 if (ofmt == .c)
818833 break :blk false;
819834
820 // If we are the stage1 compiler, we depend on the stage1 c++ llvm backend
835 // The stage1 compiler depends on the stage1 C++ LLVM backend
821836 // to compile zig code.
822 if (build_options.is_stage1)
837 if (use_stage1)
838 break :blk true;
839
840 // Prefer LLVM for release builds as long as it supports the target architecture.
841 if (options.optimize_mode != .Debug and target_util.hasLlvmSupport(options.target))
823842 break :blk true;
824843
825 // We would want to prefer LLVM for release builds when it is available, however
826 // we don't have an LLVM backend yet :)
827 // We would also want to prefer LLVM for architectures that we don't have self-hosted support for too.
828844 break :blk false;
829845 };
830 if (!use_llvm and options.machine_code_model != .default) {
831 return error.MachineCodeModelNotSupported;
846 if (!use_llvm) {
847 if (options.use_llvm == true) {
848 return error.ZigCompilerNotBuiltWithLLVMExtensions;
849 }
850 if (options.machine_code_model != .default) {
851 return error.MachineCodeModelNotSupportedWithoutLlvm;
852 }
832853 }
833854
834855 const tsan = options.want_tsan orelse false;
......@@ -1344,6 +1365,7 @@ pub fn create(gpa: *Allocator, options: InitOptions) !*Compilation {
13441365 .subsystem = options.subsystem,
13451366 .is_test = options.is_test,
13461367 .wasi_exec_model = wasi_exec_model,
1368 .use_stage1 = use_stage1,
13471369 });
13481370 errdefer bin_file.destroy();
13491371 comp.* = .{
......@@ -1486,9 +1508,9 @@ pub fn create(gpa: *Allocator, options: InitOptions) !*Compilation {
14861508 try comp.work_queue.writeItem(.libtsan);
14871509 }
14881510
1489 // The `is_stage1` condition is here only because stage2 cannot yet build compiler-rt.
1511 // The `use_stage1` condition is here only because stage2 cannot yet build compiler-rt.
14901512 // Once it is capable this condition should be removed.
1491 if (build_options.is_stage1) {
1513 if (comp.bin_file.options.use_stage1) {
14921514 if (comp.bin_file.options.include_compiler_rt) {
14931515 if (is_exe_or_dyn_lib) {
14941516 try comp.work_queue.writeItem(.{ .compiler_rt_lib = {} });
......@@ -1519,7 +1541,7 @@ pub fn create(gpa: *Allocator, options: InitOptions) !*Compilation {
15191541 }
15201542 }
15211543
1522 if (build_options.is_stage1 and comp.bin_file.options.use_llvm) {
1544 if (comp.bin_file.options.use_stage1) {
15231545 try comp.work_queue.writeItem(.{ .stage1_module = {} });
15241546 }
15251547
......@@ -1625,8 +1647,7 @@ pub fn update(self: *Compilation) !void {
16251647 self.c_object_work_queue.writeItemAssumeCapacity(key);
16261648 }
16271649
1628 const use_stage1 = build_options.omit_stage2 or
1629 (build_options.is_stage1 and self.bin_file.options.use_llvm);
1650 const use_stage1 = build_options.is_stage1 and self.bin_file.options.use_stage1;
16301651 if (self.bin_file.options.module) |module| {
16311652 module.compile_log_text.shrinkAndFree(module.gpa, 0);
16321653 module.generation += 1;
......@@ -1921,7 +1942,7 @@ pub fn performAllTheWork(self: *Compilation) error{ TimerUnsupported, OutOfMemor
19211942 // (at least for now) single-threaded main work queue. However, C object compilation
19221943 // only needs to be finished by the end of this function.
19231944
1924 var zir_prog_node = main_progress_node.start("AST Lowering", self.astgen_work_queue.count);
1945 var zir_prog_node = main_progress_node.start("AST Lowering", 0);
19251946 defer zir_prog_node.end();
19261947
19271948 var c_obj_prog_node = main_progress_node.start("Compile C Objects", self.c_source_files.len);
......@@ -1949,13 +1970,18 @@ pub fn performAllTheWork(self: *Compilation) error{ TimerUnsupported, OutOfMemor
19491970 }
19501971 }
19511972
1952 const use_stage1 = build_options.omit_stage2 or
1953 (build_options.is_stage1 and self.bin_file.options.use_llvm);
1973 const use_stage1 = build_options.is_stage1 and self.bin_file.options.use_stage1;
19541974 if (!use_stage1) {
19551975 // Iterate over all the files and look for outdated and deleted declarations.
19561976 if (self.bin_file.options.module) |mod| {
19571977 try mod.processOutdatedAndDeletedDecls();
19581978 }
1979 } else if (self.bin_file.options.module) |mod| {
1980 // If there are any AstGen compile errors, report them now to avoid
1981 // hitting stage1 bugs.
1982 if (mod.failed_files.count() != 0) {
1983 return;
1984 }
19591985 }
19601986
19611987 while (self.work_queue.readItem()) |work_item| switch (work_item) {
......@@ -3486,8 +3512,7 @@ pub fn generateBuiltinZigSource(comp: *Compilation, allocator: *Allocator) Alloc
34863512
34873513 const target = comp.getTarget();
34883514 const generic_arch_name = target.cpu.arch.genericName();
3489 const use_stage1 = build_options.omit_stage2 or
3490 (build_options.is_stage1 and comp.bin_file.options.use_llvm);
3515 const use_stage1 = build_options.is_stage1 and comp.bin_file.options.use_stage1;
34913516
34923517 @setEvalBranchQuota(4000);
34933518 try buffer.writer().print(
src/link.zig+3-2
......@@ -92,6 +92,7 @@ pub const Options = struct {
9292 each_lib_rpath: bool,
9393 disable_lld_caching: bool,
9494 is_test: bool,
95 use_stage1: bool,
9596 major_subsystem_version: ?u32,
9697 minor_subsystem_version: ?u32,
9798 gc_sections: ?bool = null,
......@@ -181,7 +182,7 @@ pub const File = struct {
181182 /// rewriting it. A malicious file is detected as incremental link failure
182183 /// and does not cause Illegal Behavior. This operation is not atomic.
183184 pub fn openPath(allocator: *Allocator, options: Options) !*File {
184 const use_stage1 = build_options.is_stage1 and options.use_llvm;
185 const use_stage1 = build_options.is_stage1 and options.use_stage1;
185186 if (use_stage1 or options.emit == null) {
186187 return switch (options.object_format) {
187188 .coff, .pe => &(try Coff.createEmpty(allocator, options)).base,
......@@ -507,7 +508,7 @@ pub const File = struct {
507508 // If there is no Zig code to compile, then we should skip flushing the output file because it
508509 // will not be part of the linker line anyway.
509510 const module_obj_path: ?[]const u8 = if (base.options.module) |module| blk: {
510 const use_stage1 = build_options.is_stage1 and base.options.use_llvm;
511 const use_stage1 = build_options.is_stage1 and base.options.use_stage1;
511512 if (use_stage1) {
512513 const obj_basename = try std.zig.binNameAlloc(arena, .{
513514 .root_name = base.options.root_name,
src/link/MachO.zig+1-1
......@@ -606,7 +606,7 @@ fn linkWithZld(self: *MachO, comp: *Compilation) !void {
606606 // If there is no Zig code to compile, then we should skip flushing the output file because it
607607 // will not be part of the linker line anyway.
608608 const module_obj_path: ?[]const u8 = if (self.base.options.module) |module| blk: {
609 const use_stage1 = build_options.is_stage1 and self.base.options.use_llvm;
609 const use_stage1 = build_options.is_stage1 and self.base.options.use_stage1;
610610 if (use_stage1) {
611611 const obj_basename = try std.zig.binNameAlloc(arena, .{
612612 .root_name = self.base.options.root_name,
src/link/Wasm.zig+1-1
......@@ -556,7 +556,7 @@ fn linkWithLLD(self: *Wasm, comp: *Compilation) !void {
556556 // If there is no Zig code to compile, then we should skip flushing the output file because it
557557 // will not be part of the linker line anyway.
558558 const module_obj_path: ?[]const u8 = if (self.base.options.module) |module| blk: {
559 const use_stage1 = build_options.is_stage1 and self.base.options.use_llvm;
559 const use_stage1 = build_options.is_stage1 and self.base.options.use_stage1;
560560 if (use_stage1) {
561561 const obj_basename = try std.zig.binNameAlloc(arena, .{
562562 .root_name = self.base.options.root_name,
src/main.zig+9-1
......@@ -350,9 +350,11 @@ const usage_build_generic =
350350 \\ -funwind-tables Always produce unwind table entries for all functions
351351 \\ -fno-unwind-tables Never produce unwind table entries
352352 \\ -fLLVM Force using LLVM as the codegen backend
353 \\ -fno-LLVM Prevent using LLVM as a codegen backend
353 \\ -fno-LLVM Prevent using LLVM as the codegen backend
354354 \\ -fClang Force using Clang as the C/C++ compilation backend
355355 \\ -fno-Clang Prevent using Clang as the C/C++ compilation backend
356 \\ -fstage1 Force using bootstrap compiler as the codegen backend
357 \\ -fno-stage1 Prevent using bootstrap compiler as the codegen backend
356358 \\ --strip Omit debug symbols
357359 \\ --single-threaded Code assumes it is only used single-threaded
358360 \\ -ofmt=[mode] Override target object format
......@@ -602,6 +604,7 @@ fn buildOutputType(
602604 var use_llvm: ?bool = null;
603605 var use_lld: ?bool = null;
604606 var use_clang: ?bool = null;
607 var use_stage1: ?bool = null;
605608 var link_eh_frame_hdr = false;
606609 var link_emit_relocs = false;
607610 var each_lib_rpath: ?bool = null;
......@@ -975,6 +978,10 @@ fn buildOutputType(
975978 use_clang = true;
976979 } else if (mem.eql(u8, arg, "-fno-Clang")) {
977980 use_clang = false;
981 } else if (mem.eql(u8, arg, "-fstage1")) {
982 use_stage1 = true;
983 } else if (mem.eql(u8, arg, "-fno-stage1")) {
984 use_stage1 = false;
978985 } else if (mem.eql(u8, arg, "-rdynamic")) {
979986 rdynamic = true;
980987 } else if (mem.eql(u8, arg, "-fsoname")) {
......@@ -2020,6 +2027,7 @@ fn buildOutputType(
20202027 .use_llvm = use_llvm,
20212028 .use_lld = use_lld,
20222029 .use_clang = use_clang,
2030 .use_stage1 = use_stage1,
20232031 .rdynamic = rdynamic,
20242032 .linker_script = linker_script,
20252033 .version_script = version_script,
src/stage1.zig+9-4
......@@ -7,6 +7,7 @@ const assert = std.debug.assert;
77const mem = std.mem;
88const CrossTarget = std.zig.CrossTarget;
99const Target = std.Target;
10const builtin = @import("builtin");
1011
1112const build_options = @import("build_options");
1213const stage2 = @import("main.zig");
......@@ -16,16 +17,19 @@ const translate_c = @import("translate_c.zig");
1617const target_util = @import("target.zig");
1718
1819comptime {
19 assert(std.builtin.link_libc);
20 assert(builtin.link_libc);
2021 assert(build_options.is_stage1);
2122 assert(build_options.have_llvm);
22 _ = @import("compiler_rt");
23 if (!builtin.is_test) {
24 _ = @import("compiler_rt");
25 @export(main, .{ .name = "main" });
26 }
2327}
2428
2529pub const log = stage2.log;
2630pub const log_level = stage2.log_level;
2731
28pub export fn main(argc: c_int, argv: [*][*:0]u8) c_int {
32pub fn main(argc: c_int, argv: [*][*:0]u8) callconv(.C) c_int {
2933 std.os.argv = argv[0..@intCast(usize, argc)];
3034
3135 std.debug.maybeEnableSegfaultHandler();
......@@ -41,7 +45,7 @@ pub export fn main(argc: c_int, argv: [*][*:0]u8) c_int {
4145 for (args) |*arg, i| {
4246 arg.* = mem.spanZ(argv[i]);
4347 }
44 if (std.builtin.mode == .Debug) {
48 if (builtin.mode == .Debug) {
4549 stage2.mainArgs(gpa, arena, args) catch unreachable;
4650 } else {
4751 stage2.mainArgs(gpa, arena, args) catch |err| fatal("{s}", .{@errorName(err)});
......@@ -147,6 +151,7 @@ pub const Module = extern struct {
147151 }
148152};
149153
154pub const os_init = zig_stage1_os_init;
150155extern fn zig_stage1_os_init() void;
151156
152157pub const create = zig_stage1_create;
src/target.zig+67
......@@ -170,6 +170,73 @@ pub fn hasValgrindSupport(target: std.Target) bool {
170170 }
171171}
172172
173/// The set of targets that LLVM has non-experimental support for.
174/// Used to select between LLVM backend and self-hosted backend when compiling in
175/// release modes.
176pub fn hasLlvmSupport(target: std.Target) bool {
177 return switch (target.cpu.arch) {
178 .arm,
179 .armeb,
180 .aarch64,
181 .aarch64_be,
182 .aarch64_32,
183 .arc,
184 .avr,
185 .bpfel,
186 .bpfeb,
187 .csky,
188 .hexagon,
189 .mips,
190 .mipsel,
191 .mips64,
192 .mips64el,
193 .msp430,
194 .powerpc,
195 .powerpcle,
196 .powerpc64,
197 .powerpc64le,
198 .r600,
199 .amdgcn,
200 .riscv32,
201 .riscv64,
202 .sparc,
203 .sparcv9,
204 .sparcel,
205 .s390x,
206 .tce,
207 .tcele,
208 .thumb,
209 .thumbeb,
210 .i386,
211 .x86_64,
212 .xcore,
213 .nvptx,
214 .nvptx64,
215 .le32,
216 .le64,
217 .amdil,
218 .amdil64,
219 .hsail,
220 .hsail64,
221 .spir,
222 .spir64,
223 .kalimba,
224 .shave,
225 .lanai,
226 .wasm32,
227 .wasm64,
228 .renderscript32,
229 .renderscript64,
230 .ve,
231 => true,
232
233 .spu_2,
234 .spirv32,
235 .spirv64,
236 => false,
237 };
238}
239
173240pub fn supportsStackProbing(target: std.Target) bool {
174241 return target.os.tag != .windows and target.os.tag != .uefi and
175242 (target.cpu.arch == .i386 or target.cpu.arch == .x86_64);
src/test.zig+254-54
......@@ -10,18 +10,25 @@ const enable_wine: bool = build_options.enable_wine;
1010const enable_wasmtime: bool = build_options.enable_wasmtime;
1111const enable_darling: bool = build_options.enable_darling;
1212const glibc_multi_install_dir: ?[]const u8 = build_options.glibc_multi_install_dir;
13const skip_compile_errors = build_options.skip_compile_errors;
1314const ThreadPool = @import("ThreadPool.zig");
1415const CrossTarget = std.zig.CrossTarget;
16const print = std.debug.print;
17const assert = std.debug.assert;
1518
1619const zig_h = link.File.C.zig_h;
1720
1821const hr = "=" ** 80;
1922
20test "self-hosted" {
23test {
24 if (build_options.is_stage1) {
25 @import("stage1.zig").os_init();
26 }
27
2128 var ctx = TestContext.init();
2229 defer ctx.deinit();
2330
24 try @import("stage2_tests").addCases(&ctx);
31 try @import("test_cases").addCases(&ctx);
2532
2633 try ctx.run();
2734}
......@@ -83,14 +90,13 @@ const ErrorMsg = union(enum) {
8390 });
8491 },
8592 .plain => |plain| {
86 return writer.print("{s}: {s}", .{ plain.msg, @tagName(plain.kind) });
93 return writer.print("{s}: {s}", .{ @tagName(plain.kind), plain.msg });
8794 },
8895 }
8996 }
9097};
9198
9299pub const TestContext = struct {
93 /// TODO: find a way to treat cases as individual tests (shouldn't show "1 test passed" if there are 200 cases)
94100 cases: std.ArrayList(Case),
95101
96102 pub const Update = struct {
......@@ -127,6 +133,12 @@ pub const TestContext = struct {
127133 path: []const u8,
128134 };
129135
136 pub const Backend = enum {
137 stage1,
138 stage2,
139 llvm,
140 };
141
130142 /// A `Case` consists of a list of `Update`. The same `Compilation` is used for each
131143 /// update, so each update's source is treated as a single file being
132144 /// updated by the test harness and incrementally compiled.
......@@ -140,13 +152,20 @@ pub const TestContext = struct {
140152 /// In order to be able to run e.g. Execution updates, this must be set
141153 /// to Executable.
142154 output_mode: std.builtin.OutputMode,
155 optimize_mode: std.builtin.Mode = .Debug,
143156 updates: std.ArrayList(Update),
144157 object_format: ?std.Target.ObjectFormat = null,
145158 emit_h: bool = false,
146 llvm_backend: bool = false,
159 is_test: bool = false,
160 expect_exact: bool = false,
161 backend: Backend = .stage2,
147162
148163 files: std.ArrayList(File),
149164
165 pub fn addSourceFile(case: *Case, name: []const u8, src: [:0]const u8) void {
166 case.files.append(.{ .path = name, .src = src }) catch @panic("out of memory");
167 }
168
150169 /// Adds a subcase in which the module is updated with `src`, and a C
151170 /// header is generated.
152171 pub fn addHeader(self: *Case, src: [:0]const u8, result: [:0]const u8) void {
......@@ -254,11 +273,6 @@ pub const TestContext = struct {
254273 return ctx.addExe(name, target);
255274 }
256275
257 /// Adds a test case for ZIR input, producing an executable
258 pub fn exeZIR(ctx: *TestContext, name: []const u8, target: CrossTarget) *Case {
259 return ctx.addExe(name, target, .ZIR);
260 }
261
262276 pub fn exeFromCompiledC(ctx: *TestContext, name: []const u8, target: CrossTarget) *Case {
263277 const prefixed_name = std.fmt.allocPrint(ctx.cases.allocator, "CBE: {s}", .{name}) catch
264278 @panic("out of memory");
......@@ -282,7 +296,7 @@ pub const TestContext = struct {
282296 .updates = std.ArrayList(Update).init(ctx.cases.allocator),
283297 .output_mode = .Exe,
284298 .files = std.ArrayList(File).init(ctx.cases.allocator),
285 .llvm_backend = true,
299 .backend = .llvm,
286300 }) catch @panic("out of memory");
287301 return &ctx.cases.items[ctx.cases.items.len - 1];
288302 }
......@@ -302,6 +316,22 @@ pub const TestContext = struct {
302316 return &ctx.cases.items[ctx.cases.items.len - 1];
303317 }
304318
319 pub fn addTest(
320 ctx: *TestContext,
321 name: []const u8,
322 target: CrossTarget,
323 ) *Case {
324 ctx.cases.append(Case{
325 .name = name,
326 .target = target,
327 .updates = std.ArrayList(Update).init(ctx.cases.allocator),
328 .output_mode = .Exe,
329 .is_test = true,
330 .files = std.ArrayList(File).init(ctx.cases.allocator),
331 }) catch @panic("out of memory");
332 return &ctx.cases.items[ctx.cases.items.len - 1];
333 }
334
305335 /// Adds a test case for Zig input, producing an object file.
306336 pub fn obj(ctx: *TestContext, name: []const u8, target: CrossTarget) *Case {
307337 return ctx.addObj(name, target);
......@@ -333,6 +363,45 @@ pub const TestContext = struct {
333363 ctx.addC(name, target).addHeader(src, zig_h ++ out);
334364 }
335365
366 pub fn objErrStage1(
367 ctx: *TestContext,
368 name: []const u8,
369 src: [:0]const u8,
370 expected_errors: []const []const u8,
371 ) void {
372 if (skip_compile_errors) return;
373
374 const case = ctx.addObj(name, .{});
375 case.backend = .stage1;
376 case.addError(src, expected_errors);
377 }
378
379 pub fn testErrStage1(
380 ctx: *TestContext,
381 name: []const u8,
382 src: [:0]const u8,
383 expected_errors: []const []const u8,
384 ) void {
385 if (skip_compile_errors) return;
386
387 const case = ctx.addTest(name, .{});
388 case.backend = .stage1;
389 case.addError(src, expected_errors);
390 }
391
392 pub fn exeErrStage1(
393 ctx: *TestContext,
394 name: []const u8,
395 src: [:0]const u8,
396 expected_errors: []const []const u8,
397 ) void {
398 if (skip_compile_errors) return;
399
400 const case = ctx.addExe(name, .{});
401 case.backend = .stage1;
402 case.addError(src, expected_errors);
403 }
404
336405 pub fn addCompareOutput(
337406 ctx: *TestContext,
338407 name: []const u8,
......@@ -386,18 +455,6 @@ pub const TestContext = struct {
386455 ctx.addTransform(name, target, src, result);
387456 }
388457
389 /// Adds a test case that cleans up the ZIR source given in `src`, and
390 /// tests the resulting ZIR against `result`
391 pub fn transformZIR(
392 ctx: *TestContext,
393 name: []const u8,
394 target: CrossTarget,
395 src: [:0]const u8,
396 result: [:0]const u8,
397 ) void {
398 ctx.addTransform(name, target, .ZIR, src, result);
399 }
400
401458 pub fn addError(
402459 ctx: *TestContext,
403460 name: []const u8,
......@@ -555,7 +612,7 @@ pub const TestContext = struct {
555612 continue;
556613
557614 // Skip tests that require LLVM backend when it is not available
558 if (!build_options.have_llvm and case.llvm_backend)
615 if (!build_options.have_llvm and case.backend == .llvm)
559616 continue;
560617
561618 var prg_node = root_node.start(case.name, case.updates.items.len);
......@@ -567,7 +624,7 @@ pub const TestContext = struct {
567624 progress.initial_delay_ns = 0;
568625 progress.refresh_rate_ns = 0;
569626
570 self.runOneCase(
627 runOneCase(
571628 std.testing.allocator,
572629 &prg_node,
573630 case,
......@@ -576,17 +633,16 @@ pub const TestContext = struct {
576633 global_cache_directory,
577634 ) catch |err| {
578635 fail_count += 1;
579 std.debug.print("test '{s}' failed: {s}\n\n", .{ case.name, @errorName(err) });
636 print("test '{s}' failed: {s}\n\n", .{ case.name, @errorName(err) });
580637 };
581638 }
582639 if (fail_count != 0) {
583 std.debug.print("{d} tests failed\n", .{fail_count});
640 print("{d} tests failed\n", .{fail_count});
584641 return error.TestFailed;
585642 }
586643 }
587644
588645 fn runOneCase(
589 self: *TestContext,
590646 allocator: *Allocator,
591647 root_node: *std.Progress.Node,
592648 case: Case,
......@@ -594,7 +650,6 @@ pub const TestContext = struct {
594650 thread_pool: *ThreadPool,
595651 global_cache_directory: Compilation.Directory,
596652 ) !void {
597 _ = self;
598653 const target_info = try std.zig.system.NativeTargetInfo.detect(allocator, case.target);
599654 const target = target_info.target;
600655
......@@ -607,14 +662,137 @@ pub const TestContext = struct {
607662
608663 var cache_dir = try tmp.dir.makeOpenPath("zig-cache", .{});
609664 defer cache_dir.close();
610 const tmp_dir_path = try std.fs.path.join(arena, &[_][]const u8{ ".", "zig-cache", "tmp", &tmp.sub_path });
665
666 const tmp_dir_path = try std.fs.path.join(
667 arena,
668 &[_][]const u8{ ".", "zig-cache", "tmp", &tmp.sub_path },
669 );
670 const local_cache_path = try std.fs.path.join(
671 arena,
672 &[_][]const u8{ tmp_dir_path, "zig-cache" },
673 );
674
675 for (case.files.items) |file| {
676 try tmp.dir.writeFile(file.path, file.src);
677 }
678
679 if (case.backend == .stage1) {
680 // stage1 backend has limitations:
681 // * leaks memory
682 // * calls exit() when a compile error happens
683 // * cannot handle updates
684 // because of this we must spawn a child process rather than
685 // using Compilation directly.
686 assert(case.updates.items.len == 1);
687 const update = case.updates.items[0];
688 try tmp.dir.writeFile(tmp_src_path, update.src);
689
690 var zig_args = std.ArrayList([]const u8).init(arena);
691 try zig_args.append(std.testing.zig_exe_path);
692
693 if (case.is_test) {
694 try zig_args.append("test");
695 } else switch (case.output_mode) {
696 .Obj => try zig_args.append("build-obj"),
697 .Exe => try zig_args.append("build-exe"),
698 .Lib => try zig_args.append("build-lib"),
699 }
700
701 try zig_args.append(try std.fs.path.join(arena, &.{ tmp_dir_path, tmp_src_path }));
702
703 try zig_args.append("--name");
704 try zig_args.append("test");
705
706 try zig_args.append("--cache-dir");
707 try zig_args.append(local_cache_path);
708
709 try zig_args.append("--global-cache-dir");
710 try zig_args.append(global_cache_directory.path orelse ".");
711
712 if (!case.target.isNative()) {
713 try zig_args.append("-target");
714 try zig_args.append(try target.zigTriple(arena));
715 }
716
717 try zig_args.append("-O");
718 try zig_args.append(@tagName(case.optimize_mode));
719
720 const result = try std.ChildProcess.exec(.{
721 .allocator = arena,
722 .argv = zig_args.items,
723 });
724 switch (update.case) {
725 .Error => |case_error_list| {
726 switch (result.term) {
727 .Exited => |code| {
728 if (code == 0) {
729 dumpArgs(zig_args.items);
730 return error.CompilationIncorrectlySucceeded;
731 }
732 },
733 else => {
734 dumpArgs(zig_args.items);
735 return error.CompilationCrashed;
736 },
737 }
738 var ok = true;
739 if (case.expect_exact) {
740 var err_iter = ErrLineIter.init(result.stderr);
741 var i: usize = 0;
742 ok = while (err_iter.next()) |line| : (i += 1) {
743 if (i >= case_error_list.len) break false;
744 const expected = try std.fmt.allocPrint(arena, "{s}", .{case_error_list[i]});
745 if (std.mem.indexOf(u8, line, expected) == null) break false;
746 continue;
747 } else true;
748
749 ok = ok and i == case_error_list.len;
750
751 if (!ok) {
752 print("\n======== Expected these compile errors: ========\n", .{});
753 for (case_error_list) |msg| {
754 const expected = try std.fmt.allocPrint(arena, "{s}", .{msg});
755 print("{s}\n", .{expected});
756 }
757 }
758 } else {
759 for (case_error_list) |msg| {
760 const expected = try std.fmt.allocPrint(arena, "{s}", .{msg});
761 if (std.mem.indexOf(u8, result.stderr, expected) == null) {
762 print(
763 \\
764 \\=========== Expected compile error: ============
765 \\{s}
766 \\
767 , .{expected});
768 ok = false;
769 break;
770 }
771 }
772 }
773
774 if (!ok) {
775 print(
776 \\================= Full output: =================
777 \\{s}
778 \\================================================
779 \\
780 , .{result.stderr});
781 return error.TestFailed;
782 }
783 },
784 .CompareObjectFile => @panic("TODO implement in the test harness"),
785 .Execution => @panic("TODO implement in the test harness"),
786 .Header => @panic("TODO implement in the test harness"),
787 }
788 return;
789 }
790
611791 const zig_cache_directory: Compilation.Directory = .{
612792 .handle = cache_dir,
613 .path = try std.fs.path.join(arena, &[_][]const u8{ tmp_dir_path, "zig-cache" }),
793 .path = local_cache_path,
614794 };
615795
616 const tmp_src_path = "test_case.zig";
617
618796 var root_pkg: Package = .{
619797 .root_src_directory = .{ .path = tmp_dir_path, .handle = tmp.dir },
620798 .root_src_path = tmp_src_path,
......@@ -640,6 +818,14 @@ pub const TestContext = struct {
640818 .directory = emit_directory,
641819 .basename = "test_case.h",
642820 } else null;
821 const use_llvm: ?bool = switch (case.backend) {
822 .llvm => true,
823 else => null,
824 };
825 const use_stage1: ?bool = switch (case.backend) {
826 .stage1 => true,
827 else => null,
828 };
643829 const comp = try Compilation.create(allocator, .{
644830 .local_cache_directory = zig_cache_directory,
645831 .global_cache_directory = global_cache_directory,
......@@ -651,8 +837,8 @@ pub const TestContext = struct {
651837 // and linking. This will require a rework to support multi-file
652838 // tests.
653839 .output_mode = case.output_mode,
654 // TODO: support testing optimizations
655 .optimize_mode = .Debug,
840 .is_test = case.is_test,
841 .optimize_mode = case.optimize_mode,
656842 .emit_bin = emit_bin,
657843 .emit_h = emit_h,
658844 .root_pkg = &root_pkg,
......@@ -661,17 +847,13 @@ pub const TestContext = struct {
661847 .is_native_os = case.target.isNativeOs(),
662848 .is_native_abi = case.target.isNativeAbi(),
663849 .dynamic_linker = target_info.dynamic_linker.get(),
664 .link_libc = case.llvm_backend,
665 .use_llvm = case.llvm_backend,
666 .use_lld = case.llvm_backend,
850 .link_libc = case.backend == .llvm,
851 .use_llvm = use_llvm,
852 .use_stage1 = use_stage1,
667853 .self_exe_path = std.testing.zig_exe_path,
668854 });
669855 defer comp.destroy();
670856
671 for (case.files.items) |file| {
672 try tmp.dir.writeFile(file.path, file.src);
673 }
674
675857 for (case.updates.items) |update, update_index| {
676858 var update_node = root_node.start("update", 3);
677859 update_node.activate();
......@@ -692,19 +874,19 @@ pub const TestContext = struct {
692874 var all_errors = try comp.getAllErrorsAlloc();
693875 defer all_errors.deinit(allocator);
694876 if (all_errors.list.len != 0) {
695 std.debug.print(
877 print(
696878 "\nCase '{s}': unexpected errors at update_index={d}:\n{s}\n",
697879 .{ case.name, update_index, hr },
698880 );
699881 for (all_errors.list) |err_msg| {
700882 switch (err_msg) {
701883 .src => |src| {
702 std.debug.print("{s}:{d}:{d}: error: {s}\n{s}\n", .{
884 print("{s}:{d}:{d}: error: {s}\n{s}\n", .{
703885 src.src_path, src.line + 1, src.column + 1, src.msg, hr,
704886 });
705887 },
706888 .plain => |plain| {
707 std.debug.print("error: {s}\n{s}\n", .{ plain.msg, hr });
889 print("error: {s}\n{s}\n", .{ plain.msg, hr });
708890 },
709891 }
710892 }
......@@ -779,7 +961,7 @@ pub const TestContext = struct {
779961 },
780962 }
781963 } else {
782 std.debug.print(
964 print(
783965 "\nUnexpected error:\n{s}\n{}\n{s}",
784966 .{ hr, ErrorMsg.init(actual_error, .@"error"), hr },
785967 );
......@@ -817,7 +999,7 @@ pub const TestContext = struct {
817999 },
8181000 }
8191001 } else {
820 std.debug.print(
1002 print(
8211003 "\nUnexpected note:\n{s}\n{}\n{s}",
8221004 .{ hr, ErrorMsg.init(note.*, .note), hr },
8231005 );
......@@ -827,7 +1009,7 @@ pub const TestContext = struct {
8271009
8281010 for (handled_errors) |handled, i| {
8291011 if (!handled) {
830 std.debug.print(
1012 print(
8311013 "\nExpected error not found:\n{s}\n{}\n{s}",
8321014 .{ hr, case_error_list[i], hr },
8331015 );
......@@ -836,7 +1018,7 @@ pub const TestContext = struct {
8361018 }
8371019
8381020 if (any_failed) {
839 std.debug.print("\nupdate_index={d} ", .{update_index});
1021 print("\nupdate_index={d} ", .{update_index});
8401022 return error.WrongCompileErrors;
8411023 }
8421024 },
......@@ -932,7 +1114,7 @@ pub const TestContext = struct {
9321114 .cwd_dir = tmp.dir,
9331115 .cwd = tmp_dir_path,
9341116 }) catch |err| {
935 std.debug.print("\nupdate_index={d} The following command failed with {s}:\n", .{
1117 print("\nupdate_index={d} The following command failed with {s}:\n", .{
9361118 update_index, @errorName(err),
9371119 });
9381120 dumpArgs(argv.items);
......@@ -947,7 +1129,7 @@ pub const TestContext = struct {
9471129 switch (exec_result.term) {
9481130 .Exited => |code| {
9491131 if (code != 0) {
950 std.debug.print("\n{s}\n{s}: execution exited with code {d}:\n", .{
1132 print("\n{s}\n{s}: execution exited with code {d}:\n", .{
9511133 exec_result.stderr, case.name, code,
9521134 });
9531135 dumpArgs(argv.items);
......@@ -955,7 +1137,7 @@ pub const TestContext = struct {
9551137 }
9561138 },
9571139 else => {
958 std.debug.print("\n{s}\n{s}: execution crashed:\n", .{
1140 print("\n{s}\n{s}: execution crashed:\n", .{
9591141 exec_result.stderr, case.name,
9601142 });
9611143 dumpArgs(argv.items);
......@@ -974,7 +1156,25 @@ pub const TestContext = struct {
9741156
9751157fn dumpArgs(argv: []const []const u8) void {
9761158 for (argv) |arg| {
977 std.debug.print("{s} ", .{arg});
1159 print("{s} ", .{arg});
9781160 }
979 std.debug.print("\n", .{});
1161 print("\n", .{});
9801162}
1163
1164const tmp_src_path = "tmp.zig";
1165
1166const ErrLineIter = struct {
1167 lines: std.mem.SplitIterator,
1168
1169 fn init(input: []const u8) ErrLineIter {
1170 return ErrLineIter{ .lines = std.mem.split(input, "\n") };
1171 }
1172
1173 fn next(self: *ErrLineIter) ?[]const u8 {
1174 while (self.lines.next()) |line| {
1175 if (std.mem.indexOf(u8, line, tmp_src_path) != null)
1176 return line;
1177 }
1178 return null;
1179 }
1180};
test/cases.zig created+1627
......@@ -0,0 +1,1627 @@
1const std = @import("std");
2const TestContext = @import("../src/test.zig").TestContext;
3
4// Self-hosted has differing levels of support for various architectures. For now we pass explicit
5// target parameters to each test case. At some point we will take this to the next level and have
6// a set of targets that all test cases run on unless specifically overridden. For now, each test
7// case applies to only the specified target.
8
9const linux_x64 = std.zig.CrossTarget{
10 .cpu_arch = .x86_64,
11 .os_tag = .linux,
12};
13
14pub fn addCases(ctx: *TestContext) !void {
15 try @import("compile_errors.zig").addCases(ctx);
16 try @import("stage2/cbe.zig").addCases(ctx);
17 try @import("stage2/arm.zig").addCases(ctx);
18 try @import("stage2/aarch64.zig").addCases(ctx);
19 try @import("stage2/llvm.zig").addCases(ctx);
20 try @import("stage2/wasm.zig").addCases(ctx);
21 try @import("stage2/darwin.zig").addCases(ctx);
22 try @import("stage2/riscv64.zig").addCases(ctx);
23
24 {
25 var case = ctx.exe("hello world with updates", linux_x64);
26
27 case.addError("", &[_][]const u8{
28 ":93:9: error: struct 'test_case.test_case' has no member named 'main'",
29 });
30
31 // Incorrect return type
32 case.addError(
33 \\pub export fn _start() noreturn {
34 \\}
35 , &[_][]const u8{":2:1: error: expected noreturn, found void"});
36
37 // Regular old hello world
38 case.addCompareOutput(
39 \\pub export fn _start() noreturn {
40 \\ print();
41 \\
42 \\ exit();
43 \\}
44 \\
45 \\fn print() void {
46 \\ asm volatile ("syscall"
47 \\ :
48 \\ : [number] "{rax}" (1),
49 \\ [arg1] "{rdi}" (1),
50 \\ [arg2] "{rsi}" (@ptrToInt("Hello, World!\n")),
51 \\ [arg3] "{rdx}" (14)
52 \\ : "rcx", "r11", "memory"
53 \\ );
54 \\ return;
55 \\}
56 \\
57 \\fn exit() noreturn {
58 \\ asm volatile ("syscall"
59 \\ :
60 \\ : [number] "{rax}" (231),
61 \\ [arg1] "{rdi}" (0)
62 \\ : "rcx", "r11", "memory"
63 \\ );
64 \\ unreachable;
65 \\}
66 ,
67 "Hello, World!\n",
68 );
69
70 // Convert to pub fn main
71 case.addCompareOutput(
72 \\pub fn main() void {
73 \\ print();
74 \\}
75 \\
76 \\fn print() void {
77 \\ asm volatile ("syscall"
78 \\ :
79 \\ : [number] "{rax}" (1),
80 \\ [arg1] "{rdi}" (1),
81 \\ [arg2] "{rsi}" (@ptrToInt("Hello, World!\n")),
82 \\ [arg3] "{rdx}" (14)
83 \\ : "rcx", "r11", "memory"
84 \\ );
85 \\ return;
86 \\}
87 ,
88 "Hello, World!\n",
89 );
90
91 // Now change the message only
92 case.addCompareOutput(
93 \\pub fn main() void {
94 \\ print();
95 \\}
96 \\
97 \\fn print() void {
98 \\ asm volatile ("syscall"
99 \\ :
100 \\ : [number] "{rax}" (1),
101 \\ [arg1] "{rdi}" (1),
102 \\ [arg2] "{rsi}" (@ptrToInt("What is up? This is a longer message that will force the data to be relocated in virtual address space.\n")),
103 \\ [arg3] "{rdx}" (104)
104 \\ : "rcx", "r11", "memory"
105 \\ );
106 \\ return;
107 \\}
108 ,
109 "What is up? This is a longer message that will force the data to be relocated in virtual address space.\n",
110 );
111 // Now we print it twice.
112 case.addCompareOutput(
113 \\pub fn main() void {
114 \\ print();
115 \\ print();
116 \\}
117 \\
118 \\fn print() void {
119 \\ asm volatile ("syscall"
120 \\ :
121 \\ : [number] "{rax}" (1),
122 \\ [arg1] "{rdi}" (1),
123 \\ [arg2] "{rsi}" (@ptrToInt("What is up? This is a longer message that will force the data to be relocated in virtual address space.\n")),
124 \\ [arg3] "{rdx}" (104)
125 \\ : "rcx", "r11", "memory"
126 \\ );
127 \\ return;
128 \\}
129 ,
130 \\What is up? This is a longer message that will force the data to be relocated in virtual address space.
131 \\What is up? This is a longer message that will force the data to be relocated in virtual address space.
132 \\
133 );
134 }
135
136 {
137 var case = ctx.exe("adding numbers at comptime", linux_x64);
138 case.addCompareOutput(
139 \\pub export fn _start() noreturn {
140 \\ asm volatile ("syscall"
141 \\ :
142 \\ : [number] "{rax}" (1),
143 \\ [arg1] "{rdi}" (1),
144 \\ [arg2] "{rsi}" (@ptrToInt("Hello, World!\n")),
145 \\ [arg3] "{rdx}" (10 + 4)
146 \\ : "rcx", "r11", "memory"
147 \\ );
148 \\ asm volatile ("syscall"
149 \\ :
150 \\ : [number] "{rax}" (@as(usize, 230) + @as(usize, 1)),
151 \\ [arg1] "{rdi}" (0)
152 \\ : "rcx", "r11", "memory"
153 \\ );
154 \\ unreachable;
155 \\}
156 ,
157 "Hello, World!\n",
158 );
159 }
160
161 {
162 var case = ctx.exe("adding numbers at runtime and comptime", linux_x64);
163 case.addCompareOutput(
164 \\pub export fn _start() noreturn {
165 \\ add(3, 4);
166 \\
167 \\ exit();
168 \\}
169 \\
170 \\fn add(a: u32, b: u32) void {
171 \\ if (a + b != 7) unreachable;
172 \\}
173 \\
174 \\fn exit() noreturn {
175 \\ asm volatile ("syscall"
176 \\ :
177 \\ : [number] "{rax}" (231),
178 \\ [arg1] "{rdi}" (0)
179 \\ : "rcx", "r11", "memory"
180 \\ );
181 \\ unreachable;
182 \\}
183 ,
184 "",
185 );
186 // comptime function call
187 case.addCompareOutput(
188 \\pub export fn _start() noreturn {
189 \\ exit();
190 \\}
191 \\
192 \\fn add(a: u32, b: u32) u32 {
193 \\ return a + b;
194 \\}
195 \\
196 \\const x = add(3, 4);
197 \\
198 \\fn exit() noreturn {
199 \\ asm volatile ("syscall"
200 \\ :
201 \\ : [number] "{rax}" (231),
202 \\ [arg1] "{rdi}" (x - 7)
203 \\ : "rcx", "r11", "memory"
204 \\ );
205 \\ unreachable;
206 \\}
207 ,
208 "",
209 );
210 // Inline function call
211 case.addCompareOutput(
212 \\pub export fn _start() noreturn {
213 \\ var x: usize = 3;
214 \\ const y = add(1, 2, x);
215 \\ exit(y - 6);
216 \\}
217 \\
218 \\fn add(a: usize, b: usize, c: usize) callconv(.Inline) usize {
219 \\ return a + b + c;
220 \\}
221 \\
222 \\fn exit(code: usize) noreturn {
223 \\ asm volatile ("syscall"
224 \\ :
225 \\ : [number] "{rax}" (231),
226 \\ [arg1] "{rdi}" (code)
227 \\ : "rcx", "r11", "memory"
228 \\ );
229 \\ unreachable;
230 \\}
231 ,
232 "",
233 );
234 }
235
236 {
237 var case = ctx.exe("subtracting numbers at runtime", linux_x64);
238 case.addCompareOutput(
239 \\pub fn main() void {
240 \\ sub(7, 4);
241 \\}
242 \\
243 \\fn sub(a: u32, b: u32) void {
244 \\ if (a - b != 3) unreachable;
245 \\}
246 ,
247 "",
248 );
249 }
250 {
251 var case = ctx.exe("unused vars", linux_x64);
252 case.addError(
253 \\pub fn main() void {
254 \\ const x = 1;
255 \\}
256 , &.{":2:11: error: unused local constant"});
257 }
258 {
259 var case = ctx.exe("@TypeOf", linux_x64);
260 case.addCompareOutput(
261 \\pub fn main() void {
262 \\ var x: usize = 0;
263 \\ _ = x;
264 \\ const z = @TypeOf(x, @as(u128, 5));
265 \\ assert(z == u128);
266 \\}
267 \\
268 \\pub fn assert(ok: bool) void {
269 \\ if (!ok) unreachable; // assertion failure
270 \\}
271 ,
272 "",
273 );
274 case.addCompareOutput(
275 \\pub fn main() void {
276 \\ const z = @TypeOf(true);
277 \\ assert(z == bool);
278 \\}
279 \\
280 \\pub fn assert(ok: bool) void {
281 \\ if (!ok) unreachable; // assertion failure
282 \\}
283 ,
284 "",
285 );
286 case.addError(
287 \\pub fn main() void {
288 \\ _ = @TypeOf(true, 1);
289 \\}
290 , &[_][]const u8{":2:9: error: incompatible types: 'bool' and 'comptime_int'"});
291 }
292
293 {
294 var case = ctx.exe("multiplying numbers at runtime and comptime", linux_x64);
295 case.addCompareOutput(
296 \\pub export fn _start() noreturn {
297 \\ mul(3, 4);
298 \\
299 \\ exit();
300 \\}
301 \\
302 \\fn mul(a: u32, b: u32) void {
303 \\ if (a * b != 12) unreachable;
304 \\}
305 \\
306 \\fn exit() noreturn {
307 \\ asm volatile ("syscall"
308 \\ :
309 \\ : [number] "{rax}" (231),
310 \\ [arg1] "{rdi}" (0)
311 \\ : "rcx", "r11", "memory"
312 \\ );
313 \\ unreachable;
314 \\}
315 ,
316 "",
317 );
318 // comptime function call
319 case.addCompareOutput(
320 \\pub fn _start() noreturn {
321 \\ exit();
322 \\}
323 \\
324 \\fn mul(a: u32, b: u32) u32 {
325 \\ return a * b;
326 \\}
327 \\
328 \\const x = mul(3, 4);
329 \\
330 \\fn exit() noreturn {
331 \\ asm volatile ("syscall"
332 \\ :
333 \\ : [number] "{rax}" (231),
334 \\ [arg1] "{rdi}" (x - 12)
335 \\ : "rcx", "r11", "memory"
336 \\ );
337 \\ unreachable;
338 \\}
339 ,
340 "",
341 );
342 // Inline function call
343 case.addCompareOutput(
344 \\pub export fn _start() noreturn {
345 \\ var x: usize = 5;
346 \\ const y = mul(2, 3, x);
347 \\ exit(y - 30);
348 \\}
349 \\
350 \\fn mul(a: usize, b: usize, c: usize) callconv(.Inline) usize {
351 \\ return a * b * c;
352 \\}
353 \\
354 \\fn exit(code: usize) noreturn {
355 \\ asm volatile ("syscall"
356 \\ :
357 \\ : [number] "{rax}" (231),
358 \\ [arg1] "{rdi}" (code)
359 \\ : "rcx", "r11", "memory"
360 \\ );
361 \\ unreachable;
362 \\}
363 ,
364 "",
365 );
366 }
367
368 {
369 var case = ctx.exe("assert function", linux_x64);
370 case.addCompareOutput(
371 \\pub fn main() void {
372 \\ add(3, 4);
373 \\}
374 \\
375 \\fn add(a: u32, b: u32) void {
376 \\ assert(a + b == 7);
377 \\}
378 \\
379 \\pub fn assert(ok: bool) void {
380 \\ if (!ok) unreachable; // assertion failure
381 \\}
382 \\
383 \\fn exit() noreturn {
384 \\ asm volatile ("syscall"
385 \\ :
386 \\ : [number] "{rax}" (231),
387 \\ [arg1] "{rdi}" (0)
388 \\ : "rcx", "r11", "memory"
389 \\ );
390 \\ unreachable;
391 \\}
392 ,
393 "",
394 );
395
396 // Tests copying a register. For the `c = a + b`, it has to
397 // preserve both a and b, because they are both used later.
398 case.addCompareOutput(
399 \\pub fn main() void {
400 \\ add(3, 4);
401 \\}
402 \\
403 \\fn add(a: u32, b: u32) void {
404 \\ const c = a + b; // 7
405 \\ const d = a + c; // 10
406 \\ const e = d + b; // 14
407 \\ assert(e == 14);
408 \\}
409 \\
410 \\pub fn assert(ok: bool) void {
411 \\ if (!ok) unreachable; // assertion failure
412 \\}
413 ,
414 "",
415 );
416
417 // More stress on the liveness detection.
418 case.addCompareOutput(
419 \\pub fn main() void {
420 \\ add(3, 4);
421 \\}
422 \\
423 \\fn add(a: u32, b: u32) void {
424 \\ const c = a + b; // 7
425 \\ const d = a + c; // 10
426 \\ const e = d + b; // 14
427 \\ const f = d + e; // 24
428 \\ const g = e + f; // 38
429 \\ const h = f + g; // 62
430 \\ const i = g + h; // 100
431 \\ assert(i == 100);
432 \\}
433 \\
434 \\pub fn assert(ok: bool) void {
435 \\ if (!ok) unreachable; // assertion failure
436 \\}
437 ,
438 "",
439 );
440
441 // Requires a second move. The register allocator should figure out to re-use rax.
442 case.addCompareOutput(
443 \\pub fn main() void {
444 \\ add(3, 4);
445 \\}
446 \\
447 \\fn add(a: u32, b: u32) void {
448 \\ const c = a + b; // 7
449 \\ const d = a + c; // 10
450 \\ const e = d + b; // 14
451 \\ const f = d + e; // 24
452 \\ const g = e + f; // 38
453 \\ const h = f + g; // 62
454 \\ const i = g + h; // 100
455 \\ const j = i + d; // 110
456 \\ assert(j == 110);
457 \\}
458 \\
459 \\pub fn assert(ok: bool) void {
460 \\ if (!ok) unreachable; // assertion failure
461 \\}
462 ,
463 "",
464 );
465
466 // Now we test integer return values.
467 case.addCompareOutput(
468 \\pub fn main() void {
469 \\ assert(add(3, 4) == 7);
470 \\ assert(add(20, 10) == 30);
471 \\}
472 \\
473 \\fn add(a: u32, b: u32) u32 {
474 \\ return a + b;
475 \\}
476 \\
477 \\pub fn assert(ok: bool) void {
478 \\ if (!ok) unreachable; // assertion failure
479 \\}
480 ,
481 "",
482 );
483
484 // Local mutable variables.
485 case.addCompareOutput(
486 \\pub fn main() void {
487 \\ assert(add(3, 4) == 7);
488 \\ assert(add(20, 10) == 30);
489 \\}
490 \\
491 \\fn add(a: u32, b: u32) u32 {
492 \\ var x: u32 = undefined;
493 \\ x = 0;
494 \\ x += a;
495 \\ x += b;
496 \\ return x;
497 \\}
498 \\
499 \\pub fn assert(ok: bool) void {
500 \\ if (!ok) unreachable; // assertion failure
501 \\}
502 ,
503 "",
504 );
505
506 // Optionals
507 case.addCompareOutput(
508 \\pub fn main() void {
509 \\ const a: u32 = 2;
510 \\ const b: ?u32 = a;
511 \\ const c = b.?;
512 \\ if (c != 2) unreachable;
513 \\}
514 ,
515 "",
516 );
517
518 // While loops
519 case.addCompareOutput(
520 \\pub fn main() void {
521 \\ var i: u32 = 0;
522 \\ while (i < 4) : (i += 1) print();
523 \\ assert(i == 4);
524 \\}
525 \\
526 \\fn print() void {
527 \\ asm volatile ("syscall"
528 \\ :
529 \\ : [number] "{rax}" (1),
530 \\ [arg1] "{rdi}" (1),
531 \\ [arg2] "{rsi}" (@ptrToInt("hello\n")),
532 \\ [arg3] "{rdx}" (6)
533 \\ : "rcx", "r11", "memory"
534 \\ );
535 \\ return;
536 \\}
537 \\
538 \\pub fn assert(ok: bool) void {
539 \\ if (!ok) unreachable; // assertion failure
540 \\}
541 ,
542 "hello\nhello\nhello\nhello\n",
543 );
544
545 // inline while requires the condition to be comptime known.
546 case.addError(
547 \\pub fn main() void {
548 \\ var i: u32 = 0;
549 \\ inline while (i < 4) : (i += 1) print();
550 \\ assert(i == 4);
551 \\}
552 \\
553 \\fn print() void {
554 \\ asm volatile ("syscall"
555 \\ :
556 \\ : [number] "{rax}" (1),
557 \\ [arg1] "{rdi}" (1),
558 \\ [arg2] "{rsi}" (@ptrToInt("hello\n")),
559 \\ [arg3] "{rdx}" (6)
560 \\ : "rcx", "r11", "memory"
561 \\ );
562 \\ return;
563 \\}
564 \\
565 \\pub fn assert(ok: bool) void {
566 \\ if (!ok) unreachable; // assertion failure
567 \\}
568 , &[_][]const u8{":3:21: error: unable to resolve comptime value"});
569
570 // Labeled blocks (no conditional branch)
571 case.addCompareOutput(
572 \\pub fn main() void {
573 \\ assert(add(3, 4) == 20);
574 \\}
575 \\
576 \\fn add(a: u32, b: u32) u32 {
577 \\ const x: u32 = blk: {
578 \\ const c = a + b; // 7
579 \\ const d = a + c; // 10
580 \\ const e = d + b; // 14
581 \\ break :blk e;
582 \\ };
583 \\ const y = x + a; // 17
584 \\ const z = y + a; // 20
585 \\ return z;
586 \\}
587 \\
588 \\pub fn assert(ok: bool) void {
589 \\ if (!ok) unreachable; // assertion failure
590 \\}
591 ,
592 "",
593 );
594
595 // This catches a possible bug in the logic for re-using dying operands.
596 case.addCompareOutput(
597 \\pub fn main() void {
598 \\ assert(add(3, 4) == 116);
599 \\}
600 \\
601 \\fn add(a: u32, b: u32) u32 {
602 \\ const x: u32 = blk: {
603 \\ const c = a + b; // 7
604 \\ const d = a + c; // 10
605 \\ const e = d + b; // 14
606 \\ const f = d + e; // 24
607 \\ const g = e + f; // 38
608 \\ const h = f + g; // 62
609 \\ const i = g + h; // 100
610 \\ const j = i + d; // 110
611 \\ break :blk j;
612 \\ };
613 \\ const y = x + a; // 113
614 \\ const z = y + a; // 116
615 \\ return z;
616 \\}
617 \\
618 \\pub fn assert(ok: bool) void {
619 \\ if (!ok) unreachable; // assertion failure
620 \\}
621 ,
622 "",
623 );
624
625 // Spilling registers to the stack.
626 case.addCompareOutput(
627 \\pub fn main() void {
628 \\ assert(add(3, 4) == 1221);
629 \\ assert(mul(3, 4) == 21609);
630 \\}
631 \\
632 \\fn add(a: u32, b: u32) u32 {
633 \\ const x: u32 = blk: {
634 \\ const c = a + b; // 7
635 \\ const d = a + c; // 10
636 \\ const e = d + b; // 14
637 \\ const f = d + e; // 24
638 \\ const g = e + f; // 38
639 \\ const h = f + g; // 62
640 \\ const i = g + h; // 100
641 \\ const j = i + d; // 110
642 \\ const k = i + j; // 210
643 \\ const l = j + k; // 320
644 \\ const m = l + c; // 327
645 \\ const n = m + d; // 337
646 \\ const o = n + e; // 351
647 \\ const p = o + f; // 375
648 \\ const q = p + g; // 413
649 \\ const r = q + h; // 475
650 \\ const s = r + i; // 575
651 \\ const t = s + j; // 685
652 \\ const u = t + k; // 895
653 \\ const v = u + l; // 1215
654 \\ break :blk v;
655 \\ };
656 \\ const y = x + a; // 1218
657 \\ const z = y + a; // 1221
658 \\ return z;
659 \\}
660 \\
661 \\fn mul(a: u32, b: u32) u32 {
662 \\ const x: u32 = blk: {
663 \\ const c = a * a * a * a; // 81
664 \\ const d = a * a * a * b; // 108
665 \\ const e = a * a * b * a; // 108
666 \\ const f = a * a * b * b; // 144
667 \\ const g = a * b * a * a; // 108
668 \\ const h = a * b * a * b; // 144
669 \\ const i = a * b * b * a; // 144
670 \\ const j = a * b * b * b; // 192
671 \\ const k = b * a * a * a; // 108
672 \\ const l = b * a * a * b; // 144
673 \\ const m = b * a * b * a; // 144
674 \\ const n = b * a * b * b; // 192
675 \\ const o = b * b * a * a; // 144
676 \\ const p = b * b * a * b; // 192
677 \\ const q = b * b * b * a; // 192
678 \\ const r = b * b * b * b; // 256
679 \\ const s = c + d + e + f + g + h + i + j + k + l + m + n + o + p + q + r; // 2401
680 \\ break :blk s;
681 \\ };
682 \\ const y = x * a; // 7203
683 \\ const z = y * a; // 21609
684 \\ return z;
685 \\}
686 \\
687 \\pub fn assert(ok: bool) void {
688 \\ if (!ok) unreachable; // assertion failure
689 \\}
690 ,
691 "",
692 );
693
694 // Reusing the registers of dead operands playing nicely with conditional branching.
695 case.addCompareOutput(
696 \\pub fn main() void {
697 \\ assert(add(3, 4) == 791);
698 \\ assert(add(4, 3) == 79);
699 \\}
700 \\
701 \\fn add(a: u32, b: u32) u32 {
702 \\ const x: u32 = if (a < b) blk: {
703 \\ const c = a + b; // 7
704 \\ const d = a + c; // 10
705 \\ const e = d + b; // 14
706 \\ const f = d + e; // 24
707 \\ const g = e + f; // 38
708 \\ const h = f + g; // 62
709 \\ const i = g + h; // 100
710 \\ const j = i + d; // 110
711 \\ const k = i + j; // 210
712 \\ const l = k + c; // 217
713 \\ const m = l + d; // 227
714 \\ const n = m + e; // 241
715 \\ const o = n + f; // 265
716 \\ const p = o + g; // 303
717 \\ const q = p + h; // 365
718 \\ const r = q + i; // 465
719 \\ const s = r + j; // 575
720 \\ const t = s + k; // 785
721 \\ break :blk t;
722 \\ } else blk: {
723 \\ const t = b + b + a; // 10
724 \\ const c = a + t; // 14
725 \\ const d = c + t; // 24
726 \\ const e = d + t; // 34
727 \\ const f = e + t; // 44
728 \\ const g = f + t; // 54
729 \\ const h = c + g; // 68
730 \\ break :blk h + b; // 71
731 \\ };
732 \\ const y = x + a; // 788, 75
733 \\ const z = y + a; // 791, 79
734 \\ return z;
735 \\}
736 \\
737 \\pub fn assert(ok: bool) void {
738 \\ if (!ok) unreachable; // assertion failure
739 \\}
740 ,
741 "",
742 );
743
744 // Character literals and multiline strings.
745 case.addCompareOutput(
746 \\pub fn main() void {
747 \\ const ignore =
748 \\ \\ cool thx
749 \\ \\
750 \\ ;
751 \\ _ = ignore;
752 \\ add('ぁ', '\x03');
753 \\}
754 \\
755 \\fn add(a: u32, b: u32) void {
756 \\ assert(a + b == 12356);
757 \\}
758 \\
759 \\pub fn assert(ok: bool) void {
760 \\ if (!ok) unreachable; // assertion failure
761 \\}
762 ,
763 "",
764 );
765
766 // Global const.
767 case.addCompareOutput(
768 \\pub fn main() void {
769 \\ add(aa, bb);
770 \\}
771 \\
772 \\const aa = 'ぁ';
773 \\const bb = '\x03';
774 \\
775 \\fn add(a: u32, b: u32) void {
776 \\ assert(a + b == 12356);
777 \\}
778 \\
779 \\pub fn assert(ok: bool) void {
780 \\ if (!ok) unreachable; // assertion failure
781 \\}
782 ,
783 "",
784 );
785
786 // Array access.
787 case.addCompareOutput(
788 \\pub fn main() void {
789 \\ assert("hello"[0] == 'h');
790 \\}
791 \\
792 \\pub fn assert(ok: bool) void {
793 \\ if (!ok) unreachable; // assertion failure
794 \\}
795 ,
796 "",
797 );
798
799 // Array access to a global array.
800 case.addCompareOutput(
801 \\const hello = "hello".*;
802 \\pub fn main() void {
803 \\ assert(hello[1] == 'e');
804 \\}
805 \\
806 \\pub fn assert(ok: bool) void {
807 \\ if (!ok) unreachable; // assertion failure
808 \\}
809 ,
810 "",
811 );
812
813 // 64bit set stack
814 case.addCompareOutput(
815 \\pub fn main() void {
816 \\ var i: u64 = 0xFFEEDDCCBBAA9988;
817 \\ assert(i == 0xFFEEDDCCBBAA9988);
818 \\}
819 \\
820 \\pub fn assert(ok: bool) void {
821 \\ if (!ok) unreachable; // assertion failure
822 \\}
823 ,
824 "",
825 );
826
827 // Basic for loop
828 case.addCompareOutput(
829 \\pub fn main() void {
830 \\ for ("hello") |_| print();
831 \\}
832 \\
833 \\fn print() void {
834 \\ asm volatile ("syscall"
835 \\ :
836 \\ : [number] "{rax}" (1),
837 \\ [arg1] "{rdi}" (1),
838 \\ [arg2] "{rsi}" (@ptrToInt("hello\n")),
839 \\ [arg3] "{rdx}" (6)
840 \\ : "rcx", "r11", "memory"
841 \\ );
842 \\ return;
843 \\}
844 ,
845 "hello\nhello\nhello\nhello\nhello\n",
846 );
847 }
848
849 {
850 var case = ctx.exe("basic import", linux_x64);
851 case.addCompareOutput(
852 \\pub fn main() void {
853 \\ @import("print.zig").print();
854 \\}
855 ,
856 "Hello, World!\n",
857 );
858 try case.files.append(.{
859 .src =
860 \\pub fn print() void {
861 \\ asm volatile ("syscall"
862 \\ :
863 \\ : [number] "{rax}" (@as(usize, 1)),
864 \\ [arg1] "{rdi}" (@as(usize, 1)),
865 \\ [arg2] "{rsi}" (@ptrToInt("Hello, World!\n")),
866 \\ [arg3] "{rdx}" (@as(usize, 14))
867 \\ : "rcx", "r11", "memory"
868 \\ );
869 \\ return;
870 \\}
871 ,
872 .path = "print.zig",
873 });
874 }
875 {
876 var case = ctx.exe("redundant comptime", linux_x64);
877 case.addError(
878 \\pub fn main() void {
879 \\ var a: comptime u32 = 0;
880 \\}
881 ,
882 &.{":2:12: error: redundant comptime keyword in already comptime scope"},
883 );
884 case.addError(
885 \\pub fn main() void {
886 \\ comptime {
887 \\ var a: u32 = comptime 0;
888 \\ }
889 \\}
890 ,
891 &.{":3:22: error: redundant comptime keyword in already comptime scope"},
892 );
893 }
894 {
895 var case = ctx.exe("try in comptime in struct in test", linux_x64);
896 case.addError(
897 \\test "@unionInit on union w/ tag but no fields" {
898 \\ const S = struct {
899 \\ comptime {
900 \\ try expect(false);
901 \\ }
902 \\ };
903 \\ _ = S;
904 \\}
905 ,
906 &.{":4:13: error: invalid 'try' outside function scope"},
907 );
908 }
909 {
910 var case = ctx.exe("import private", linux_x64);
911 case.addError(
912 \\pub fn main() void {
913 \\ @import("print.zig").print();
914 \\}
915 ,
916 &.{
917 ":2:25: error: 'print' is not marked 'pub'",
918 "print.zig:2:1: note: declared here",
919 },
920 );
921 try case.files.append(.{
922 .src =
923 \\// dummy comment to make print be on line 2
924 \\fn print() void {
925 \\ asm volatile ("syscall"
926 \\ :
927 \\ : [number] "{rax}" (@as(usize, 1)),
928 \\ [arg1] "{rdi}" (@as(usize, 1)),
929 \\ [arg2] "{rsi}" (@ptrToInt("Hello, World!\n")),
930 \\ [arg3] "{rdx}" (@as(usize, 14))
931 \\ : "rcx", "r11", "memory"
932 \\ );
933 \\ return;
934 \\}
935 ,
936 .path = "print.zig",
937 });
938 }
939
940 ctx.compileError("function redeclaration", linux_x64,
941 \\// dummy comment
942 \\fn entry() void {}
943 \\fn entry() void {}
944 \\
945 \\fn foo() void {
946 \\ var foo = 1234;
947 \\}
948 , &[_][]const u8{
949 ":3:1: error: redeclaration of 'entry'",
950 ":2:1: note: other declaration here",
951 ":6:9: error: local shadows declaration of 'foo'",
952 ":5:1: note: declared here",
953 });
954
955 ctx.compileError("returns in try", linux_x64,
956 \\pub fn main() !void {
957 \\ try a();
958 \\ try b();
959 \\}
960 \\
961 \\pub fn a() !void {
962 \\ defer try b();
963 \\}
964 \\pub fn b() !void {
965 \\ defer return a();
966 \\}
967 , &[_][]const u8{
968 ":7:8: error: try is not allowed inside defer expression",
969 ":10:8: error: cannot return from defer expression",
970 });
971
972 ctx.compileError("ambiguous references", linux_x64,
973 \\const T = struct {
974 \\ const T = struct {
975 \\ fn f() void {
976 \\ _ = T;
977 \\ }
978 \\ };
979 \\};
980 , &.{
981 ":4:17: error: ambiguous reference",
982 ":1:1: note: declared here",
983 ":2:5: note: also declared here",
984 });
985
986 ctx.compileError("inner func accessing outer var", linux_x64,
987 \\pub fn f() void {
988 \\ var bar: bool = true;
989 \\ const S = struct {
990 \\ fn baz() bool {
991 \\ return bar;
992 \\ }
993 \\ };
994 \\ _ = S;
995 \\}
996 , &.{
997 ":5:20: error: 'bar' not accessible from inner function",
998 ":2:9: note: declared here",
999 });
1000
1001 ctx.compileError("global variable redeclaration", linux_x64,
1002 \\// dummy comment
1003 \\var foo = false;
1004 \\var foo = true;
1005 , &[_][]const u8{
1006 ":3:1: error: redeclaration of 'foo'",
1007 ":2:1: note: other declaration here",
1008 });
1009
1010 ctx.compileError("compileError", linux_x64,
1011 \\export fn foo() void {
1012 \\ @compileError("this is an error");
1013 \\}
1014 , &[_][]const u8{":2:3: error: this is an error"});
1015
1016 {
1017 var case = ctx.exe("intToPtr", linux_x64);
1018 case.addError(
1019 \\pub fn main() void {
1020 \\ _ = @intToPtr(*u8, 0);
1021 \\}
1022 , &[_][]const u8{
1023 ":2:24: error: pointer type '*u8' does not allow address zero",
1024 });
1025 case.addError(
1026 \\pub fn main() void {
1027 \\ _ = @intToPtr(*u32, 2);
1028 \\}
1029 , &[_][]const u8{
1030 ":2:25: error: pointer type '*u32' requires aligned address",
1031 });
1032 }
1033
1034 {
1035 var case = ctx.obj("variable shadowing", linux_x64);
1036 case.addError(
1037 \\pub fn main() void {
1038 \\ var i: u32 = 10;
1039 \\ var i: u32 = 10;
1040 \\}
1041 , &[_][]const u8{
1042 ":3:9: error: redeclaration of 'i'",
1043 ":2:9: note: previously declared here",
1044 });
1045 case.addError(
1046 \\var testing: i64 = 10;
1047 \\pub fn main() void {
1048 \\ var testing: i64 = 20;
1049 \\}
1050 , &[_][]const u8{
1051 ":3:9: error: local shadows declaration of 'testing'",
1052 ":1:1: note: declared here",
1053 });
1054 case.addError(
1055 \\fn a() type {
1056 \\ return struct {
1057 \\ pub fn b() void {
1058 \\ const c = 6;
1059 \\ const c = 69;
1060 \\ }
1061 \\ };
1062 \\}
1063 , &[_][]const u8{
1064 ":5:19: error: redeclaration of 'c'",
1065 ":4:19: note: previously declared here",
1066 });
1067 }
1068
1069 {
1070 // TODO make the test harness support checking the compile log output too
1071 var case = ctx.obj("@compileLog", linux_x64);
1072 // The other compile error prevents emission of a "found compile log" statement.
1073 case.addError(
1074 \\export fn _start() noreturn {
1075 \\ const b = true;
1076 \\ var f: u32 = 1;
1077 \\ @compileLog(b, 20, f, x);
1078 \\ @compileLog(1000);
1079 \\ var bruh: usize = true;
1080 \\ _ = bruh;
1081 \\ unreachable;
1082 \\}
1083 \\export fn other() void {
1084 \\ @compileLog(1234);
1085 \\}
1086 \\fn x() void {}
1087 , &[_][]const u8{
1088 ":6:23: error: expected usize, found bool",
1089 });
1090
1091 // Now only compile log statements remain. One per Decl.
1092 case.addError(
1093 \\export fn _start() noreturn {
1094 \\ const b = true;
1095 \\ var f: u32 = 1;
1096 \\ @compileLog(b, 20, f, x);
1097 \\ @compileLog(1000);
1098 \\ unreachable;
1099 \\}
1100 \\export fn other() void {
1101 \\ @compileLog(1234);
1102 \\}
1103 \\fn x() void {}
1104 , &[_][]const u8{
1105 ":9:5: error: found compile log statement",
1106 ":4:5: note: also here",
1107 });
1108 }
1109
1110 {
1111 var case = ctx.obj("extern variable has no type", linux_x64);
1112 case.addError(
1113 \\comptime {
1114 \\ _ = foo;
1115 \\}
1116 \\extern var foo: i32;
1117 , &[_][]const u8{":2:9: error: unable to resolve comptime value"});
1118 case.addError(
1119 \\export fn entry() void {
1120 \\ _ = foo;
1121 \\}
1122 \\extern var foo;
1123 , &[_][]const u8{":4:8: error: unable to infer variable type"});
1124 }
1125
1126 {
1127 var case = ctx.exe("break/continue", linux_x64);
1128
1129 // Break out of loop
1130 case.addCompareOutput(
1131 \\pub fn main() void {
1132 \\ while (true) {
1133 \\ break;
1134 \\ }
1135 \\}
1136 ,
1137 "",
1138 );
1139 case.addCompareOutput(
1140 \\pub fn main() void {
1141 \\ foo: while (true) {
1142 \\ break :foo;
1143 \\ }
1144 \\}
1145 ,
1146 "",
1147 );
1148
1149 // Continue in loop
1150 case.addCompareOutput(
1151 \\pub export fn _start() noreturn {
1152 \\ var i: u64 = 0;
1153 \\ while (true) : (i+=1) {
1154 \\ if (i == 4) exit();
1155 \\ continue;
1156 \\ }
1157 \\}
1158 \\
1159 \\fn exit() noreturn {
1160 \\ asm volatile ("syscall"
1161 \\ :
1162 \\ : [number] "{rax}" (231),
1163 \\ [arg1] "{rdi}" (0)
1164 \\ : "rcx", "r11", "memory"
1165 \\ );
1166 \\ unreachable;
1167 \\}
1168 ,
1169 "",
1170 );
1171 case.addCompareOutput(
1172 \\pub export fn _start() noreturn {
1173 \\ var i: u64 = 0;
1174 \\ foo: while (true) : (i+=1) {
1175 \\ if (i == 4) exit();
1176 \\ continue :foo;
1177 \\ }
1178 \\}
1179 \\
1180 \\fn exit() noreturn {
1181 \\ asm volatile ("syscall"
1182 \\ :
1183 \\ : [number] "{rax}" (231),
1184 \\ [arg1] "{rdi}" (0)
1185 \\ : "rcx", "r11", "memory"
1186 \\ );
1187 \\ unreachable;
1188 \\}
1189 ,
1190 "",
1191 );
1192 }
1193
1194 {
1195 var case = ctx.exe("unused labels", linux_x64);
1196 case.addError(
1197 \\comptime {
1198 \\ foo: {}
1199 \\}
1200 , &[_][]const u8{":2:5: error: unused block label"});
1201 case.addError(
1202 \\comptime {
1203 \\ foo: while (true) {}
1204 \\}
1205 , &[_][]const u8{":2:5: error: unused while loop label"});
1206 case.addError(
1207 \\comptime {
1208 \\ foo: for ("foo") |_| {}
1209 \\}
1210 , &[_][]const u8{":2:5: error: unused for loop label"});
1211 case.addError(
1212 \\comptime {
1213 \\ blk: {blk: {}}
1214 \\}
1215 , &[_][]const u8{
1216 ":2:11: error: redefinition of label 'blk'",
1217 ":2:5: note: previous definition is here",
1218 });
1219 }
1220
1221 {
1222 var case = ctx.exe("bad inferred variable type", linux_x64);
1223 case.addError(
1224 \\pub fn main() void {
1225 \\ var x = null;
1226 \\ _ = x;
1227 \\}
1228 , &[_][]const u8{
1229 ":2:9: error: variable of type '@Type(.Null)' must be const or comptime",
1230 });
1231 }
1232
1233 {
1234 var case = ctx.exe("compile error in inline fn call fixed", linux_x64);
1235 case.addError(
1236 \\pub export fn _start() noreturn {
1237 \\ var x: usize = 3;
1238 \\ const y = add(10, 2, x);
1239 \\ exit(y - 6);
1240 \\}
1241 \\
1242 \\fn add(a: usize, b: usize, c: usize) callconv(.Inline) usize {
1243 \\ if (a == 10) @compileError("bad");
1244 \\ return a + b + c;
1245 \\}
1246 \\
1247 \\fn exit(code: usize) noreturn {
1248 \\ asm volatile ("syscall"
1249 \\ :
1250 \\ : [number] "{rax}" (231),
1251 \\ [arg1] "{rdi}" (code)
1252 \\ : "rcx", "r11", "memory"
1253 \\ );
1254 \\ unreachable;
1255 \\}
1256 , &[_][]const u8{":8:18: error: bad"});
1257
1258 case.addCompareOutput(
1259 \\pub export fn _start() noreturn {
1260 \\ var x: usize = 3;
1261 \\ const y = add(1, 2, x);
1262 \\ exit(y - 6);
1263 \\}
1264 \\
1265 \\fn add(a: usize, b: usize, c: usize) callconv(.Inline) usize {
1266 \\ if (a == 10) @compileError("bad");
1267 \\ return a + b + c;
1268 \\}
1269 \\
1270 \\fn exit(code: usize) noreturn {
1271 \\ asm volatile ("syscall"
1272 \\ :
1273 \\ : [number] "{rax}" (231),
1274 \\ [arg1] "{rdi}" (code)
1275 \\ : "rcx", "r11", "memory"
1276 \\ );
1277 \\ unreachable;
1278 \\}
1279 ,
1280 "",
1281 );
1282 }
1283 {
1284 var case = ctx.exe("recursive inline function", linux_x64);
1285 case.addCompareOutput(
1286 \\pub export fn _start() noreturn {
1287 \\ const y = fibonacci(7);
1288 \\ exit(y - 21);
1289 \\}
1290 \\
1291 \\fn fibonacci(n: usize) callconv(.Inline) usize {
1292 \\ if (n <= 2) return n;
1293 \\ return fibonacci(n - 2) + fibonacci(n - 1);
1294 \\}
1295 \\
1296 \\fn exit(code: usize) noreturn {
1297 \\ asm volatile ("syscall"
1298 \\ :
1299 \\ : [number] "{rax}" (231),
1300 \\ [arg1] "{rdi}" (code)
1301 \\ : "rcx", "r11", "memory"
1302 \\ );
1303 \\ unreachable;
1304 \\}
1305 ,
1306 "",
1307 );
1308 // This additionally tests that the compile error reports the correct source location.
1309 // Without storing source locations relative to the owner decl, the compile error
1310 // here would be off by 2 bytes (from the "7" -> "999").
1311 case.addError(
1312 \\pub export fn _start() noreturn {
1313 \\ const y = fibonacci(999);
1314 \\ exit(y - 21);
1315 \\}
1316 \\
1317 \\fn fibonacci(n: usize) callconv(.Inline) usize {
1318 \\ if (n <= 2) return n;
1319 \\ return fibonacci(n - 2) + fibonacci(n - 1);
1320 \\}
1321 \\
1322 \\fn exit(code: usize) noreturn {
1323 \\ asm volatile ("syscall"
1324 \\ :
1325 \\ : [number] "{rax}" (231),
1326 \\ [arg1] "{rdi}" (code)
1327 \\ : "rcx", "r11", "memory"
1328 \\ );
1329 \\ unreachable;
1330 \\}
1331 , &[_][]const u8{":8:21: error: evaluation exceeded 1000 backwards branches"});
1332 }
1333 {
1334 var case = ctx.exe("orelse at comptime", linux_x64);
1335 case.addCompareOutput(
1336 \\pub fn main() void {
1337 \\ const i: ?u64 = 0;
1338 \\ const result = i orelse 5;
1339 \\ assert(result == 0);
1340 \\}
1341 \\fn assert(b: bool) void {
1342 \\ if (!b) unreachable;
1343 \\}
1344 ,
1345 "",
1346 );
1347 case.addCompareOutput(
1348 \\pub fn main() void {
1349 \\ const i: ?u64 = null;
1350 \\ const result = i orelse 5;
1351 \\ assert(result == 5);
1352 \\}
1353 \\fn assert(b: bool) void {
1354 \\ if (!b) unreachable;
1355 \\}
1356 ,
1357 "",
1358 );
1359 }
1360
1361 {
1362 var case = ctx.exe("only 1 function and it gets updated", linux_x64);
1363 case.addCompareOutput(
1364 \\pub export fn _start() noreturn {
1365 \\ asm volatile ("syscall"
1366 \\ :
1367 \\ : [number] "{rax}" (60), // exit
1368 \\ [arg1] "{rdi}" (0)
1369 \\ : "rcx", "r11", "memory"
1370 \\ );
1371 \\ unreachable;
1372 \\}
1373 ,
1374 "",
1375 );
1376 case.addCompareOutput(
1377 \\pub export fn _start() noreturn {
1378 \\ asm volatile ("syscall"
1379 \\ :
1380 \\ : [number] "{rax}" (231), // exit_group
1381 \\ [arg1] "{rdi}" (0)
1382 \\ : "rcx", "r11", "memory"
1383 \\ );
1384 \\ unreachable;
1385 \\}
1386 ,
1387 "",
1388 );
1389 }
1390 {
1391 var case = ctx.exe("passing u0 to function", linux_x64);
1392 case.addCompareOutput(
1393 \\pub fn main() void {
1394 \\ doNothing(0);
1395 \\}
1396 \\fn doNothing(arg: u0) void {
1397 \\ _ = arg;
1398 \\}
1399 ,
1400 "",
1401 );
1402 }
1403 {
1404 var case = ctx.exe("catch at comptime", linux_x64);
1405 case.addCompareOutput(
1406 \\pub fn main() void {
1407 \\ const i: anyerror!u64 = 0;
1408 \\ const caught = i catch 5;
1409 \\ assert(caught == 0);
1410 \\}
1411 \\fn assert(b: bool) void {
1412 \\ if (!b) unreachable;
1413 \\}
1414 ,
1415 "",
1416 );
1417
1418 case.addCompareOutput(
1419 \\pub fn main() void {
1420 \\ const i: anyerror!u64 = error.B;
1421 \\ const caught = i catch 5;
1422 \\ assert(caught == 5);
1423 \\}
1424 \\fn assert(b: bool) void {
1425 \\ if (!b) unreachable;
1426 \\}
1427 ,
1428 "",
1429 );
1430
1431 case.addCompareOutput(
1432 \\pub fn main() void {
1433 \\ const a: anyerror!comptime_int = 42;
1434 \\ const b: *const comptime_int = &(a catch unreachable);
1435 \\ assert(b.* == 42);
1436 \\}
1437 \\fn assert(b: bool) void {
1438 \\ if (!b) unreachable; // assertion failure
1439 \\}
1440 , "");
1441
1442 case.addCompareOutput(
1443 \\pub fn main() void {
1444 \\ const a: anyerror!u32 = error.B;
1445 \\ _ = &(a catch |err| assert(err == error.B));
1446 \\}
1447 \\fn assert(b: bool) void {
1448 \\ if (!b) unreachable;
1449 \\}
1450 , "");
1451
1452 case.addCompareOutput(
1453 \\pub fn main() void {
1454 \\ const a: anyerror!u32 = error.Bar;
1455 \\ a catch |err| assert(err == error.Bar);
1456 \\}
1457 \\fn assert(b: bool) void {
1458 \\ if (!b) unreachable;
1459 \\}
1460 , "");
1461 }
1462 {
1463 var case = ctx.exe("merge error sets", linux_x64);
1464
1465 case.addCompareOutput(
1466 \\pub fn main() void {
1467 \\ const E = error{ A, B, D } || error { A, B, C };
1468 \\ E.A catch {};
1469 \\ E.B catch {};
1470 \\ E.C catch {};
1471 \\ E.D catch {};
1472 \\ const E2 = error { X, Y } || @TypeOf(error.Z);
1473 \\ E2.X catch {};
1474 \\ E2.Y catch {};
1475 \\ E2.Z catch {};
1476 \\ assert(anyerror || error { Z } == anyerror);
1477 \\}
1478 \\fn assert(b: bool) void {
1479 \\ if (!b) unreachable;
1480 \\}
1481 ,
1482 "",
1483 );
1484 }
1485 {
1486 var case = ctx.exe("inline assembly", linux_x64);
1487
1488 case.addError(
1489 \\pub fn main() void {
1490 \\ const number = 1234;
1491 \\ const x = asm volatile ("syscall"
1492 \\ : [o] "{rax}" (-> number)
1493 \\ : [number] "{rax}" (231),
1494 \\ [arg1] "{rdi}" (code)
1495 \\ : "rcx", "r11", "memory"
1496 \\ );
1497 \\ _ = x;
1498 \\}
1499 , &[_][]const u8{":4:27: error: expected type, found comptime_int"});
1500 }
1501 {
1502 var case = ctx.exe("comptime var", linux_x64);
1503
1504 case.addError(
1505 \\pub fn main() void {
1506 \\ var a: u32 = 0;
1507 \\ comptime var b: u32 = 0;
1508 \\ if (a == 0) b = 3;
1509 \\}
1510 , &.{
1511 ":4:21: error: store to comptime variable depends on runtime condition",
1512 ":4:11: note: runtime condition here",
1513 });
1514
1515 case.addError(
1516 \\pub fn main() void {
1517 \\ var a: u32 = 0;
1518 \\ comptime var b: u32 = 0;
1519 \\ switch (a) {
1520 \\ 0 => {},
1521 \\ else => b = 3,
1522 \\ }
1523 \\}
1524 , &.{
1525 ":6:21: error: store to comptime variable depends on runtime condition",
1526 ":4:13: note: runtime condition here",
1527 });
1528
1529 case.addCompareOutput(
1530 \\pub fn main() void {
1531 \\ comptime var len: u32 = 5;
1532 \\ print(len);
1533 \\ len += 9;
1534 \\ print(len);
1535 \\}
1536 \\
1537 \\fn print(len: usize) void {
1538 \\ asm volatile ("syscall"
1539 \\ :
1540 \\ : [number] "{rax}" (1),
1541 \\ [arg1] "{rdi}" (1),
1542 \\ [arg2] "{rsi}" (@ptrToInt("Hello, World!\n")),
1543 \\ [arg3] "{rdx}" (len)
1544 \\ : "rcx", "r11", "memory"
1545 \\ );
1546 \\ return;
1547 \\}
1548 , "HelloHello, World!\n");
1549
1550 case.addError(
1551 \\comptime {
1552 \\ var x: i32 = 1;
1553 \\ x += 1;
1554 \\ if (x != 1) unreachable;
1555 \\}
1556 \\pub fn main() void {}
1557 , &.{":4:17: error: unable to resolve comptime value"});
1558
1559 case.addError(
1560 \\pub fn main() void {
1561 \\ comptime var i: u64 = 0;
1562 \\ while (i < 5) : (i += 1) {}
1563 \\}
1564 , &.{
1565 ":3:24: error: cannot store to comptime variable in non-inline loop",
1566 ":3:5: note: non-inline loop here",
1567 });
1568
1569 case.addCompareOutput(
1570 \\pub fn main() void {
1571 \\ var a: u32 = 0;
1572 \\ if (a == 0) {
1573 \\ comptime var b: u32 = 0;
1574 \\ b = 1;
1575 \\ }
1576 \\}
1577 \\comptime {
1578 \\ var x: i32 = 1;
1579 \\ x += 1;
1580 \\ if (x != 2) unreachable;
1581 \\}
1582 , "");
1583
1584 case.addCompareOutput(
1585 \\pub fn main() void {
1586 \\ comptime var i: u64 = 2;
1587 \\ inline while (i < 6) : (i+=1) {
1588 \\ print(i);
1589 \\ }
1590 \\}
1591 \\fn print(len: usize) void {
1592 \\ asm volatile ("syscall"
1593 \\ :
1594 \\ : [number] "{rax}" (1),
1595 \\ [arg1] "{rdi}" (1),
1596 \\ [arg2] "{rsi}" (@ptrToInt("Hello")),
1597 \\ [arg3] "{rdx}" (len)
1598 \\ : "rcx", "r11", "memory"
1599 \\ );
1600 \\ return;
1601 \\}
1602 , "HeHelHellHello");
1603 }
1604
1605 {
1606 var case = ctx.exe("double ampersand", linux_x64);
1607
1608 case.addError(
1609 \\pub const a = if (true && false) 1 else 2;
1610 , &[_][]const u8{":1:24: error: `&&` is invalid; note that `and` is boolean AND"});
1611
1612 case.addError(
1613 \\pub fn main() void {
1614 \\ const a = true;
1615 \\ const b = false;
1616 \\ _ = a & &b;
1617 \\}
1618 , &[_][]const u8{":4:11: error: incompatible types: 'bool' and '*const bool'"});
1619
1620 case.addCompareOutput(
1621 \\pub fn main() void {
1622 \\ const b: u8 = 1;
1623 \\ _ = &&b;
1624 \\}
1625 , "");
1626 }
1627}
test/compile_errors.zig+1291-989
......@@ -1,53 +1,50 @@
1const tests = @import("tests.zig");
21const std = @import("std");
2const TestContext = @import("../src/test.zig").TestContext;
33
4pub fn addCases(cases: *tests.CompileErrorContext) void {
5 cases.add("std.fmt error for unused arguments",
6 \\pub fn main() !void {
7 \\ @import("std").debug.print("{d} {d} {d} {d} {d}", .{1,2,3,4,5,6,7,8,9,10,11,12,13,14,15});
8 \\}
9 , &.{
10 \\error: 10 unused arguments in "{d} {d} {d} {d} {d}"
11 });
12
13 cases.add("lazy pointer with undefined element type",
4pub fn addCases(ctx: *TestContext) !void {
5 ctx.objErrStage1("lazy pointer with undefined element type",
146 \\export fn foo() void {
157 \\ comptime var T: type = undefined;
168 \\ const S = struct { x: *T };
179 \\ const I = @typeInfo(S);
10 \\ _ = I;
1811 \\}
1912 , &[_][]const u8{
20 "tmp.zig:3:28: error: use of undefined value here causes undefined behavior",
13 ":3:28: error: use of undefined value here causes undefined behavior",
2114 });
2215
23 cases.add("pointer arithmetic on pointer-to-array",
16 ctx.objErrStage1("pointer arithmetic on pointer-to-array",
2417 \\export fn foo() void {
2518 \\ var x: [10]u8 = undefined;
2619 \\ var y = &x;
2720 \\ var z = y + 1;
21 \\ _ = z;
2822 \\}
2923 , &[_][]const u8{
3024 "tmp.zig:4:17: error: integer value 1 cannot be coerced to type '*[10]u8'",
3125 });
3226
33 cases.add("pointer attributes checked when coercing pointer to anon literal",
27 ctx.objErrStage1("pointer attributes checked when coercing pointer to anon literal",
3428 \\comptime {
3529 \\ const c: [][]const u8 = &.{"hello", "world" };
30 \\ _ = c;
3631 \\}
3732 \\comptime {
3833 \\ const c: *[2][]const u8 = &.{"hello", "world" };
34 \\ _ = c;
3935 \\}
4036 \\const S = struct {a: u8 = 1, b: u32 = 2};
4137 \\comptime {
4238 \\ const c: *S = &.{};
39 \\ _ = c;
4340 \\}
4441 , &[_][]const u8{
4542 "tmp.zig:2:31: error: expected type '[][]const u8', found '*const struct:2:31'",
46 "tmp.zig:5:33: error: expected type '*[2][]const u8', found '*const struct:5:33'",
47 "tmp.zig:9:21: error: expected type '*S', found '*const struct:9:21'",
43 "tmp.zig:6:33: error: expected type '*[2][]const u8', found '*const struct:6:33'",
44 "tmp.zig:11:21: error: expected type '*S', found '*const struct:11:21'",
4845 });
4946
50 cases.add("@Type() union payload is undefined",
47 ctx.objErrStage1("@Type() union payload is undefined",
5148 \\const Foo = @Type(@import("std").builtin.TypeInfo{
5249 \\ .Struct = undefined,
5350 \\});
......@@ -56,7 +53,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
5653 "tmp.zig:1:50: error: use of undefined value here causes undefined behavior",
5754 });
5855
59 cases.add("wrong initializer for union payload of type 'type'",
56 ctx.objErrStage1("wrong initializer for union payload of type 'type'",
6057 \\const U = union(enum) {
6158 \\ A: type,
6259 \\};
......@@ -71,7 +68,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
7168 "tmp.zig:9:8: error: use of undefined value here causes undefined behavior",
7269 });
7370
74 cases.add("union with too small explicit signed tag type",
71 ctx.objErrStage1("union with too small explicit signed tag type",
7572 \\const U = union(enum(i2)) {
7673 \\ A: u8,
7774 \\ B: u8,
......@@ -86,7 +83,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
8683 "tmp.zig:1:22: note: type i2 cannot fit values in range 0...3",
8784 });
8885
89 cases.add("union with too small explicit unsigned tag type",
86 ctx.objErrStage1("union with too small explicit unsigned tag type",
9087 \\const U = union(enum(u2)) {
9188 \\ A: u8,
9289 \\ B: u8,
......@@ -102,56 +99,60 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
10299 "tmp.zig:1:22: note: type u2 cannot fit values in range 0...4",
103100 });
104101
105 cases.addCase(x: {
106 var tc = cases.create("callconv(.Interrupt) on unsupported platform",
102 {
103 const case = ctx.obj("callconv(.Interrupt) on unsupported platform", .{
104 .cpu_arch = .aarch64,
105 .os_tag = .linux,
106 .abi = .none,
107 });
108 case.backend = .stage1;
109 case.addError(
107110 \\export fn entry() callconv(.Interrupt) void {}
108111 , &[_][]const u8{
109112 "tmp.zig:1:28: error: callconv 'Interrupt' is only available on x86, x86_64, AVR, and MSP430, not aarch64",
110113 });
111 tc.target = std.zig.CrossTarget{
112 .cpu_arch = .aarch64,
114 }
115 {
116 var case = ctx.obj("callconv(.Signal) on unsupported platform", .{
117 .cpu_arch = .x86_64,
113118 .os_tag = .linux,
114119 .abi = .none,
115 };
116 break :x tc;
117 });
118
119 cases.addCase(x: {
120 var tc = cases.create("callconv(.Signal) on unsupported platform",
120 });
121 case.backend = .stage1;
122 case.addError(
121123 \\export fn entry() callconv(.Signal) void {}
122124 , &[_][]const u8{
123125 "tmp.zig:1:28: error: callconv 'Signal' is only available on AVR, not x86_64",
124126 });
125 tc.target = std.zig.CrossTarget{
127 }
128 {
129 const case = ctx.obj("callconv(.Stdcall, .Fastcall, .Thiscall) on unsupported platform", .{
126130 .cpu_arch = .x86_64,
127131 .os_tag = .linux,
128132 .abi = .none,
129 };
130 break :x tc;
131 });
132 cases.addCase(x: {
133 var tc = cases.create("callconv(.Stdcall, .Fastcall, .Thiscall) on unsupported platform",
133 });
134 case.backend = .stage1;
135 case.addError(
134136 \\const F1 = fn () callconv(.Stdcall) void;
135137 \\const F2 = fn () callconv(.Fastcall) void;
136138 \\const F3 = fn () callconv(.Thiscall) void;
137 \\export fn entry1() void { var a: F1 = undefined; }
138 \\export fn entry2() void { var a: F2 = undefined; }
139 \\export fn entry3() void { var a: F3 = undefined; }
139 \\export fn entry1() void { var a: F1 = undefined; _ = a; }
140 \\export fn entry2() void { var a: F2 = undefined; _ = a; }
141 \\export fn entry3() void { var a: F3 = undefined; _ = a; }
140142 , &[_][]const u8{
141143 "tmp.zig:1:27: error: callconv 'Stdcall' is only available on x86, not x86_64",
142144 "tmp.zig:2:27: error: callconv 'Fastcall' is only available on x86, not x86_64",
143145 "tmp.zig:3:27: error: callconv 'Thiscall' is only available on x86, not x86_64",
144146 });
145 tc.target = std.zig.CrossTarget{
147 }
148 {
149 const case = ctx.obj("callconv(.Stdcall, .Fastcall, .Thiscall) on unsupported platform", .{
146150 .cpu_arch = .x86_64,
147151 .os_tag = .linux,
148152 .abi = .none,
149 };
150 break :x tc;
151 });
152
153 cases.addCase(x: {
154 var tc = cases.create("callconv(.Stdcall, .Fastcall, .Thiscall) on unsupported platform",
153 });
154 case.backend = .stage1;
155 case.addError(
155156 \\export fn entry1() callconv(.Stdcall) void {}
156157 \\export fn entry2() callconv(.Fastcall) void {}
157158 \\export fn entry3() callconv(.Thiscall) void {}
......@@ -160,30 +161,28 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
160161 "tmp.zig:2:29: error: callconv 'Fastcall' is only available on x86, not x86_64",
161162 "tmp.zig:3:29: error: callconv 'Thiscall' is only available on x86, not x86_64",
162163 });
163 tc.target = std.zig.CrossTarget{
164 }
165 {
166 const case = ctx.obj("callconv(.Vectorcall) on unsupported platform", .{
164167 .cpu_arch = .x86_64,
165168 .os_tag = .linux,
166169 .abi = .none,
167 };
168 break :x tc;
169 });
170
171 cases.addCase(x: {
172 var tc = cases.create("callconv(.Vectorcall) on unsupported platform",
170 });
171 case.backend = .stage1;
172 case.addError(
173173 \\export fn entry() callconv(.Vectorcall) void {}
174174 , &[_][]const u8{
175175 "tmp.zig:1:28: error: callconv 'Vectorcall' is only available on x86 and AArch64, not x86_64",
176176 });
177 tc.target = std.zig.CrossTarget{
177 }
178 {
179 const case = ctx.obj("callconv(.APCS, .AAPCS, .AAPCSVFP) on unsupported platform", .{
178180 .cpu_arch = .x86_64,
179181 .os_tag = .linux,
180182 .abi = .none,
181 };
182 break :x tc;
183 });
184
185 cases.addCase(x: {
186 var tc = cases.create("callconv(.APCS, .AAPCS, .AAPCSVFP) on unsupported platform",
183 });
184 case.backend = .stage1;
185 case.addError(
187186 \\export fn entry1() callconv(.APCS) void {}
188187 \\export fn entry2() callconv(.AAPCS) void {}
189188 \\export fn entry3() callconv(.AAPCSVFP) void {}
......@@ -192,15 +191,9 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
192191 "tmp.zig:2:29: error: callconv 'AAPCS' is only available on ARM, not x86_64",
193192 "tmp.zig:3:29: error: callconv 'AAPCSVFP' is only available on ARM, not x86_64",
194193 });
195 tc.target = std.zig.CrossTarget{
196 .cpu_arch = .x86_64,
197 .os_tag = .linux,
198 .abi = .none,
199 };
200 break :x tc;
201 });
194 }
202195
203 cases.add("unreachable executed at comptime",
196 ctx.objErrStage1("unreachable executed at comptime",
204197 \\fn foo(comptime x: i32) i32 {
205198 \\ comptime {
206199 \\ if (x >= 0) return -x;
......@@ -215,7 +208,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
215208 "tmp.zig:8:12: note: called from here",
216209 });
217210
218 cases.add("@Type with TypeInfo.Int",
211 ctx.objErrStage1("@Type with TypeInfo.Int",
219212 \\const builtin = @import("std").builtin;
220213 \\export fn entry() void {
221214 \\ _ = @Type(builtin.TypeInfo.Int {
......@@ -227,7 +220,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
227220 "tmp.zig:3:36: error: expected type 'std.builtin.TypeInfo', found 'std.builtin.Int'",
228221 });
229222
230 cases.add("indexing a undefined slice at comptime",
223 ctx.objErrStage1("indexing a undefined slice at comptime",
231224 \\comptime {
232225 \\ var slice: []u8 = undefined;
233226 \\ slice[0] = 2;
......@@ -236,7 +229,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
236229 "tmp.zig:3:10: error: index 0 outside slice of size 0",
237230 });
238231
239 cases.add("array in c exported function",
232 ctx.objErrStage1("array in c exported function",
240233 \\export fn zig_array(x: [10]u8) void {
241234 \\try expect(std.mem.eql(u8, &x, "1234567890"));
242235 \\}
......@@ -249,7 +242,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
249242 "tmp.zig:5:30: error: return type '[10]u8' not allowed in function with calling convention 'C'",
250243 });
251244
252 cases.add("@Type for exhaustive enum with undefined tag type",
245 ctx.objErrStage1("@Type for exhaustive enum with undefined tag type",
253246 \\const TypeInfo = @import("std").builtin.TypeInfo;
254247 \\const Tag = @Type(.{
255248 \\ .Enum = .{
......@@ -267,19 +260,20 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
267260 "tmp.zig:2:20: error: use of undefined value here causes undefined behavior",
268261 });
269262
270 cases.add("extern struct with non-extern-compatible integer tag type",
263 ctx.objErrStage1("extern struct with non-extern-compatible integer tag type",
271264 \\pub const E = enum(u31) { A, B, C };
272265 \\pub const S = extern struct {
273266 \\ e: E,
274267 \\};
275268 \\export fn entry() void {
276269 \\ const s: S = undefined;
270 \\ _ = s;
277271 \\}
278272 , &[_][]const u8{
279273 "tmp.zig:3:5: error: extern structs cannot contain fields of type 'E'",
280274 });
281275
282 cases.add("@Type for exhaustive enum with non-integer tag type",
276 ctx.objErrStage1("@Type for exhaustive enum with non-integer tag type",
283277 \\const TypeInfo = @import("std").builtin.TypeInfo;
284278 \\const Tag = @Type(.{
285279 \\ .Enum = .{
......@@ -297,7 +291,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
297291 "tmp.zig:2:20: error: TypeInfo.Enum.tag_type must be an integer type, not 'bool'",
298292 });
299293
300 cases.add("extern struct with extern-compatible but inferred integer tag type",
294 ctx.objErrStage1("extern struct with extern-compatible but inferred integer tag type",
301295 \\pub const E = enum {
302296 \\@"0",@"1",@"2",@"3",@"4",@"5",@"6",@"7",@"8",@"9",@"10",@"11",@"12",
303297 \\@"13",@"14",@"15",@"16",@"17",@"18",@"19",@"20",@"21",@"22",@"23",
......@@ -333,12 +327,13 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
333327 \\export fn entry() void {
334328 \\ if (@typeInfo(E).Enum.tag_type != u8) @compileError("did not infer u8 tag type");
335329 \\ const s: S = undefined;
330 \\ _ = s;
336331 \\}
337332 , &[_][]const u8{
338333 "tmp.zig:31:5: error: extern structs cannot contain fields of type 'E'",
339334 });
340335
341 cases.add("@Type for tagged union with extra enum field",
336 ctx.objErrStage1("@Type for tagged union with extra enum field",
342337 \\const TypeInfo = @import("std").builtin.TypeInfo;
343338 \\const Tag = @Type(.{
344339 \\ .Enum = .{
......@@ -373,7 +368,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
373368 "tmp.zig:27:24: note: referenced here",
374369 });
375370
376 cases.add("field access of opaque type",
371 ctx.objErrStage1("field access of opaque type",
377372 \\const MyType = opaque {};
378373 \\
379374 \\export fn entry() bool {
......@@ -388,16 +383,17 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
388383 "tmp.zig:9:13: error: no member named 'blah' in opaque type 'MyType'",
389384 });
390385
391 cases.add("opaque type with field",
386 ctx.objErrStage1("opaque type with field",
392387 \\const Opaque = opaque { foo: i32 };
393388 \\export fn entry() void {
394389 \\ const foo: ?*Opaque = null;
390 \\ _ = foo;
395391 \\}
396392 , &[_][]const u8{
397393 "tmp.zig:1:25: error: opaque types cannot have fields",
398394 });
399395
400 cases.add("@Type(.Fn) with is_generic = true",
396 ctx.objErrStage1("@Type(.Fn) with is_generic = true",
401397 \\const Foo = @Type(.{
402398 \\ .Fn = .{
403399 \\ .calling_convention = .Unspecified,
......@@ -413,7 +409,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
413409 "tmp.zig:1:20: error: TypeInfo.Fn.is_generic must be false for @Type",
414410 });
415411
416 cases.add("@Type(.Fn) with is_var_args = true and non-C callconv",
412 ctx.objErrStage1("@Type(.Fn) with is_var_args = true and non-C callconv",
417413 \\const Foo = @Type(.{
418414 \\ .Fn = .{
419415 \\ .calling_convention = .Unspecified,
......@@ -429,7 +425,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
429425 "tmp.zig:1:20: error: varargs functions must have C calling convention",
430426 });
431427
432 cases.add("@Type(.Fn) with return_type = null",
428 ctx.objErrStage1("@Type(.Fn) with return_type = null",
433429 \\const Foo = @Type(.{
434430 \\ .Fn = .{
435431 \\ .calling_convention = .Unspecified,
......@@ -445,7 +441,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
445441 "tmp.zig:1:20: error: TypeInfo.Fn.return_type must be non-null for @Type",
446442 });
447443
448 cases.add("@Type for union with opaque field",
444 ctx.objErrStage1("@Type for union with opaque field",
449445 \\const TypeInfo = @import("std").builtin.TypeInfo;
450446 \\const Untagged = @Type(.{
451447 \\ .Union = .{
......@@ -465,23 +461,25 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
465461 "tmp.zig:13:17: note: referenced here",
466462 });
467463
468 cases.add("slice sentinel mismatch",
464 ctx.objErrStage1("slice sentinel mismatch",
469465 \\export fn entry() void {
470466 \\ const x = @import("std").meta.Vector(3, f32){ 25, 75, 5, 0 };
467 \\ _ = x;
471468 \\}
472469 , &[_][]const u8{
473470 "tmp.zig:2:62: error: index 3 outside vector of size 3",
474471 });
475472
476 cases.add("slice sentinel mismatch",
473 ctx.objErrStage1("slice sentinel mismatch",
477474 \\export fn entry() void {
478475 \\ const y: [:1]const u8 = &[_:2]u8{ 1, 2 };
476 \\ _ = y;
479477 \\}
480478 , &[_][]const u8{
481479 "tmp.zig:2:37: error: expected type '[:1]const u8', found '*const [2:2]u8'",
482480 });
483481
484 cases.add("@Type for union with zero fields",
482 ctx.objErrStage1("@Type for union with zero fields",
485483 \\const TypeInfo = @import("std").builtin.TypeInfo;
486484 \\const Untagged = @Type(.{
487485 \\ .Union = .{
......@@ -499,7 +497,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
499497 "tmp.zig:11:17: note: referenced here",
500498 });
501499
502 cases.add("@Type for exhaustive enum with zero fields",
500 ctx.objErrStage1("@Type for exhaustive enum with zero fields",
503501 \\const TypeInfo = @import("std").builtin.TypeInfo;
504502 \\const Tag = @Type(.{
505503 \\ .Enum = .{
......@@ -518,7 +516,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
518516 "tmp.zig:12:9: note: referenced here",
519517 });
520518
521 cases.add("@Type for tagged union with extra union field",
519 ctx.objErrStage1("@Type for tagged union with extra union field",
522520 \\const TypeInfo = @import("std").builtin.TypeInfo;
523521 \\const Tag = @Type(.{
524522 \\ .Enum = .{
......@@ -554,7 +552,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
554552 "tmp.zig:27:24: note: referenced here",
555553 });
556554
557 cases.add("@Type with undefined",
555 ctx.objErrStage1("@Type with undefined",
558556 \\comptime {
559557 \\ _ = @Type(.{ .Array = .{ .len = 0, .child = u8, .sentinel = undefined } });
560558 \\}
......@@ -573,7 +571,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
573571 "tmp.zig:5:16: error: use of undefined value here causes undefined behavior",
574572 });
575573
576 cases.add("struct with declarations unavailable for @Type",
574 ctx.objErrStage1("struct with declarations unavailable for @Type",
577575 \\export fn entry() void {
578576 \\ _ = @Type(@typeInfo(struct { const foo = 1; }));
579577 \\}
......@@ -581,7 +579,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
581579 "tmp.zig:2:15: error: TypeInfo.Struct.decls must be empty for @Type",
582580 });
583581
584 cases.add("enum with declarations unavailable for @Type",
582 ctx.objErrStage1("enum with declarations unavailable for @Type",
585583 \\export fn entry() void {
586584 \\ _ = @Type(@typeInfo(enum { foo, const bar = 1; }));
587585 \\}
......@@ -589,36 +587,44 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
589587 "tmp.zig:2:15: error: TypeInfo.Enum.decls must be empty for @Type",
590588 });
591589
592 cases.addTest("reject extern variables with initializers",
590 ctx.testErrStage1("reject extern variables with initializers",
593591 \\extern var foo: int = 2;
594592 , &[_][]const u8{
595 "tmp.zig:1:1: error: extern variables have no initializers",
593 "tmp.zig:1:23: error: extern variables have no initializers",
596594 });
597595
598 cases.addTest("duplicate/unused labels",
596 ctx.testErrStage1("duplicate/unused labels",
599597 \\comptime {
600598 \\ blk: { blk: while (false) {} }
599 \\}
600 \\comptime {
601601 \\ blk: while (false) { blk: for (@as([0]void, undefined)) |_| {} }
602 \\}
603 \\comptime {
602604 \\ blk: for (@as([0]void, undefined)) |_| { blk: {} }
603605 \\}
604606 \\comptime {
605607 \\ blk: {}
608 \\}
609 \\comptime {
606610 \\ blk: while(false) {}
611 \\}
612 \\comptime {
607613 \\ blk: for(@as([0]void, undefined)) |_| {}
608614 \\}
609615 , &[_][]const u8{
610 "tmp.zig:2:17: error: redeclaration of label 'blk'",
611 "tmp.zig:2:10: note: previous declaration is here",
612 "tmp.zig:3:31: error: redeclaration of label 'blk'",
613 "tmp.zig:3:10: note: previous declaration is here",
614 "tmp.zig:4:51: error: redeclaration of label 'blk'",
615 "tmp.zig:4:10: note: previous declaration is here",
616 "tmp.zig:7:10: error: unused block label",
617 "tmp.zig:8:10: error: unused while label",
618 "tmp.zig:9:10: error: unused for label",
616 "tmp.zig:2:12: error: redefinition of label 'blk'",
617 "tmp.zig:2:5: note: previous definition is here",
618 "tmp.zig:5:26: error: redefinition of label 'blk'",
619 "tmp.zig:5:5: note: previous definition is here",
620 "tmp.zig:8:46: error: redefinition of label 'blk'",
621 "tmp.zig:8:5: note: previous definition is here",
622 "tmp.zig:11:5: error: unused block label",
623 "tmp.zig:14:5: error: unused while loop label",
624 "tmp.zig:17:5: error: unused for loop label",
619625 });
620626
621 cases.addTest("@alignCast of zero sized types",
627 ctx.testErrStage1("@alignCast of zero sized types",
622628 \\export fn foo() void {
623629 \\ const a: *void = undefined;
624630 \\ _ = @alignCast(2, a);
......@@ -633,7 +639,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
633639 \\}
634640 \\export fn qux() void {
635641 \\ const a = struct {
636 \\ fn a(comptime b: u32) void {}
642 \\ fn a(comptime b: u32) void { _ = b; }
637643 \\ }.a;
638644 \\ _ = @alignCast(2, a);
639645 \\}
......@@ -644,7 +650,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
644650 "tmp.zig:17:23: error: cannot adjust alignment of zero sized type 'fn(u32) anytype'",
645651 });
646652
647 cases.addTest("invalid non-exhaustive enum to union",
653 ctx.testErrStage1("invalid non-exhaustive enum to union",
648654 \\const E = enum(u8) {
649655 \\ a,
650656 \\ b,
......@@ -657,17 +663,19 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
657663 \\export fn foo() void {
658664 \\ var e = @intToEnum(E, 15);
659665 \\ var u: U = e;
666 \\ _ = u;
660667 \\}
661668 \\export fn bar() void {
662669 \\ const e = @intToEnum(E, 15);
663670 \\ var u: U = e;
671 \\ _ = u;
664672 \\}
665673 , &[_][]const u8{
666674 "tmp.zig:12:16: error: runtime cast to union 'U' from non-exhustive enum",
667 "tmp.zig:16:16: error: no tag by value 15",
675 "tmp.zig:17:16: error: no tag by value 15",
668676 });
669677
670 cases.addTest("switching with exhaustive enum has '_' prong ",
678 ctx.testErrStage1("switching with exhaustive enum has '_' prong ",
671679 \\const E = enum{
672680 \\ a,
673681 \\ b,
......@@ -684,7 +692,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
684692 "tmp.zig:7:5: error: switch on exhaustive enum has `_` prong",
685693 });
686694
687 cases.addTest("invalid pointer with @Type",
695 ctx.testErrStage1("invalid pointer with @Type",
688696 \\export fn entry() void {
689697 \\ _ = @Type(.{ .Pointer = .{
690698 \\ .size = .One,
......@@ -700,7 +708,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
700708 "tmp.zig:2:16: error: sentinels are only allowed on slices and unknown-length pointers",
701709 });
702710
703 cases.addTest("helpful return type error message",
711 ctx.testErrStage1("helpful return type error message",
704712 \\export fn foo() u32 {
705713 \\ return error.Ohno;
706714 \\}
......@@ -728,7 +736,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
728736 "tmp.zig:14:5: note: cannot store an error in type 'u32'",
729737 });
730738
731 cases.addTest("int/float conversion to comptime_int/float",
739 ctx.testErrStage1("int/float conversion to comptime_int/float",
732740 \\export fn foo() void {
733741 \\ var a: f32 = 2;
734742 \\ _ = @floatToInt(comptime_int, a);
......@@ -744,16 +752,16 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
744752 "tmp.zig:7:9: note: referenced here",
745753 });
746754
747 cases.add("extern variable has no type",
755 ctx.objErrStage1("extern variable has no type",
748756 \\extern var foo;
749757 \\pub export fn entry() void {
750758 \\ foo;
751759 \\}
752760 , &[_][]const u8{
753 "tmp.zig:1:1: error: unable to infer variable type",
761 "tmp.zig:1:8: error: unable to infer variable type",
754762 });
755763
756 cases.add("@src outside function",
764 ctx.objErrStage1("@src outside function",
757765 \\comptime {
758766 \\ @src();
759767 \\}
......@@ -761,7 +769,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
761769 "tmp.zig:2:5: error: @src outside function",
762770 });
763771
764 cases.add("call assigned to constant",
772 ctx.objErrStage1("call assigned to constant",
765773 \\const Foo = struct {
766774 \\ x: i32,
767775 \\};
......@@ -784,15 +792,15 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
784792 "tmp.zig:16:14: error: cannot assign to constant",
785793 });
786794
787 cases.add("invalid pointer syntax",
795 ctx.objErrStage1("invalid pointer syntax",
788796 \\export fn foo() void {
789797 \\ var guid: *:0 const u8 = undefined;
790798 \\}
791799 , &[_][]const u8{
792 "tmp.zig:2:15: error: sentinels are only allowed on unknown-length pointers",
800 "tmp.zig:2:16: error: expected type expression, found ':'",
793801 });
794802
795 cases.add("declaration between fields",
803 ctx.objErrStage1("declaration between fields",
796804 \\const S = struct {
797805 \\ const foo = 2;
798806 \\ const bar = 2;
......@@ -810,16 +818,16 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
810818 "tmp.zig:6:5: error: declarations are not allowed between container fields",
811819 });
812820
813 cases.add("non-extern function with var args",
821 ctx.objErrStage1("non-extern function with var args",
814822 \\fn foo(args: ...) void {}
815823 \\export fn entry() void {
816824 \\ foo();
817825 \\}
818826 , &[_][]const u8{
819 "tmp.zig:1:1: error: non-extern function is variadic",
827 "tmp.zig:1:14: error: expected type expression, found '...'",
820828 });
821829
822 cases.addTest("invalid int casts",
830 ctx.testErrStage1("invalid int casts",
823831 \\export fn foo() void {
824832 \\ var a: u32 = 2;
825833 \\ _ = @intCast(comptime_int, a);
......@@ -847,7 +855,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
847855 "tmp.zig:15:9: note: referenced here",
848856 });
849857
850 cases.addTest("invalid float casts",
858 ctx.testErrStage1("invalid float casts",
851859 \\export fn foo() void {
852860 \\ var a: f32 = 2;
853861 \\ _ = @floatCast(comptime_float, a);
......@@ -875,7 +883,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
875883 "tmp.zig:15:9: note: referenced here",
876884 });
877885
878 cases.addTest("invalid assignments",
886 ctx.testErrStage1("invalid assignments",
879887 \\export fn entry1() void {
880888 \\ var a: []const u8 = "foo";
881889 \\ a[0..2] = "bar";
......@@ -893,7 +901,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
893901 "tmp.zig:10:7: error: invalid left-hand side to assignment",
894902 });
895903
896 cases.addTest("reassign to array parameter",
904 ctx.testErrStage1("reassign to array parameter",
897905 \\fn reassign(a: [3]f32) void {
898906 \\ a = [3]f32{4, 5, 6};
899907 \\}
......@@ -904,7 +912,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
904912 "tmp.zig:2:15: error: cannot assign to constant",
905913 });
906914
907 cases.addTest("reassign to slice parameter",
915 ctx.testErrStage1("reassign to slice parameter",
908916 \\pub fn reassign(s: []const u8) void {
909917 \\ s = s[0..];
910918 \\}
......@@ -915,7 +923,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
915923 "tmp.zig:2:10: error: cannot assign to constant",
916924 });
917925
918 cases.addTest("reassign to struct parameter",
926 ctx.testErrStage1("reassign to struct parameter",
919927 \\const S = struct {
920928 \\ x: u32,
921929 \\};
......@@ -929,7 +937,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
929937 "tmp.zig:5:10: error: cannot assign to constant",
930938 });
931939
932 cases.addTest("reference to const data",
940 ctx.testErrStage1("reference to const data",
933941 \\export fn foo() void {
934942 \\ var ptr = &[_]u8{0,0,0,0};
935943 \\ ptr[1] = 2;
......@@ -957,7 +965,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
957965 "tmp.zig:19:13: error: cannot assign to constant",
958966 });
959967
960 cases.addTest("cast between ?T where T is not a pointer",
968 ctx.testErrStage1("cast between ?T where T is not a pointer",
961969 \\pub const fnty1 = ?fn (i8) void;
962970 \\pub const fnty2 = ?fn (u64) void;
963971 \\export fn entry() void {
......@@ -970,7 +978,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
970978 "tmp.zig:6:9: note: optional type child 'fn(u64) void' cannot cast into optional type child 'fn(i8) void'",
971979 });
972980
973 cases.addTest("unused variable error on errdefer",
981 ctx.testErrStage1("unused variable error on errdefer",
974982 \\fn foo() !void {
975983 \\ errdefer |a| unreachable;
976984 \\ return error.A;
......@@ -982,18 +990,19 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
982990 "tmp.zig:2:15: error: unused variable: 'a'",
983991 });
984992
985 cases.addTest("comparison of non-tagged union and enum literal",
993 ctx.testErrStage1("comparison of non-tagged union and enum literal",
986994 \\export fn entry() void {
987995 \\ const U = union { A: u32, B: u64 };
988996 \\ var u = U{ .A = 42 };
989997 \\ var ok = u == .A;
998 \\ _ = ok;
990999 \\}
9911000 , &[_][]const u8{
9921001 "tmp.zig:4:16: error: comparison of union and enum literal is only valid for tagged union types",
9931002 "tmp.zig:2:15: note: type U is not a tagged union",
9941003 });
9951004
996 cases.addTest("shift on type with non-power-of-two size",
1005 ctx.testErrStage1("shift on type with non-power-of-two size",
9971006 \\export fn entry() void {
9981007 \\ const S = struct {
9991008 \\ fn a() void {
......@@ -1025,7 +1034,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
10251034 "tmp.zig:17:17: error: RHS of shift is too large for LHS type",
10261035 });
10271036
1028 cases.addTest("combination of nosuspend and async",
1037 ctx.testErrStage1("combination of nosuspend and async",
10291038 \\export fn entry() void {
10301039 \\ nosuspend {
10311040 \\ const bar = async foo();
......@@ -1035,10 +1044,11 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
10351044 \\}
10361045 \\fn foo() void {}
10371046 , &[_][]const u8{
1038 "tmp.zig:4:9: error: suspend in nosuspend scope",
1047 "tmp.zig:4:9: error: suspend inside nosuspend block",
1048 "tmp.zig:2:5: note: nosuspend block here",
10391049 });
10401050
1041 cases.add("atomicrmw with bool op not .Xchg",
1051 ctx.objErrStage1("atomicrmw with bool op not .Xchg",
10421052 \\export fn entry() void {
10431053 \\ var x = false;
10441054 \\ _ = @atomicRmw(bool, &x, .Add, true, .SeqCst);
......@@ -1047,7 +1057,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
10471057 "tmp.zig:3:30: error: @atomicRmw with bool only allowed with .Xchg",
10481058 });
10491059
1050 cases.addTest("@TypeOf with no arguments",
1060 ctx.testErrStage1("@TypeOf with no arguments",
10511061 \\export fn entry() void {
10521062 \\ _ = @TypeOf();
10531063 \\}
......@@ -1055,7 +1065,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
10551065 "tmp.zig:2:9: error: expected at least 1 argument, found 0",
10561066 });
10571067
1058 cases.addTest("@TypeOf with incompatible arguments",
1068 ctx.testErrStage1("@TypeOf with incompatible arguments",
10591069 \\export fn entry() void {
10601070 \\ var var_1: f32 = undefined;
10611071 \\ var var_2: u32 = undefined;
......@@ -1065,7 +1075,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
10651075 "tmp.zig:4:9: error: incompatible types: 'f32' and 'u32'",
10661076 });
10671077
1068 cases.addTest("type mismatch with tuple concatenation",
1078 ctx.testErrStage1("type mismatch with tuple concatenation",
10691079 \\export fn entry() void {
10701080 \\ var x = .{};
10711081 \\ x = x ++ .{ 1, 2, 3 };
......@@ -1074,7 +1084,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
10741084 "tmp.zig:3:11: error: expected type 'struct:2:14', found 'struct:3:11'",
10751085 });
10761086
1077 cases.addTest("@tagName on invalid value of non-exhaustive enum",
1087 ctx.testErrStage1("@tagName on invalid value of non-exhaustive enum",
10781088 \\test "enum" {
10791089 \\ const E = enum(u8) {A, B, _};
10801090 \\ _ = @tagName(@intToEnum(E, 5));
......@@ -1083,16 +1093,17 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
10831093 "tmp.zig:3:18: error: no tag by value 5",
10841094 });
10851095
1086 cases.addTest("@ptrToInt with pointer to zero-sized type",
1096 ctx.testErrStage1("@ptrToInt with pointer to zero-sized type",
10871097 \\export fn entry() void {
10881098 \\ var pointer: ?*u0 = null;
10891099 \\ var x = @ptrToInt(pointer);
1100 \\ _ = x;
10901101 \\}
10911102 , &[_][]const u8{
10921103 "tmp.zig:3:23: error: pointer to size 0 type has no address",
10931104 });
10941105
1095 cases.addTest("access invalid @typeInfo decl",
1106 ctx.testErrStage1("access invalid @typeInfo decl",
10961107 \\const A = B;
10971108 \\test "Crash" {
10981109 \\ _ = @typeInfo(@This()).Struct.decls[0];
......@@ -1101,7 +1112,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
11011112 "tmp.zig:1:11: error: use of undeclared identifier 'B'",
11021113 });
11031114
1104 cases.addTest("reject extern function definitions with body",
1115 ctx.testErrStage1("reject extern function definitions with body",
11051116 \\extern "c" fn definitelyNotInLibC(a: i32, b: i32) i32 {
11061117 \\ return a + b;
11071118 \\}
......@@ -1109,7 +1120,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
11091120 "tmp.zig:1:1: error: extern functions have no body",
11101121 });
11111122
1112 cases.addTest("duplicate field in anonymous struct literal",
1123 ctx.testErrStage1("duplicate field in anonymous struct literal",
11131124 \\export fn entry() void {
11141125 \\ const anon = .{
11151126 \\ .inner = .{
......@@ -1119,31 +1130,33 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
11191130 \\ .a = .{},
11201131 \\ },
11211132 \\ };
1133 \\ _ = anon;
11221134 \\}
11231135 , &[_][]const u8{
11241136 "tmp.zig:7:13: error: duplicate field",
11251137 "tmp.zig:4:13: note: other field here",
11261138 });
11271139
1128 cases.addTest("type mismatch in C prototype with varargs",
1140 ctx.testErrStage1("type mismatch in C prototype with varargs",
11291141 \\const fn_ty = ?fn ([*c]u8, ...) callconv(.C) void;
11301142 \\extern fn fn_decl(fmt: [*:0]u8, ...) void;
11311143 \\
11321144 \\export fn main() void {
11331145 \\ const x: fn_ty = fn_decl;
1146 \\ _ = x;
11341147 \\}
11351148 , &[_][]const u8{
11361149 "tmp.zig:5:22: error: expected type 'fn([*c]u8, ...) callconv(.C) void', found 'fn([*:0]u8, ...) callconv(.C) void'",
11371150 });
11381151
1139 cases.addTest("dependency loop in top-level decl with @TypeInfo when accessing the decls",
1152 ctx.testErrStage1("dependency loop in top-level decl with @TypeInfo when accessing the decls",
11401153 \\export const foo = @typeInfo(@This()).Struct.decls;
11411154 , &[_][]const u8{
11421155 "tmp.zig:1:20: error: dependency loop detected",
11431156 "tmp.zig:1:45: note: referenced here",
11441157 });
11451158
1146 cases.add("function call assigned to incorrect type",
1159 ctx.objErrStage1("function call assigned to incorrect type",
11471160 \\export fn entry() void {
11481161 \\ var arr: [4]f32 = undefined;
11491162 \\ arr = concat();
......@@ -1155,7 +1168,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
11551168 "tmp.zig:3:17: error: expected type '[4]f32', found '[16]f32'",
11561169 });
11571170
1158 cases.add("generic function call assigned to incorrect type",
1171 ctx.objErrStage1("generic function call assigned to incorrect type",
11591172 \\pub export fn entry() void {
11601173 \\ var res: []i32 = undefined;
11611174 \\ res = myAlloc(i32);
......@@ -1167,12 +1180,25 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
11671180 "tmp.zig:3:18: error: expected type '[]i32', found 'anyerror!i32",
11681181 });
11691182
1170 cases.addTest("non-exhaustive enums",
1183 ctx.testErrStage1("non-exhaustive enum marker assigned a value",
11711184 \\const A = enum {
11721185 \\ a,
11731186 \\ b,
11741187 \\ _ = 1,
11751188 \\};
1189 \\const B = enum {
1190 \\ a,
1191 \\ b,
1192 \\ _,
1193 \\};
1194 \\comptime { _ = A; _ = B; }
1195 , &[_][]const u8{
1196 "tmp.zig:4:9: error: '_' is used to mark an enum as non-exhaustive and cannot be assigned a value",
1197 "tmp.zig:6:11: error: non-exhaustive enum missing integer tag type",
1198 "tmp.zig:9:5: note: marked non-exhaustive here",
1199 });
1200
1201 ctx.testErrStage1("non-exhaustive enums",
11761202 \\const B = enum(u1) {
11771203 \\ a,
11781204 \\ _,
......@@ -1184,18 +1210,15 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
11841210 \\ _,
11851211 \\};
11861212 \\pub export fn entry() void {
1187 \\ _ = A;
11881213 \\ _ = B;
11891214 \\ _ = C;
11901215 \\}
11911216 , &[_][]const u8{
1192 "tmp.zig:4:5: error: value assigned to '_' field of non-exhaustive enum",
1193 "error: non-exhaustive enum must specify size",
1194 "error: non-exhaustive enum specifies every value",
1195 "error: '_' field of non-exhaustive enum must be last",
1217 "tmp.zig:3:5: error: '_' field of non-exhaustive enum must be last",
1218 "tmp.zig:6:11: error: non-exhaustive enum specifies every value",
11961219 });
11971220
1198 cases.addTest("switching with non-exhaustive enums",
1221 ctx.testErrStage1("switching with non-exhaustive enums",
11991222 \\const E = enum(u8) {
12001223 \\ a,
12011224 \\ b,
......@@ -1228,7 +1251,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
12281251 "tmp.zig:21:5: error: `_` prong not allowed when switching on tagged union",
12291252 });
12301253
1231 cases.add("switch expression - unreachable else prong (bool)",
1254 ctx.objErrStage1("switch expression - unreachable else prong (bool)",
12321255 \\fn foo(x: bool) void {
12331256 \\ switch (x) {
12341257 \\ true => {},
......@@ -1241,7 +1264,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
12411264 "tmp.zig:5:9: error: unreachable else prong, all cases already handled",
12421265 });
12431266
1244 cases.add("switch expression - unreachable else prong (u1)",
1267 ctx.objErrStage1("switch expression - unreachable else prong (u1)",
12451268 \\fn foo(x: u1) void {
12461269 \\ switch (x) {
12471270 \\ 0 => {},
......@@ -1254,7 +1277,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
12541277 "tmp.zig:5:9: error: unreachable else prong, all cases already handled",
12551278 });
12561279
1257 cases.add("switch expression - unreachable else prong (u2)",
1280 ctx.objErrStage1("switch expression - unreachable else prong (u2)",
12581281 \\fn foo(x: u2) void {
12591282 \\ switch (x) {
12601283 \\ 0 => {},
......@@ -1269,7 +1292,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
12691292 "tmp.zig:7:9: error: unreachable else prong, all cases already handled",
12701293 });
12711294
1272 cases.add("switch expression - unreachable else prong (range u8)",
1295 ctx.objErrStage1("switch expression - unreachable else prong (range u8)",
12731296 \\fn foo(x: u8) void {
12741297 \\ switch (x) {
12751298 \\ 0 => {},
......@@ -1285,7 +1308,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
12851308 "tmp.zig:8:9: error: unreachable else prong, all cases already handled",
12861309 });
12871310
1288 cases.add("switch expression - unreachable else prong (range i8)",
1311 ctx.objErrStage1("switch expression - unreachable else prong (range i8)",
12891312 \\fn foo(x: i8) void {
12901313 \\ switch (x) {
12911314 \\ -128...0 => {},
......@@ -1301,7 +1324,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
13011324 "tmp.zig:8:9: error: unreachable else prong, all cases already handled",
13021325 });
13031326
1304 cases.add("switch expression - unreachable else prong (enum)",
1327 ctx.objErrStage1("switch expression - unreachable else prong (enum)",
13051328 \\const TestEnum = enum{ T1, T2 };
13061329 \\
13071330 \\fn err(x: u8) TestEnum {
......@@ -1324,7 +1347,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
13241347 "tmp.zig:14:9: error: unreachable else prong, all cases already handled",
13251348 });
13261349
1327 cases.addTest("@export with empty name string",
1350 ctx.testErrStage1("@export with empty name string",
13281351 \\pub export fn entry() void { }
13291352 \\comptime {
13301353 \\ @export(entry, .{ .name = "" });
......@@ -1333,7 +1356,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
13331356 "tmp.zig:3:5: error: exported symbol name cannot be empty",
13341357 });
13351358
1336 cases.addTest("switch ranges endpoints are validated",
1359 ctx.testErrStage1("switch ranges endpoints are validated",
13371360 \\pub export fn entry() void {
13381361 \\ var x: i32 = 0;
13391362 \\ switch (x) {
......@@ -1347,16 +1370,16 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
13471370 "tmp.zig:5:9: error: range start value is greater than the end value",
13481371 });
13491372
1350 cases.addTest("errors in for loop bodies are propagated",
1373 ctx.testErrStage1("errors in for loop bodies are propagated",
13511374 \\pub export fn entry() void {
13521375 \\ var arr: [100]u8 = undefined;
13531376 \\ for (arr) |bits| _ = @popCount(bits);
13541377 \\}
13551378 , &[_][]const u8{
1356 "tmp.zig:3:26: error: expected 2 argument(s), found 1",
1379 "tmp.zig:3:26: error: expected 2 arguments, found 1",
13571380 });
13581381
1359 cases.addTest("@call rejects non comptime-known fn - always_inline",
1382 ctx.testErrStage1("@call rejects non comptime-known fn - always_inline",
13601383 \\pub export fn entry() void {
13611384 \\ var call_me: fn () void = undefined;
13621385 \\ @call(.{ .modifier = .always_inline }, call_me, .{});
......@@ -1365,7 +1388,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
13651388 "tmp.zig:3:5: error: the specified modifier requires a comptime-known function",
13661389 });
13671390
1368 cases.addTest("@call rejects non comptime-known fn - compile_time",
1391 ctx.testErrStage1("@call rejects non comptime-known fn - compile_time",
13691392 \\pub export fn entry() void {
13701393 \\ var call_me: fn () void = undefined;
13711394 \\ @call(.{ .modifier = .compile_time }, call_me, .{});
......@@ -1374,19 +1397,20 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
13741397 "tmp.zig:3:5: error: the specified modifier requires a comptime-known function",
13751398 });
13761399
1377 cases.addTest("error in struct initializer doesn't crash the compiler",
1400 ctx.testErrStage1("error in struct initializer doesn't crash the compiler",
13781401 \\pub export fn entry() void {
13791402 \\ const bitfield = struct {
13801403 \\ e: u8,
13811404 \\ e: u8,
13821405 \\ };
13831406 \\ var a = .{@sizeOf(bitfield)};
1407 \\ _ = a;
13841408 \\}
13851409 , &[_][]const u8{
13861410 "tmp.zig:4:9: error: duplicate struct field: 'e'",
13871411 });
13881412
1389 cases.addTest("repeated invalid field access to generic function returning type crashes compiler. #2655",
1413 ctx.testErrStage1("repeated invalid field access to generic function returning type crashes compiler. #2655",
13901414 \\pub fn A() type {
13911415 \\ return Q;
13921416 \\}
......@@ -1398,15 +1422,16 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
13981422 "tmp.zig:2:12: error: use of undeclared identifier 'Q'",
13991423 });
14001424
1401 cases.add("bitCast to enum type",
1425 ctx.objErrStage1("bitCast to enum type",
14021426 \\export fn entry() void {
14031427 \\ const y = @bitCast(enum(u32) { a, b }, @as(u32, 3));
1428 \\ _ = y;
14041429 \\}
14051430 , &[_][]const u8{
14061431 "tmp.zig:2:24: error: cannot cast a value of type 'y'",
14071432 });
14081433
1409 cases.add("comparing against undefined produces undefined value",
1434 ctx.objErrStage1("comparing against undefined produces undefined value",
14101435 \\export fn entry() void {
14111436 \\ if (2 == undefined) {}
14121437 \\}
......@@ -1414,17 +1439,18 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
14141439 "tmp.zig:2:11: error: use of undefined value here causes undefined behavior",
14151440 });
14161441
1417 cases.add("comptime ptrcast of zero-sized type",
1442 ctx.objErrStage1("comptime ptrcast of zero-sized type",
14181443 \\fn foo() void {
14191444 \\ const node: struct {} = undefined;
14201445 \\ const vla_ptr = @ptrCast([*]const u8, &node);
1446 \\ _ = vla_ptr;
14211447 \\}
14221448 \\comptime { foo(); }
14231449 , &[_][]const u8{
14241450 "tmp.zig:3:21: error: '*const struct:2:17' and '[*]const u8' do not have the same in-memory representation",
14251451 });
14261452
1427 cases.add("slice sentinel mismatch",
1453 ctx.objErrStage1("slice sentinel mismatch",
14281454 \\fn foo() [:0]u8 {
14291455 \\ var x: []u8 = undefined;
14301456 \\ return x;
......@@ -1435,7 +1461,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
14351461 "tmp.zig:3:12: note: destination pointer requires a terminating '0' sentinel",
14361462 });
14371463
1438 cases.add("cmpxchg with float",
1464 ctx.objErrStage1("cmpxchg with float",
14391465 \\export fn entry() void {
14401466 \\ var x: f32 = 0;
14411467 \\ _ = @cmpxchgWeak(f32, &x, 1, 2, .SeqCst, .SeqCst);
......@@ -1444,7 +1470,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
14441470 "tmp.zig:3:22: error: expected bool, integer, enum or pointer type, found 'f32'",
14451471 });
14461472
1447 cases.add("atomicrmw with float op not .Xchg, .Add or .Sub",
1473 ctx.objErrStage1("atomicrmw with float op not .Xchg, .Add or .Sub",
14481474 \\export fn entry() void {
14491475 \\ var x: f32 = 0;
14501476 \\ _ = @atomicRmw(f32, &x, .And, 2, .SeqCst);
......@@ -1453,15 +1479,16 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
14531479 "tmp.zig:3:29: error: @atomicRmw with float only allowed with .Xchg, .Add and .Sub",
14541480 });
14551481
1456 cases.add("intToPtr with misaligned address",
1482 ctx.objErrStage1("intToPtr with misaligned address",
14571483 \\pub fn main() void {
14581484 \\ var y = @intToPtr([*]align(4) u8, 5);
1485 \\ _ = y;
14591486 \\}
14601487 , &[_][]const u8{
14611488 "tmp.zig:2:13: error: pointer type '[*]align(4) u8' requires aligned address",
14621489 });
14631490
1464 cases.add("invalid float literal",
1491 ctx.objErrStage1("invalid float literal",
14651492 \\const std = @import("std");
14661493 \\
14671494 \\pub fn main() void {
......@@ -1473,170 +1500,190 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
14731500 "tmp.zig:5:29: error: invalid token: '.'",
14741501 });
14751502
1476 cases.add("invalid exponent in float literal - 1",
1503 ctx.objErrStage1("invalid exponent in float literal - 1",
14771504 \\fn main() void {
14781505 \\ var bad: f128 = 0x1.0p1ab1;
1506 \\ _ = bad;
14791507 \\}
14801508 , &[_][]const u8{
14811509 "tmp.zig:2:28: error: invalid character: 'a'",
14821510 });
14831511
1484 cases.add("invalid exponent in float literal - 2",
1512 ctx.objErrStage1("invalid exponent in float literal - 2",
14851513 \\fn main() void {
14861514 \\ var bad: f128 = 0x1.0p50F;
1515 \\ _ = bad;
14871516 \\}
14881517 , &[_][]const u8{
14891518 "tmp.zig:2:29: error: invalid character: 'F'",
14901519 });
14911520
1492 cases.add("invalid underscore placement in float literal - 1",
1521 ctx.objErrStage1("invalid underscore placement in float literal - 1",
14931522 \\fn main() void {
14941523 \\ var bad: f128 = 0._0;
1524 \\ _ = bad;
14951525 \\}
14961526 , &[_][]const u8{
14971527 "tmp.zig:2:23: error: invalid character: '_'",
14981528 });
14991529
1500 cases.add("invalid underscore placement in float literal - 2",
1530 ctx.objErrStage1("invalid underscore placement in float literal - 2",
15011531 \\fn main() void {
15021532 \\ var bad: f128 = 0_.0;
1533 \\ _ = bad;
15031534 \\}
15041535 , &[_][]const u8{
15051536 "tmp.zig:2:23: error: invalid character: '.'",
15061537 });
15071538
1508 cases.add("invalid underscore placement in float literal - 3",
1539 ctx.objErrStage1("invalid underscore placement in float literal - 3",
15091540 \\fn main() void {
15101541 \\ var bad: f128 = 0.0_;
1542 \\ _ = bad;
15111543 \\}
15121544 , &[_][]const u8{
15131545 "tmp.zig:2:25: error: invalid character: ';'",
15141546 });
15151547
1516 cases.add("invalid underscore placement in float literal - 4",
1548 ctx.objErrStage1("invalid underscore placement in float literal - 4",
15171549 \\fn main() void {
15181550 \\ var bad: f128 = 1.0e_1;
1551 \\ _ = bad;
15191552 \\}
15201553 , &[_][]const u8{
15211554 "tmp.zig:2:25: error: invalid character: '_'",
15221555 });
15231556
1524 cases.add("invalid underscore placement in float literal - 5",
1557 ctx.objErrStage1("invalid underscore placement in float literal - 5",
15251558 \\fn main() void {
15261559 \\ var bad: f128 = 1.0e+_1;
1560 \\ _ = bad;
15271561 \\}
15281562 , &[_][]const u8{
15291563 "tmp.zig:2:26: error: invalid character: '_'",
15301564 });
15311565
1532 cases.add("invalid underscore placement in float literal - 6",
1566 ctx.objErrStage1("invalid underscore placement in float literal - 6",
15331567 \\fn main() void {
15341568 \\ var bad: f128 = 1.0e-_1;
1569 \\ _ = bad;
15351570 \\}
15361571 , &[_][]const u8{
15371572 "tmp.zig:2:26: error: invalid character: '_'",
15381573 });
15391574
1540 cases.add("invalid underscore placement in float literal - 7",
1575 ctx.objErrStage1("invalid underscore placement in float literal - 7",
15411576 \\fn main() void {
15421577 \\ var bad: f128 = 1.0e-1_;
1578 \\ _ = bad;
15431579 \\}
15441580 , &[_][]const u8{
15451581 "tmp.zig:2:28: error: invalid character: ';'",
15461582 });
15471583
1548 cases.add("invalid underscore placement in float literal - 9",
1584 ctx.objErrStage1("invalid underscore placement in float literal - 9",
15491585 \\fn main() void {
15501586 \\ var bad: f128 = 1__0.0e-1;
1587 \\ _ = bad;
15511588 \\}
15521589 , &[_][]const u8{
15531590 "tmp.zig:2:23: error: invalid character: '_'",
15541591 });
15551592
1556 cases.add("invalid underscore placement in float literal - 10",
1593 ctx.objErrStage1("invalid underscore placement in float literal - 10",
15571594 \\fn main() void {
15581595 \\ var bad: f128 = 1.0__0e-1;
1596 \\ _ = bad;
15591597 \\}
15601598 , &[_][]const u8{
15611599 "tmp.zig:2:25: error: invalid character: '_'",
15621600 });
15631601
1564 cases.add("invalid underscore placement in float literal - 11",
1602 ctx.objErrStage1("invalid underscore placement in float literal - 11",
15651603 \\fn main() void {
15661604 \\ var bad: f128 = 1.0e-1__0;
1605 \\ _ = bad;
15671606 \\}
15681607 , &[_][]const u8{
15691608 "tmp.zig:2:28: error: invalid character: '_'",
15701609 });
15711610
1572 cases.add("invalid underscore placement in float literal - 12",
1611 ctx.objErrStage1("invalid underscore placement in float literal - 12",
15731612 \\fn main() void {
15741613 \\ var bad: f128 = 0_x0.0;
1614 \\ _ = bad;
15751615 \\}
15761616 , &[_][]const u8{
15771617 "tmp.zig:2:23: error: invalid character: 'x'",
15781618 });
15791619
1580 cases.add("invalid underscore placement in float literal - 13",
1620 ctx.objErrStage1("invalid underscore placement in float literal - 13",
15811621 \\fn main() void {
15821622 \\ var bad: f128 = 0x_0.0;
1623 \\ _ = bad;
15831624 \\}
15841625 , &[_][]const u8{
15851626 "tmp.zig:2:23: error: invalid character: '_'",
15861627 });
15871628
1588 cases.add("invalid underscore placement in float literal - 14",
1629 ctx.objErrStage1("invalid underscore placement in float literal - 14",
15891630 \\fn main() void {
15901631 \\ var bad: f128 = 0x0.0_p1;
1632 \\ _ = bad;
15911633 \\}
15921634 , &[_][]const u8{
15931635 "tmp.zig:2:27: error: invalid character: 'p'",
15941636 });
15951637
1596 cases.add("invalid underscore placement in int literal - 1",
1638 ctx.objErrStage1("invalid underscore placement in int literal - 1",
15971639 \\fn main() void {
15981640 \\ var bad: u128 = 0010_;
1641 \\ _ = bad;
15991642 \\}
16001643 , &[_][]const u8{
16011644 "tmp.zig:2:26: error: invalid character: ';'",
16021645 });
16031646
1604 cases.add("invalid underscore placement in int literal - 2",
1647 ctx.objErrStage1("invalid underscore placement in int literal - 2",
16051648 \\fn main() void {
16061649 \\ var bad: u128 = 0b0010_;
1650 \\ _ = bad;
16071651 \\}
16081652 , &[_][]const u8{
16091653 "tmp.zig:2:28: error: invalid character: ';'",
16101654 });
16111655
1612 cases.add("invalid underscore placement in int literal - 3",
1656 ctx.objErrStage1("invalid underscore placement in int literal - 3",
16131657 \\fn main() void {
16141658 \\ var bad: u128 = 0o0010_;
1659 \\ _ = bad;
16151660 \\}
16161661 , &[_][]const u8{
16171662 "tmp.zig:2:28: error: invalid character: ';'",
16181663 });
16191664
1620 cases.add("invalid underscore placement in int literal - 4",
1665 ctx.objErrStage1("invalid underscore placement in int literal - 4",
16211666 \\fn main() void {
16221667 \\ var bad: u128 = 0x0010_;
1668 \\ _ = bad;
16231669 \\}
16241670 , &[_][]const u8{
16251671 "tmp.zig:2:28: error: invalid character: ';'",
16261672 });
16271673
1628 cases.add("comptime struct field, no init value",
1674 ctx.objErrStage1("comptime struct field, no init value",
16291675 \\const Foo = struct {
16301676 \\ comptime b: i32,
16311677 \\};
16321678 \\export fn entry() void {
16331679 \\ var f: Foo = undefined;
1680 \\ _ = f;
16341681 \\}
16351682 , &[_][]const u8{
1636 "tmp.zig:2:5: error: comptime struct field missing initialization value",
1683 "tmp.zig:2:5: error: comptime field without default initialization value",
16371684 });
16381685
1639 cases.add("bad usage of @call",
1686 ctx.objErrStage1("bad usage of @call",
16401687 \\export fn entry1() void {
16411688 \\ @call(.{}, foo, {});
16421689 \\}
......@@ -1665,20 +1712,26 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
16651712 "tmp.zig:15:5: error: the specified modifier requires a comptime-known function",
16661713 });
16671714
1668 cases.add("exported async function",
1715 ctx.objErrStage1("exported async function",
16691716 \\export fn foo() callconv(.Async) void {}
16701717 , &[_][]const u8{
16711718 "tmp.zig:1:1: error: exported function cannot be async",
16721719 });
16731720
1674 cases.addExe("main missing name",
1721 ctx.exeErrStage1("main missing name",
16751722 \\pub fn (main) void {}
16761723 , &[_][]const u8{
16771724 "tmp.zig:1:5: error: missing function name",
16781725 });
16791726
1680 cases.addCase(x: {
1681 var tc = cases.create("call with new stack on unsupported target",
1727 {
1728 const case = ctx.obj("call with new stack on unsupported target", .{
1729 .cpu_arch = .wasm32,
1730 .os_tag = .wasi,
1731 .abi = .none,
1732 });
1733 case.backend = .stage1;
1734 case.addError(
16821735 \\var buf: [10]u8 align(16) = undefined;
16831736 \\export fn entry() void {
16841737 \\ @call(.{.stack = &buf}, foo, .{});
......@@ -1687,17 +1740,11 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
16871740 , &[_][]const u8{
16881741 "tmp.zig:3:5: error: target arch 'wasm32' does not support calling with a new stack",
16891742 });
1690 tc.target = std.zig.CrossTarget{
1691 .cpu_arch = .wasm32,
1692 .os_tag = .wasi,
1693 .abi = .none,
1694 };
1695 break :x tc;
1696 });
1743 }
16971744
16981745 // Note: One of the error messages here is backwards. It would be nice to fix, but that's not
16991746 // going to stop me from merging this branch which fixes a bunch of other stuff.
1700 cases.add("incompatible sentinels",
1747 ctx.objErrStage1("incompatible sentinels",
17011748 \\export fn entry1(ptr: [*:255]u8) [*:0]u8 {
17021749 \\ return ptr;
17031750 \\}
......@@ -1706,9 +1753,11 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
17061753 \\}
17071754 \\export fn entry3() void {
17081755 \\ var array: [2:0]u8 = [_:255]u8{1, 2};
1756 \\ _ = array;
17091757 \\}
17101758 \\export fn entry4() void {
17111759 \\ var array: [2:0]u8 = [_]u8{1, 2};
1760 \\ _ = array;
17121761 \\}
17131762 , &[_][]const u8{
17141763 "tmp.zig:2:12: error: expected type '[*:0]u8', found '[*:255]u8'",
......@@ -1718,11 +1767,11 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
17181767
17191768 "tmp.zig:8:35: error: expected type '[2:255]u8', found '[2:0]u8'",
17201769 "tmp.zig:8:35: note: destination array requires a terminating '255' sentinel, but source array has a terminating '0' sentinel",
1721 "tmp.zig:11:31: error: expected type '[2:0]u8', found '[2]u8'",
1722 "tmp.zig:11:31: note: destination array requires a terminating '0' sentinel",
1770 "tmp.zig:12:31: error: expected type '[2:0]u8', found '[2]u8'",
1771 "tmp.zig:12:31: note: destination array requires a terminating '0' sentinel",
17231772 });
17241773
1725 cases.add("empty switch on an integer",
1774 ctx.objErrStage1("empty switch on an integer",
17261775 \\export fn entry() void {
17271776 \\ var x: u32 = 0;
17281777 \\ switch(x) {}
......@@ -1731,7 +1780,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
17311780 "tmp.zig:3:5: error: switch must handle all possibilities",
17321781 });
17331782
1734 cases.add("incorrect return type",
1783 ctx.objErrStage1("incorrect return type",
17351784 \\ pub export fn entry() void{
17361785 \\ _ = foo();
17371786 \\ }
......@@ -1751,12 +1800,13 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
17511800 "tmp.zig:8:16: error: expected type 'A', found 'B'",
17521801 });
17531802
1754 cases.add("regression test #2980: base type u32 is not type checked properly when assigning a value within a struct",
1803 ctx.objErrStage1("regression test #2980: base type u32 is not type checked properly when assigning a value within a struct",
17551804 \\const Foo = struct {
17561805 \\ ptr: ?*usize,
17571806 \\ uval: u32,
17581807 \\};
17591808 \\fn get_uval(x: u32) !u32 {
1809 \\ _ = x;
17601810 \\ return error.NotFound;
17611811 \\}
17621812 \\export fn entry() void {
......@@ -1764,12 +1814,13 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
17641814 \\ .ptr = null,
17651815 \\ .uval = get_uval(42),
17661816 \\ };
1817 \\ _ = afoo;
17671818 \\}
17681819 , &[_][]const u8{
1769 "tmp.zig:11:25: error: expected type 'u32', found '@typeInfo(@typeInfo(@TypeOf(get_uval)).Fn.return_type.?).ErrorUnion.error_set!u32'",
1820 "tmp.zig:12:25: error: expected type 'u32', found '@typeInfo(@typeInfo(@TypeOf(get_uval)).Fn.return_type.?).ErrorUnion.error_set!u32'",
17701821 });
17711822
1772 cases.add("assigning to struct or union fields that are not optionals with a function that returns an optional",
1823 ctx.objErrStage1("assigning to struct or union fields that are not optionals with a function that returns an optional",
17731824 \\fn maybe(is: bool) ?u8 {
17741825 \\ if (is) return @as(u8, 10) else return null;
17751826 \\}
......@@ -1782,12 +1833,14 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
17821833 \\export fn entry() void {
17831834 \\ var u = U{ .Ye = maybe(false) };
17841835 \\ var s = S{ .num = maybe(false) };
1836 \\ _ = u;
1837 \\ _ = s;
17851838 \\}
17861839 , &[_][]const u8{
17871840 "tmp.zig:11:27: error: expected type 'u8', found '?u8'",
17881841 });
17891842
1790 cases.add("missing result type for phi node",
1843 ctx.objErrStage1("missing result type for phi node",
17911844 \\fn foo() !void {
17921845 \\ return anyerror.Foo;
17931846 \\}
......@@ -1798,7 +1851,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
17981851 "tmp.zig:5:17: error: integer value 0 cannot be coerced to type 'void'",
17991852 });
18001853
1801 cases.add("atomicrmw with enum op not .Xchg",
1854 ctx.objErrStage1("atomicrmw with enum op not .Xchg",
18021855 \\export fn entry() void {
18031856 \\ const E = enum(u8) {
18041857 \\ a,
......@@ -1813,7 +1866,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
18131866 "tmp.zig:9:27: error: @atomicRmw with enum only allowed with .Xchg",
18141867 });
18151868
1816 cases.add("disallow coercion from non-null-terminated pointer to null-terminated pointer",
1869 ctx.objErrStage1("disallow coercion from non-null-terminated pointer to null-terminated pointer",
18171870 \\extern fn puts(s: [*:0]const u8) c_int;
18181871 \\pub fn main() void {
18191872 \\ const no_zero_array = [_]u8{'h', 'e', 'l', 'l', 'o'};
......@@ -1824,7 +1877,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
18241877 "tmp.zig:5:14: error: expected type '[*:0]const u8', found '[*]const u8'",
18251878 });
18261879
1827 cases.add("atomic orderings of atomicStore Acquire or AcqRel",
1880 ctx.objErrStage1("atomic orderings of atomicStore Acquire or AcqRel",
18281881 \\export fn entry() void {
18291882 \\ var x: u32 = 0;
18301883 \\ @atomicStore(u32, &x, 1, .Acquire);
......@@ -1833,7 +1886,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
18331886 "tmp.zig:3:30: error: @atomicStore atomic ordering must not be Acquire or AcqRel",
18341887 });
18351888
1836 cases.add("missing const in slice with nested array type",
1889 ctx.objErrStage1("missing const in slice with nested array type",
18371890 \\const Geo3DTex2D = struct { vertices: [][2]f32 };
18381891 \\pub fn getGeo3DTex2D() Geo3DTex2D {
18391892 \\ return Geo3DTex2D{
......@@ -1844,12 +1897,13 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
18441897 \\}
18451898 \\export fn entry() void {
18461899 \\ var geo_data = getGeo3DTex2D();
1900 \\ _ = geo_data;
18471901 \\}
18481902 , &[_][]const u8{
18491903 "tmp.zig:4:30: error: array literal requires address-of operator to coerce to slice type '[][2]f32'",
18501904 });
18511905
1852 cases.add("slicing of global undefined pointer",
1906 ctx.objErrStage1("slicing of global undefined pointer",
18531907 \\var buf: *[1]u8 = undefined;
18541908 \\export fn entry() void {
18551909 \\ _ = buf[0..1];
......@@ -1858,18 +1912,17 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
18581912 "tmp.zig:3:12: error: non-zero length slice of undefined pointer",
18591913 });
18601914
1861 cases.add("using invalid types in function call raises an error",
1915 ctx.objErrStage1("using invalid types in function call raises an error",
18621916 \\const MenuEffect = enum {};
1863 \\fn func(effect: MenuEffect) void {}
1917 \\fn func(effect: MenuEffect) void { _ = effect; }
18641918 \\export fn entry() void {
18651919 \\ func(MenuEffect.ThisDoesNotExist);
18661920 \\}
18671921 , &[_][]const u8{
1868 "tmp.zig:1:20: error: enums must have 1 or more fields",
1869 "tmp.zig:4:20: note: referenced here",
1922 "tmp.zig:1:20: error: enum declarations must have at least one tag",
18701923 });
18711924
1872 cases.add("store vector pointer with unknown runtime index",
1925 ctx.objErrStage1("store vector pointer with unknown runtime index",
18731926 \\export fn entry() void {
18741927 \\ var v: @import("std").meta.Vector(4, i32) = [_]i32{ 1, 5, 3, undefined };
18751928 \\
......@@ -1884,22 +1937,23 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
18841937 "tmp.zig:9:8: error: unable to determine vector element index of type '*align(16:0:4:?) i32",
18851938 });
18861939
1887 cases.add("load vector pointer with unknown runtime index",
1940 ctx.objErrStage1("load vector pointer with unknown runtime index",
18881941 \\export fn entry() void {
18891942 \\ var v: @import("std").meta.Vector(4, i32) = [_]i32{ 1, 5, 3, undefined };
18901943 \\
18911944 \\ var i: u32 = 0;
18921945 \\ var x = loadv(&v[i]);
1946 \\ _ = x;
18931947 \\}
18941948 \\
18951949 \\fn loadv(ptr: anytype) i32 {
18961950 \\ return ptr.*;
18971951 \\}
18981952 , &[_][]const u8{
1899 "tmp.zig:9:12: error: unable to determine vector element index of type '*align(16:0:4:?) i32",
1953 "tmp.zig:10:12: error: unable to determine vector element index of type '*align(16:0:4:?) i32",
19001954 });
19011955
1902 cases.add("using an unknown len ptr type instead of array",
1956 ctx.objErrStage1("using an unknown len ptr type instead of array",
19031957 \\const resolutions = [*][*]const u8{
19041958 \\ "[320 240 ]",
19051959 \\ null,
......@@ -1911,7 +1965,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
19111965 "tmp.zig:1:21: error: expected array type or [_], found '[*][*]const u8'",
19121966 });
19131967
1914 cases.add("comparison with error union and error value",
1968 ctx.objErrStage1("comparison with error union and error value",
19151969 \\export fn entry() void {
19161970 \\ var number_or_error: anyerror!i32 = error.SomethingAwful;
19171971 \\ _ = number_or_error == error.SomethingAwful;
......@@ -1920,7 +1974,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
19201974 "tmp.zig:3:25: error: operator not allowed for type 'anyerror!i32'",
19211975 });
19221976
1923 cases.add("switch with overlapping case ranges",
1977 ctx.objErrStage1("switch with overlapping case ranges",
19241978 \\export fn entry() void {
19251979 \\ var q: u8 = 0;
19261980 \\ switch (q) {
......@@ -1932,28 +1986,29 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
19321986 "tmp.zig:5:9: error: duplicate switch value",
19331987 });
19341988
1935 cases.add("invalid optional type in extern struct",
1989 ctx.objErrStage1("invalid optional type in extern struct",
19361990 \\const stroo = extern struct {
19371991 \\ moo: ?[*c]u8,
19381992 \\};
1939 \\export fn testf(fluff: *stroo) void {}
1993 \\export fn testf(fluff: *stroo) void { _ = fluff; }
19401994 , &[_][]const u8{
19411995 "tmp.zig:2:5: error: extern structs cannot contain fields of type '?[*c]u8'",
19421996 });
19431997
1944 cases.add("attempt to negate a non-integer, non-float or non-vector type",
1998 ctx.objErrStage1("attempt to negate a non-integer, non-float or non-vector type",
19451999 \\fn foo() anyerror!u32 {
19462000 \\ return 1;
19472001 \\}
19482002 \\
19492003 \\export fn entry() void {
19502004 \\ const x = -foo();
2005 \\ _ = x;
19512006 \\}
19522007 , &[_][]const u8{
19532008 "tmp.zig:6:15: error: negation of type 'anyerror!u32'",
19542009 });
19552010
1956 cases.add("attempt to create 17 bit float type",
2011 ctx.objErrStage1("attempt to create 17 bit float type",
19572012 \\const builtin = @import("std").builtin;
19582013 \\comptime {
19592014 \\ _ = @Type(builtin.TypeInfo { .Float = builtin.TypeInfo.Float { .bits = 17 } });
......@@ -1962,7 +2017,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
19622017 "tmp.zig:3:32: error: 17-bit float unsupported",
19632018 });
19642019
1965 cases.add("wrong type for @Type",
2020 ctx.objErrStage1("wrong type for @Type",
19662021 \\export fn entry() void {
19672022 \\ _ = @Type(0);
19682023 \\}
......@@ -1970,7 +2025,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
19702025 "tmp.zig:2:15: error: expected type 'std.builtin.TypeInfo', found 'comptime_int'",
19712026 });
19722027
1973 cases.add("@Type with non-constant expression",
2028 ctx.objErrStage1("@Type with non-constant expression",
19742029 \\const builtin = @import("std").builtin;
19752030 \\var globalTypeInfo : builtin.TypeInfo = undefined;
19762031 \\export fn entry() void {
......@@ -1980,7 +2035,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
19802035 "tmp.zig:4:15: error: unable to evaluate constant expression",
19812036 });
19822037
1983 cases.add("wrong type for argument tuple to @asyncCall",
2038 ctx.objErrStage1("wrong type for argument tuple to @asyncCall",
19842039 \\export fn entry1() void {
19852040 \\ var frame: @Frame(foo) = undefined;
19862041 \\ @asyncCall(&frame, {}, foo, {});
......@@ -1993,7 +2048,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
19932048 "tmp.zig:3:33: error: expected tuple or struct, found 'void'",
19942049 });
19952050
1996 cases.add("wrong type for result ptr to @asyncCall",
2051 ctx.objErrStage1("wrong type for result ptr to @asyncCall",
19972052 \\export fn entry() void {
19982053 \\ _ = async amain();
19992054 \\}
......@@ -2008,25 +2063,27 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
20082063 "tmp.zig:6:37: error: expected type '*i32', found 'bool'",
20092064 });
20102065
2011 cases.add("shift amount has to be an integer type",
2066 ctx.objErrStage1("shift amount has to be an integer type",
20122067 \\export fn entry() void {
20132068 \\ const x = 1 << &@as(u8, 10);
2069 \\ _ = x;
20142070 \\}
20152071 , &[_][]const u8{
20162072 "tmp.zig:2:21: error: shift amount has to be an integer type, but found '*const u8'",
20172073 "tmp.zig:2:17: note: referenced here",
20182074 });
20192075
2020 cases.add("bit shifting only works on integer types",
2076 ctx.objErrStage1("bit shifting only works on integer types",
20212077 \\export fn entry() void {
20222078 \\ const x = &@as(u8, 1) << 10;
2079 \\ _ = x;
20232080 \\}
20242081 , &[_][]const u8{
20252082 "tmp.zig:2:16: error: bit shifting operation expected integer type, found '*const u8'",
20262083 "tmp.zig:2:27: note: referenced here",
20272084 });
20282085
2029 cases.add("struct depends on itself via optional field",
2086 ctx.objErrStage1("struct depends on itself via optional field",
20302087 \\const LhsExpr = struct {
20312088 \\ rhsExpr: ?AstObject,
20322089 \\};
......@@ -2036,6 +2093,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
20362093 \\export fn entry() void {
20372094 \\ const lhsExpr = LhsExpr{ .rhsExpr = null };
20382095 \\ const obj = AstObject{ .lhsExpr = lhsExpr };
2096 \\ _ = obj;
20392097 \\}
20402098 , &[_][]const u8{
20412099 "tmp.zig:1:17: error: struct 'LhsExpr' depends on itself",
......@@ -2043,51 +2101,55 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
20432101 "tmp.zig:2:5: note: while checking this field",
20442102 });
20452103
2046 cases.add("alignment of enum field specified",
2104 ctx.objErrStage1("alignment of enum field specified",
20472105 \\const Number = enum {
20482106 \\ a,
20492107 \\ b align(i32),
20502108 \\};
20512109 \\export fn entry1() void {
20522110 \\ var x: Number = undefined;
2111 \\ _ = x;
20532112 \\}
20542113 , &[_][]const u8{
20552114 "tmp.zig:3:13: error: structs and unions, not enums, support field alignment",
20562115 "tmp.zig:1:16: note: consider 'union(enum)' here",
20572116 });
20582117
2059 cases.add("bad alignment type",
2118 ctx.objErrStage1("bad alignment type",
20602119 \\export fn entry1() void {
20612120 \\ var x: []align(true) i32 = undefined;
2121 \\ _ = x;
20622122 \\}
20632123 \\export fn entry2() void {
20642124 \\ var x: *align(@as(f64, 12.34)) i32 = undefined;
2125 \\ _ = x;
20652126 \\}
20662127 , &[_][]const u8{
20672128 "tmp.zig:2:20: error: expected type 'u29', found 'bool'",
2068 "tmp.zig:5:19: error: fractional component prevents float value 12.340000 from being casted to type 'u29'",
2129 "tmp.zig:6:19: error: fractional component prevents float value 12.340000 from being casted to type 'u29'",
20692130 });
20702131
2071 cases.addCase(x: {
2072 var tc = cases.create("variable in inline assembly template cannot be found",
2132 {
2133 const case = ctx.obj("variable in inline assembly template cannot be found", .{
2134 .cpu_arch = .x86_64,
2135 .os_tag = .linux,
2136 .abi = .gnu,
2137 });
2138 case.backend = .stage1;
2139 case.addError(
20732140 \\export fn entry() void {
20742141 \\ var sp = asm volatile (
20752142 \\ "mov %[foo], sp"
20762143 \\ : [bar] "=r" (-> usize)
20772144 \\ );
2145 \\ _ = sp;
20782146 \\}
20792147 , &[_][]const u8{
20802148 "tmp.zig:2:14: error: could not find 'foo' in the inputs or outputs",
20812149 });
2082 tc.target = std.zig.CrossTarget{
2083 .cpu_arch = .x86_64,
2084 .os_tag = .linux,
2085 .abi = .gnu,
2086 };
2087 break :x tc;
2088 });
2150 }
20892151
2090 cases.add("indirect recursion of async functions detected",
2152 ctx.objErrStage1("indirect recursion of async functions detected",
20912153 \\var frame: ?anyframe = null;
20922154 \\
20932155 \\export fn a() void {
......@@ -2122,7 +2184,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
21222184 "tmp.zig:26:25: note: when analyzing type '@Frame(rangeSumIndirect)' here",
21232185 });
21242186
2125 cases.add("non-async function pointer eventually is inferred to become async",
2187 ctx.objErrStage1("non-async function pointer eventually is inferred to become async",
21262188 \\export fn a() void {
21272189 \\ var non_async_fn: fn () void = undefined;
21282190 \\ non_async_fn = func;
......@@ -2136,20 +2198,26 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
21362198 "tmp.zig:6:5: note: suspends here",
21372199 });
21382200
2139 cases.add("bad alignment in @asyncCall",
2140 \\export fn entry() void {
2141 \\ var ptr: fn () callconv(.Async) void = func;
2142 \\ var bytes: [64]u8 = undefined;
2143 \\ _ = @asyncCall(&bytes, {}, ptr, .{});
2144 \\}
2145 \\fn func() callconv(.Async) void {}
2146 , &[_][]const u8{
2147 // Split the check in two as the alignment value is target dependent.
2148 "tmp.zig:4:21: error: expected type '[]align(",
2149 ") u8', found '*[64]u8'",
2150 });
2201 {
2202 const case = ctx.obj("bad alignment in @asyncCall", .{
2203 .cpu_arch = .aarch64,
2204 .os_tag = .linux,
2205 .abi = .none,
2206 });
2207 case.backend = .stage1;
2208 case.addError(
2209 \\export fn entry() void {
2210 \\ var ptr: fn () callconv(.Async) void = func;
2211 \\ var bytes: [64]u8 = undefined;
2212 \\ _ = @asyncCall(&bytes, {}, ptr, .{});
2213 \\}
2214 \\fn func() callconv(.Async) void {}
2215 , &[_][]const u8{
2216 "tmp.zig:4:21: error: expected type '[]align(8) u8', found '*[64]u8'",
2217 });
2218 }
21512219
2152 cases.add("atomic orderings of fence Acquire or stricter",
2220 ctx.objErrStage1("atomic orderings of fence Acquire or stricter",
21532221 \\export fn entry() void {
21542222 \\ @fence(.Monotonic);
21552223 \\}
......@@ -2157,20 +2225,22 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
21572225 "tmp.zig:2:12: error: atomic ordering must be Acquire or stricter",
21582226 });
21592227
2160 cases.add("bad alignment in implicit cast from array pointer to slice",
2228 ctx.objErrStage1("bad alignment in implicit cast from array pointer to slice",
21612229 \\export fn a() void {
21622230 \\ var x: [10]u8 = undefined;
21632231 \\ var y: []align(16) u8 = &x;
2232 \\ _ = y;
21642233 \\}
21652234 , &[_][]const u8{
21662235 "tmp.zig:3:30: error: expected type '[]align(16) u8', found '*[10]u8'",
21672236 });
21682237
2169 cases.add("result location incompatibility mismatching handle_is_ptr (generic call)",
2238 ctx.objErrStage1("result location incompatibility mismatching handle_is_ptr (generic call)",
21702239 \\export fn entry() void {
21712240 \\ var damn = Container{
21722241 \\ .not_optional = getOptional(i32),
21732242 \\ };
2243 \\ _ = damn;
21742244 \\}
21752245 \\pub fn getOptional(comptime T: type) ?T {
21762246 \\ return 0;
......@@ -2182,11 +2252,12 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
21822252 "tmp.zig:3:36: error: expected type 'i32', found '?i32'",
21832253 });
21842254
2185 cases.add("result location incompatibility mismatching handle_is_ptr",
2255 ctx.objErrStage1("result location incompatibility mismatching handle_is_ptr",
21862256 \\export fn entry() void {
21872257 \\ var damn = Container{
21882258 \\ .not_optional = getOptional(),
21892259 \\ };
2260 \\ _ = damn;
21902261 \\}
21912262 \\pub fn getOptional() ?i32 {
21922263 \\ return 0;
......@@ -2198,7 +2269,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
21982269 "tmp.zig:3:36: error: expected type 'i32', found '?i32'",
21992270 });
22002271
2201 cases.add("const frame cast to anyframe",
2272 ctx.objErrStage1("const frame cast to anyframe",
22022273 \\export fn a() void {
22032274 \\ const f = async func();
22042275 \\ resume f;
......@@ -2206,6 +2277,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
22062277 \\export fn b() void {
22072278 \\ const f = async func();
22082279 \\ var x: anyframe = &f;
2280 \\ _ = x;
22092281 \\}
22102282 \\fn func() void {
22112283 \\ suspend {}
......@@ -2215,27 +2287,30 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
22152287 "tmp.zig:7:24: error: expected type 'anyframe', found '*const @Frame(func)'",
22162288 });
22172289
2218 cases.add("prevent bad implicit casting of anyframe types",
2290 ctx.objErrStage1("prevent bad implicit casting of anyframe types",
22192291 \\export fn a() void {
22202292 \\ var x: anyframe = undefined;
22212293 \\ var y: anyframe->i32 = x;
2294 \\ _ = y;
22222295 \\}
22232296 \\export fn b() void {
22242297 \\ var x: i32 = undefined;
22252298 \\ var y: anyframe->i32 = x;
2299 \\ _ = y;
22262300 \\}
22272301 \\export fn c() void {
22282302 \\ var x: @Frame(func) = undefined;
22292303 \\ var y: anyframe->i32 = &x;
2304 \\ _ = y;
22302305 \\}
22312306 \\fn func() void {}
22322307 , &[_][]const u8{
22332308 "tmp.zig:3:28: error: expected type 'anyframe->i32', found 'anyframe'",
2234 "tmp.zig:7:28: error: expected type 'anyframe->i32', found 'i32'",
2235 "tmp.zig:11:29: error: expected type 'anyframe->i32', found '*@Frame(func)'",
2309 "tmp.zig:8:28: error: expected type 'anyframe->i32', found 'i32'",
2310 "tmp.zig:13:29: error: expected type 'anyframe->i32', found '*@Frame(func)'",
22362311 });
22372312
2238 cases.add("wrong frame type used for async call",
2313 ctx.objErrStage1("wrong frame type used for async call",
22392314 \\export fn entry() void {
22402315 \\ var frame: @Frame(foo) = undefined;
22412316 \\ frame = async bar();
......@@ -2250,18 +2325,20 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
22502325 "tmp.zig:3:13: error: expected type '*@Frame(bar)', found '*@Frame(foo)'",
22512326 });
22522327
2253 cases.add("@Frame() of generic function",
2328 ctx.objErrStage1("@Frame() of generic function",
22542329 \\export fn entry() void {
22552330 \\ var frame: @Frame(func) = undefined;
2331 \\ _ = frame;
22562332 \\}
22572333 \\fn func(comptime T: type) void {
22582334 \\ var x: T = undefined;
2335 \\ _ = x;
22592336 \\}
22602337 , &[_][]const u8{
22612338 "tmp.zig:2:16: error: @Frame() of generic function",
22622339 });
22632340
2264 cases.add("@frame() causes function to be async",
2341 ctx.objErrStage1("@frame() causes function to be async",
22652342 \\export fn entry() void {
22662343 \\ func();
22672344 \\}
......@@ -2273,10 +2350,11 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
22732350 "tmp.zig:5:9: note: @frame() causes function to be async",
22742351 });
22752352
2276 cases.add("invalid suspend in exported function",
2353 ctx.objErrStage1("invalid suspend in exported function",
22772354 \\export fn entry() void {
22782355 \\ var frame = async func();
22792356 \\ var result = await frame;
2357 \\ _ = result;
22802358 \\}
22812359 \\fn func() void {
22822360 \\ suspend {}
......@@ -2286,7 +2364,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
22862364 "tmp.zig:3:18: note: await here is a suspend point",
22872365 });
22882366
2289 cases.add("async function indirectly depends on its own frame",
2367 ctx.objErrStage1("async function indirectly depends on its own frame",
22902368 \\export fn entry() void {
22912369 \\ _ = async amain();
22922370 \\}
......@@ -2295,6 +2373,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
22952373 \\}
22962374 \\fn other() void {
22972375 \\ var x: [@sizeOf(@Frame(amain))]u8 = undefined;
2376 \\ _ = x;
22982377 \\}
22992378 , &[_][]const u8{
23002379 "tmp.zig:4:1: error: unable to determine async function frame of 'amain'",
......@@ -2302,19 +2381,20 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
23022381 "tmp.zig:8:13: note: referenced here",
23032382 });
23042383
2305 cases.add("async function depends on its own frame",
2384 ctx.objErrStage1("async function depends on its own frame",
23062385 \\export fn entry() void {
23072386 \\ _ = async amain();
23082387 \\}
23092388 \\fn amain() callconv(.Async) void {
23102389 \\ var x: [@sizeOf(@Frame(amain))]u8 = undefined;
2390 \\ _ = x;
23112391 \\}
23122392 , &[_][]const u8{
23132393 "tmp.zig:4:1: error: cannot resolve '@Frame(amain)': function not fully analyzed yet",
23142394 "tmp.zig:5:13: note: referenced here",
23152395 });
23162396
2317 cases.add("non async function pointer passed to @asyncCall",
2397 ctx.objErrStage1("non async function pointer passed to @asyncCall",
23182398 \\export fn entry() void {
23192399 \\ var ptr = afunc;
23202400 \\ var bytes: [100]u8 align(16) = undefined;
......@@ -2325,7 +2405,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
23252405 "tmp.zig:4:32: error: expected async function, found 'fn() void'",
23262406 });
23272407
2328 cases.add("runtime-known async function called",
2408 ctx.objErrStage1("runtime-known async function called",
23292409 \\export fn entry() void {
23302410 \\ _ = async amain();
23312411 \\}
......@@ -2338,7 +2418,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
23382418 "tmp.zig:6:12: error: function is not comptime-known; @asyncCall required",
23392419 });
23402420
2341 cases.add("runtime-known function called with async keyword",
2421 ctx.objErrStage1("runtime-known function called with async keyword",
23422422 \\export fn entry() void {
23432423 \\ var ptr = afunc;
23442424 \\ _ = async ptr();
......@@ -2349,7 +2429,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
23492429 "tmp.zig:3:15: error: function is not comptime-known; @asyncCall required",
23502430 });
23512431
2352 cases.add("function with ccc indirectly calling async function",
2432 ctx.objErrStage1("function with ccc indirectly calling async function",
23532433 \\export fn entry() void {
23542434 \\ foo();
23552435 \\}
......@@ -2366,7 +2446,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
23662446 "tmp.zig:8:5: note: suspends here",
23672447 });
23682448
2369 cases.add("capture group on switch prong with incompatible payload types",
2449 ctx.objErrStage1("capture group on switch prong with incompatible payload types",
23702450 \\const Union = union(enum) {
23712451 \\ A: usize,
23722452 \\ B: isize,
......@@ -2374,7 +2454,10 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
23742454 \\comptime {
23752455 \\ var u = Union{ .A = 8 };
23762456 \\ switch (u) {
2377 \\ .A, .B => |e| unreachable,
2457 \\ .A, .B => |e| {
2458 \\ _ = e;
2459 \\ unreachable;
2460 \\ },
23782461 \\ }
23792462 \\}
23802463 , &[_][]const u8{
......@@ -2383,7 +2466,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
23832466 "tmp.zig:8:13: note: type 'isize' here",
23842467 });
23852468
2386 cases.add("wrong type to @hasField",
2469 ctx.objErrStage1("wrong type to @hasField",
23872470 \\export fn entry() bool {
23882471 \\ return @hasField(i32, "hi");
23892472 \\}
......@@ -2391,44 +2474,49 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
23912474 "tmp.zig:2:22: error: type 'i32' does not support @hasField",
23922475 });
23932476
2394 cases.add("slice passed as array init type with elems",
2477 ctx.objErrStage1("slice passed as array init type with elems",
23952478 \\export fn entry() void {
23962479 \\ const x = []u8{1, 2};
2480 \\ _ = x;
23972481 \\}
23982482 , &[_][]const u8{
23992483 "tmp.zig:2:15: error: array literal requires address-of operator to coerce to slice type '[]u8'",
24002484 });
24012485
2402 cases.add("slice passed as array init type",
2486 ctx.objErrStage1("slice passed as array init type",
24032487 \\export fn entry() void {
24042488 \\ const x = []u8{};
2489 \\ _ = x;
24052490 \\}
24062491 , &[_][]const u8{
24072492 "tmp.zig:2:15: error: array literal requires address-of operator to coerce to slice type '[]u8'",
24082493 });
24092494
2410 cases.add("inferred array size invalid here",
2495 ctx.objErrStage1("inferred array size invalid here",
24112496 \\export fn entry() void {
24122497 \\ const x = [_]u8;
2498 \\ _ = x;
24132499 \\}
24142500 \\export fn entry2() void {
24152501 \\ const S = struct { a: *const [_]u8 };
24162502 \\ var a = .{ S{} };
2503 \\ _ = a;
24172504 \\}
24182505 , &[_][]const u8{
2419 "tmp.zig:2:15: error: inferred array size invalid here",
2420 "tmp.zig:5:34: error: inferred array size invalid here",
2506 "tmp.zig:2:16: error: unable to infer array size",
2507 "tmp.zig:6:35: error: unable to infer array size",
24212508 });
24222509
2423 cases.add("initializing array with struct syntax",
2510 ctx.objErrStage1("initializing array with struct syntax",
24242511 \\export fn entry() void {
24252512 \\ const x = [_]u8{ .y = 2 };
2513 \\ _ = x;
24262514 \\}
24272515 , &[_][]const u8{
24282516 "tmp.zig:2:15: error: initializing array with struct syntax",
24292517 });
24302518
2431 cases.add("compile error in struct init expression",
2519 ctx.objErrStage1("compile error in struct init expression",
24322520 \\const Foo = struct {
24332521 \\ a: i32 = crap,
24342522 \\ b: i32,
......@@ -2437,23 +2525,25 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
24372525 \\ var x = Foo{
24382526 \\ .b = 5,
24392527 \\ };
2528 \\ _ = x;
24402529 \\}
24412530 , &[_][]const u8{
24422531 "tmp.zig:2:14: error: use of undeclared identifier 'crap'",
24432532 });
24442533
2445 cases.add("undefined as field type is rejected",
2534 ctx.objErrStage1("undefined as field type is rejected",
24462535 \\const Foo = struct {
24472536 \\ a: undefined,
24482537 \\};
24492538 \\export fn entry1() void {
24502539 \\ const foo: Foo = undefined;
2540 \\ _ = foo;
24512541 \\}
24522542 , &[_][]const u8{
24532543 "tmp.zig:2:8: error: use of undefined value here causes undefined behavior",
24542544 });
24552545
2456 cases.add("@hasDecl with non-container",
2546 ctx.objErrStage1("@hasDecl with non-container",
24572547 \\export fn entry() void {
24582548 \\ _ = @hasDecl(i32, "hi");
24592549 \\}
......@@ -2461,16 +2551,17 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
24612551 "tmp.zig:2:18: error: expected struct, enum, or union; found 'i32'",
24622552 });
24632553
2464 cases.add("field access of slices",
2554 ctx.objErrStage1("field access of slices",
24652555 \\export fn entry() void {
24662556 \\ var slice: []i32 = undefined;
24672557 \\ const info = @TypeOf(slice).unknown;
2558 \\ _ = info;
24682559 \\}
24692560 , &[_][]const u8{
24702561 "tmp.zig:3:32: error: type 'type' does not support field access",
24712562 });
24722563
2473 cases.add("peer cast then implicit cast const pointer to mutable C pointer",
2564 ctx.objErrStage1("peer cast then implicit cast const pointer to mutable C pointer",
24742565 \\export fn func() void {
24752566 \\ var strValue: [*c]u8 = undefined;
24762567 \\ strValue = strValue orelse "";
......@@ -2480,19 +2571,20 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
24802571 "tmp.zig:3:32: note: cast discards const qualifier",
24812572 });
24822573
2483 cases.add("overflow in enum value allocation",
2574 ctx.objErrStage1("overflow in enum value allocation",
24842575 \\const Moo = enum(u8) {
24852576 \\ Last = 255,
24862577 \\ Over,
24872578 \\};
24882579 \\pub fn main() void {
24892580 \\ var y = Moo.Last;
2581 \\ _ = y;
24902582 \\}
24912583 , &[_][]const u8{
24922584 "tmp.zig:3:5: error: enumeration value 256 too large for type 'u8'",
24932585 });
24942586
2495 cases.add("attempt to cast enum literal to error",
2587 ctx.objErrStage1("attempt to cast enum literal to error",
24962588 \\export fn entry() void {
24972589 \\ switch (error.Hi) {
24982590 \\ .Hi => {},
......@@ -2502,7 +2594,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
25022594 "tmp.zig:3:9: error: expected type 'error{Hi}', found '(enum literal)'",
25032595 });
25042596
2505 cases.add("@sizeOf bad type",
2597 ctx.objErrStage1("@sizeOf bad type",
25062598 \\export fn entry() usize {
25072599 \\ return @sizeOf(@TypeOf(null));
25082600 \\}
......@@ -2510,7 +2602,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
25102602 "tmp.zig:2:20: error: no size available for type '(null)'",
25112603 });
25122604
2513 cases.add("generic function where return type is self-referenced",
2605 ctx.objErrStage1("generic function where return type is self-referenced",
25142606 \\fn Foo(comptime T: type) Foo(T) {
25152607 \\ return struct{ x: T };
25162608 \\}
......@@ -2518,34 +2610,37 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
25182610 \\ const t = Foo(u32) {
25192611 \\ .x = 1
25202612 \\ };
2613 \\ _ = t;
25212614 \\}
25222615 , &[_][]const u8{
25232616 "tmp.zig:1:29: error: evaluation exceeded 1000 backwards branches",
25242617 "tmp.zig:5:18: note: referenced here",
25252618 });
25262619
2527 cases.add("@ptrToInt 0 to non optional pointer",
2620 ctx.objErrStage1("@ptrToInt 0 to non optional pointer",
25282621 \\export fn entry() void {
25292622 \\ var b = @intToPtr(*i32, 0);
2623 \\ _ = b;
25302624 \\}
25312625 , &[_][]const u8{
25322626 "tmp.zig:2:13: error: pointer type '*i32' does not allow address zero",
25332627 });
25342628
2535 cases.add("cast enum literal to enum but it doesn't match",
2629 ctx.objErrStage1("cast enum literal to enum but it doesn't match",
25362630 \\const Foo = enum {
25372631 \\ a,
25382632 \\ b,
25392633 \\};
25402634 \\export fn entry() void {
25412635 \\ const x: Foo = .c;
2636 \\ _ = x;
25422637 \\}
25432638 , &[_][]const u8{
25442639 "tmp.zig:6:20: error: enum 'Foo' has no field named 'c'",
25452640 "tmp.zig:1:13: note: 'Foo' declared here",
25462641 });
25472642
2548 cases.add("discarding error value",
2643 ctx.objErrStage1("discarding error value",
25492644 \\export fn entry() void {
25502645 \\ _ = foo();
25512646 \\}
......@@ -2556,7 +2651,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
25562651 "tmp.zig:2:12: error: error is discarded. consider using `try`, `catch`, or `if`",
25572652 });
25582653
2559 cases.add("volatile on global assembly",
2654 ctx.objErrStage1("volatile on global assembly",
25602655 \\comptime {
25612656 \\ asm volatile ("");
25622657 \\}
......@@ -2564,7 +2659,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
25642659 "tmp.zig:2:9: error: volatile is meaningless on global assembly",
25652660 });
25662661
2567 cases.add("invalid multiple dereferences",
2662 ctx.objErrStage1("invalid multiple dereferences",
25682663 \\export fn a() void {
25692664 \\ var box = Box{ .field = 0 };
25702665 \\ box.*.field = 1;
......@@ -2582,13 +2677,13 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
25822677 "tmp.zig:8:13: error: attempt to dereference non-pointer type 'Box'",
25832678 });
25842679
2585 cases.add("usingnamespace with wrong type",
2680 ctx.objErrStage1("usingnamespace with wrong type",
25862681 \\usingnamespace void;
25872682 , &[_][]const u8{
25882683 "tmp.zig:1:1: error: expected struct, enum, or union; found 'void'",
25892684 });
25902685
2591 cases.add("ignored expression in while continuation",
2686 ctx.objErrStage1("ignored expression in while continuation",
25922687 \\export fn a() void {
25932688 \\ while (true) : (bad()) {}
25942689 \\}
......@@ -2609,31 +2704,31 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
26092704 "tmp.zig:10:25: error: error is ignored. consider using `try`, `catch`, or `if`",
26102705 });
26112706
2612 cases.add("empty while loop body",
2707 ctx.objErrStage1("empty while loop body",
26132708 \\export fn a() void {
26142709 \\ while(true);
26152710 \\}
26162711 , &[_][]const u8{
2617 "tmp.zig:2:16: error: expected loop body, found ';'",
2712 "tmp.zig:2:16: error: expected block or assignment, found ';'",
26182713 });
26192714
2620 cases.add("empty for loop body",
2715 ctx.objErrStage1("empty for loop body",
26212716 \\export fn a() void {
26222717 \\ for(undefined) |x|;
26232718 \\}
26242719 , &[_][]const u8{
2625 "tmp.zig:2:23: error: expected loop body, found ';'",
2720 "tmp.zig:2:23: error: expected block or assignment, found ';'",
26262721 });
26272722
2628 cases.add("empty if body",
2723 ctx.objErrStage1("empty if body",
26292724 \\export fn a() void {
26302725 \\ if(true);
26312726 \\}
26322727 , &[_][]const u8{
2633 "tmp.zig:2:13: error: expected if body, found ';'",
2728 "tmp.zig:2:13: error: expected block or assignment, found ';'",
26342729 });
26352730
2636 cases.add("import outside package path",
2731 ctx.objErrStage1("import outside package path",
26372732 \\comptime{
26382733 \\ _ = @import("../a.zig");
26392734 \\}
......@@ -2641,14 +2736,14 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
26412736 "tmp.zig:2:9: error: import of file outside package path: '../a.zig'",
26422737 });
26432738
2644 cases.add("bogus compile var",
2739 ctx.objErrStage1("bogus compile var",
26452740 \\const x = @import("builtin").bogus;
26462741 \\export fn entry() usize { return @sizeOf(@TypeOf(x)); }
26472742 , &[_][]const u8{
26482743 "tmp.zig:1:29: error: container 'builtin' has no member called 'bogus'",
26492744 });
26502745
2651 cases.add("wrong panic signature, runtime function",
2746 ctx.objErrStage1("wrong panic signature, runtime function",
26522747 \\test "" {}
26532748 \\
26542749 \\pub fn panic() void {}
......@@ -2657,8 +2752,9 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
26572752 "error: expected type 'fn([]const u8, ?*std.builtin.StackTrace) noreturn', found 'fn() void'",
26582753 });
26592754
2660 cases.add("wrong panic signature, generic function",
2755 ctx.objErrStage1("wrong panic signature, generic function",
26612756 \\pub fn panic(comptime msg: []const u8, error_return_trace: ?*builtin.StackTrace) noreturn {
2757 \\ _ = msg; _ = error_return_trace;
26622758 \\ while (true) {}
26632759 \\}
26642760 , &[_][]const u8{
......@@ -2666,14 +2762,14 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
26662762 "note: only one of the functions is generic",
26672763 });
26682764
2669 cases.add("direct struct loop",
2765 ctx.objErrStage1("direct struct loop",
26702766 \\const A = struct { a : A, };
26712767 \\export fn entry() usize { return @sizeOf(A); }
26722768 , &[_][]const u8{
26732769 "tmp.zig:1:11: error: struct 'A' depends on itself",
26742770 });
26752771
2676 cases.add("indirect struct loop",
2772 ctx.objErrStage1("indirect struct loop",
26772773 \\const A = struct { b : B, };
26782774 \\const B = struct { c : C, };
26792775 \\const C = struct { a : A, };
......@@ -2682,7 +2778,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
26822778 "tmp.zig:1:11: error: struct 'A' depends on itself",
26832779 });
26842780
2685 cases.add("instantiating an undefined value for an invalid struct that contains itself",
2781 ctx.objErrStage1("instantiating an undefined value for an invalid struct that contains itself",
26862782 \\const Foo = struct {
26872783 \\ x: Foo,
26882784 \\};
......@@ -2697,23 +2793,25 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
26972793 "tmp.zig:8:28: note: referenced here",
26982794 });
26992795
2700 cases.add("enum field value references enum",
2701 \\pub const Foo = extern enum {
2796 ctx.objErrStage1("enum field value references enum",
2797 \\pub const Foo = enum(c_int) {
27022798 \\ A = Foo.B,
27032799 \\ C = D,
27042800 \\};
27052801 \\export fn entry() void {
27062802 \\ var s: Foo = Foo.E;
2803 \\ _ = s;
27072804 \\}
27082805 , &[_][]const u8{
27092806 "tmp.zig:1:17: error: enum 'Foo' depends on itself",
27102807 });
27112808
2712 cases.add("top level decl dependency loop",
2809 ctx.objErrStage1("top level decl dependency loop",
27132810 \\const a : @TypeOf(b) = 0;
27142811 \\const b : @TypeOf(a) = 0;
27152812 \\export fn entry() void {
27162813 \\ const c = a + b;
2814 \\ _ = c;
27172815 \\}
27182816 , &[_][]const u8{
27192817 "tmp.zig:2:19: error: dependency loop detected",
......@@ -2721,7 +2819,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
27212819 "tmp.zig:4:15: note: referenced here",
27222820 });
27232821
2724 cases.addTest("not an enum type",
2822 ctx.testErrStage1("not an enum type",
27252823 \\export fn entry() void {
27262824 \\ var self: Error = undefined;
27272825 \\ switch (self) {
......@@ -2739,18 +2837,19 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
27392837 "tmp.zig:4:9: error: expected type '@typeInfo(Error).Union.tag_type.?', found 'type'",
27402838 });
27412839
2742 cases.addTest("binary OR operator on error sets",
2840 ctx.testErrStage1("binary OR operator on error sets",
27432841 \\pub const A = error.A;
27442842 \\pub const AB = A | error.B;
27452843 \\export fn entry() void {
27462844 \\ var x: AB = undefined;
2845 \\ _ = x;
27472846 \\}
27482847 , &[_][]const u8{
27492848 "tmp.zig:2:18: error: invalid operands to binary expression: 'error{A}' and 'error{B}'",
27502849 });
27512850
27522851 if (std.Target.current.os.tag == .linux) {
2753 cases.addTest("implicit dependency on libc",
2852 ctx.testErrStage1("implicit dependency on libc",
27542853 \\extern "c" fn exit(u8) void;
27552854 \\export fn entry() void {
27562855 \\ exit(0);
......@@ -2759,7 +2858,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
27592858 "tmp.zig:3:5: error: dependency on libc must be explicitly specified in the build command",
27602859 });
27612860
2762 cases.addTest("libc headers note",
2861 ctx.testErrStage1("libc headers note",
27632862 \\const c = @cImport(@cInclude("stdio.h"));
27642863 \\export fn entry() void {
27652864 \\ _ = c.printf("hello, world!\n");
......@@ -2770,18 +2869,19 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
27702869 });
27712870 }
27722871
2773 cases.addTest("comptime vector overflow shows the index",
2872 ctx.testErrStage1("comptime vector overflow shows the index",
27742873 \\comptime {
27752874 \\ var a: @import("std").meta.Vector(4, u8) = [_]u8{ 1, 2, 255, 4 };
27762875 \\ var b: @import("std").meta.Vector(4, u8) = [_]u8{ 5, 6, 1, 8 };
27772876 \\ var x = a + b;
2877 \\ _ = x;
27782878 \\}
27792879 , &[_][]const u8{
27802880 "tmp.zig:4:15: error: operation caused overflow",
27812881 "tmp.zig:4:15: note: when computing vector element at index 2",
27822882 });
27832883
2784 cases.addTest("packed struct with fields of not allowed types",
2884 ctx.testErrStage1("packed struct with fields of not allowed types",
27852885 \\const A = packed struct {
27862886 \\ x: anyerror,
27872887 \\};
......@@ -2805,24 +2905,31 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
28052905 \\};
28062906 \\export fn entry1() void {
28072907 \\ var a: A = undefined;
2908 \\ _ = a;
28082909 \\}
28092910 \\export fn entry2() void {
28102911 \\ var b: B = undefined;
2912 \\ _ = b;
28112913 \\}
28122914 \\export fn entry3() void {
28132915 \\ var r: C = undefined;
2916 \\ _ = r;
28142917 \\}
28152918 \\export fn entry4() void {
28162919 \\ var d: D = undefined;
2920 \\ _ = d;
28172921 \\}
28182922 \\export fn entry5() void {
28192923 \\ var e: E = undefined;
2924 \\ _ = e;
28202925 \\}
28212926 \\export fn entry6() void {
28222927 \\ var f: F = undefined;
2928 \\ _ = f;
28232929 \\}
28242930 \\export fn entry7() void {
28252931 \\ var g: G = undefined;
2932 \\ _ = g;
28262933 \\}
28272934 \\const S = struct {
28282935 \\ x: i32,
......@@ -2843,42 +2950,40 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
28432950 "tmp.zig:14:5: error: non-packed, non-extern struct 'U' not allowed in packed struct; no guaranteed in-memory representation",
28442951 "tmp.zig:17:5: error: type '?anyerror' not allowed in packed struct; no guaranteed in-memory representation",
28452952 "tmp.zig:20:5: error: type 'Enum' not allowed in packed struct; no guaranteed in-memory representation",
2846 "tmp.zig:50:14: note: enum declaration does not specify an integer tag type",
2953 "tmp.zig:57:14: note: enum declaration does not specify an integer tag type",
28472954 });
28482955
2849 cases.addCase(x: {
2850 var tc = cases.create("deduplicate undeclared identifier",
2851 \\export fn a() void {
2852 \\ x += 1;
2853 \\}
2854 \\export fn b() void {
2855 \\ x += 1;
2856 \\}
2857 , &[_][]const u8{
2858 "tmp.zig:2:5: error: use of undeclared identifier 'x'",
2859 });
2860 tc.expect_exact = true;
2861 break :x tc;
2956 ctx.objErrStage1("deduplicate undeclared identifier",
2957 \\export fn a() void {
2958 \\ x += 1;
2959 \\}
2960 \\export fn b() void {
2961 \\ x += 1;
2962 \\}
2963 , &[_][]const u8{
2964 "tmp.zig:2:5: error: use of undeclared identifier 'x'",
28622965 });
28632966
2864 cases.add("export generic function",
2967 ctx.objErrStage1("export generic function",
28652968 \\export fn foo(num: anytype) i32 {
2969 \\ _ = num;
28662970 \\ return 0;
28672971 \\}
28682972 , &[_][]const u8{
28692973 "tmp.zig:1:15: error: parameter of type 'anytype' not allowed in function with calling convention 'C'",
28702974 });
28712975
2872 cases.add("C pointer to c_void",
2976 ctx.objErrStage1("C pointer to c_void",
28732977 \\export fn a() void {
28742978 \\ var x: *c_void = undefined;
28752979 \\ var y: [*c]c_void = x;
2980 \\ _ = y;
28762981 \\}
28772982 , &[_][]const u8{
28782983 "tmp.zig:3:16: error: C pointers cannot point to opaque types",
28792984 });
28802985
2881 cases.add("directly embedding opaque type in struct and union",
2986 ctx.objErrStage1("directly embedding opaque type in struct and union",
28822987 \\const O = opaque {};
28832988 \\const Foo = struct {
28842989 \\ o: O,
......@@ -2889,74 +2994,85 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
28892994 \\};
28902995 \\export fn a() void {
28912996 \\ var foo: Foo = undefined;
2997 \\ _ = foo;
28922998 \\}
28932999 \\export fn b() void {
28943000 \\ var bar: Bar = undefined;
3001 \\ _ = bar;
28953002 \\}
28963003 \\export fn c() void {
28973004 \\ var baz: *opaque {} = undefined;
28983005 \\ const qux = .{baz.*};
3006 \\ _ = qux;
28993007 \\}
29003008 , &[_][]const u8{
29013009 "tmp.zig:3:5: error: opaque types have unknown size and therefore cannot be directly embedded in structs",
29023010 "tmp.zig:7:5: error: opaque types have unknown size and therefore cannot be directly embedded in unions",
2903 "tmp.zig:17:22: error: opaque types have unknown size and therefore cannot be directly embedded in structs",
3011 "tmp.zig:19:22: error: opaque types have unknown size and therefore cannot be directly embedded in structs",
29043012 });
29053013
2906 cases.add("implicit cast between C pointer and Zig pointer - bad const/align/child",
3014 ctx.objErrStage1("implicit cast between C pointer and Zig pointer - bad const/align/child",
29073015 \\export fn a() void {
29083016 \\ var x: [*c]u8 = undefined;
29093017 \\ var y: *align(4) u8 = x;
3018 \\ _ = y;
29103019 \\}
29113020 \\export fn b() void {
29123021 \\ var x: [*c]const u8 = undefined;
29133022 \\ var y: *u8 = x;
3023 \\ _ = y;
29143024 \\}
29153025 \\export fn c() void {
29163026 \\ var x: [*c]u8 = undefined;
29173027 \\ var y: *u32 = x;
3028 \\ _ = y;
29183029 \\}
29193030 \\export fn d() void {
29203031 \\ var y: *align(1) u32 = undefined;
29213032 \\ var x: [*c]u32 = y;
3033 \\ _ = x;
29223034 \\}
29233035 \\export fn e() void {
29243036 \\ var y: *const u8 = undefined;
29253037 \\ var x: [*c]u8 = y;
3038 \\ _ = x;
29263039 \\}
29273040 \\export fn f() void {
29283041 \\ var y: *u8 = undefined;
29293042 \\ var x: [*c]u32 = y;
3043 \\ _ = x;
29303044 \\}
29313045 , &[_][]const u8{
29323046 "tmp.zig:3:27: error: cast increases pointer alignment",
2933 "tmp.zig:7:18: error: cast discards const qualifier",
2934 "tmp.zig:11:19: error: expected type '*u32', found '[*c]u8'",
2935 "tmp.zig:11:19: note: pointer type child 'u8' cannot cast into pointer type child 'u32'",
2936 "tmp.zig:15:22: error: cast increases pointer alignment",
2937 "tmp.zig:19:21: error: cast discards const qualifier",
2938 "tmp.zig:23:22: error: expected type '[*c]u32', found '*u8'",
3047 "tmp.zig:8:18: error: cast discards const qualifier",
3048 "tmp.zig:13:19: error: expected type '*u32', found '[*c]u8'",
3049 "tmp.zig:13:19: note: pointer type child 'u8' cannot cast into pointer type child 'u32'",
3050 "tmp.zig:18:22: error: cast increases pointer alignment",
3051 "tmp.zig:23:21: error: cast discards const qualifier",
3052 "tmp.zig:28:22: error: expected type '[*c]u32', found '*u8'",
29393053 });
29403054
2941 cases.add("implicit casting null c pointer to zig pointer",
3055 ctx.objErrStage1("implicit casting null c pointer to zig pointer",
29423056 \\comptime {
29433057 \\ var c_ptr: [*c]u8 = 0;
29443058 \\ var zig_ptr: *u8 = c_ptr;
3059 \\ _ = zig_ptr;
29453060 \\}
29463061 , &[_][]const u8{
29473062 "tmp.zig:3:24: error: null pointer casted to type '*u8'",
29483063 });
29493064
2950 cases.add("implicit casting undefined c pointer to zig pointer",
3065 ctx.objErrStage1("implicit casting undefined c pointer to zig pointer",
29513066 \\comptime {
29523067 \\ var c_ptr: [*c]u8 = undefined;
29533068 \\ var zig_ptr: *u8 = c_ptr;
3069 \\ _ = zig_ptr;
29543070 \\}
29553071 , &[_][]const u8{
29563072 "tmp.zig:3:24: error: use of undefined value here causes undefined behavior",
29573073 });
29583074
2959 cases.add("implicit casting C pointers which would mess up null semantics",
3075 ctx.objErrStage1("implicit casting C pointers which would mess up null semantics",
29603076 \\export fn entry() void {
29613077 \\ var slice: []const u8 = "aoeu";
29623078 \\ const opt_many_ptr: [*]const u8 = slice.ptr;
......@@ -2970,6 +3086,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
29703086 \\ var opt_many_ptr: [*]u8 = slice.ptr;
29713087 \\ var ptr_opt_many_ptr = &opt_many_ptr;
29723088 \\ var c_ptr: [*c][*c]const u8 = ptr_opt_many_ptr;
3089 \\ _ = c_ptr;
29733090 \\}
29743091 , &[_][]const u8{
29753092 "tmp.zig:6:24: error: expected type '*const [*]const u8', found '[*c]const [*c]const u8'",
......@@ -2980,47 +3097,46 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
29803097 "tmp.zig:13:35: note: mutable '[*c]const u8' allows illegal null values stored to type '[*]u8'",
29813098 });
29823099
2983 cases.add("implicit casting too big integers to C pointers",
3100 ctx.objErrStage1("implicit casting too big integers to C pointers",
29843101 \\export fn a() void {
29853102 \\ var ptr: [*c]u8 = (1 << 64) + 1;
3103 \\ _ = ptr;
29863104 \\}
29873105 \\export fn b() void {
29883106 \\ var x: u65 = 0x1234;
29893107 \\ var ptr: [*c]u8 = x;
3108 \\ _ = ptr;
29903109 \\}
29913110 , &[_][]const u8{
29923111 "tmp.zig:2:33: error: integer value 18446744073709551617 cannot be coerced to type 'usize'",
2993 "tmp.zig:6:23: error: integer type 'u65' too big for implicit @intToPtr to type '[*c]u8'",
3112 "tmp.zig:7:23: error: integer type 'u65' too big for implicit @intToPtr to type '[*c]u8'",
29943113 });
29953114
2996 cases.add("C pointer pointing to non C ABI compatible type or has align attr",
3115 ctx.objErrStage1("C pointer pointing to non C ABI compatible type or has align attr",
29973116 \\const Foo = struct {};
29983117 \\export fn a() void {
29993118 \\ const T = [*c]Foo;
30003119 \\ var t: T = undefined;
3120 \\ _ = t;
30013121 \\}
30023122 , &[_][]const u8{
30033123 "tmp.zig:3:19: error: C pointers cannot point to non-C-ABI-compatible type 'Foo'",
30043124 });
30053125
3006 cases.addCase(x: {
3007 var tc = cases.create("compile log statement warning deduplication in generic fn",
3008 \\export fn entry() void {
3009 \\ inner(1);
3010 \\ inner(2);
3011 \\}
3012 \\fn inner(comptime n: usize) void {
3013 \\ comptime var i = 0;
3014 \\ inline while (i < n) : (i += 1) { @compileLog("!@#$"); }
3015 \\}
3016 , &[_][]const u8{
3017 "tmp.zig:7:39: error: found compile log statement",
3018 });
3019 tc.expect_exact = true;
3020 break :x tc;
3126 ctx.objErrStage1("compile log statement warning deduplication in generic fn",
3127 \\export fn entry() void {
3128 \\ inner(1);
3129 \\ inner(2);
3130 \\}
3131 \\fn inner(comptime n: usize) void {
3132 \\ comptime var i = 0;
3133 \\ inline while (i < n) : (i += 1) { @compileLog("!@#$"); }
3134 \\}
3135 , &[_][]const u8{
3136 "tmp.zig:7:39: error: found compile log statement",
30213137 });
30223138
3023 cases.add("assign to invalid dereference",
3139 ctx.objErrStage1("assign to invalid dereference",
30243140 \\export fn entry() void {
30253141 \\ 'a'.* = 1;
30263142 \\}
......@@ -3028,54 +3144,58 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
30283144 "tmp.zig:2:8: error: attempt to dereference non-pointer type 'comptime_int'",
30293145 });
30303146
3031 cases.add("take slice of invalid dereference",
3147 ctx.objErrStage1("take slice of invalid dereference",
30323148 \\export fn entry() void {
30333149 \\ const x = 'a'.*[0..];
3150 \\ _ = x;
30343151 \\}
30353152 , &[_][]const u8{
30363153 "tmp.zig:2:18: error: attempt to dereference non-pointer type 'comptime_int'",
30373154 });
30383155
3039 cases.add("@truncate undefined value",
3156 ctx.objErrStage1("@truncate undefined value",
30403157 \\export fn entry() void {
30413158 \\ var z = @truncate(u8, @as(u16, undefined));
3159 \\ _ = z;
30423160 \\}
30433161 , &[_][]const u8{
30443162 "tmp.zig:2:27: error: use of undefined value here causes undefined behavior",
30453163 });
30463164
3047 cases.addTest("return invalid type from test",
3165 ctx.testErrStage1("return invalid type from test",
30483166 \\test "example" { return 1; }
30493167 , &[_][]const u8{
30503168 "tmp.zig:1:25: error: expected type 'void', found 'comptime_int'",
30513169 });
30523170
3053 cases.add("threadlocal qualifier on const",
3171 ctx.objErrStage1("threadlocal qualifier on const",
30543172 \\threadlocal const x: i32 = 1234;
30553173 \\export fn entry() i32 {
30563174 \\ return x;
30573175 \\}
30583176 , &[_][]const u8{
3059 "tmp.zig:1:13: error: threadlocal variable cannot be constant",
3177 "tmp.zig:1:1: error: threadlocal variable cannot be constant",
30603178 });
30613179
3062 cases.add("@bitCast same size but bit count mismatch",
3180 ctx.objErrStage1("@bitCast same size but bit count mismatch",
30633181 \\export fn entry(byte: u8) void {
30643182 \\ var oops = @bitCast(u7, byte);
3183 \\ _ = oops;
30653184 \\}
30663185 , &[_][]const u8{
30673186 "tmp.zig:2:25: error: destination type 'u7' has 7 bits but source type 'u8' has 8 bits",
30683187 });
30693188
3070 cases.add("@bitCast with different sizes inside an expression",
3189 ctx.objErrStage1("@bitCast with different sizes inside an expression",
30713190 \\export fn entry() void {
30723191 \\ var foo = (@bitCast(u8, @as(f32, 1.0)) == 0xf);
3192 \\ _ = foo;
30733193 \\}
30743194 , &[_][]const u8{
30753195 "tmp.zig:2:25: error: destination type 'u8' has size 1 but source type 'f32' has size 4",
30763196 });
30773197
3078 cases.add("attempted `&&`",
3198 ctx.objErrStage1("attempted `&&`",
30793199 \\export fn entry(a: bool, b: bool) i32 {
30803200 \\ if (a && b) {
30813201 \\ return 1234;
......@@ -3083,10 +3203,10 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
30833203 \\ return 5678;
30843204 \\}
30853205 , &[_][]const u8{
3086 "tmp.zig:2:12: error: `&&` is invalid. Note that `and` is boolean AND",
3206 "tmp.zig:2:11: error: `&&` is invalid; note that `and` is boolean AND",
30873207 });
30883208
3089 cases.add("attempted `||` on boolean values",
3209 ctx.objErrStage1("attempted `||` on boolean values",
30903210 \\export fn entry(a: bool, b: bool) i32 {
30913211 \\ if (a || b) {
30923212 \\ return 1234;
......@@ -3098,7 +3218,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
30983218 "tmp.zig:2:11: note: `||` merges error sets; `or` performs boolean OR",
30993219 });
31003220
3101 cases.add("compile log a pointer to an opaque value",
3221 ctx.objErrStage1("compile log a pointer to an opaque value",
31023222 \\export fn entry() void {
31033223 \\ @compileLog(@ptrCast(*const c_void, &entry));
31043224 \\}
......@@ -3106,13 +3226,14 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
31063226 "tmp.zig:2:5: error: found compile log statement",
31073227 });
31083228
3109 cases.add("duplicate boolean switch value",
3229 ctx.objErrStage1("duplicate boolean switch value",
31103230 \\comptime {
31113231 \\ const x = switch (true) {
31123232 \\ true => false,
31133233 \\ false => true,
31143234 \\ true => false,
31153235 \\ };
3236 \\ _ = x;
31163237 \\}
31173238 \\comptime {
31183239 \\ const x = switch (true) {
......@@ -3120,42 +3241,46 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
31203241 \\ true => false,
31213242 \\ false => true,
31223243 \\ };
3244 \\ _ = x;
31233245 \\}
31243246 , &[_][]const u8{
31253247 "tmp.zig:5:9: error: duplicate switch value",
3126 "tmp.zig:12:9: error: duplicate switch value",
3248 "tmp.zig:13:9: error: duplicate switch value",
31273249 });
31283250
3129 cases.add("missing boolean switch value",
3251 ctx.objErrStage1("missing boolean switch value",
31303252 \\comptime {
31313253 \\ const x = switch (true) {
31323254 \\ true => false,
31333255 \\ };
3256 \\ _ = x;
31343257 \\}
31353258 \\comptime {
31363259 \\ const x = switch (true) {
31373260 \\ false => true,
31383261 \\ };
3262 \\ _ = x;
31393263 \\}
31403264 , &[_][]const u8{
31413265 "tmp.zig:2:15: error: switch must handle all possibilities",
3142 "tmp.zig:7:15: error: switch must handle all possibilities",
3266 "tmp.zig:8:15: error: switch must handle all possibilities",
31433267 });
31443268
3145 cases.add("reading past end of pointer casted array",
3269 ctx.objErrStage1("reading past end of pointer casted array",
31463270 \\comptime {
31473271 \\ const array: [4]u8 = "aoeu".*;
31483272 \\ const sub_array = array[1..];
31493273 \\ const int_ptr = @ptrCast(*const u24, sub_array);
31503274 \\ const deref = int_ptr.*;
3275 \\ _ = deref;
31513276 \\}
31523277 , &[_][]const u8{
31533278 "tmp.zig:5:26: error: attempt to read 4 bytes from [4]u8 at index 1 which is 3 bytes",
31543279 });
31553280
3156 cases.add("error note for function parameter incompatibility",
3157 \\fn do_the_thing(func: fn (arg: i32) void) void {}
3158 \\fn bar(arg: bool) void {}
3281 ctx.objErrStage1("error note for function parameter incompatibility",
3282 \\fn do_the_thing(func: fn (arg: i32) void) void { _ = func; }
3283 \\fn bar(arg: bool) void { _ = arg; }
31593284 \\export fn entry() void {
31603285 \\ do_the_thing(bar);
31613286 \\}
......@@ -3163,56 +3288,63 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
31633288 "tmp.zig:4:18: error: expected type 'fn(i32) void', found 'fn(bool) void",
31643289 "tmp.zig:4:18: note: parameter 0: 'bool' cannot cast into 'i32'",
31653290 });
3166 cases.add("cast negative value to unsigned integer",
3291 ctx.objErrStage1("cast negative value to unsigned integer",
31673292 \\comptime {
31683293 \\ const value: i32 = -1;
31693294 \\ const unsigned = @intCast(u32, value);
3295 \\ _ = unsigned;
31703296 \\}
31713297 \\export fn entry1() void {
31723298 \\ const value: i32 = -1;
31733299 \\ const unsigned: u32 = value;
3300 \\ _ = unsigned;
31743301 \\}
31753302 , &[_][]const u8{
31763303 "tmp.zig:3:22: error: attempt to cast negative value to unsigned integer",
3177 "tmp.zig:7:27: error: cannot cast negative value -1 to unsigned integer type 'u32'",
3304 "tmp.zig:8:27: error: cannot cast negative value -1 to unsigned integer type 'u32'",
31783305 });
31793306
3180 cases.add("integer cast truncates bits",
3307 ctx.objErrStage1("integer cast truncates bits",
31813308 \\export fn entry1() void {
31823309 \\ const spartan_count: u16 = 300;
31833310 \\ const byte = @intCast(u8, spartan_count);
3311 \\ _ = byte;
31843312 \\}
31853313 \\export fn entry2() void {
31863314 \\ const spartan_count: u16 = 300;
31873315 \\ const byte: u8 = spartan_count;
3316 \\ _ = byte;
31883317 \\}
31893318 \\export fn entry3() void {
31903319 \\ var spartan_count: u16 = 300;
31913320 \\ var byte: u8 = spartan_count;
3321 \\ _ = byte;
31923322 \\}
31933323 \\export fn entry4() void {
31943324 \\ var signed: i8 = -1;
31953325 \\ var unsigned: u64 = signed;
3326 \\ _ = unsigned;
31963327 \\}
31973328 , &[_][]const u8{
31983329 "tmp.zig:3:18: error: cast from 'u16' to 'u8' truncates bits",
3199 "tmp.zig:7:22: error: integer value 300 cannot be coerced to type 'u8'",
3200 "tmp.zig:11:20: error: expected type 'u8', found 'u16'",
3201 "tmp.zig:11:20: note: unsigned 8-bit int cannot represent all possible unsigned 16-bit values",
3202 "tmp.zig:15:25: error: expected type 'u64', found 'i8'",
3203 "tmp.zig:15:25: note: unsigned 64-bit int cannot represent all possible signed 8-bit values",
3330 "tmp.zig:8:22: error: integer value 300 cannot be coerced to type 'u8'",
3331 "tmp.zig:13:20: error: expected type 'u8', found 'u16'",
3332 "tmp.zig:13:20: note: unsigned 8-bit int cannot represent all possible unsigned 16-bit values",
3333 "tmp.zig:18:25: error: expected type 'u64', found 'i8'",
3334 "tmp.zig:18:25: note: unsigned 64-bit int cannot represent all possible signed 8-bit values",
32043335 });
32053336
3206 cases.add("comptime implicit cast f64 to f32",
3337 ctx.objErrStage1("comptime implicit cast f64 to f32",
32073338 \\export fn entry() void {
32083339 \\ const x: f64 = 16777217;
32093340 \\ const y: f32 = x;
3341 \\ _ = y;
32103342 \\}
32113343 , &[_][]const u8{
32123344 "tmp.zig:3:20: error: cast of value 16777217.000000 to type 'f32' loses information",
32133345 });
32143346
3215 cases.add("implicit cast from f64 to f32",
3347 ctx.objErrStage1("implicit cast from f64 to f32",
32163348 \\var x: f64 = 1.0;
32173349 \\var y: f32 = x;
32183350 \\
......@@ -3221,41 +3353,46 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
32213353 "tmp.zig:2:14: error: expected type 'f32', found 'f64'",
32223354 });
32233355
3224 cases.add("exceeded maximum bit width of integer",
3356 ctx.objErrStage1("exceeded maximum bit width of integer",
32253357 \\export fn entry1() void {
32263358 \\ const T = u65536;
3359 \\ _ = T;
32273360 \\}
32283361 \\export fn entry2() void {
32293362 \\ var x: i65536 = 1;
3363 \\ _ = x;
32303364 \\}
32313365 , &[_][]const u8{
3232 "tmp.zig:5:12: error: primitive integer type 'i65536' exceeds maximum bit width of 65535",
3366 "tmp.zig:2:15: error: primitive integer type 'u65536' exceeds maximum bit width of 65535",
3367 "tmp.zig:6:12: error: primitive integer type 'i65536' exceeds maximum bit width of 65535",
32333368 });
32343369
3235 cases.add("compile error when evaluating return type of inferred error set",
3370 ctx.objErrStage1("compile error when evaluating return type of inferred error set",
32363371 \\const Car = struct {
32373372 \\ foo: *SymbolThatDoesNotExist,
32383373 \\ pub fn init() !Car {}
32393374 \\};
32403375 \\export fn entry() void {
32413376 \\ const car = Car.init();
3377 \\ _ = car;
32423378 \\}
32433379 , &[_][]const u8{
32443380 "tmp.zig:2:11: error: use of undeclared identifier 'SymbolThatDoesNotExist'",
32453381 });
32463382
3247 cases.add("don't implicit cast double pointer to *c_void",
3383 ctx.objErrStage1("don't implicit cast double pointer to *c_void",
32483384 \\export fn entry() void {
32493385 \\ var a: u32 = 1;
32503386 \\ var ptr: *align(@alignOf(u32)) c_void = &a;
32513387 \\ var b: *u32 = @ptrCast(*u32, ptr);
32523388 \\ var ptr2: *c_void = &b;
3389 \\ _ = ptr2;
32533390 \\}
32543391 , &[_][]const u8{
32553392 "tmp.zig:5:26: error: expected type '*c_void', found '**u32'",
32563393 });
32573394
3258 cases.add("runtime index into comptime type slice",
3395 ctx.objErrStage1("runtime index into comptime type slice",
32593396 \\const Struct = struct {
32603397 \\ a: u32,
32613398 \\};
......@@ -3265,12 +3402,13 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
32653402 \\export fn entry() void {
32663403 \\ const index = getIndex();
32673404 \\ const field = @typeInfo(Struct).Struct.fields[index];
3405 \\ _ = field;
32683406 \\}
32693407 , &[_][]const u8{
32703408 "tmp.zig:9:51: error: values of type 'std.builtin.StructField' must be comptime known, but index value is runtime known",
32713409 });
32723410
3273 cases.add("compile log statement inside function which must be comptime evaluated",
3411 ctx.objErrStage1("compile log statement inside function which must be comptime evaluated",
32743412 \\fn Foo(comptime T: type) type {
32753413 \\ @compileLog(@typeName(T));
32763414 \\ return T;
......@@ -3283,25 +3421,27 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
32833421 "tmp.zig:2:5: error: found compile log statement",
32843422 });
32853423
3286 cases.add("comptime slice of an undefined slice",
3424 ctx.objErrStage1("comptime slice of an undefined slice",
32873425 \\comptime {
32883426 \\ var a: []u8 = undefined;
32893427 \\ var b = a[0..10];
3428 \\ _ = b;
32903429 \\}
32913430 , &[_][]const u8{
32923431 "tmp.zig:3:14: error: slice of undefined",
32933432 });
32943433
3295 cases.add("implicit cast const array to mutable slice",
3434 ctx.objErrStage1("implicit cast const array to mutable slice",
32963435 \\export fn entry() void {
32973436 \\ const buffer: [1]u8 = [_]u8{8};
32983437 \\ const sliceA: []u8 = &buffer;
3438 \\ _ = sliceA;
32993439 \\}
33003440 , &[_][]const u8{
33013441 "tmp.zig:3:27: error: expected type '[]u8', found '*const [1]u8'",
33023442 });
33033443
3304 cases.add("deref slice and get len field",
3444 ctx.objErrStage1("deref slice and get len field",
33053445 \\export fn entry() void {
33063446 \\ var a: []u8 = undefined;
33073447 \\ _ = a.*.len;
......@@ -3310,7 +3450,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
33103450 "tmp.zig:3:10: error: attempt to dereference non-pointer type '[]u8'",
33113451 });
33123452
3313 cases.add("@ptrCast a 0 bit type to a non- 0 bit type",
3453 ctx.objErrStage1("@ptrCast a 0 bit type to a non- 0 bit type",
33143454 \\export fn entry() bool {
33153455 \\ var x: u0 = 0;
33163456 \\ const p = @ptrCast(?*u0, &x);
......@@ -3322,7 +3462,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
33223462 "tmp.zig:3:24: note: '?*u0' has in-memory bits",
33233463 });
33243464
3325 cases.add("comparing a non-optional pointer against null",
3465 ctx.objErrStage1("comparing a non-optional pointer against null",
33263466 \\export fn entry() void {
33273467 \\ var x: i32 = 1;
33283468 \\ _ = &x == null;
......@@ -3331,21 +3471,23 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
33313471 "tmp.zig:3:12: error: comparison of '*i32' with null",
33323472 });
33333473
3334 cases.add("non error sets used in merge error sets operator",
3474 ctx.objErrStage1("non error sets used in merge error sets operator",
33353475 \\export fn foo() void {
33363476 \\ const Errors = u8 || u16;
3477 \\ _ = Errors;
33373478 \\}
33383479 \\export fn bar() void {
33393480 \\ const Errors = error{} || u16;
3481 \\ _ = Errors;
33403482 \\}
33413483 , &[_][]const u8{
33423484 "tmp.zig:2:20: error: expected error set type, found type 'u8'",
33433485 "tmp.zig:2:23: note: `||` merges error sets; `or` performs boolean OR",
3344 "tmp.zig:5:31: error: expected error set type, found type 'u16'",
3345 "tmp.zig:5:28: note: `||` merges error sets; `or` performs boolean OR",
3486 "tmp.zig:6:31: error: expected error set type, found type 'u16'",
3487 "tmp.zig:6:28: note: `||` merges error sets; `or` performs boolean OR",
33463488 });
33473489
3348 cases.add("variable initialization compile error then referenced",
3490 ctx.objErrStage1("variable initialization compile error then referenced",
33493491 \\fn Undeclared() type {
33503492 \\ return T;
33513493 \\}
......@@ -3357,12 +3499,13 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
33573499 \\}
33583500 \\export fn entry() void {
33593501 \\ const S = Gen();
3502 \\ _ = S;
33603503 \\}
33613504 , &[_][]const u8{
33623505 "tmp.zig:2:12: error: use of undeclared identifier 'T'",
33633506 });
33643507
3365 cases.add("refer to the type of a generic function",
3508 ctx.objErrStage1("refer to the type of a generic function",
33663509 \\export fn entry() void {
33673510 \\ const Func = fn (type) void;
33683511 \\ const f: Func = undefined;
......@@ -3372,7 +3515,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
33723515 "tmp.zig:4:5: error: use of undefined value here causes undefined behavior",
33733516 });
33743517
3375 cases.add("accessing runtime parameter from outer function",
3518 ctx.objErrStage1("accessing runtime parameter from outer function",
33763519 \\fn outer(y: u32) fn (u32) u32 {
33773520 \\ const st = struct {
33783521 \\ fn get(z: u32) u32 {
......@@ -3384,6 +3527,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
33843527 \\export fn entry() void {
33853528 \\ var func = outer(10);
33863529 \\ var x = func(3);
3530 \\ _ = x;
33873531 \\}
33883532 , &[_][]const u8{
33893533 "tmp.zig:4:24: error: 'y' not accessible from inner function",
......@@ -3391,69 +3535,74 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
33913535 "tmp.zig:1:10: note: declared here",
33923536 });
33933537
3394 cases.add("non int passed to @intToFloat",
3538 ctx.objErrStage1("non int passed to @intToFloat",
33953539 \\export fn entry() void {
33963540 \\ const x = @intToFloat(f32, 1.1);
3541 \\ _ = x;
33973542 \\}
33983543 , &[_][]const u8{
33993544 "tmp.zig:2:32: error: expected int type, found 'comptime_float'",
34003545 });
34013546
3402 cases.add("non float passed to @floatToInt",
3547 ctx.objErrStage1("non float passed to @floatToInt",
34033548 \\export fn entry() void {
34043549 \\ const x = @floatToInt(i32, @as(i32, 54));
3550 \\ _ = x;
34053551 \\}
34063552 , &[_][]const u8{
34073553 "tmp.zig:2:32: error: expected float type, found 'i32'",
34083554 });
34093555
3410 cases.add("out of range comptime_int passed to @floatToInt",
3556 ctx.objErrStage1("out of range comptime_int passed to @floatToInt",
34113557 \\export fn entry() void {
34123558 \\ const x = @floatToInt(i8, 200);
3559 \\ _ = x;
34133560 \\}
34143561 , &[_][]const u8{
34153562 "tmp.zig:2:31: error: integer value 200 cannot be coerced to type 'i8'",
34163563 });
34173564
3418 cases.add("load too many bytes from comptime reinterpreted pointer",
3565 ctx.objErrStage1("load too many bytes from comptime reinterpreted pointer",
34193566 \\export fn entry() void {
34203567 \\ const float: f32 = 5.99999999999994648725e-01;
34213568 \\ const float_ptr = &float;
34223569 \\ const int_ptr = @ptrCast(*const i64, float_ptr);
34233570 \\ const int_val = int_ptr.*;
3571 \\ _ = int_val;
34243572 \\}
34253573 , &[_][]const u8{
34263574 "tmp.zig:5:28: error: attempt to read 8 bytes from pointer to f32 which is 4 bytes",
34273575 });
34283576
3429 cases.add("invalid type used in array type",
3577 ctx.objErrStage1("invalid type used in array type",
34303578 \\const Item = struct {
34313579 \\ field: SomeNonexistentType,
34323580 \\};
34333581 \\var items: [100]Item = undefined;
34343582 \\export fn entry() void {
34353583 \\ const a = items[0];
3584 \\ _ = a;
34363585 \\}
34373586 , &[_][]const u8{
34383587 "tmp.zig:2:12: error: use of undeclared identifier 'SomeNonexistentType'",
34393588 });
34403589
3441 cases.add("comptime continue inside runtime catch",
3442 \\export fn entry(c: bool) void {
3590 ctx.objErrStage1("comptime continue inside runtime catch",
3591 \\export fn entry() void {
34433592 \\ const ints = [_]u8{ 1, 2 };
34443593 \\ inline for (ints) |_| {
3445 \\ bad() catch |_| continue;
3594 \\ bad() catch continue;
34463595 \\ }
34473596 \\}
34483597 \\fn bad() !void {
34493598 \\ return error.Bad;
34503599 \\}
34513600 , &[_][]const u8{
3452 "tmp.zig:4:25: error: comptime control flow inside runtime block",
3601 "tmp.zig:4:21: error: comptime control flow inside runtime block",
34533602 "tmp.zig:4:15: note: runtime block created here",
34543603 });
34553604
3456 cases.add("comptime continue inside runtime switch",
3605 ctx.objErrStage1("comptime continue inside runtime switch",
34573606 \\export fn entry() void {
34583607 \\ var p: i32 = undefined;
34593608 \\ comptime var q = true;
......@@ -3470,7 +3619,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
34703619 "tmp.zig:5:9: note: runtime block created here",
34713620 });
34723621
3473 cases.add("comptime continue inside runtime while error",
3622 ctx.objErrStage1("comptime continue inside runtime while error",
34743623 \\export fn entry() void {
34753624 \\ var p: anyerror!usize = undefined;
34763625 \\ comptime var q = true;
......@@ -3486,7 +3635,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
34863635 "tmp.zig:5:9: note: runtime block created here",
34873636 });
34883637
3489 cases.add("comptime continue inside runtime while optional",
3638 ctx.objErrStage1("comptime continue inside runtime while optional",
34903639 \\export fn entry() void {
34913640 \\ var p: ?usize = undefined;
34923641 \\ comptime var q = true;
......@@ -3500,7 +3649,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
35003649 "tmp.zig:5:9: note: runtime block created here",
35013650 });
35023651
3503 cases.add("comptime continue inside runtime while bool",
3652 ctx.objErrStage1("comptime continue inside runtime while bool",
35043653 \\export fn entry() void {
35053654 \\ var p: usize = undefined;
35063655 \\ comptime var q = true;
......@@ -3514,7 +3663,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
35143663 "tmp.zig:5:9: note: runtime block created here",
35153664 });
35163665
3517 cases.add("comptime continue inside runtime if error",
3666 ctx.objErrStage1("comptime continue inside runtime if error",
35183667 \\export fn entry() void {
35193668 \\ var p: anyerror!i32 = undefined;
35203669 \\ comptime var q = true;
......@@ -3528,7 +3677,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
35283677 "tmp.zig:5:9: note: runtime block created here",
35293678 });
35303679
3531 cases.add("comptime continue inside runtime if optional",
3680 ctx.objErrStage1("comptime continue inside runtime if optional",
35323681 \\export fn entry() void {
35333682 \\ var p: ?i32 = undefined;
35343683 \\ comptime var q = true;
......@@ -3542,7 +3691,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
35423691 "tmp.zig:5:9: note: runtime block created here",
35433692 });
35443693
3545 cases.add("comptime continue inside runtime if bool",
3694 ctx.objErrStage1("comptime continue inside runtime if bool",
35463695 \\export fn entry() void {
35473696 \\ var p: usize = undefined;
35483697 \\ comptime var q = true;
......@@ -3556,22 +3705,23 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
35563705 "tmp.zig:5:9: note: runtime block created here",
35573706 });
35583707
3559 cases.add("switch with invalid expression parameter",
3708 ctx.objErrStage1("switch with invalid expression parameter",
35603709 \\export fn entry() void {
35613710 \\ Test(i32);
35623711 \\}
35633712 \\fn Test(comptime T: type) void {
35643713 \\ const x = switch (T) {
3565 \\ []u8 => |x| 123,
3566 \\ i32 => |x| 456,
3714 \\ []u8 => |x| x,
3715 \\ i32 => |x| x,
35673716 \\ else => unreachable,
35683717 \\ };
3718 \\ _ = x;
35693719 \\}
35703720 , &[_][]const u8{
35713721 "tmp.zig:7:17: error: switch on type 'type' provides no expression parameter",
35723722 });
35733723
3574 cases.add("function prototype with no body",
3724 ctx.objErrStage1("function prototype with no body",
35753725 \\fn foo() void;
35763726 \\export fn entry() void {
35773727 \\ foo();
......@@ -3580,7 +3730,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
35803730 "tmp.zig:1:1: error: non-extern function has no body",
35813731 });
35823732
3583 cases.add("@frame() called outside of function definition",
3733 ctx.objErrStage1("@frame() called outside of function definition",
35843734 \\var handle_undef: anyframe = undefined;
35853735 \\var handle_dummy: anyframe = @frame();
35863736 \\export fn entry() bool {
......@@ -3590,16 +3740,17 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
35903740 "tmp.zig:2:30: error: @frame() called outside of function definition",
35913741 });
35923742
3593 cases.add("`_` is not a declarable symbol",
3743 ctx.objErrStage1("`_` is not a declarable symbol",
35943744 \\export fn f1() usize {
35953745 \\ var _: usize = 2;
35963746 \\ return _;
35973747 \\}
35983748 , &[_][]const u8{
3599 "tmp.zig:2:5: error: `_` is not a declarable symbol",
3749 "tmp.zig:2:5: error: '_' used as an identifier without @\"_\" syntax",
3750 "tmp.zig:3:12: error: '_' used as an identifier without @\"_\" syntax",
36003751 });
36013752
3602 cases.add("`_` should not be usable inside for",
3753 ctx.objErrStage1("`_` should not be usable inside for",
36033754 \\export fn returns() void {
36043755 \\ for ([_]void{}) |_, i| {
36053756 \\ for ([_]void{}) |_, j| {
......@@ -3608,10 +3759,10 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
36083759 \\ }
36093760 \\}
36103761 , &[_][]const u8{
3611 "tmp.zig:4:20: error: `_` may only be used to assign things to",
3762 "tmp.zig:4:20: error: '_' used as an identifier without @\"_\" syntax",
36123763 });
36133764
3614 cases.add("`_` should not be usable inside while",
3765 ctx.objErrStage1("`_` should not be usable inside while",
36153766 \\export fn returns() void {
36163767 \\ while (optionalReturn()) |_| {
36173768 \\ while (optionalReturn()) |_| {
......@@ -3623,10 +3774,10 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
36233774 \\ return 1;
36243775 \\}
36253776 , &[_][]const u8{
3626 "tmp.zig:4:20: error: `_` may only be used to assign things to",
3777 "tmp.zig:4:20: error: '_' used as an identifier without @\"_\" syntax",
36273778 });
36283779
3629 cases.add("`_` should not be usable inside while else",
3780 ctx.objErrStage1("`_` should not be usable inside while else",
36303781 \\export fn returns() void {
36313782 \\ while (optionalReturnError()) |_| {
36323783 \\ while (optionalReturnError()) |_| {
......@@ -3640,10 +3791,10 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
36403791 \\ return error.optionalReturnError;
36413792 \\}
36423793 , &[_][]const u8{
3643 "tmp.zig:6:17: error: `_` may only be used to assign things to",
3794 "tmp.zig:6:17: error: '_' used as an identifier without @\"_\" syntax",
36443795 });
36453796
3646 cases.add("while loop body expression ignored",
3797 ctx.objErrStage1("while loop body expression ignored",
36473798 \\fn returns() usize {
36483799 \\ return 2;
36493800 \\}
......@@ -3664,7 +3815,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
36643815 "tmp.zig:13:26: error: expression value is ignored",
36653816 });
36663817
3667 cases.add("missing parameter name of generic function",
3818 ctx.objErrStage1("missing parameter name of generic function",
36683819 \\fn dump(anytype) void {}
36693820 \\export fn entry() void {
36703821 \\ var a: u8 = 9;
......@@ -3674,20 +3825,20 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
36743825 "tmp.zig:1:9: error: missing parameter name",
36753826 });
36763827
3677 cases.add("non-inline for loop on a type that requires comptime",
3828 ctx.objErrStage1("non-inline for loop on a type that requires comptime",
36783829 \\const Foo = struct {
36793830 \\ name: []const u8,
36803831 \\ T: type,
36813832 \\};
36823833 \\export fn entry() void {
36833834 \\ const xx: [2]Foo = undefined;
3684 \\ for (xx) |f| {}
3835 \\ for (xx) |f| { _ = f;}
36853836 \\}
36863837 , &[_][]const u8{
36873838 "tmp.zig:7:5: error: values of type 'Foo' must be comptime known, but index value is runtime known",
36883839 });
36893840
3690 cases.add("generic fn as parameter without comptime keyword",
3841 ctx.objErrStage1("generic fn as parameter without comptime keyword",
36913842 \\fn f(_: fn (anytype) void) void {}
36923843 \\fn g(_: anytype) void {}
36933844 \\export fn entry() void {
......@@ -3697,7 +3848,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
36973848 "tmp.zig:1:9: error: parameter of type 'fn(anytype) anytype' must be declared comptime",
36983849 });
36993850
3700 cases.add("optional pointer to void in extern struct",
3851 ctx.objErrStage1("optional pointer to void in extern struct",
37013852 \\const Foo = extern struct {
37023853 \\ x: ?*const void,
37033854 \\};
......@@ -3705,12 +3856,12 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
37053856 \\ foo: Foo,
37063857 \\ y: i32,
37073858 \\};
3708 \\export fn entry(bar: *Bar) void {}
3859 \\export fn entry(bar: *Bar) void {_ = bar;}
37093860 , &[_][]const u8{
37103861 "tmp.zig:2:5: error: extern structs cannot contain fields of type '?*const void'",
37113862 });
37123863
3713 cases.add("use of comptime-known undefined function value",
3864 ctx.objErrStage1("use of comptime-known undefined function value",
37143865 \\const Cmd = struct {
37153866 \\ exec: fn () void,
37163867 \\};
......@@ -3722,7 +3873,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
37223873 "tmp.zig:6:12: error: use of undefined value here causes undefined behavior",
37233874 });
37243875
3725 cases.add("use of comptime-known undefined function value",
3876 ctx.objErrStage1("use of comptime-known undefined function value",
37263877 \\const Cmd = struct {
37273878 \\ exec: fn () void,
37283879 \\};
......@@ -3734,16 +3885,17 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
37343885 "tmp.zig:6:12: error: use of undefined value here causes undefined behavior",
37353886 });
37363887
3737 cases.add("bad @alignCast at comptime",
3888 ctx.objErrStage1("bad @alignCast at comptime",
37383889 \\comptime {
37393890 \\ const ptr = @intToPtr(*align(1) i32, 0x1);
37403891 \\ const aligned = @alignCast(4, ptr);
3892 \\ _ = aligned;
37413893 \\}
37423894 , &[_][]const u8{
37433895 "tmp.zig:3:35: error: pointer address 0x1 is not aligned to 4 bytes",
37443896 });
37453897
3746 cases.add("@ptrToInt on *void",
3898 ctx.objErrStage1("@ptrToInt on *void",
37473899 \\export fn entry() bool {
37483900 \\ return @ptrToInt(&{}) == @ptrToInt(&{});
37493901 \\}
......@@ -3751,7 +3903,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
37513903 "tmp.zig:2:23: error: pointer to size 0 type has no address",
37523904 });
37533905
3754 cases.add("@popCount - non-integer",
3906 ctx.objErrStage1("@popCount - non-integer",
37553907 \\export fn entry(x: f32) u32 {
37563908 \\ return @popCount(f32, x);
37573909 \\}
......@@ -3759,8 +3911,23 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
37593911 "tmp.zig:2:22: error: expected integer type, found 'f32'",
37603912 });
37613913
3762 cases.addCase(x: {
3763 const tc = cases.create("wrong same named struct",
3914 {
3915 const case = ctx.obj("wrong same named struct", .{});
3916 case.backend = .stage1;
3917
3918 case.addSourceFile("a.zig",
3919 \\pub const Foo = struct {
3920 \\ x: i32,
3921 \\};
3922 );
3923
3924 case.addSourceFile("b.zig",
3925 \\pub const Foo = struct {
3926 \\ z: f64,
3927 \\};
3928 );
3929
3930 case.addError(
37643931 \\const a = @import("a.zig");
37653932 \\const b = @import("b.zig");
37663933 \\
......@@ -3769,30 +3936,16 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
37693936 \\ bar(&a1);
37703937 \\}
37713938 \\
3772 \\fn bar(x: *b.Foo) void {}
3939 \\fn bar(x: *b.Foo) void {_ = x;}
37733940 , &[_][]const u8{
37743941 "tmp.zig:6:10: error: expected type '*b.Foo', found '*a.Foo'",
37753942 "tmp.zig:6:10: note: pointer type child 'a.Foo' cannot cast into pointer type child 'b.Foo'",
37763943 "a.zig:1:17: note: a.Foo declared here",
37773944 "b.zig:1:17: note: b.Foo declared here",
37783945 });
3946 }
37793947
3780 tc.addSourceFile("a.zig",
3781 \\pub const Foo = struct {
3782 \\ x: i32,
3783 \\};
3784 );
3785
3786 tc.addSourceFile("b.zig",
3787 \\pub const Foo = struct {
3788 \\ z: f64,
3789 \\};
3790 );
3791
3792 break :x tc;
3793 });
3794
3795 cases.add("@floatToInt comptime safety",
3948 ctx.objErrStage1("@floatToInt comptime safety",
37963949 \\comptime {
37973950 \\ _ = @floatToInt(i8, @as(f32, -129.1));
37983951 \\}
......@@ -3808,35 +3961,38 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
38083961 "tmp.zig:8:9: error: integer value '256' cannot be stored in type 'u8'",
38093962 });
38103963
3811 cases.add("use c_void as return type of fn ptr",
3964 ctx.objErrStage1("use c_void as return type of fn ptr",
38123965 \\export fn entry() void {
38133966 \\ const a: fn () c_void = undefined;
3967 \\ _ = a;
38143968 \\}
38153969 , &[_][]const u8{
38163970 "tmp.zig:2:20: error: return type cannot be opaque",
38173971 });
38183972
3819 cases.add("use implicit casts to assign null to non-nullable pointer",
3973 ctx.objErrStage1("use implicit casts to assign null to non-nullable pointer",
38203974 \\export fn entry() void {
38213975 \\ var x: i32 = 1234;
38223976 \\ var p: *i32 = &x;
38233977 \\ var pp: *?*i32 = &p;
38243978 \\ pp.* = null;
38253979 \\ var y = p.*;
3980 \\ _ = y;
38263981 \\}
38273982 , &[_][]const u8{
38283983 "tmp.zig:4:23: error: expected type '*?*i32', found '**i32'",
38293984 });
38303985
3831 cases.add("attempted implicit cast from T to [*]const T",
3986 ctx.objErrStage1("attempted implicit cast from T to [*]const T",
38323987 \\export fn entry() void {
38333988 \\ const x: [*]const bool = true;
3989 \\ _ = x;
38343990 \\}
38353991 , &[_][]const u8{
38363992 "tmp.zig:2:30: error: expected type '[*]const bool', found 'bool'",
38373993 });
38383994
3839 cases.add("dereference unknown length pointer",
3995 ctx.objErrStage1("dereference unknown length pointer",
38403996 \\export fn entry(x: [*]i32) i32 {
38413997 \\ return x.*;
38423998 \\}
......@@ -3844,7 +4000,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
38444000 "tmp.zig:2:13: error: index syntax required for unknown-length pointer type '[*]i32'",
38454001 });
38464002
3847 cases.add("field access of unknown length pointer",
4003 ctx.objErrStage1("field access of unknown length pointer",
38484004 \\const Foo = extern struct {
38494005 \\ a: i32,
38504006 \\};
......@@ -3856,13 +4012,13 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
38564012 "tmp.zig:6:8: error: type '[*]Foo' does not support field access",
38574013 });
38584014
3859 cases.add("unknown length pointer to opaque",
4015 ctx.objErrStage1("unknown length pointer to opaque",
38604016 \\export const T = [*]opaque {};
38614017 , &[_][]const u8{
38624018 "tmp.zig:1:21: error: unknown-length pointer to opaque",
38634019 });
38644020
3865 cases.add("error when evaluating return type",
4021 ctx.objErrStage1("error when evaluating return type",
38664022 \\const Foo = struct {
38674023 \\ map: @as(i32, i32),
38684024 \\
......@@ -3872,20 +4028,22 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
38724028 \\};
38734029 \\export fn entry() void {
38744030 \\ var rule_set = try Foo.init();
4031 \\ _ = rule_set;
38754032 \\}
38764033 , &[_][]const u8{
38774034 "tmp.zig:2:19: error: expected type 'i32', found 'type'",
38784035 });
38794036
3880 cases.add("slicing single-item pointer",
4037 ctx.objErrStage1("slicing single-item pointer",
38814038 \\export fn entry(ptr: *i32) void {
38824039 \\ const slice = ptr[0..2];
4040 \\ _ = slice;
38834041 \\}
38844042 , &[_][]const u8{
38854043 "tmp.zig:2:22: error: slice of single-item pointer",
38864044 });
38874045
3888 cases.add("indexing single-item pointer",
4046 ctx.objErrStage1("indexing single-item pointer",
38894047 \\export fn entry(ptr: *i32) i32 {
38904048 \\ return ptr[1];
38914049 \\}
......@@ -3893,12 +4051,13 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
38934051 "tmp.zig:2:15: error: index of single-item pointer",
38944052 });
38954053
3896 cases.add("nested error set mismatch",
4054 ctx.objErrStage1("nested error set mismatch",
38974055 \\const NextError = error{NextError};
38984056 \\const OtherError = error{OutOfMemory};
38994057 \\
39004058 \\export fn entry() void {
39014059 \\ const a: ?NextError!i32 = foo();
4060 \\ _ = a;
39024061 \\}
39034062 \\
39044063 \\fn foo() ?OtherError!i32 {
......@@ -3911,7 +4070,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
39114070 "tmp.zig:2:26: note: 'error.OutOfMemory' not a member of destination error set",
39124071 });
39134072
3914 cases.add("invalid deref on switch target",
4073 ctx.objErrStage1("invalid deref on switch target",
39154074 \\comptime {
39164075 \\ var tile = Tile.Empty;
39174076 \\ switch (tile.*) {
......@@ -3927,13 +4086,13 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
39274086 "tmp.zig:3:17: error: attempt to dereference non-pointer type 'Tile'",
39284087 });
39294088
3930 cases.add("invalid field access in comptime",
3931 \\comptime { var x = doesnt_exist.whatever; }
4089 ctx.objErrStage1("invalid field access in comptime",
4090 \\comptime { var x = doesnt_exist.whatever; _ = x; }
39324091 , &[_][]const u8{
39334092 "tmp.zig:1:20: error: use of undeclared identifier 'doesnt_exist'",
39344093 });
39354094
3936 cases.add("suspend inside suspend block",
4095 ctx.objErrStage1("suspend inside suspend block",
39374096 \\export fn entry() void {
39384097 \\ _ = async foo();
39394098 \\}
......@@ -3948,17 +4107,18 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
39484107 "tmp.zig:5:5: note: other suspend block here",
39494108 });
39504109
3951 cases.add("assign inline fn to non-comptime var",
4110 ctx.objErrStage1("assign inline fn to non-comptime var",
39524111 \\export fn entry() void {
39534112 \\ var a = b;
4113 \\ _ = a;
39544114 \\}
39554115 \\fn b() callconv(.Inline) void { }
39564116 , &[_][]const u8{
39574117 "tmp.zig:2:5: error: functions marked inline must be stored in const or comptime var",
3958 "tmp.zig:4:1: note: declared here",
4118 "tmp.zig:5:1: note: declared here",
39594119 });
39604120
3961 cases.add("wrong type passed to @panic",
4121 ctx.objErrStage1("wrong type passed to @panic",
39624122 \\export fn entry() void {
39634123 \\ var e = error.Foo;
39644124 \\ @panic(e);
......@@ -3967,7 +4127,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
39674127 "tmp.zig:3:12: error: expected type '[]const u8', found 'error{Foo}'",
39684128 });
39694129
3970 cases.add("@tagName used on union with no associated enum tag",
4130 ctx.objErrStage1("@tagName used on union with no associated enum tag",
39714131 \\const FloatInt = extern union {
39724132 \\ Float: f32,
39734133 \\ Int: i32,
......@@ -3975,13 +4135,14 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
39754135 \\export fn entry() void {
39764136 \\ var fi = FloatInt{.Float = 123.45};
39774137 \\ var tagName = @tagName(fi);
4138 \\ _ = tagName;
39784139 \\}
39794140 , &[_][]const u8{
39804141 "tmp.zig:7:19: error: union has no associated enum",
39814142 "tmp.zig:1:18: note: declared here",
39824143 });
39834144
3984 cases.add("returning error from void async function",
4145 ctx.objErrStage1("returning error from void async function",
39854146 \\export fn entry() void {
39864147 \\ _ = async amain();
39874148 \\}
......@@ -3992,37 +4153,40 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
39924153 "tmp.zig:5:17: error: expected type 'void', found 'error{ShouldBeCompileError}'",
39934154 });
39944155
3995 cases.add("var makes structs required to be comptime known",
4156 ctx.objErrStage1("var makes structs required to be comptime known",
39964157 \\export fn entry() void {
39974158 \\ const S = struct{v: anytype};
39984159 \\ var s = S{.v=@as(i32, 10)};
4160 \\ _ = s;
39994161 \\}
40004162 , &[_][]const u8{
40014163 "tmp.zig:3:4: error: variable of type 'S' must be const or comptime",
40024164 });
40034165
4004 cases.add("@ptrCast discards const qualifier",
4166 ctx.objErrStage1("@ptrCast discards const qualifier",
40054167 \\export fn entry() void {
40064168 \\ const x: i32 = 1234;
40074169 \\ const y = @ptrCast(*i32, &x);
4170 \\ _ = y;
40084171 \\}
40094172 , &[_][]const u8{
40104173 "tmp.zig:3:15: error: cast discards const qualifier",
40114174 });
40124175
4013 cases.add("comptime slice of undefined pointer non-zero len",
4176 ctx.objErrStage1("comptime slice of undefined pointer non-zero len",
40144177 \\export fn entry() void {
40154178 \\ const slice = @as([*]i32, undefined)[0..1];
4179 \\ _ = slice;
40164180 \\}
40174181 , &[_][]const u8{
40184182 "tmp.zig:2:41: error: non-zero length slice of undefined pointer",
40194183 });
40204184
4021 cases.add("type checking function pointers",
4185 ctx.objErrStage1("type checking function pointers",
40224186 \\fn a(b: fn (*const u8) void) void {
40234187 \\ b('a');
40244188 \\}
4025 \\fn c(d: u8) void {}
4189 \\fn c(d: u8) void {_ = d;}
40264190 \\export fn entry() void {
40274191 \\ a(c);
40284192 \\}
......@@ -4030,7 +4194,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
40304194 "tmp.zig:6:7: error: expected type 'fn(*const u8) void', found 'fn(u8) void'",
40314195 });
40324196
4033 cases.add("no else prong on switch on global error set",
4197 ctx.objErrStage1("no else prong on switch on global error set",
40344198 \\export fn entry() void {
40354199 \\ foo(error.A);
40364200 \\}
......@@ -4043,7 +4207,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
40434207 "tmp.zig:5:5: error: else prong required when switching on type 'anyerror'",
40444208 });
40454209
4046 cases.add("error not handled in switch",
4210 ctx.objErrStage1("error not handled in switch",
40474211 \\export fn entry() void {
40484212 \\ foo(452) catch |err| switch (err) {
40494213 \\ error.Foo => {},
......@@ -4062,7 +4226,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
40624226 "tmp.zig:2:26: error: error.Bar not handled in switch",
40634227 });
40644228
4065 cases.add("duplicate error in switch",
4229 ctx.objErrStage1("duplicate error in switch",
40664230 \\export fn entry() void {
40674231 \\ foo(452) catch |err| switch (err) {
40684232 \\ error.Foo => {},
......@@ -4083,7 +4247,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
40834247 "tmp.zig:3:14: note: other value is here",
40844248 });
40854249
4086 cases.add("invalid cast from integral type to enum",
4250 ctx.objErrStage1("invalid cast from integral type to enum",
40874251 \\const E = enum(usize) { One, Two };
40884252 \\
40894253 \\export fn entry() void {
......@@ -4099,7 +4263,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
40994263 "tmp.zig:9:10: error: expected type 'usize', found 'E'",
41004264 });
41014265
4102 cases.add("range operator in switch used on error set",
4266 ctx.objErrStage1("range operator in switch used on error set",
41034267 \\export fn entry() void {
41044268 \\ try foo(452) catch |err| switch (err) {
41054269 \\ error.A ... error.B => {},
......@@ -4117,33 +4281,35 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
41174281 "tmp.zig:3:17: error: operator not allowed for errors",
41184282 });
41194283
4120 cases.add("inferring error set of function pointer",
4284 ctx.objErrStage1("inferring error set of function pointer",
41214285 \\comptime {
41224286 \\ const z: ?fn()!void = null;
41234287 \\}
41244288 , &[_][]const u8{
4125 "tmp.zig:2:15: error: inferring error set of return type valid only for function definitions",
4289 "tmp.zig:2:19: error: function prototype may not have inferred error set",
41264290 });
41274291
4128 cases.add("access non-existent member of error set",
4292 ctx.objErrStage1("access non-existent member of error set",
41294293 \\const Foo = error{A};
41304294 \\comptime {
41314295 \\ const z = Foo.Bar;
4296 \\ _ = z;
41324297 \\}
41334298 , &[_][]const u8{
41344299 "tmp.zig:3:18: error: no error named 'Bar' in 'Foo'",
41354300 });
41364301
4137 cases.add("error union operator with non error set LHS",
4302 ctx.objErrStage1("error union operator with non error set LHS",
41384303 \\comptime {
41394304 \\ const z = i32!i32;
41404305 \\ var x: z = undefined;
4306 \\ _ = x;
41414307 \\}
41424308 , &[_][]const u8{
41434309 "tmp.zig:2:15: error: expected error set type, found type 'i32'",
41444310 });
41454311
4146 cases.add("error equality but sets have no common members",
4312 ctx.objErrStage1("error equality but sets have no common members",
41474313 \\const Set1 = error{A, C};
41484314 \\const Set2 = error{B, D};
41494315 \\export fn entry() void {
......@@ -4158,29 +4324,32 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
41584324 "tmp.zig:7:11: error: error sets 'Set1' and 'Set2' have no common errors",
41594325 });
41604326
4161 cases.add("only equality binary operator allowed for error sets",
4327 ctx.objErrStage1("only equality binary operator allowed for error sets",
41624328 \\comptime {
41634329 \\ const z = error.A > error.B;
4330 \\ _ = z;
41644331 \\}
41654332 , &[_][]const u8{
41664333 "tmp.zig:2:23: error: operator not allowed for errors",
41674334 });
41684335
4169 cases.add("explicit error set cast known at comptime violates error sets",
4336 ctx.objErrStage1("explicit error set cast known at comptime violates error sets",
41704337 \\const Set1 = error {A, B};
41714338 \\const Set2 = error {A, C};
41724339 \\comptime {
41734340 \\ var x = Set1.B;
41744341 \\ var y = @errSetCast(Set2, x);
4342 \\ _ = y;
41754343 \\}
41764344 , &[_][]const u8{
41774345 "tmp.zig:5:13: error: error.B not a member of error set 'Set2'",
41784346 });
41794347
4180 cases.add("cast error union of global error set to error union of smaller error set",
4348 ctx.objErrStage1("cast error union of global error set to error union of smaller error set",
41814349 \\const SmallErrorSet = error{A};
41824350 \\export fn entry() void {
41834351 \\ var x: SmallErrorSet!i32 = foo();
4352 \\ _ = x;
41844353 \\}
41854354 \\fn foo() anyerror!i32 {
41864355 \\ return error.B;
......@@ -4191,10 +4360,11 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
41914360 "tmp.zig:3:35: note: cannot cast global error set into smaller set",
41924361 });
41934362
4194 cases.add("cast global error set to error set",
4363 ctx.objErrStage1("cast global error set to error set",
41954364 \\const SmallErrorSet = error{A};
41964365 \\export fn entry() void {
41974366 \\ var x: SmallErrorSet = foo();
4367 \\ _ = x;
41984368 \\}
41994369 \\fn foo() anyerror {
42004370 \\ return error.B;
......@@ -4203,7 +4373,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
42034373 "tmp.zig:3:31: error: expected type 'SmallErrorSet', found 'anyerror'",
42044374 "tmp.zig:3:31: note: cannot cast global error set into smaller set",
42054375 });
4206 cases.add("recursive inferred error set",
4376 ctx.objErrStage1("recursive inferred error set",
42074377 \\export fn entry() void {
42084378 \\ foo() catch unreachable;
42094379 \\}
......@@ -4214,7 +4384,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
42144384 "tmp.zig:5:5: error: cannot resolve inferred error set '@typeInfo(@typeInfo(@TypeOf(foo)).Fn.return_type.?).ErrorUnion.error_set': function 'foo' not fully analyzed yet",
42154385 });
42164386
4217 cases.add("implicit cast of error set not a subset",
4387 ctx.objErrStage1("implicit cast of error set not a subset",
42184388 \\const Set1 = error{A, B};
42194389 \\const Set2 = error{A, C};
42204390 \\export fn entry() void {
......@@ -4222,13 +4392,14 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
42224392 \\}
42234393 \\fn foo(set1: Set1) void {
42244394 \\ var x: Set2 = set1;
4395 \\ _ = x;
42254396 \\}
42264397 , &[_][]const u8{
42274398 "tmp.zig:7:19: error: expected type 'Set2', found 'Set1'",
42284399 "tmp.zig:1:23: note: 'error.B' not a member of destination error set",
42294400 });
42304401
4231 cases.add("int to err global invalid number",
4402 ctx.objErrStage1("int to err global invalid number",
42324403 \\const Set1 = error{
42334404 \\ A,
42344405 \\ B,
......@@ -4236,12 +4407,13 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
42364407 \\comptime {
42374408 \\ var x: u16 = 3;
42384409 \\ var y = @intToError(x);
4410 \\ _ = y;
42394411 \\}
42404412 , &[_][]const u8{
42414413 "tmp.zig:7:13: error: integer value 3 represents no error",
42424414 });
42434415
4244 cases.add("int to err non global invalid number",
4416 ctx.objErrStage1("int to err non global invalid number",
42454417 \\const Set1 = error{
42464418 \\ A,
42474419 \\ B,
......@@ -4253,68 +4425,74 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
42534425 \\comptime {
42544426 \\ var x = @errorToInt(Set1.B);
42554427 \\ var y = @errSetCast(Set2, @intToError(x));
4428 \\ _ = y;
42564429 \\}
42574430 , &[_][]const u8{
42584431 "tmp.zig:11:13: error: error.B not a member of error set 'Set2'",
42594432 });
42604433
4261 cases.add("duplicate error value in error set",
4434 ctx.objErrStage1("duplicate error value in error set",
42624435 \\const Foo = error {
42634436 \\ Bar,
42644437 \\ Bar,
42654438 \\};
42664439 \\export fn entry() void {
42674440 \\ const a: Foo = undefined;
4441 \\ _ = a;
42684442 \\}
42694443 , &[_][]const u8{
42704444 "tmp.zig:3:5: error: duplicate error: 'Bar'",
42714445 "tmp.zig:2:5: note: other error here",
42724446 });
42734447
4274 cases.add("cast negative integer literal to usize",
4448 ctx.objErrStage1("cast negative integer literal to usize",
42754449 \\export fn entry() void {
42764450 \\ const x = @as(usize, -10);
4451 \\ _ = x;
42774452 \\}
42784453 , &[_][]const u8{
42794454 "tmp.zig:2:26: error: cannot cast negative value -10 to unsigned integer type 'usize'",
42804455 });
42814456
4282 cases.add("use invalid number literal as array index",
4457 ctx.objErrStage1("use invalid number literal as array index",
42834458 \\var v = 25;
42844459 \\export fn entry() void {
42854460 \\ var arr: [v]u8 = undefined;
4461 \\ _ = arr;
42864462 \\}
42874463 , &[_][]const u8{
42884464 "tmp.zig:1:1: error: unable to infer variable type",
42894465 });
42904466
4291 cases.add("duplicate struct field",
4467 ctx.objErrStage1("duplicate struct field",
42924468 \\const Foo = struct {
42934469 \\ Bar: i32,
42944470 \\ Bar: usize,
42954471 \\};
42964472 \\export fn entry() void {
42974473 \\ const a: Foo = undefined;
4474 \\ _ = a;
42984475 \\}
42994476 , &[_][]const u8{
43004477 "tmp.zig:3:5: error: duplicate struct field: 'Bar'",
43014478 "tmp.zig:2:5: note: other field here",
43024479 });
43034480
4304 cases.add("duplicate union field",
4481 ctx.objErrStage1("duplicate union field",
43054482 \\const Foo = union {
43064483 \\ Bar: i32,
43074484 \\ Bar: usize,
43084485 \\};
43094486 \\export fn entry() void {
43104487 \\ const a: Foo = undefined;
4488 \\ _ = a;
43114489 \\}
43124490 , &[_][]const u8{
43134491 "tmp.zig:3:5: error: duplicate union field: 'Bar'",
43144492 "tmp.zig:2:5: note: other field here",
43154493 });
43164494
4317 cases.add("duplicate enum field",
4495 ctx.objErrStage1("duplicate enum field",
43184496 \\const Foo = enum {
43194497 \\ Bar,
43204498 \\ Bar,
......@@ -4322,13 +4500,14 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
43224500 \\
43234501 \\export fn entry() void {
43244502 \\ const a: Foo = undefined;
4503 \\ _ = a;
43254504 \\}
43264505 , &[_][]const u8{
43274506 "tmp.zig:3:5: error: duplicate enum field: 'Bar'",
43284507 "tmp.zig:2:5: note: other field here",
43294508 });
43304509
4331 cases.add("calling function with naked calling convention",
4510 ctx.objErrStage1("calling function with naked calling convention",
43324511 \\export fn entry() void {
43334512 \\ foo();
43344513 \\}
......@@ -4338,42 +4517,42 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
43384517 "tmp.zig:4:1: note: declared here",
43394518 });
43404519
4341 cases.add("function with invalid return type",
4520 ctx.objErrStage1("function with invalid return type",
43424521 \\export fn foo() boid {}
43434522 , &[_][]const u8{
43444523 "tmp.zig:1:17: error: use of undeclared identifier 'boid'",
43454524 });
43464525
4347 cases.add("function with non-extern non-packed enum parameter",
4526 ctx.objErrStage1("function with non-extern non-packed enum parameter",
43484527 \\const Foo = enum { A, B, C };
4349 \\export fn entry(foo: Foo) void { }
4528 \\export fn entry(foo: Foo) void { _ = foo; }
43504529 , &[_][]const u8{
43514530 "tmp.zig:2:22: error: parameter of type 'Foo' not allowed in function with calling convention 'C'",
43524531 });
43534532
4354 cases.add("function with non-extern non-packed struct parameter",
4533 ctx.objErrStage1("function with non-extern non-packed struct parameter",
43554534 \\const Foo = struct {
43564535 \\ A: i32,
43574536 \\ B: f32,
43584537 \\ C: bool,
43594538 \\};
4360 \\export fn entry(foo: Foo) void { }
4539 \\export fn entry(foo: Foo) void { _ = foo; }
43614540 , &[_][]const u8{
43624541 "tmp.zig:6:22: error: parameter of type 'Foo' not allowed in function with calling convention 'C'",
43634542 });
43644543
4365 cases.add("function with non-extern non-packed union parameter",
4544 ctx.objErrStage1("function with non-extern non-packed union parameter",
43664545 \\const Foo = union {
43674546 \\ A: i32,
43684547 \\ B: f32,
43694548 \\ C: bool,
43704549 \\};
4371 \\export fn entry(foo: Foo) void { }
4550 \\export fn entry(foo: Foo) void { _ = foo; }
43724551 , &[_][]const u8{
43734552 "tmp.zig:6:22: error: parameter of type 'Foo' not allowed in function with calling convention 'C'",
43744553 });
43754554
4376 cases.add("switch on enum with 1 field with no prongs",
4555 ctx.objErrStage1("switch on enum with 1 field with no prongs",
43774556 \\const Foo = enum { M };
43784557 \\
43794558 \\export fn entry() void {
......@@ -4384,15 +4563,16 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
43844563 "tmp.zig:5:5: error: enumeration value 'Foo.M' not handled in switch",
43854564 });
43864565
4387 cases.add("shift by negative comptime integer",
4566 ctx.objErrStage1("shift by negative comptime integer",
43884567 \\comptime {
43894568 \\ var a = 1 >> -1;
4569 \\ _ = a;
43904570 \\}
43914571 , &[_][]const u8{
43924572 "tmp.zig:2:18: error: shift by negative value -1",
43934573 });
43944574
4395 cases.add("@panic called at compile time",
4575 ctx.objErrStage1("@panic called at compile time",
43964576 \\export fn entry() void {
43974577 \\ comptime {
43984578 \\ @panic("aoeu",);
......@@ -4402,20 +4582,20 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
44024582 "tmp.zig:3:9: error: encountered @panic at compile-time",
44034583 });
44044584
4405 cases.add("wrong return type for main",
4585 ctx.objErrStage1("wrong return type for main",
44064586 \\pub fn main() f32 { }
44074587 , &[_][]const u8{
44084588 "error: expected return type of main to be 'void', '!void', 'noreturn', 'u8', or '!u8'",
44094589 });
44104590
4411 cases.add("double ?? on main return value",
4591 ctx.objErrStage1("double ?? on main return value",
44124592 \\pub fn main() ??void {
44134593 \\}
44144594 , &[_][]const u8{
44154595 "error: expected return type of main to be 'void', '!void', 'noreturn', 'u8', or '!u8'",
44164596 });
44174597
4418 cases.add("bad identifier in function with struct defined inside function which references local const",
4598 ctx.objErrStage1("bad identifier in function with struct defined inside function which references local const",
44194599 \\export fn entry() void {
44204600 \\ const BlockKind = u32;
44214601 \\
......@@ -4424,12 +4604,14 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
44244604 \\ };
44254605 \\
44264606 \\ bogus;
4607 \\
4608 \\ _ = Block;
44274609 \\}
44284610 , &[_][]const u8{
44294611 "tmp.zig:8:5: error: use of undeclared identifier 'bogus'",
44304612 });
44314613
4432 cases.add("labeled break not found",
4614 ctx.objErrStage1("labeled break not found",
44334615 \\export fn entry() void {
44344616 \\ blah: while (true) {
44354617 \\ while (true) {
......@@ -4438,10 +4620,10 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
44384620 \\ }
44394621 \\}
44404622 , &[_][]const u8{
4441 "tmp.zig:4:13: error: label not found: 'outer'",
4623 "tmp.zig:4:20: error: label not found: 'outer'",
44424624 });
44434625
4444 cases.add("labeled continue not found",
4626 ctx.objErrStage1("labeled continue not found",
44454627 \\export fn entry() void {
44464628 \\ var i: usize = 0;
44474629 \\ blah: while (i < 10) : (i += 1) {
......@@ -4451,17 +4633,17 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
44514633 \\ }
44524634 \\}
44534635 , &[_][]const u8{
4454 "tmp.zig:5:13: error: labeled loop not found: 'outer'",
4636 "tmp.zig:5:23: error: label not found: 'outer'",
44554637 });
44564638
4457 cases.add("attempt to use 0 bit type in extern fn",
4639 ctx.objErrStage1("attempt to use 0 bit type in extern fn",
44584640 \\extern fn foo(ptr: fn(*void) callconv(.C) void) void;
44594641 \\
44604642 \\export fn entry() void {
44614643 \\ foo(bar);
44624644 \\}
44634645 \\
4464 \\fn bar(x: *void) callconv(.C) void { }
4646 \\fn bar(x: *void) callconv(.C) void { _ = x; }
44654647 \\export fn entry2() void {
44664648 \\ bar(&{});
44674649 \\}
......@@ -4470,7 +4652,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
44704652 "tmp.zig:7:11: error: parameter of type '*void' has 0 bits; not allowed in function with calling convention 'C'",
44714653 });
44724654
4473 cases.add("implicit semicolon - block statement",
4655 ctx.objErrStage1("implicit semicolon - block statement",
44744656 \\export fn entry() void {
44754657 \\ {}
44764658 \\ var good = {};
......@@ -4478,10 +4660,10 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
44784660 \\ var bad = {};
44794661 \\}
44804662 , &[_][]const u8{
4481 "tmp.zig:5:5: error: expected token ';', found 'var'",
4663 "tmp.zig:5:5: error: expected ';', found 'var'",
44824664 });
44834665
4484 cases.add("implicit semicolon - block expr",
4666 ctx.objErrStage1("implicit semicolon - block expr",
44854667 \\export fn entry() void {
44864668 \\ _ = {};
44874669 \\ var good = {};
......@@ -4489,10 +4671,10 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
44894671 \\ var bad = {};
44904672 \\}
44914673 , &[_][]const u8{
4492 "tmp.zig:5:5: error: expected token ';', found 'var'",
4674 "tmp.zig:5:5: error: expected ';', found 'var'",
44934675 });
44944676
4495 cases.add("implicit semicolon - comptime statement",
4677 ctx.objErrStage1("implicit semicolon - comptime statement",
44964678 \\export fn entry() void {
44974679 \\ comptime {}
44984680 \\ var good = {};
......@@ -4500,10 +4682,10 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
45004682 \\ var bad = {};
45014683 \\}
45024684 , &[_][]const u8{
4503 "tmp.zig:5:5: error: expected token ';', found 'var'",
4685 "tmp.zig:5:5: error: expected ';', found 'var'",
45044686 });
45054687
4506 cases.add("implicit semicolon - comptime expression",
4688 ctx.objErrStage1("implicit semicolon - comptime expression",
45074689 \\export fn entry() void {
45084690 \\ _ = comptime {};
45094691 \\ var good = {};
......@@ -4511,10 +4693,10 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
45114693 \\ var bad = {};
45124694 \\}
45134695 , &[_][]const u8{
4514 "tmp.zig:5:5: error: expected token ';', found 'var'",
4696 "tmp.zig:5:5: error: expected ';', found 'var'",
45154697 });
45164698
4517 cases.add("implicit semicolon - defer",
4699 ctx.objErrStage1("implicit semicolon - defer",
45184700 \\export fn entry() void {
45194701 \\ defer {}
45204702 \\ var good = {};
......@@ -4522,10 +4704,10 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
45224704 \\ var bad = {};
45234705 \\}
45244706 , &[_][]const u8{
4525 "tmp.zig:5:5: error: expected token ';', found 'var'",
4707 "tmp.zig:5:5: error: expected ';', found 'var'",
45264708 });
45274709
4528 cases.add("implicit semicolon - if statement",
4710 ctx.objErrStage1("implicit semicolon - if statement",
45294711 \\export fn entry() void {
45304712 \\ if(true) {}
45314713 \\ var good = {};
......@@ -4533,10 +4715,10 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
45334715 \\ var bad = {};
45344716 \\}
45354717 , &[_][]const u8{
4536 "tmp.zig:5:5: error: expected token ';', found 'var'",
4718 "tmp.zig:5:5: error: expected ';' or 'else', found 'var'",
45374719 });
45384720
4539 cases.add("implicit semicolon - if expression",
4721 ctx.objErrStage1("implicit semicolon - if expression",
45404722 \\export fn entry() void {
45414723 \\ _ = if(true) {};
45424724 \\ var good = {};
......@@ -4544,10 +4726,10 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
45444726 \\ var bad = {};
45454727 \\}
45464728 , &[_][]const u8{
4547 "tmp.zig:5:5: error: expected token ';', found 'var'",
4729 "tmp.zig:5:5: error: expected ';', found 'var'",
45484730 });
45494731
4550 cases.add("implicit semicolon - if-else statement",
4732 ctx.objErrStage1("implicit semicolon - if-else statement",
45514733 \\export fn entry() void {
45524734 \\ if(true) {} else {}
45534735 \\ var good = {};
......@@ -4555,10 +4737,10 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
45554737 \\ var bad = {};
45564738 \\}
45574739 , &[_][]const u8{
4558 "tmp.zig:5:5: error: expected token ';', found 'var'",
4740 "tmp.zig:5:5: error: expected ';', found 'var'",
45594741 });
45604742
4561 cases.add("implicit semicolon - if-else expression",
4743 ctx.objErrStage1("implicit semicolon - if-else expression",
45624744 \\export fn entry() void {
45634745 \\ _ = if(true) {} else {};
45644746 \\ var good = {};
......@@ -4566,10 +4748,10 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
45664748 \\ var bad = {};
45674749 \\}
45684750 , &[_][]const u8{
4569 "tmp.zig:5:5: error: expected token ';', found 'var'",
4751 "tmp.zig:5:5: error: expected ';', found 'var'",
45704752 });
45714753
4572 cases.add("implicit semicolon - if-else-if statement",
4754 ctx.objErrStage1("implicit semicolon - if-else-if statement",
45734755 \\export fn entry() void {
45744756 \\ if(true) {} else if(true) {}
45754757 \\ var good = {};
......@@ -4577,10 +4759,10 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
45774759 \\ var bad = {};
45784760 \\}
45794761 , &[_][]const u8{
4580 "tmp.zig:5:5: error: expected token ';', found 'var'",
4762 "tmp.zig:5:5: error: expected ';' or 'else', found 'var'",
45814763 });
45824764
4583 cases.add("implicit semicolon - if-else-if expression",
4765 ctx.objErrStage1("implicit semicolon - if-else-if expression",
45844766 \\export fn entry() void {
45854767 \\ _ = if(true) {} else if(true) {};
45864768 \\ var good = {};
......@@ -4588,10 +4770,10 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
45884770 \\ var bad = {};
45894771 \\}
45904772 , &[_][]const u8{
4591 "tmp.zig:5:5: error: expected token ';', found 'var'",
4773 "tmp.zig:5:5: error: expected ';', found 'var'",
45924774 });
45934775
4594 cases.add("implicit semicolon - if-else-if-else statement",
4776 ctx.objErrStage1("implicit semicolon - if-else-if-else statement",
45954777 \\export fn entry() void {
45964778 \\ if(true) {} else if(true) {} else {}
45974779 \\ var good = {};
......@@ -4599,10 +4781,10 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
45994781 \\ var bad = {};
46004782 \\}
46014783 , &[_][]const u8{
4602 "tmp.zig:5:5: error: expected token ';', found 'var'",
4784 "tmp.zig:5:5: error: expected ';', found 'var'",
46034785 });
46044786
4605 cases.add("implicit semicolon - if-else-if-else expression",
4787 ctx.objErrStage1("implicit semicolon - if-else-if-else expression",
46064788 \\export fn entry() void {
46074789 \\ _ = if(true) {} else if(true) {} else {};
46084790 \\ var good = {};
......@@ -4610,10 +4792,10 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
46104792 \\ var bad = {};
46114793 \\}
46124794 , &[_][]const u8{
4613 "tmp.zig:5:5: error: expected token ';', found 'var'",
4795 "tmp.zig:5:5: error: expected ';', found 'var'",
46144796 });
46154797
4616 cases.add("implicit semicolon - test statement",
4798 ctx.objErrStage1("implicit semicolon - test statement",
46174799 \\export fn entry() void {
46184800 \\ if (foo()) |_| {}
46194801 \\ var good = {};
......@@ -4621,10 +4803,10 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
46214803 \\ var bad = {};
46224804 \\}
46234805 , &[_][]const u8{
4624 "tmp.zig:5:5: error: expected token ';', found 'var'",
4806 "tmp.zig:5:5: error: expected ';' or 'else', found 'var'",
46254807 });
46264808
4627 cases.add("implicit semicolon - test expression",
4809 ctx.objErrStage1("implicit semicolon - test expression",
46284810 \\export fn entry() void {
46294811 \\ _ = if (foo()) |_| {};
46304812 \\ var good = {};
......@@ -4632,10 +4814,10 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
46324814 \\ var bad = {};
46334815 \\}
46344816 , &[_][]const u8{
4635 "tmp.zig:5:5: error: expected token ';', found 'var'",
4817 "tmp.zig:5:5: error: expected ';', found 'var'",
46364818 });
46374819
4638 cases.add("implicit semicolon - while statement",
4820 ctx.objErrStage1("implicit semicolon - while statement",
46394821 \\export fn entry() void {
46404822 \\ while(true) {}
46414823 \\ var good = {};
......@@ -4643,10 +4825,10 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
46434825 \\ var bad = {};
46444826 \\}
46454827 , &[_][]const u8{
4646 "tmp.zig:5:5: error: expected token ';', found 'var'",
4828 "tmp.zig:5:5: error: expected ';' or 'else', found 'var'",
46474829 });
46484830
4649 cases.add("implicit semicolon - while expression",
4831 ctx.objErrStage1("implicit semicolon - while expression",
46504832 \\export fn entry() void {
46514833 \\ _ = while(true) {};
46524834 \\ var good = {};
......@@ -4654,10 +4836,10 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
46544836 \\ var bad = {};
46554837 \\}
46564838 , &[_][]const u8{
4657 "tmp.zig:5:5: error: expected token ';', found 'var'",
4839 "tmp.zig:5:5: error: expected ';', found 'var'",
46584840 });
46594841
4660 cases.add("implicit semicolon - while-continue statement",
4842 ctx.objErrStage1("implicit semicolon - while-continue statement",
46614843 \\export fn entry() void {
46624844 \\ while(true):({}) {}
46634845 \\ var good = {};
......@@ -4665,10 +4847,10 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
46654847 \\ var bad = {};
46664848 \\}
46674849 , &[_][]const u8{
4668 "tmp.zig:5:5: error: expected token ';', found 'var'",
4850 "tmp.zig:5:5: error: expected ';' or 'else', found 'var'",
46694851 });
46704852
4671 cases.add("implicit semicolon - while-continue expression",
4853 ctx.objErrStage1("implicit semicolon - while-continue expression",
46724854 \\export fn entry() void {
46734855 \\ _ = while(true):({}) {};
46744856 \\ var good = {};
......@@ -4676,10 +4858,10 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
46764858 \\ var bad = {};
46774859 \\}
46784860 , &[_][]const u8{
4679 "tmp.zig:5:5: error: expected token ';', found 'var'",
4861 "tmp.zig:5:5: error: expected ';', found 'var'",
46804862 });
46814863
4682 cases.add("implicit semicolon - for statement",
4864 ctx.objErrStage1("implicit semicolon - for statement",
46834865 \\export fn entry() void {
46844866 \\ for(foo()) |_| {}
46854867 \\ var good = {};
......@@ -4687,10 +4869,10 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
46874869 \\ var bad = {};
46884870 \\}
46894871 , &[_][]const u8{
4690 "tmp.zig:5:5: error: expected token ';', found 'var'",
4872 "tmp.zig:5:5: error: expected ';' or 'else', found 'var'",
46914873 });
46924874
4693 cases.add("implicit semicolon - for expression",
4875 ctx.objErrStage1("implicit semicolon - for expression",
46944876 \\export fn entry() void {
46954877 \\ _ = for(foo()) |_| {};
46964878 \\ var good = {};
......@@ -4698,32 +4880,33 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
46984880 \\ var bad = {};
46994881 \\}
47004882 , &[_][]const u8{
4701 "tmp.zig:5:5: error: expected token ';', found 'var'",
4883 "tmp.zig:5:5: error: expected ';', found 'var'",
47024884 });
47034885
4704 cases.add("multiple function definitions",
4886 ctx.objErrStage1("multiple function definitions",
47054887 \\fn a() void {}
47064888 \\fn a() void {}
47074889 \\export fn entry() void { a(); }
47084890 , &[_][]const u8{
4709 "tmp.zig:2:1: error: redefinition of 'a'",
4891 "tmp.zig:2:1: error: redeclaration of 'a'",
4892 "tmp.zig:1:1: error: other declaration here",
47104893 });
47114894
4712 cases.add("unreachable with return",
4895 ctx.objErrStage1("unreachable with return",
47134896 \\fn a() noreturn {return;}
47144897 \\export fn entry() void { a(); }
47154898 , &[_][]const u8{
47164899 "tmp.zig:1:18: error: expected type 'noreturn', found 'void'",
47174900 });
47184901
4719 cases.add("control reaches end of non-void function",
4902 ctx.objErrStage1("control reaches end of non-void function",
47204903 \\fn a() i32 {}
47214904 \\export fn entry() void { _ = a(); }
47224905 , &[_][]const u8{
47234906 "tmp.zig:1:12: error: expected type 'i32', found 'void'",
47244907 });
47254908
4726 cases.add("undefined function call",
4909 ctx.objErrStage1("undefined function call",
47274910 \\export fn a() void {
47284911 \\ b();
47294912 \\}
......@@ -4731,30 +4914,30 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
47314914 "tmp.zig:2:5: error: use of undeclared identifier 'b'",
47324915 });
47334916
4734 cases.add("wrong number of arguments",
4917 ctx.objErrStage1("wrong number of arguments",
47354918 \\export fn a() void {
47364919 \\ b(1);
47374920 \\}
4738 \\fn b(a: i32, b: i32, c: i32) void { }
4921 \\fn b(a: i32, b: i32, c: i32) void { _ = a; _ = b; _ = c; }
47394922 , &[_][]const u8{
47404923 "tmp.zig:2:6: error: expected 3 argument(s), found 1",
47414924 });
47424925
4743 cases.add("invalid type",
4926 ctx.objErrStage1("invalid type",
47444927 \\fn a() bogus {}
47454928 \\export fn entry() void { _ = a(); }
47464929 , &[_][]const u8{
47474930 "tmp.zig:1:8: error: use of undeclared identifier 'bogus'",
47484931 });
47494932
4750 cases.add("pointer to noreturn",
4933 ctx.objErrStage1("pointer to noreturn",
47514934 \\fn a() *noreturn {}
47524935 \\export fn entry() void { _ = a(); }
47534936 , &[_][]const u8{
47544937 "tmp.zig:1:9: error: pointer to noreturn not allowed",
47554938 });
47564939
4757 cases.add("unreachable code",
4940 ctx.objErrStage1("unreachable code",
47584941 \\export fn a() void {
47594942 \\ return;
47604943 \\ b();
......@@ -4766,14 +4949,14 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
47664949 "tmp.zig:2:5: note: control flow is diverted here",
47674950 });
47684951
4769 cases.add("bad import",
4952 ctx.objErrStage1("bad import",
47704953 \\const bogus = @import("bogus-does-not-exist.zig",);
47714954 \\export fn entry() void { bogus.bogo(); }
47724955 , &[_][]const u8{
47734956 "tmp.zig:1:15: error: unable to find 'bogus-does-not-exist.zig'",
47744957 });
47754958
4776 cases.add("undeclared identifier",
4959 ctx.objErrStage1("undeclared identifier",
47774960 \\export fn a() void {
47784961 \\ return
47794962 \\ b +
......@@ -4783,33 +4966,36 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
47834966 "tmp.zig:3:5: error: use of undeclared identifier 'b'",
47844967 });
47854968
4786 cases.add("parameter redeclaration",
4969 ctx.objErrStage1("parameter redeclaration",
47874970 \\fn f(a : i32, a : i32) void {
47884971 \\}
47894972 \\export fn entry() void { f(1, 2); }
47904973 , &[_][]const u8{
4791 "tmp.zig:1:15: error: redeclaration of variable 'a'",
4974 "tmp.zig:1:15: error: redeclaration of parameter 'a'",
4975 "tmp.zig:1:6: note: previous declaration here",
47924976 });
47934977
4794 cases.add("local variable redeclaration",
4978 ctx.objErrStage1("local variable redeclaration",
47954979 \\export fn f() void {
47964980 \\ const a : i32 = 0;
4797 \\ const a = 0;
4981 \\ var a = 0;
47984982 \\}
47994983 , &[_][]const u8{
4800 "tmp.zig:3:5: error: redeclaration of variable 'a'",
4984 "tmp.zig:3:9: error: redeclaration of local const 'a'",
4985 "tmp.zig:2:11: note: previous declaration here",
48014986 });
48024987
4803 cases.add("local variable redeclares parameter",
4988 ctx.objErrStage1("local variable redeclares parameter",
48044989 \\fn f(a : i32) void {
48054990 \\ const a = 0;
48064991 \\}
48074992 \\export fn entry() void { f(1); }
48084993 , &[_][]const u8{
4809 "tmp.zig:2:5: error: redeclaration of variable 'a'",
4994 "tmp.zig:2:11: error: redeclaration of parameter 'a'",
4995 "tmp.zig:1:6: note: previous declaration here",
48104996 });
48114997
4812 cases.add("variable has wrong type",
4998 ctx.objErrStage1("variable has wrong type",
48134999 \\export fn f() i32 {
48145000 \\ const a = "a";
48155001 \\ return a;
......@@ -4818,7 +5004,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
48185004 "tmp.zig:3:12: error: expected type 'i32', found '*const [1:0]u8'",
48195005 });
48205006
4821 cases.add("if condition is bool, not int",
5007 ctx.objErrStage1("if condition is bool, not int",
48225008 \\export fn f() void {
48235009 \\ if (0) {}
48245010 \\}
......@@ -4826,7 +5012,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
48265012 "tmp.zig:2:9: error: expected type 'bool', found 'comptime_int'",
48275013 });
48285014
4829 cases.add("assign unreachable",
5015 ctx.objErrStage1("assign unreachable",
48305016 \\export fn f() void {
48315017 \\ const a = return;
48325018 \\}
......@@ -4835,22 +5021,23 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
48355021 "tmp.zig:2:15: note: control flow is diverted here",
48365022 });
48375023
4838 cases.add("unreachable variable",
5024 ctx.objErrStage1("unreachable variable",
48395025 \\export fn f() void {
48405026 \\ const a: noreturn = {};
5027 \\ _ = a;
48415028 \\}
48425029 , &[_][]const u8{
48435030 "tmp.zig:2:25: error: expected type 'noreturn', found 'void'",
48445031 });
48455032
4846 cases.add("unreachable parameter",
4847 \\fn f(a: noreturn) void {}
5033 ctx.objErrStage1("unreachable parameter",
5034 \\fn f(a: noreturn) void { _ = a; }
48485035 \\export fn entry() void { f(); }
48495036 , &[_][]const u8{
48505037 "tmp.zig:1:9: error: parameter of type 'noreturn' not allowed",
48515038 });
48525039
4853 cases.add("assign to constant variable",
5040 ctx.objErrStage1("assign to constant variable",
48545041 \\export fn f() void {
48555042 \\ const a = 3;
48565043 \\ a = 4;
......@@ -4859,7 +5046,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
48595046 "tmp.zig:3:9: error: cannot assign to constant",
48605047 });
48615048
4862 cases.add("use of undeclared identifier",
5049 ctx.objErrStage1("use of undeclared identifier",
48635050 \\export fn f() void {
48645051 \\ b = 3;
48655052 \\}
......@@ -4867,15 +5054,15 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
48675054 "tmp.zig:2:5: error: use of undeclared identifier 'b'",
48685055 });
48695056
4870 cases.add("const is a statement, not an expression",
5057 ctx.objErrStage1("const is a statement, not an expression",
48715058 \\export fn f() void {
48725059 \\ (const a = 0);
48735060 \\}
48745061 , &[_][]const u8{
4875 "tmp.zig:2:6: error: invalid token: 'const'",
5062 "tmp.zig:2:6: error: expected expression, found 'const'",
48765063 });
48775064
4878 cases.add("array access of undeclared identifier",
5065 ctx.objErrStage1("array access of undeclared identifier",
48795066 \\export fn f() void {
48805067 \\ i[i] = i[i];
48815068 \\}
......@@ -4883,7 +5070,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
48835070 "tmp.zig:2:5: error: use of undeclared identifier 'i'",
48845071 });
48855072
4886 cases.add("array access of non array",
5073 ctx.objErrStage1("array access of non array",
48875074 \\export fn f() void {
48885075 \\ var bad : bool = undefined;
48895076 \\ bad[0] = bad[0];
......@@ -4897,7 +5084,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
48975084 "tmp.zig:7:12: error: array access of non-array type 'bool'",
48985085 });
48995086
4900 cases.add("array access with non integer index",
5087 ctx.objErrStage1("array access with non integer index",
49015088 \\export fn f() void {
49025089 \\ var array = "aoeu";
49035090 \\ var bad = false;
......@@ -4913,7 +5100,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
49135100 "tmp.zig:9:15: error: expected type 'usize', found 'bool'",
49145101 });
49155102
4916 cases.add("write to const global variable",
5103 ctx.objErrStage1("write to const global variable",
49175104 \\const x : i32 = 99;
49185105 \\fn f() void {
49195106 \\ x = 1;
......@@ -4923,58 +5110,64 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
49235110 "tmp.zig:3:9: error: cannot assign to constant",
49245111 });
49255112
4926 cases.add("missing else clause",
5113 ctx.objErrStage1("missing else clause",
49275114 \\fn f(b: bool) void {
49285115 \\ const x : i32 = if (b) h: { break :h 1; };
5116 \\ _ = x;
49295117 \\}
49305118 \\fn g(b: bool) void {
49315119 \\ const y = if (b) h: { break :h @as(i32, 1); };
5120 \\ _ = y;
49325121 \\}
49335122 \\export fn entry() void { f(true); g(true); }
49345123 , &[_][]const u8{
49355124 "tmp.zig:2:21: error: expected type 'i32', found 'void'",
4936 "tmp.zig:5:15: error: incompatible types: 'i32' and 'void'",
5125 "tmp.zig:6:15: error: incompatible types: 'i32' and 'void'",
49375126 });
49385127
4939 cases.add("invalid struct field",
5128 ctx.objErrStage1("invalid struct field",
49405129 \\const A = struct { x : i32, };
49415130 \\export fn f() void {
49425131 \\ var a : A = undefined;
49435132 \\ a.foo = 1;
49445133 \\ const y = a.bar;
5134 \\ _ = y;
49455135 \\}
49465136 \\export fn g() void {
49475137 \\ var a : A = undefined;
49485138 \\ const y = a.bar;
5139 \\ _ = y;
49495140 \\}
49505141 , &[_][]const u8{
49515142 "tmp.zig:4:6: error: no member named 'foo' in struct 'A'",
4952 "tmp.zig:9:16: error: no member named 'bar' in struct 'A'",
5143 "tmp.zig:10:16: error: no member named 'bar' in struct 'A'",
49535144 });
49545145
4955 cases.add("redefinition of struct",
5146 ctx.objErrStage1("redefinition of struct",
49565147 \\const A = struct { x : i32, };
49575148 \\const A = struct { y : i32, };
49585149 , &[_][]const u8{
4959 "tmp.zig:2:1: error: redefinition of 'A'",
5150 "tmp.zig:2:1: error: redeclaration of 'A'",
5151 "tmp.zig:1:1: note: other declaration here",
49605152 });
49615153
4962 cases.add("redefinition of enums",
4963 \\const A = enum {};
4964 \\const A = enum {};
5154 ctx.objErrStage1("redefinition of enums",
5155 \\const A = enum {x};
5156 \\const A = enum {x};
49655157 , &[_][]const u8{
4966 "tmp.zig:2:1: error: redefinition of 'A'",
5158 "tmp.zig:2:1: error: redeclaration of 'A'",
5159 "tmp.zig:1:1: note: other declaration here",
49675160 });
49685161
4969 cases.add("redefinition of global variables",
5162 ctx.objErrStage1("redefinition of global variables",
49705163 \\var a : i32 = 1;
49715164 \\var a : i32 = 2;
49725165 , &[_][]const u8{
4973 "tmp.zig:2:1: error: redefinition of 'a'",
4974 "tmp.zig:1:1: note: previous definition is here",
5166 "tmp.zig:2:1: error: redeclaration of 'a'",
5167 "tmp.zig:1:1: note: other declaration here",
49755168 });
49765169
4977 cases.add("duplicate field in struct value expression",
5170 ctx.objErrStage1("duplicate field in struct value expression",
49785171 \\const A = struct {
49795172 \\ x : i32,
49805173 \\ y : i32,
......@@ -4987,12 +5180,13 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
49875180 \\ .x = 3,
49885181 \\ .z = 4,
49895182 \\ };
5183 \\ _ = a;
49905184 \\}
49915185 , &[_][]const u8{
49925186 "tmp.zig:11:9: error: duplicate field",
49935187 });
49945188
4995 cases.add("missing field in struct value expression",
5189 ctx.objErrStage1("missing field in struct value expression",
49965190 \\const A = struct {
49975191 \\ x : i32,
49985192 \\ y : i32,
......@@ -5010,7 +5204,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
50105204 "tmp.zig:9:17: error: missing field: 'x'",
50115205 });
50125206
5013 cases.add("invalid field in struct value expression",
5207 ctx.objErrStage1("invalid field in struct value expression",
50145208 \\const A = struct {
50155209 \\ x : i32,
50165210 \\ y : i32,
......@@ -5022,12 +5216,13 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
50225216 \\ .y = 2,
50235217 \\ .foo = 42,
50245218 \\ };
5219 \\ _ = a;
50255220 \\}
50265221 , &[_][]const u8{
50275222 "tmp.zig:10:9: error: no member named 'foo' in struct 'A'",
50285223 });
50295224
5030 cases.add("invalid break expression",
5225 ctx.objErrStage1("invalid break expression",
50315226 \\export fn f() void {
50325227 \\ break;
50335228 \\}
......@@ -5035,7 +5230,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
50355230 "tmp.zig:2:5: error: break expression outside loop",
50365231 });
50375232
5038 cases.add("invalid continue expression",
5233 ctx.objErrStage1("invalid continue expression",
50395234 \\export fn f() void {
50405235 \\ continue;
50415236 \\}
......@@ -5043,15 +5238,15 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
50435238 "tmp.zig:2:5: error: continue expression outside loop",
50445239 });
50455240
5046 cases.add("invalid maybe type",
5241 ctx.objErrStage1("invalid maybe type",
50475242 \\export fn f() void {
5048 \\ if (true) |x| { }
5243 \\ if (true) |x| { _ = x; }
50495244 \\}
50505245 , &[_][]const u8{
50515246 "tmp.zig:2:9: error: expected optional type, found 'bool'",
50525247 });
50535248
5054 cases.add("cast unreachable",
5249 ctx.objErrStage1("cast unreachable",
50555250 \\fn f() i32 {
50565251 \\ return @as(i32, return 1);
50575252 \\}
......@@ -5061,22 +5256,22 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
50615256 "tmp.zig:2:21: note: control flow is diverted here",
50625257 });
50635258
5064 cases.add("invalid builtin fn",
5259 ctx.objErrStage1("invalid builtin fn",
50655260 \\fn f() @bogus(foo) {
50665261 \\}
50675262 \\export fn entry() void { _ = f(); }
50685263 , &[_][]const u8{
5069 "tmp.zig:1:8: error: invalid builtin function: 'bogus'",
5264 "tmp.zig:1:8: error: invalid builtin function: '@bogus'",
50705265 });
50715266
5072 cases.add("noalias on non pointer param",
5073 \\fn f(noalias x: i32) void {}
5267 ctx.objErrStage1("noalias on non pointer param",
5268 \\fn f(noalias x: i32) void { _ = x; }
50745269 \\export fn entry() void { f(1234); }
50755270 , &[_][]const u8{
50765271 "tmp.zig:1:6: error: noalias on non-pointer parameter",
50775272 });
50785273
5079 cases.add("struct init syntax for array",
5274 ctx.objErrStage1("struct init syntax for array",
50805275 \\const foo = [3]u16{ .x = 1024 };
50815276 \\comptime {
50825277 \\ _ = foo;
......@@ -5085,7 +5280,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
50855280 "tmp.zig:1:21: error: type '[3]u16' does not support struct initialization syntax",
50865281 });
50875282
5088 cases.add("type variables must be constant",
5283 ctx.objErrStage1("type variables must be constant",
50895284 \\var foo = u8;
50905285 \\export fn entry() foo {
50915286 \\ return 1;
......@@ -5094,12 +5289,13 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
50945289 "tmp.zig:1:1: error: variable of type 'type' must be constant",
50955290 });
50965291
5097 cases.add("variables shadowing types",
5292 ctx.objErrStage1("variables shadowing types",
50985293 \\const Foo = struct {};
50995294 \\const Bar = struct {};
51005295 \\
51015296 \\fn f(Foo: i32) void {
51025297 \\ var Bar : i32 = undefined;
5298 \\ _ = Bar;
51035299 \\}
51045300 \\
51055301 \\export fn entry() void {
......@@ -5112,7 +5308,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
51125308 "tmp.zig:2:1: note: previous definition is here",
51135309 });
51145310
5115 cases.add("switch expression - missing enumeration prong",
5311 ctx.objErrStage1("switch expression - missing enumeration prong",
51165312 \\const Number = enum {
51175313 \\ One,
51185314 \\ Two,
......@@ -5132,7 +5328,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
51325328 "tmp.zig:8:5: error: enumeration value 'Number.Four' not handled in switch",
51335329 });
51345330
5135 cases.add("switch expression - duplicate enumeration prong",
5331 ctx.objErrStage1("switch expression - duplicate enumeration prong",
51365332 \\const Number = enum {
51375333 \\ One,
51385334 \\ Two,
......@@ -5155,7 +5351,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
51555351 "tmp.zig:10:15: note: other value is here",
51565352 });
51575353
5158 cases.add("switch expression - duplicate enumeration prong when else present",
5354 ctx.objErrStage1("switch expression - duplicate enumeration prong when else present",
51595355 \\const Number = enum {
51605356 \\ One,
51615357 \\ Two,
......@@ -5179,7 +5375,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
51795375 "tmp.zig:10:15: note: other value is here",
51805376 });
51815377
5182 cases.add("switch expression - multiple else prongs",
5378 ctx.objErrStage1("switch expression - multiple else prongs",
51835379 \\fn f(x: u32) void {
51845380 \\ const value: bool = switch (x) {
51855381 \\ 1234 => false,
......@@ -5194,7 +5390,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
51945390 "tmp.zig:5:9: error: multiple else prongs in switch expression",
51955391 });
51965392
5197 cases.add("switch expression - non exhaustive integer prongs",
5393 ctx.objErrStage1("switch expression - non exhaustive integer prongs",
51985394 \\fn foo(x: u8) void {
51995395 \\ switch (x) {
52005396 \\ 0 => {},
......@@ -5205,7 +5401,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
52055401 "tmp.zig:2:5: error: switch must handle all possibilities",
52065402 });
52075403
5208 cases.add("switch expression - duplicate or overlapping integer value",
5404 ctx.objErrStage1("switch expression - duplicate or overlapping integer value",
52095405 \\fn foo(x: u8) u8 {
52105406 \\ return switch (x) {
52115407 \\ 0 ... 100 => @as(u8, 0),
......@@ -5220,8 +5416,9 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
52205416 "tmp.zig:5:14: note: previous value is here",
52215417 });
52225418
5223 cases.add("switch expression - duplicate type",
5419 ctx.objErrStage1("switch expression - duplicate type",
52245420 \\fn foo(comptime T: type, x: T) u8 {
5421 \\ _ = x;
52255422 \\ return switch (T) {
52265423 \\ u32 => 0,
52275424 \\ u64 => 1,
......@@ -5231,16 +5428,17 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
52315428 \\}
52325429 \\export fn entry() usize { return @sizeOf(@TypeOf(foo(u32, 0))); }
52335430 , &[_][]const u8{
5234 "tmp.zig:5:9: error: duplicate switch value",
5235 "tmp.zig:3:9: note: previous value is here",
5431 "tmp.zig:6:9: error: duplicate switch value",
5432 "tmp.zig:4:9: note: previous value is here",
52365433 });
52375434
5238 cases.add("switch expression - duplicate type (struct alias)",
5435 ctx.objErrStage1("switch expression - duplicate type (struct alias)",
52395436 \\const Test = struct {
52405437 \\ bar: i32,
52415438 \\};
52425439 \\const Test2 = Test;
52435440 \\fn foo(comptime T: type, x: T) u8 {
5441 \\ _ = x;
52445442 \\ return switch (T) {
52455443 \\ Test => 0,
52465444 \\ u64 => 1,
......@@ -5250,11 +5448,11 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
52505448 \\}
52515449 \\export fn entry() usize { return @sizeOf(@TypeOf(foo(u32, 0))); }
52525450 , &[_][]const u8{
5253 "tmp.zig:9:9: error: duplicate switch value",
5254 "tmp.zig:7:9: note: previous value is here",
5451 "tmp.zig:10:9: error: duplicate switch value",
5452 "tmp.zig:8:9: note: previous value is here",
52555453 });
52565454
5257 cases.add("switch expression - switch on pointer type with no else",
5455 ctx.objErrStage1("switch expression - switch on pointer type with no else",
52585456 \\fn foo(x: *u8) void {
52595457 \\ switch (x) {
52605458 \\ &y => {},
......@@ -5266,7 +5464,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
52665464 "tmp.zig:2:5: error: else prong required when switching on type '*u8'",
52675465 });
52685466
5269 cases.add("global variable initializer must be constant expression",
5467 ctx.objErrStage1("global variable initializer must be constant expression",
52705468 \\extern fn foo() i32;
52715469 \\const x = foo();
52725470 \\export fn entry() i32 { return x; }
......@@ -5274,7 +5472,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
52745472 "tmp.zig:2:11: error: unable to evaluate constant expression",
52755473 });
52765474
5277 cases.add("array concatenation with wrong type",
5475 ctx.objErrStage1("array concatenation with wrong type",
52785476 \\const src = "aoeu";
52795477 \\const derp: usize = 1234;
52805478 \\const a = derp ++ "foo";
......@@ -5284,7 +5482,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
52845482 "tmp.zig:3:11: error: expected array, found 'usize'",
52855483 });
52865484
5287 cases.add("non compile time array concatenation",
5485 ctx.objErrStage1("non compile time array concatenation",
52885486 \\fn f() []u8 {
52895487 \\ return s ++ "foo";
52905488 \\}
......@@ -5294,7 +5492,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
52945492 "tmp.zig:2:12: error: unable to evaluate constant expression",
52955493 });
52965494
5297 cases.add("@cImport with bogus include",
5495 ctx.objErrStage1("@cImport with bogus include",
52985496 \\const c = @cImport(@cInclude("bogus.h"));
52995497 \\export fn entry() usize { return @sizeOf(@TypeOf(c.bogo)); }
53005498 , &[_][]const u8{
......@@ -5302,7 +5500,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
53025500 ".h:1:10: note: 'bogus.h' file not found",
53035501 });
53045502
5305 cases.add("address of number literal",
5503 ctx.objErrStage1("address of number literal",
53065504 \\const x = 3;
53075505 \\const y = &x;
53085506 \\fn foo() *const i32 { return y; }
......@@ -5311,14 +5509,14 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
53115509 "tmp.zig:3:30: error: expected type '*const i32', found '*const comptime_int'",
53125510 });
53135511
5314 cases.add("integer overflow error",
5512 ctx.objErrStage1("integer overflow error",
53155513 \\const x : u8 = 300;
53165514 \\export fn entry() usize { return @sizeOf(@TypeOf(x)); }
53175515 , &[_][]const u8{
53185516 "tmp.zig:1:16: error: integer value 300 cannot be coerced to type 'u8'",
53195517 });
53205518
5321 cases.add("invalid shift amount error",
5519 ctx.objErrStage1("invalid shift amount error",
53225520 \\const x : u8 = 2;
53235521 \\fn f() u16 {
53245522 \\ return x << 8;
......@@ -5328,7 +5526,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
53285526 "tmp.zig:3:17: error: integer value 8 cannot be coerced to type 'u3'",
53295527 });
53305528
5331 cases.add("missing function call param",
5529 ctx.objErrStage1("missing function call param",
53325530 \\const Foo = struct {
53335531 \\ a: i32,
53345532 \\ b: i32,
......@@ -5349,6 +5547,8 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
53495547 \\
53505548 \\fn f(foo: *const Foo, index: usize) void {
53515549 \\ const result = members[index]();
5550 \\ _ = foo;
5551 \\ _ = result;
53525552 \\}
53535553 \\
53545554 \\export fn entry() usize { return @sizeOf(@TypeOf(f)); }
......@@ -5356,21 +5556,21 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
53565556 "tmp.zig:20:34: error: expected 1 argument(s), found 0",
53575557 });
53585558
5359 cases.add("missing function name",
5559 ctx.objErrStage1("missing function name",
53605560 \\fn () void {}
53615561 \\export fn entry() usize { return @sizeOf(@TypeOf(f)); }
53625562 , &[_][]const u8{
53635563 "tmp.zig:1:1: error: missing function name",
53645564 });
53655565
5366 cases.add("missing param name",
5566 ctx.objErrStage1("missing param name",
53675567 \\fn f(i32) void {}
53685568 \\export fn entry() usize { return @sizeOf(@TypeOf(f)); }
53695569 , &[_][]const u8{
53705570 "tmp.zig:1:6: error: missing parameter name",
53715571 });
53725572
5373 cases.add("wrong function type",
5573 ctx.objErrStage1("wrong function type",
53745574 \\const fns = [_]fn() void { a, b, c };
53755575 \\fn a() i32 {return 0;}
53765576 \\fn b() i32 {return 1;}
......@@ -5380,7 +5580,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
53805580 "tmp.zig:1:28: error: expected type 'fn() void', found 'fn() i32'",
53815581 });
53825582
5383 cases.add("extern function pointer mismatch",
5583 ctx.objErrStage1("extern function pointer mismatch",
53845584 \\const fns = [_](fn(i32)i32) { a, b, c };
53855585 \\pub fn a(x: i32) i32 {return x + 0;}
53865586 \\pub fn b(x: i32) i32 {return x + 1;}
......@@ -5391,15 +5591,16 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
53915591 "tmp.zig:1:37: error: expected type 'fn(i32) i32', found 'fn(i32) callconv(.C) i32'",
53925592 });
53935593
5394 cases.add("colliding invalid top level functions",
5594 ctx.objErrStage1("colliding invalid top level functions",
53955595 \\fn func() bogus {}
53965596 \\fn func() bogus {}
53975597 \\export fn entry() usize { return @sizeOf(@TypeOf(func)); }
53985598 , &[_][]const u8{
5399 "tmp.zig:2:1: error: redefinition of 'func'",
5599 "tmp.zig:2:1: error: redeclaration of 'func'",
5600 "tmp.zig:1:1: note: other declaration here",
54005601 });
54015602
5402 cases.add("non constant expression in array size",
5603 ctx.objErrStage1("non constant expression in array size",
54035604 \\const Foo = struct {
54045605 \\ y: [get()]u8,
54055606 \\};
......@@ -5412,7 +5613,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
54125613 "tmp.zig:2:12: note: called from here",
54135614 });
54145615
5415 cases.add("addition with non numbers",
5616 ctx.objErrStage1("addition with non numbers",
54165617 \\const Foo = struct {
54175618 \\ field: i32,
54185619 \\};
......@@ -5423,7 +5624,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
54235624 "tmp.zig:4:28: error: invalid operands to binary expression: 'Foo' and 'Foo'",
54245625 });
54255626
5426 cases.add("division by zero",
5627 ctx.objErrStage1("division by zero",
54275628 \\const lit_int_x = 1 / 0;
54285629 \\const lit_float_x = 1.0 / 0.0;
54295630 \\const int_x = @as(u32, 1) / @as(u32, 0);
......@@ -5440,7 +5641,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
54405641 "tmp.zig:4:31: error: division by zero",
54415642 });
54425643
5443 cases.add("normal string with newline",
5644 ctx.objErrStage1("normal string with newline",
54445645 \\const foo = "a
54455646 \\b";
54465647 \\
......@@ -5449,7 +5650,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
54495650 "tmp.zig:1:15: error: newline not allowed in string literal",
54505651 });
54515652
5452 cases.add("invalid comparison for function pointers",
5653 ctx.objErrStage1("invalid comparison for function pointers",
54535654 \\fn foo() void {}
54545655 \\const invalid = foo > foo;
54555656 \\
......@@ -5458,7 +5659,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
54585659 "tmp.zig:2:21: error: operator not allowed for type 'fn() void'",
54595660 });
54605661
5461 cases.add("generic function instance with non-constant expression",
5662 ctx.objErrStage1("generic function instance with non-constant expression",
54625663 \\fn foo(comptime x: i32, y: i32) i32 { return x + y; }
54635664 \\fn test1(a: i32, b: i32) i32 {
54645665 \\ return foo(a, b);
......@@ -5469,7 +5670,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
54695670 "tmp.zig:3:16: error: runtime value cannot be passed to comptime arg",
54705671 });
54715672
5472 cases.add("assign null to non-optional pointer",
5673 ctx.objErrStage1("assign null to non-optional pointer",
54735674 \\const a: *u8 = null;
54745675 \\
54755676 \\export fn entry() usize { return @sizeOf(@TypeOf(a)); }
......@@ -5477,26 +5678,28 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
54775678 "tmp.zig:1:16: error: expected type '*u8', found '(null)'",
54785679 });
54795680
5480 cases.add("indexing an array of size zero",
5681 ctx.objErrStage1("indexing an array of size zero",
54815682 \\const array = [_]u8{};
54825683 \\export fn foo() void {
54835684 \\ const pointer = &array[0];
5685 \\ _ = pointer;
54845686 \\}
54855687 , &[_][]const u8{
54865688 "tmp.zig:3:27: error: accessing a zero length array is not allowed",
54875689 });
54885690
5489 cases.add("indexing an array of size zero with runtime index",
5691 ctx.objErrStage1("indexing an array of size zero with runtime index",
54905692 \\const array = [_]u8{};
54915693 \\export fn foo() void {
54925694 \\ var index: usize = 0;
54935695 \\ const pointer = &array[index];
5696 \\ _ = pointer;
54945697 \\}
54955698 , &[_][]const u8{
54965699 "tmp.zig:4:27: error: accessing a zero length array is not allowed",
54975700 });
54985701
5499 cases.add("compile time division by zero",
5702 ctx.objErrStage1("compile time division by zero",
55005703 \\const y = foo(0);
55015704 \\fn foo(x: u32) u32 {
55025705 \\ return 1 / x;
......@@ -5508,7 +5711,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
55085711 "tmp.zig:1:14: note: referenced here",
55095712 });
55105713
5511 cases.add("branch on undefined value",
5714 ctx.objErrStage1("branch on undefined value",
55125715 \\const x = if (undefined) true else false;
55135716 \\
55145717 \\export fn entry() usize { return @sizeOf(@TypeOf(x)); }
......@@ -5516,7 +5719,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
55165719 "tmp.zig:1:15: error: use of undefined value here causes undefined behavior",
55175720 });
55185721
5519 cases.add("div on undefined value",
5722 ctx.objErrStage1("div on undefined value",
55205723 \\comptime {
55215724 \\ var a: i64 = undefined;
55225725 \\ _ = a / a;
......@@ -5525,7 +5728,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
55255728 "tmp.zig:3:9: error: use of undefined value here causes undefined behavior",
55265729 });
55275730
5528 cases.add("div assign on undefined value",
5731 ctx.objErrStage1("div assign on undefined value",
55295732 \\comptime {
55305733 \\ var a: i64 = undefined;
55315734 \\ a /= a;
......@@ -5534,7 +5737,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
55345737 "tmp.zig:3:5: error: use of undefined value here causes undefined behavior",
55355738 });
55365739
5537 cases.add("mod on undefined value",
5740 ctx.objErrStage1("mod on undefined value",
55385741 \\comptime {
55395742 \\ var a: i64 = undefined;
55405743 \\ _ = a % a;
......@@ -5543,7 +5746,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
55435746 "tmp.zig:3:9: error: use of undefined value here causes undefined behavior",
55445747 });
55455748
5546 cases.add("mod assign on undefined value",
5749 ctx.objErrStage1("mod assign on undefined value",
55475750 \\comptime {
55485751 \\ var a: i64 = undefined;
55495752 \\ a %= a;
......@@ -5552,7 +5755,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
55525755 "tmp.zig:3:5: error: use of undefined value here causes undefined behavior",
55535756 });
55545757
5555 cases.add("add on undefined value",
5758 ctx.objErrStage1("add on undefined value",
55565759 \\comptime {
55575760 \\ var a: i64 = undefined;
55585761 \\ _ = a + a;
......@@ -5561,7 +5764,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
55615764 "tmp.zig:3:9: error: use of undefined value here causes undefined behavior",
55625765 });
55635766
5564 cases.add("add assign on undefined value",
5767 ctx.objErrStage1("add assign on undefined value",
55655768 \\comptime {
55665769 \\ var a: i64 = undefined;
55675770 \\ a += a;
......@@ -5570,7 +5773,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
55705773 "tmp.zig:3:5: error: use of undefined value here causes undefined behavior",
55715774 });
55725775
5573 cases.add("add wrap on undefined value",
5776 ctx.objErrStage1("add wrap on undefined value",
55745777 \\comptime {
55755778 \\ var a: i64 = undefined;
55765779 \\ _ = a +% a;
......@@ -5579,7 +5782,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
55795782 "tmp.zig:3:9: error: use of undefined value here causes undefined behavior",
55805783 });
55815784
5582 cases.add("add wrap assign on undefined value",
5785 ctx.objErrStage1("add wrap assign on undefined value",
55835786 \\comptime {
55845787 \\ var a: i64 = undefined;
55855788 \\ a +%= a;
......@@ -5588,7 +5791,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
55885791 "tmp.zig:3:5: error: use of undefined value here causes undefined behavior",
55895792 });
55905793
5591 cases.add("sub on undefined value",
5794 ctx.objErrStage1("sub on undefined value",
55925795 \\comptime {
55935796 \\ var a: i64 = undefined;
55945797 \\ _ = a - a;
......@@ -5597,7 +5800,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
55975800 "tmp.zig:3:9: error: use of undefined value here causes undefined behavior",
55985801 });
55995802
5600 cases.add("sub assign on undefined value",
5803 ctx.objErrStage1("sub assign on undefined value",
56015804 \\comptime {
56025805 \\ var a: i64 = undefined;
56035806 \\ a -= a;
......@@ -5606,7 +5809,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
56065809 "tmp.zig:3:5: error: use of undefined value here causes undefined behavior",
56075810 });
56085811
5609 cases.add("sub wrap on undefined value",
5812 ctx.objErrStage1("sub wrap on undefined value",
56105813 \\comptime {
56115814 \\ var a: i64 = undefined;
56125815 \\ _ = a -% a;
......@@ -5615,7 +5818,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
56155818 "tmp.zig:3:9: error: use of undefined value here causes undefined behavior",
56165819 });
56175820
5618 cases.add("sub wrap assign on undefined value",
5821 ctx.objErrStage1("sub wrap assign on undefined value",
56195822 \\comptime {
56205823 \\ var a: i64 = undefined;
56215824 \\ a -%= a;
......@@ -5624,7 +5827,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
56245827 "tmp.zig:3:5: error: use of undefined value here causes undefined behavior",
56255828 });
56265829
5627 cases.add("mult on undefined value",
5830 ctx.objErrStage1("mult on undefined value",
56285831 \\comptime {
56295832 \\ var a: i64 = undefined;
56305833 \\ _ = a * a;
......@@ -5633,7 +5836,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
56335836 "tmp.zig:3:9: error: use of undefined value here causes undefined behavior",
56345837 });
56355838
5636 cases.add("mult assign on undefined value",
5839 ctx.objErrStage1("mult assign on undefined value",
56375840 \\comptime {
56385841 \\ var a: i64 = undefined;
56395842 \\ a *= a;
......@@ -5642,7 +5845,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
56425845 "tmp.zig:3:5: error: use of undefined value here causes undefined behavior",
56435846 });
56445847
5645 cases.add("mult wrap on undefined value",
5848 ctx.objErrStage1("mult wrap on undefined value",
56465849 \\comptime {
56475850 \\ var a: i64 = undefined;
56485851 \\ _ = a *% a;
......@@ -5651,7 +5854,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
56515854 "tmp.zig:3:9: error: use of undefined value here causes undefined behavior",
56525855 });
56535856
5654 cases.add("mult wrap assign on undefined value",
5857 ctx.objErrStage1("mult wrap assign on undefined value",
56555858 \\comptime {
56565859 \\ var a: i64 = undefined;
56575860 \\ a *%= a;
......@@ -5660,7 +5863,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
56605863 "tmp.zig:3:5: error: use of undefined value here causes undefined behavior",
56615864 });
56625865
5663 cases.add("shift left on undefined value",
5866 ctx.objErrStage1("shift left on undefined value",
56645867 \\comptime {
56655868 \\ var a: i64 = undefined;
56665869 \\ _ = a << 2;
......@@ -5669,7 +5872,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
56695872 "tmp.zig:3:9: error: use of undefined value here causes undefined behavior",
56705873 });
56715874
5672 cases.add("shift left assign on undefined value",
5875 ctx.objErrStage1("shift left assign on undefined value",
56735876 \\comptime {
56745877 \\ var a: i64 = undefined;
56755878 \\ a <<= 2;
......@@ -5678,7 +5881,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
56785881 "tmp.zig:3:5: error: use of undefined value here causes undefined behavior",
56795882 });
56805883
5681 cases.add("shift right on undefined value",
5884 ctx.objErrStage1("shift right on undefined value",
56825885 \\comptime {
56835886 \\ var a: i64 = undefined;
56845887 \\ _ = a >> 2;
......@@ -5687,7 +5890,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
56875890 "tmp.zig:3:9: error: use of undefined value here causes undefined behavior",
56885891 });
56895892
5690 cases.add("shift left assign on undefined value",
5893 ctx.objErrStage1("shift left assign on undefined value",
56915894 \\comptime {
56925895 \\ var a: i64 = undefined;
56935896 \\ a >>= 2;
......@@ -5696,7 +5899,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
56965899 "tmp.zig:3:5: error: use of undefined value here causes undefined behavior",
56975900 });
56985901
5699 cases.add("bin and on undefined value",
5902 ctx.objErrStage1("bin and on undefined value",
57005903 \\comptime {
57015904 \\ var a: i64 = undefined;
57025905 \\ _ = a & a;
......@@ -5705,7 +5908,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
57055908 "tmp.zig:3:9: error: use of undefined value here causes undefined behavior",
57065909 });
57075910
5708 cases.add("bin and assign on undefined value",
5911 ctx.objErrStage1("bin and assign on undefined value",
57095912 \\comptime {
57105913 \\ var a: i64 = undefined;
57115914 \\ a &= a;
......@@ -5714,7 +5917,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
57145917 "tmp.zig:3:5: error: use of undefined value here causes undefined behavior",
57155918 });
57165919
5717 cases.add("bin or on undefined value",
5920 ctx.objErrStage1("bin or on undefined value",
57185921 \\comptime {
57195922 \\ var a: i64 = undefined;
57205923 \\ _ = a | a;
......@@ -5723,7 +5926,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
57235926 "tmp.zig:3:9: error: use of undefined value here causes undefined behavior",
57245927 });
57255928
5726 cases.add("bin or assign on undefined value",
5929 ctx.objErrStage1("bin or assign on undefined value",
57275930 \\comptime {
57285931 \\ var a: i64 = undefined;
57295932 \\ a |= a;
......@@ -5732,7 +5935,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
57325935 "tmp.zig:3:5: error: use of undefined value here causes undefined behavior",
57335936 });
57345937
5735 cases.add("bin xor on undefined value",
5938 ctx.objErrStage1("bin xor on undefined value",
57365939 \\comptime {
57375940 \\ var a: i64 = undefined;
57385941 \\ _ = a ^ a;
......@@ -5741,7 +5944,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
57415944 "tmp.zig:3:9: error: use of undefined value here causes undefined behavior",
57425945 });
57435946
5744 cases.add("bin xor assign on undefined value",
5947 ctx.objErrStage1("bin xor assign on undefined value",
57455948 \\comptime {
57465949 \\ var a: i64 = undefined;
57475950 \\ a ^= a;
......@@ -5750,7 +5953,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
57505953 "tmp.zig:3:5: error: use of undefined value here causes undefined behavior",
57515954 });
57525955
5753 cases.add("comparison operators with undefined value",
5956 ctx.objErrStage1("comparison operators with undefined value",
57545957 \\// operator ==
57555958 \\comptime {
57565959 \\ var a: i64 = undefined;
......@@ -5796,7 +5999,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
57965999 "tmp.zig:35:11: error: use of undefined value here causes undefined behavior",
57976000 });
57986001
5799 cases.add("and on undefined value",
6002 ctx.objErrStage1("and on undefined value",
58006003 \\comptime {
58016004 \\ var a: bool = undefined;
58026005 \\ _ = a and a;
......@@ -5805,7 +6008,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
58056008 "tmp.zig:3:9: error: use of undefined value here causes undefined behavior",
58066009 });
58076010
5808 cases.add("or on undefined value",
6011 ctx.objErrStage1("or on undefined value",
58096012 \\comptime {
58106013 \\ var a: bool = undefined;
58116014 \\ _ = a or a;
......@@ -5814,7 +6017,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
58146017 "tmp.zig:3:9: error: use of undefined value here causes undefined behavior",
58156018 });
58166019
5817 cases.add("negate on undefined value",
6020 ctx.objErrStage1("negate on undefined value",
58186021 \\comptime {
58196022 \\ var a: i64 = undefined;
58206023 \\ _ = -a;
......@@ -5823,7 +6026,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
58236026 "tmp.zig:3:10: error: use of undefined value here causes undefined behavior",
58246027 });
58256028
5826 cases.add("negate wrap on undefined value",
6029 ctx.objErrStage1("negate wrap on undefined value",
58276030 \\comptime {
58286031 \\ var a: i64 = undefined;
58296032 \\ _ = -%a;
......@@ -5832,7 +6035,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
58326035 "tmp.zig:3:11: error: use of undefined value here causes undefined behavior",
58336036 });
58346037
5835 cases.add("bin not on undefined value",
6038 ctx.objErrStage1("bin not on undefined value",
58366039 \\comptime {
58376040 \\ var a: i64 = undefined;
58386041 \\ _ = ~a;
......@@ -5841,7 +6044,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
58416044 "tmp.zig:3:10: error: use of undefined value here causes undefined behavior",
58426045 });
58436046
5844 cases.add("bool not on undefined value",
6047 ctx.objErrStage1("bool not on undefined value",
58456048 \\comptime {
58466049 \\ var a: bool = undefined;
58476050 \\ _ = !a;
......@@ -5850,7 +6053,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
58506053 "tmp.zig:3:10: error: use of undefined value here causes undefined behavior",
58516054 });
58526055
5853 cases.add("orelse on undefined value",
6056 ctx.objErrStage1("orelse on undefined value",
58546057 \\comptime {
58556058 \\ var a: ?bool = undefined;
58566059 \\ _ = a orelse false;
......@@ -5859,16 +6062,16 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
58596062 "tmp.zig:3:11: error: use of undefined value here causes undefined behavior",
58606063 });
58616064
5862 cases.add("catch on undefined value",
6065 ctx.objErrStage1("catch on undefined value",
58636066 \\comptime {
58646067 \\ var a: anyerror!bool = undefined;
5865 \\ _ = a catch |err| false;
6068 \\ _ = a catch false;
58666069 \\}
58676070 , &[_][]const u8{
58686071 "tmp.zig:3:11: error: use of undefined value here causes undefined behavior",
58696072 });
58706073
5871 cases.add("deref on undefined value",
6074 ctx.objErrStage1("deref on undefined value",
58726075 \\comptime {
58736076 \\ var a: *u8 = undefined;
58746077 \\ _ = a.*;
......@@ -5877,7 +6080,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
58776080 "tmp.zig:3:9: error: attempt to dereference undefined value",
58786081 });
58796082
5880 cases.add("endless loop in function evaluation",
6083 ctx.objErrStage1("endless loop in function evaluation",
58816084 \\const seventh_fib_number = fibbonaci(7);
58826085 \\fn fibbonaci(x: i32) i32 {
58836086 \\ return fibbonaci(x - 1) + fibbonaci(x - 2);
......@@ -5890,16 +6093,15 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
58906093 "tmp.zig:6:50: note: referenced here",
58916094 });
58926095
5893 cases.add("@embedFile with bogus file",
6096 ctx.objErrStage1("@embedFile with bogus file",
58946097 \\const resource = @embedFile("bogus.txt",);
58956098 \\
58966099 \\export fn entry() usize { return @sizeOf(@TypeOf(resource)); }
58976100 , &[_][]const u8{
5898 "tmp.zig:1:29: error: unable to find '",
5899 "bogus.txt'",
6101 "tmp.zig:1:29: error: unable to find 'bogus.txt'",
59006102 });
59016103
5902 cases.add("non-const expression in struct literal outside function",
6104 ctx.objErrStage1("non-const expression in struct literal outside function",
59036105 \\const Foo = struct {
59046106 \\ x: i32,
59056107 \\};
......@@ -5911,7 +6113,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
59116113 "tmp.zig:4:21: error: unable to evaluate constant expression",
59126114 });
59136115
5914 cases.add("non-const expression function call with struct return value outside function",
6116 ctx.objErrStage1("non-const expression function call with struct return value outside function",
59156117 \\const Foo = struct {
59166118 \\ x: i32,
59176119 \\};
......@@ -5928,7 +6130,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
59286130 "tmp.zig:4:17: note: referenced here",
59296131 });
59306132
5931 cases.add("undeclared identifier error should mark fn as impure",
6133 ctx.objErrStage1("undeclared identifier error should mark fn as impure",
59326134 \\export fn foo() void {
59336135 \\ test_a_thing();
59346136 \\}
......@@ -5939,7 +6141,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
59396141 "tmp.zig:5:5: error: use of undeclared identifier 'bad_fn_call'",
59406142 });
59416143
5942 cases.add("illegal comparison of types",
6144 ctx.objErrStage1("illegal comparison of types",
59436145 \\fn bad_eql_1(a: []u8, b: []u8) bool {
59446146 \\ return a == b;
59456147 \\}
......@@ -5958,13 +6160,14 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
59586160 "tmp.zig:9:16: error: operator not allowed for type 'EnumWithData'",
59596161 });
59606162
5961 cases.add("non-const switch number literal",
6163 ctx.objErrStage1("non-const switch number literal",
59626164 \\export fn foo() void {
59636165 \\ const x = switch (bar()) {
59646166 \\ 1, 2 => 1,
59656167 \\ 3, 4 => 2,
59666168 \\ else => 3,
59676169 \\ };
6170 \\ _ = x;
59686171 \\}
59696172 \\fn bar() i32 {
59706173 \\ return 2;
......@@ -5973,7 +6176,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
59736176 "tmp.zig:5:17: error: cannot store runtime value in type 'comptime_int'",
59746177 });
59756178
5976 cases.add("atomic orderings of cmpxchg - failure stricter than success",
6179 ctx.objErrStage1("atomic orderings of cmpxchg - failure stricter than success",
59776180 \\const AtomicOrder = @import("std").builtin.AtomicOrder;
59786181 \\export fn f() void {
59796182 \\ var x: i32 = 1234;
......@@ -5983,7 +6186,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
59836186 "tmp.zig:4:81: error: failure atomic ordering must be no stricter than success",
59846187 });
59856188
5986 cases.add("atomic orderings of cmpxchg - success Monotonic or stricter",
6189 ctx.objErrStage1("atomic orderings of cmpxchg - success Monotonic or stricter",
59876190 \\const AtomicOrder = @import("std").builtin.AtomicOrder;
59886191 \\export fn f() void {
59896192 \\ var x: i32 = 1234;
......@@ -5993,7 +6196,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
59936196 "tmp.zig:4:58: error: success atomic ordering must be Monotonic or stricter",
59946197 });
59956198
5996 cases.add("negation overflow in function evaluation",
6199 ctx.objErrStage1("negation overflow in function evaluation",
59976200 \\const y = neg(-128);
59986201 \\fn neg(x: i8) i8 {
59996202 \\ return -x;
......@@ -6005,7 +6208,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
60056208 "tmp.zig:1:14: note: referenced here",
60066209 });
60076210
6008 cases.add("add overflow in function evaluation",
6211 ctx.objErrStage1("add overflow in function evaluation",
60096212 \\const y = add(65530, 10);
60106213 \\fn add(a: u16, b: u16) u16 {
60116214 \\ return a + b;
......@@ -6017,7 +6220,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
60176220 "tmp.zig:1:14: note: referenced here",
60186221 });
60196222
6020 cases.add("sub overflow in function evaluation",
6223 ctx.objErrStage1("sub overflow in function evaluation",
60216224 \\const y = sub(10, 20);
60226225 \\fn sub(a: u16, b: u16) u16 {
60236226 \\ return a - b;
......@@ -6029,7 +6232,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
60296232 "tmp.zig:1:14: note: referenced here",
60306233 });
60316234
6032 cases.add("mul overflow in function evaluation",
6235 ctx.objErrStage1("mul overflow in function evaluation",
60336236 \\const y = mul(300, 6000);
60346237 \\fn mul(a: u16, b: u16) u16 {
60356238 \\ return a * b;
......@@ -6041,7 +6244,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
60416244 "tmp.zig:1:14: note: referenced here",
60426245 });
60436246
6044 cases.add("truncate sign mismatch",
6247 ctx.objErrStage1("truncate sign mismatch",
60456248 \\export fn entry1() i8 {
60466249 \\ var x: u32 = 10;
60476250 \\ return @truncate(i8, x);
......@@ -6065,7 +6268,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
60656268 "tmp.zig:15:26: error: expected unsigned integer type, found 'i32'",
60666269 });
60676270
6068 cases.add("try in function with non error return type",
6271 ctx.objErrStage1("try in function with non error return type",
60696272 \\export fn f() void {
60706273 \\ try something();
60716274 \\}
......@@ -6074,7 +6277,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
60746277 "tmp.zig:2:5: error: expected type 'void', found 'anyerror'",
60756278 });
60766279
6077 cases.add("invalid pointer for var type",
6280 ctx.objErrStage1("invalid pointer for var type",
60786281 \\extern fn ext() usize;
60796282 \\var bytes: [ext()]u8 = undefined;
60806283 \\export fn f() void {
......@@ -6086,7 +6289,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
60866289 "tmp.zig:2:13: error: unable to evaluate constant expression",
60876290 });
60886291
6089 cases.add("export function with comptime parameter",
6292 ctx.objErrStage1("export function with comptime parameter",
60906293 \\export fn foo(comptime x: i32, y: i32) i32{
60916294 \\ return x + y;
60926295 \\}
......@@ -6094,7 +6297,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
60946297 "tmp.zig:1:15: error: comptime parameter not allowed in function with calling convention 'C'",
60956298 });
60966299
6097 cases.add("extern function with comptime parameter",
6300 ctx.objErrStage1("extern function with comptime parameter",
60986301 \\extern fn foo(comptime x: i32, y: i32) i32;
60996302 \\fn f() i32 {
61006303 \\ return foo(1, 2);
......@@ -6104,7 +6307,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
61046307 "tmp.zig:1:15: error: comptime parameter not allowed in function with calling convention 'C'",
61056308 });
61066309
6107 cases.add("non-pure function returns type",
6310 ctx.objErrStage1("non-pure function returns type",
61086311 \\var a: u32 = 0;
61096312 \\pub fn List(comptime T: type) type {
61106313 \\ a += 1;
......@@ -6128,7 +6331,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
61286331 "tmp.zig:16:19: note: referenced here",
61296332 });
61306333
6131 cases.add("bogus method call on slice",
6334 ctx.objErrStage1("bogus method call on slice",
61326335 \\var self = "aoeu";
61336336 \\fn f(m: []const u8) void {
61346337 \\ m.copy(u8, self[0..], m);
......@@ -6138,9 +6341,9 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
61386341 "tmp.zig:3:6: error: no member named 'copy' in '[]const u8'",
61396342 });
61406343
6141 cases.add("wrong number of arguments for method fn call",
6344 ctx.objErrStage1("wrong number of arguments for method fn call",
61426345 \\const Foo = struct {
6143 \\ fn method(self: *const Foo, a: i32) void {}
6346 \\ fn method(self: *const Foo, a: i32) void {_ = self; _ = a;}
61446347 \\};
61456348 \\fn f(foo: *const Foo) void {
61466349 \\
......@@ -6151,7 +6354,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
61516354 "tmp.zig:6:15: error: expected 2 argument(s), found 3",
61526355 });
61536356
6154 cases.add("assign through constant pointer",
6357 ctx.objErrStage1("assign through constant pointer",
61556358 \\export fn f() void {
61566359 \\ var cstr = "Hat";
61576360 \\ cstr[0] = 'W';
......@@ -6160,7 +6363,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
61606363 "tmp.zig:3:13: error: cannot assign to constant",
61616364 });
61626365
6163 cases.add("assign through constant slice",
6366 ctx.objErrStage1("assign through constant slice",
61646367 \\export fn f() void {
61656368 \\ var cstr: []const u8 = "Hat";
61666369 \\ cstr[0] = 'W';
......@@ -6169,13 +6372,13 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
61696372 "tmp.zig:3:13: error: cannot assign to constant",
61706373 });
61716374
6172 cases.add("main function with bogus args type",
6173 \\pub fn main(args: [][]bogus) !void {}
6375 ctx.objErrStage1("main function with bogus args type",
6376 \\pub fn main(args: [][]bogus) !void {_ = args;}
61746377 , &[_][]const u8{
61756378 "tmp.zig:1:23: error: use of undeclared identifier 'bogus'",
61766379 });
61776380
6178 cases.add("misspelled type with pointer only reference",
6381 ctx.objErrStage1("misspelled type with pointer only reference",
61796382 \\const JasonHM = u8;
61806383 \\const JasonList = *JsonNode;
61816384 \\
......@@ -6203,6 +6406,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
62036406 \\ var jll: JasonList = undefined;
62046407 \\ jll.init(1234);
62056408 \\ var jd = JsonNode {.kind = JsonType.JSONArray , .jobject = JsonOA.JSONArray {jll} };
6409 \\ _ = jd;
62066410 \\}
62076411 \\
62086412 \\export fn entry() usize { return @sizeOf(@TypeOf(foo)); }
......@@ -6210,7 +6414,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
62106414 "tmp.zig:5:16: error: use of undeclared identifier 'JsonList'",
62116415 });
62126416
6213 cases.add("method call with first arg type primitive",
6417 ctx.objErrStage1("method call with first arg type primitive",
62146418 \\const Foo = struct {
62156419 \\ x: i32,
62166420 \\
......@@ -6230,7 +6434,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
62306434 "tmp.zig:14:5: error: expected type 'i32', found 'Foo'",
62316435 });
62326436
6233 cases.add("method call with first arg type wrong container",
6437 ctx.objErrStage1("method call with first arg type wrong container",
62346438 \\pub const List = struct {
62356439 \\ len: usize,
62366440 \\ allocator: *Allocator,
......@@ -6259,7 +6463,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
62596463 "tmp.zig:23:5: error: expected type '*Allocator', found '*List'",
62606464 });
62616465
6262 cases.add("binary not on number literal",
6466 ctx.objErrStage1("binary not on number literal",
62636467 \\const TINY_QUANTUM_SHIFT = 4;
62646468 \\const TINY_QUANTUM_SIZE = 1 << TINY_QUANTUM_SHIFT;
62656469 \\var block_aligned_stuff: usize = (4 + TINY_QUANTUM_SIZE) & ~(TINY_QUANTUM_SIZE - 1);
......@@ -6269,8 +6473,15 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
62696473 "tmp.zig:3:60: error: unable to perform binary not operation on type 'comptime_int'",
62706474 });
62716475
6272 cases.addCase(x: {
6273 const tc = cases.create("multiple files with private function error",
6476 {
6477 const case = ctx.obj("multiple files with private function error", .{});
6478 case.backend = .stage1;
6479
6480 case.addSourceFile("foo.zig",
6481 \\fn privateFunction() void { }
6482 );
6483
6484 case.addError(
62746485 \\const foo = @import("foo.zig",);
62756486 \\
62766487 \\export fn callPrivFunction() void {
......@@ -6280,16 +6491,19 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
62806491 "tmp.zig:4:8: error: 'privateFunction' is private",
62816492 "foo.zig:1:1: note: declared here",
62826493 });
6494 }
62836495
6284 tc.addSourceFile("foo.zig",
6285 \\fn privateFunction() void { }
6286 );
6496 {
6497 const case = ctx.obj("multiple files with private member instance function (canonical invocation) error", .{});
6498 case.backend = .stage1;
62876499
6288 break :x tc;
6289 });
6500 case.addSourceFile("foo.zig",
6501 \\pub const Foo = struct {
6502 \\ fn privateFunction(self: *Foo) void { _ = self; }
6503 \\};
6504 );
62906505
6291 cases.addCase(x: {
6292 const tc = cases.create("multiple files with private member instance function (canonical invocation) error",
6506 case.addError(
62936507 \\const Foo = @import("foo.zig",).Foo;
62946508 \\
62956509 \\export fn callPrivFunction() void {
......@@ -6300,18 +6514,19 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
63006514 "tmp.zig:5:8: error: 'privateFunction' is private",
63016515 "foo.zig:2:5: note: declared here",
63026516 });
6517 }
63036518
6304 tc.addSourceFile("foo.zig",
6519 {
6520 const case = ctx.obj("multiple files with private member instance function error", .{});
6521 case.backend = .stage1;
6522
6523 case.addSourceFile("foo.zig",
63056524 \\pub const Foo = struct {
6306 \\ fn privateFunction(self: *Foo) void { }
6525 \\ fn privateFunction(self: *Foo) void { _ = self; }
63076526 \\};
63086527 );
63096528
6310 break :x tc;
6311 });
6312
6313 cases.addCase(x: {
6314 const tc = cases.create("multiple files with private member instance function error",
6529 case.addError(
63156530 \\const Foo = @import("foo.zig",).Foo;
63166531 \\
63176532 \\export fn callPrivFunction() void {
......@@ -6322,17 +6537,9 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
63226537 "tmp.zig:5:8: error: 'privateFunction' is private",
63236538 "foo.zig:2:5: note: declared here",
63246539 });
6540 }
63256541
6326 tc.addSourceFile("foo.zig",
6327 \\pub const Foo = struct {
6328 \\ fn privateFunction(self: *Foo) void { }
6329 \\};
6330 );
6331
6332 break :x tc;
6333 });
6334
6335 cases.add("container init with non-type",
6542 ctx.objErrStage1("container init with non-type",
63366543 \\const zero: i32 = 0;
63376544 \\const a = zero{1};
63386545 \\
......@@ -6341,7 +6548,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
63416548 "tmp.zig:2:11: error: expected type 'type', found 'i32'",
63426549 });
63436550
6344 cases.add("assign to constant field",
6551 ctx.objErrStage1("assign to constant field",
63456552 \\const Foo = struct {
63466553 \\ field: i32,
63476554 \\};
......@@ -6353,7 +6560,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
63536560 "tmp.zig:6:15: error: cannot assign to constant",
63546561 });
63556562
6356 cases.add("return from defer expression",
6563 ctx.objErrStage1("return from defer expression",
63576564 \\pub fn testTrickyDefer() !void {
63586565 \\ defer canFail() catch {};
63596566 \\
......@@ -6370,32 +6577,33 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
63706577 \\
63716578 \\export fn entry() usize { return @sizeOf(@TypeOf(testTrickyDefer)); }
63726579 , &[_][]const u8{
6373 "tmp.zig:4:11: error: cannot return from defer expression",
6580 "tmp.zig:4:11: error: 'try' is not allowed inside defer expression",
63746581 });
63756582
6376 cases.add("assign too big number to u16",
6583 ctx.objErrStage1("assign too big number to u16",
63776584 \\export fn foo() void {
63786585 \\ var vga_mem: u16 = 0xB8000;
6586 \\ _ = vga_mem;
63796587 \\}
63806588 , &[_][]const u8{
63816589 "tmp.zig:2:24: error: integer value 753664 cannot be coerced to type 'u16'",
63826590 });
63836591
6384 cases.add("global variable alignment non power of 2",
6592 ctx.objErrStage1("global variable alignment non power of 2",
63856593 \\const some_data: [100]u8 align(3) = undefined;
63866594 \\export fn entry() usize { return @sizeOf(@TypeOf(some_data)); }
63876595 , &[_][]const u8{
63886596 "tmp.zig:1:32: error: alignment value 3 is not a power of 2",
63896597 });
63906598
6391 cases.add("function alignment non power of 2",
6599 ctx.objErrStage1("function alignment non power of 2",
63926600 \\extern fn foo() align(3) void;
63936601 \\export fn entry() void { return foo(); }
63946602 , &[_][]const u8{
63956603 "tmp.zig:1:23: error: alignment value 3 is not a power of 2",
63966604 });
63976605
6398 cases.add("compile log",
6606 ctx.objErrStage1("compile log",
63996607 \\export fn foo() void {
64006608 \\ comptime bar(12, "hi",);
64016609 \\}
......@@ -6410,7 +6618,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
64106618 "tmp.zig:7:5: error: found compile log statement",
64116619 });
64126620
6413 cases.add("casting bit offset pointer to regular pointer",
6621 ctx.objErrStage1("casting bit offset pointer to regular pointer",
64146622 \\const BitField = packed struct {
64156623 \\ a: u3,
64166624 \\ b: u3,
......@@ -6430,7 +6638,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
64306638 "tmp.zig:8:26: error: expected type '*const u3', found '*align(:3:1) const u3'",
64316639 });
64326640
6433 cases.add("referring to a struct that is invalid",
6641 ctx.objErrStage1("referring to a struct that is invalid",
64346642 \\const UsbDeviceRequest = struct {
64356643 \\ Type: u8,
64366644 \\};
......@@ -6447,7 +6655,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
64476655 "tmp.zig:6:20: note: referenced here",
64486656 });
64496657
6450 cases.add("control flow uses comptime var at runtime",
6658 ctx.objErrStage1("control flow uses comptime var at runtime",
64516659 \\export fn foo() void {
64526660 \\ comptime var i = 0;
64536661 \\ while (i < 5) : (i += 1) {
......@@ -6461,7 +6669,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
64616669 "tmp.zig:3:24: note: compile-time variable assigned here",
64626670 });
64636671
6464 cases.add("ignored return value",
6672 ctx.objErrStage1("ignored return value",
64656673 \\export fn foo() void {
64666674 \\ bar();
64676675 \\}
......@@ -6470,7 +6678,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
64706678 "tmp.zig:2:8: error: expression value is ignored",
64716679 });
64726680
6473 cases.add("ignored assert-err-ok return value",
6681 ctx.objErrStage1("ignored assert-err-ok return value",
64746682 \\export fn foo() void {
64756683 \\ bar() catch unreachable;
64766684 \\}
......@@ -6479,7 +6687,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
64796687 "tmp.zig:2:11: error: expression value is ignored",
64806688 });
64816689
6482 cases.add("ignored statement value",
6690 ctx.objErrStage1("ignored statement value",
64836691 \\export fn foo() void {
64846692 \\ 1;
64856693 \\}
......@@ -6487,7 +6695,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
64876695 "tmp.zig:2:5: error: expression value is ignored",
64886696 });
64896697
6490 cases.add("ignored comptime statement value",
6698 ctx.objErrStage1("ignored comptime statement value",
64916699 \\export fn foo() void {
64926700 \\ comptime {1;}
64936701 \\}
......@@ -6495,7 +6703,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
64956703 "tmp.zig:2:15: error: expression value is ignored",
64966704 });
64976705
6498 cases.add("ignored comptime value",
6706 ctx.objErrStage1("ignored comptime value",
64996707 \\export fn foo() void {
65006708 \\ comptime 1;
65016709 \\}
......@@ -6503,7 +6711,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
65036711 "tmp.zig:2:5: error: expression value is ignored",
65046712 });
65056713
6506 cases.add("ignored defered statement value",
6714 ctx.objErrStage1("ignored defered statement value",
65076715 \\export fn foo() void {
65086716 \\ defer {1;}
65096717 \\}
......@@ -6511,7 +6719,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
65116719 "tmp.zig:2:12: error: expression value is ignored",
65126720 });
65136721
6514 cases.add("ignored defered function call",
6722 ctx.objErrStage1("ignored defered function call",
65156723 \\export fn foo() void {
65166724 \\ defer bar();
65176725 \\}
......@@ -6520,7 +6728,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
65206728 "tmp.zig:2:14: error: error is ignored. consider using `try`, `catch`, or `if`",
65216729 });
65226730
6523 cases.add("dereference an array",
6731 ctx.objErrStage1("dereference an array",
65246732 \\var s_buffer: [10]u8 = undefined;
65256733 \\pub fn pass(in: []u8) []u8 {
65266734 \\ var out = &s_buffer;
......@@ -6533,13 +6741,14 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
65336741 "tmp.zig:4:10: error: attempt to dereference non-pointer type '[10]u8'",
65346742 });
65356743
6536 cases.add("pass const ptr to mutable ptr fn",
6744 ctx.objErrStage1("pass const ptr to mutable ptr fn",
65376745 \\fn foo() bool {
65386746 \\ const a = @as([]const u8, "a",);
65396747 \\ const b = &a;
65406748 \\ return ptrEql(b, b);
65416749 \\}
65426750 \\fn ptrEql(a: *[]const u8, b: *[]const u8) bool {
6751 \\ _ = a; _ = b;
65436752 \\ return true;
65446753 \\}
65456754 \\
......@@ -6548,8 +6757,16 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
65486757 "tmp.zig:4:19: error: expected type '*[]const u8', found '*const []const u8'",
65496758 });
65506759
6551 cases.addCase(x: {
6552 const tc = cases.create("export collision",
6760 {
6761 const case = ctx.obj("export collision", .{});
6762 case.backend = .stage1;
6763
6764 case.addSourceFile("foo.zig",
6765 \\export fn bar() void {}
6766 \\pub const baz = 1234;
6767 );
6768
6769 case.addError(
65536770 \\const foo = @import("foo.zig",);
65546771 \\
65556772 \\export fn bar() usize {
......@@ -6559,18 +6776,11 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
65596776 "foo.zig:1:1: error: exported symbol collision: 'bar'",
65606777 "tmp.zig:3:1: note: other symbol here",
65616778 });
6779 }
65626780
6563 tc.addSourceFile("foo.zig",
6564 \\export fn bar() void {}
6565 \\pub const baz = 1234;
6566 );
6567
6568 break :x tc;
6569 });
6570
6571 cases.add("implicit cast from array to mutable slice",
6781 ctx.objErrStage1("implicit cast from array to mutable slice",
65726782 \\var global_array: [10]i32 = undefined;
6573 \\fn foo(param: []i32) void {}
6783 \\fn foo(param: []i32) void {_ = param;}
65746784 \\export fn entry() void {
65756785 \\ foo(global_array);
65766786 \\}
......@@ -6578,7 +6788,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
65786788 "tmp.zig:4:9: error: expected type '[]i32', found '[10]i32'",
65796789 });
65806790
6581 cases.add("ptrcast to non-pointer",
6791 ctx.objErrStage1("ptrcast to non-pointer",
65826792 \\export fn entry(a: *i32) usize {
65836793 \\ return @ptrCast(usize, a);
65846794 \\}
......@@ -6586,7 +6796,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
65866796 "tmp.zig:2:21: error: expected pointer, found 'usize'",
65876797 });
65886798
6589 cases.add("asm at compile time",
6799 ctx.objErrStage1("asm at compile time",
65906800 \\comptime {
65916801 \\ doSomeAsm();
65926802 \\}
......@@ -6602,25 +6812,27 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
66026812 "tmp.zig:6:5: error: unable to evaluate constant expression",
66036813 });
66046814
6605 cases.add("invalid member of builtin enum",
6815 ctx.objErrStage1("invalid member of builtin enum",
66066816 \\const builtin = @import("std").builtin;
66076817 \\export fn entry() void {
66086818 \\ const foo = builtin.Mode.x86;
6819 \\ _ = foo;
66096820 \\}
66106821 , &[_][]const u8{
66116822 "tmp.zig:3:29: error: container 'std.builtin.Mode' has no member called 'x86'",
66126823 });
66136824
6614 cases.add("int to ptr of 0 bits",
6825 ctx.objErrStage1("int to ptr of 0 bits",
66156826 \\export fn foo() void {
66166827 \\ var x: usize = 0x1000;
66176828 \\ var y: *void = @intToPtr(*void, x);
6829 \\ _ = y;
66186830 \\}
66196831 , &[_][]const u8{
66206832 "tmp.zig:3:30: error: type '*void' has 0 bits and cannot store information",
66216833 });
66226834
6623 cases.add("@fieldParentPtr - non struct",
6835 ctx.objErrStage1("@fieldParentPtr - non struct",
66246836 \\const Foo = i32;
66256837 \\export fn foo(a: *i32) *Foo {
66266838 \\ return @fieldParentPtr(Foo, "a", a);
......@@ -6629,7 +6841,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
66296841 "tmp.zig:3:28: error: expected struct type, found 'i32'",
66306842 });
66316843
6632 cases.add("@fieldParentPtr - bad field name",
6844 ctx.objErrStage1("@fieldParentPtr - bad field name",
66336845 \\const Foo = extern struct {
66346846 \\ derp: i32,
66356847 \\};
......@@ -6640,7 +6852,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
66406852 "tmp.zig:5:33: error: struct 'Foo' has no field 'a'",
66416853 });
66426854
6643 cases.add("@fieldParentPtr - field pointer is not pointer",
6855 ctx.objErrStage1("@fieldParentPtr - field pointer is not pointer",
66446856 \\const Foo = extern struct {
66456857 \\ a: i32,
66466858 \\};
......@@ -6651,7 +6863,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
66516863 "tmp.zig:5:38: error: expected pointer, found 'i32'",
66526864 });
66536865
6654 cases.add("@fieldParentPtr - comptime field ptr not based on struct",
6866 ctx.objErrStage1("@fieldParentPtr - comptime field ptr not based on struct",
66556867 \\const Foo = struct {
66566868 \\ a: i32,
66576869 \\ b: i32,
......@@ -6661,12 +6873,13 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
66616873 \\comptime {
66626874 \\ const field_ptr = @intToPtr(*i32, 0x1234);
66636875 \\ const another_foo_ptr = @fieldParentPtr(Foo, "b", field_ptr);
6876 \\ _ = another_foo_ptr;
66646877 \\}
66656878 , &[_][]const u8{
66666879 "tmp.zig:9:55: error: pointer value not based on parent struct",
66676880 });
66686881
6669 cases.add("@fieldParentPtr - comptime wrong field index",
6882 ctx.objErrStage1("@fieldParentPtr - comptime wrong field index",
66706883 \\const Foo = struct {
66716884 \\ a: i32,
66726885 \\ b: i32,
......@@ -6675,12 +6888,13 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
66756888 \\
66766889 \\comptime {
66776890 \\ const another_foo_ptr = @fieldParentPtr(Foo, "b", &foo.a);
6891 \\ _ = another_foo_ptr;
66786892 \\}
66796893 , &[_][]const u8{
66806894 "tmp.zig:8:29: error: field 'b' has index 1 but pointer value is index 0 of struct 'Foo'",
66816895 });
66826896
6683 cases.add("@offsetOf - non struct",
6897 ctx.objErrStage1("@offsetOf - non struct",
66846898 \\const Foo = i32;
66856899 \\export fn foo() usize {
66866900 \\ return @offsetOf(Foo, "a",);
......@@ -6689,7 +6903,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
66896903 "tmp.zig:3:22: error: expected struct type, found 'i32'",
66906904 });
66916905
6692 cases.add("@offsetOf - bad field name",
6906 ctx.objErrStage1("@offsetOf - bad field name",
66936907 \\const Foo = struct {
66946908 \\ derp: i32,
66956909 \\};
......@@ -6700,20 +6914,20 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
67006914 "tmp.zig:5:27: error: struct 'Foo' has no field 'a'",
67016915 });
67026916
6703 cases.addExe("missing main fn in executable",
6917 ctx.exeErrStage1("missing main fn in executable",
67046918 \\
67056919 , &[_][]const u8{
67066920 "error: root source file has no member called 'main'",
67076921 });
67086922
6709 cases.addExe("private main fn",
6923 ctx.exeErrStage1("private main fn",
67106924 \\fn main() void {}
67116925 , &[_][]const u8{
67126926 "error: 'main' is private",
67136927 "tmp.zig:1:1: note: declared here",
67146928 });
67156929
6716 cases.add("setting a section on a local variable",
6930 ctx.objErrStage1("setting a section on a local variable",
67176931 \\export fn entry() i32 {
67186932 \\ var foo: i32 linksection(".text2") = 1234;
67196933 \\ return foo;
......@@ -6722,7 +6936,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
67226936 "tmp.zig:2:30: error: cannot set section of local variable 'foo'",
67236937 });
67246938
6725 cases.add("inner struct member shadowing outer struct member",
6939 ctx.objErrStage1("inner struct member shadowing outer struct member",
67266940 \\fn A() type {
67276941 \\ return struct {
67286942 \\ b: B(),
......@@ -6747,7 +6961,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
67476961 "tmp.zig:5:9: note: previous definition is here",
67486962 });
67496963
6750 cases.add("while expected bool, got optional",
6964 ctx.objErrStage1("while expected bool, got optional",
67516965 \\export fn foo() void {
67526966 \\ while (bar()) {}
67536967 \\}
......@@ -6756,7 +6970,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
67566970 "tmp.zig:2:15: error: expected type 'bool', found '?i32'",
67576971 });
67586972
6759 cases.add("while expected bool, got error union",
6973 ctx.objErrStage1("while expected bool, got error union",
67606974 \\export fn foo() void {
67616975 \\ while (bar()) {}
67626976 \\}
......@@ -6765,36 +6979,36 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
67656979 "tmp.zig:2:15: error: expected type 'bool', found 'anyerror!i32'",
67666980 });
67676981
6768 cases.add("while expected optional, got bool",
6982 ctx.objErrStage1("while expected optional, got bool",
67696983 \\export fn foo() void {
6770 \\ while (bar()) |x| {}
6984 \\ while (bar()) |x| {_ = x;}
67716985 \\}
67726986 \\fn bar() bool { return true; }
67736987 , &[_][]const u8{
67746988 "tmp.zig:2:15: error: expected optional type, found 'bool'",
67756989 });
67766990
6777 cases.add("while expected optional, got error union",
6991 ctx.objErrStage1("while expected optional, got error union",
67786992 \\export fn foo() void {
6779 \\ while (bar()) |x| {}
6993 \\ while (bar()) |x| {_ = x;}
67806994 \\}
67816995 \\fn bar() anyerror!i32 { return 1; }
67826996 , &[_][]const u8{
67836997 "tmp.zig:2:15: error: expected optional type, found 'anyerror!i32'",
67846998 });
67856999
6786 cases.add("while expected error union, got bool",
7000 ctx.objErrStage1("while expected error union, got bool",
67877001 \\export fn foo() void {
6788 \\ while (bar()) |x| {} else |err| {}
7002 \\ while (bar()) |x| {_ = x;} else |err| {_ = err;}
67897003 \\}
67907004 \\fn bar() bool { return true; }
67917005 , &[_][]const u8{
67927006 "tmp.zig:2:15: error: expected error union type, found 'bool'",
67937007 });
67947008
6795 cases.add("while expected error union, got optional",
7009 ctx.objErrStage1("while expected error union, got optional",
67967010 \\export fn foo() void {
6797 \\ while (bar()) |x| {} else |err| {}
7011 \\ while (bar()) |x| {_ = x;} else |err| {_ = err;}
67987012 \\}
67997013 \\fn bar() ?i32 { return 1; }
68007014 , &[_][]const u8{
......@@ -6802,7 +7016,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
68027016 });
68037017
68047018 // TODO test this in stage2, but we won't even try in stage1
6805 //cases.add("inline fn calls itself indirectly",
7019 //ctx.objErrStage1("inline fn calls itself indirectly",
68067020 // \\export fn foo() void {
68077021 // \\ bar();
68087022 // \\}
......@@ -6819,7 +7033,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
68197033 // "tmp.zig:4:1: error: unable to inline function",
68207034 //});
68217035
6822 //cases.add("save reference to inline function",
7036 //ctx.objErrStage1("save reference to inline function",
68237037 // \\export fn foo() void {
68247038 // \\ quux(@ptrToInt(bar));
68257039 // \\}
......@@ -6829,7 +7043,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
68297043 // "tmp.zig:4:1: error: unable to inline function",
68307044 //});
68317045
6832 cases.add("signed integer division",
7046 ctx.objErrStage1("signed integer division",
68337047 \\export fn foo(a: i32, b: i32) i32 {
68347048 \\ return a / b;
68357049 \\}
......@@ -6837,7 +7051,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
68377051 "tmp.zig:2:14: error: division with 'i32' and 'i32': signed integers must use @divTrunc, @divFloor, or @divExact",
68387052 });
68397053
6840 cases.add("signed integer remainder division",
7054 ctx.objErrStage1("signed integer remainder division",
68417055 \\export fn foo(a: i32, b: i32) i32 {
68427056 \\ return a % b;
68437057 \\}
......@@ -6845,27 +7059,29 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
68457059 "tmp.zig:2:14: error: remainder division with 'i32' and 'i32': signed integers and floats must use @rem or @mod",
68467060 });
68477061
6848 cases.add("compile-time division by zero",
7062 ctx.objErrStage1("compile-time division by zero",
68497063 \\comptime {
68507064 \\ const a: i32 = 1;
68517065 \\ const b: i32 = 0;
68527066 \\ const c = a / b;
7067 \\ _ = c;
68537068 \\}
68547069 , &[_][]const u8{
68557070 "tmp.zig:4:17: error: division by zero",
68567071 });
68577072
6858 cases.add("compile-time remainder division by zero",
7073 ctx.objErrStage1("compile-time remainder division by zero",
68597074 \\comptime {
68607075 \\ const a: i32 = 1;
68617076 \\ const b: i32 = 0;
68627077 \\ const c = a % b;
7078 \\ _ = c;
68637079 \\}
68647080 , &[_][]const u8{
68657081 "tmp.zig:4:17: error: division by zero",
68667082 });
68677083
6868 cases.add("@setRuntimeSafety twice for same scope",
7084 ctx.objErrStage1("@setRuntimeSafety twice for same scope",
68697085 \\export fn foo() void {
68707086 \\ @setRuntimeSafety(false);
68717087 \\ @setRuntimeSafety(false);
......@@ -6875,7 +7091,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
68757091 "tmp.zig:2:5: note: first set here",
68767092 });
68777093
6878 cases.add("@setFloatMode twice for same scope",
7094 ctx.objErrStage1("@setFloatMode twice for same scope",
68797095 \\export fn foo() void {
68807096 \\ @setFloatMode(@import("std").builtin.FloatMode.Optimized);
68817097 \\ @setFloatMode(@import("std").builtin.FloatMode.Optimized);
......@@ -6885,15 +7101,16 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
68857101 "tmp.zig:2:5: note: first set here",
68867102 });
68877103
6888 cases.add("array access of type",
7104 ctx.objErrStage1("array access of type",
68897105 \\export fn foo() void {
68907106 \\ var b: u8[40] = undefined;
7107 \\ _ = b;
68917108 \\}
68927109 , &[_][]const u8{
68937110 "tmp.zig:2:14: error: array access of non-array type 'type'",
68947111 });
68957112
6896 cases.add("cannot break out of defer expression",
7113 ctx.objErrStage1("cannot break out of defer expression",
68977114 \\export fn foo() void {
68987115 \\ while (true) {
68997116 \\ defer {
......@@ -6905,7 +7122,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
69057122 "tmp.zig:4:13: error: cannot break out of defer expression",
69067123 });
69077124
6908 cases.add("cannot continue out of defer expression",
7125 ctx.objErrStage1("cannot continue out of defer expression",
69097126 \\export fn foo() void {
69107127 \\ while (true) {
69117128 \\ defer {
......@@ -6917,11 +7134,11 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
69177134 "tmp.zig:4:13: error: cannot continue out of defer expression",
69187135 });
69197136
6920 cases.add("calling a generic function only known at runtime",
7137 ctx.objErrStage1("calling a generic function only known at runtime",
69217138 \\var foos = [_]fn(anytype) void { foo1, foo2 };
69227139 \\
6923 \\fn foo1(arg: anytype) void {}
6924 \\fn foo2(arg: anytype) void {}
7140 \\fn foo1(arg: anytype) void {_ = arg;}
7141 \\fn foo2(arg: anytype) void {_ = arg;}
69257142 \\
69267143 \\pub fn main() !void {
69277144 \\ foos[0](true);
......@@ -6930,7 +7147,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
69307147 "tmp.zig:7:9: error: calling a generic function requires compile-time known function value",
69317148 });
69327149
6933 cases.add("@compileError shows traceback of references that caused it",
7150 ctx.objErrStage1("@compileError shows traceback of references that caused it",
69347151 \\const foo = @compileError("aoeu",);
69357152 \\
69367153 \\const bar = baz + foo;
......@@ -6945,23 +7162,25 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
69457162 "tmp.zig:7:12: note: referenced here",
69467163 });
69477164
6948 cases.add("float literal too large error",
7165 ctx.objErrStage1("float literal too large error",
69497166 \\comptime {
69507167 \\ const a = 0x1.0p18495;
7168 \\ _ = a;
69517169 \\}
69527170 , &[_][]const u8{
69537171 "tmp.zig:2:15: error: float literal out of range of any type",
69547172 });
69557173
6956 cases.add("float literal too small error (denormal)",
7174 ctx.objErrStage1("float literal too small error (denormal)",
69577175 \\comptime {
69587176 \\ const a = 0x1.0p-19000;
7177 \\ _ = a;
69597178 \\}
69607179 , &[_][]const u8{
69617180 "tmp.zig:2:15: error: float literal out of range of any type",
69627181 });
69637182
6964 cases.add("explicit cast float literal to integer when there is a fraction component",
7183 ctx.objErrStage1("explicit cast float literal to integer when there is a fraction component",
69657184 \\export fn entry() i32 {
69667185 \\ return @as(i32, 12.34);
69677186 \\}
......@@ -6969,7 +7188,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
69697188 "tmp.zig:2:21: error: fractional component prevents float value 12.340000 from being casted to type 'i32'",
69707189 });
69717190
6972 cases.add("non pointer given to @ptrToInt",
7191 ctx.objErrStage1("non pointer given to @ptrToInt",
69737192 \\export fn entry(x: i32) usize {
69747193 \\ return @ptrToInt(x);
69757194 \\}
......@@ -6977,23 +7196,25 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
69777196 "tmp.zig:2:22: error: expected pointer, found 'i32'",
69787197 });
69797198
6980 cases.add("@shlExact shifts out 1 bits",
7199 ctx.objErrStage1("@shlExact shifts out 1 bits",
69817200 \\comptime {
69827201 \\ const x = @shlExact(@as(u8, 0b01010101), 2);
7202 \\ _ = x;
69837203 \\}
69847204 , &[_][]const u8{
69857205 "tmp.zig:2:15: error: operation caused overflow",
69867206 });
69877207
6988 cases.add("@shrExact shifts out 1 bits",
7208 ctx.objErrStage1("@shrExact shifts out 1 bits",
69897209 \\comptime {
69907210 \\ const x = @shrExact(@as(u8, 0b10101010), 2);
7211 \\ _ = x;
69917212 \\}
69927213 , &[_][]const u8{
69937214 "tmp.zig:2:15: error: exact shift shifted out 1 bits",
69947215 });
69957216
6996 cases.add("shifting without int type or comptime known",
7217 ctx.objErrStage1("shifting without int type or comptime known",
69977218 \\export fn entry(x: u8) u8 {
69987219 \\ return 0x11 << x;
69997220 \\}
......@@ -7001,7 +7222,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
70017222 "tmp.zig:2:17: error: LHS of shift must be a fixed-width integer type, or RHS must be compile-time known",
70027223 });
70037224
7004 cases.add("shifting RHS is log2 of LHS int bit width",
7225 ctx.objErrStage1("shifting RHS is log2 of LHS int bit width",
70057226 \\export fn entry(x: u8, y: u8) u8 {
70067227 \\ return x << y;
70077228 \\}
......@@ -7009,16 +7230,17 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
70097230 "tmp.zig:2:17: error: expected type 'u3', found 'u8'",
70107231 });
70117232
7012 cases.add("globally shadowing a primitive type",
7233 ctx.objErrStage1("globally shadowing a primitive type",
70137234 \\const u16 = u8;
70147235 \\export fn entry() void {
70157236 \\ const a: u16 = 300;
7237 \\ _ = a;
70167238 \\}
70177239 , &[_][]const u8{
70187240 "tmp.zig:1:1: error: declaration shadows primitive type 'u16'",
70197241 });
70207242
7021 cases.add("implicitly increasing pointer alignment",
7243 ctx.objErrStage1("implicitly increasing pointer alignment",
70227244 \\const Foo = packed struct {
70237245 \\ a: u8,
70247246 \\ b: u32,
......@@ -7036,7 +7258,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
70367258 "tmp.zig:8:13: error: expected type '*u32', found '*align(1) u32'",
70377259 });
70387260
7039 cases.add("implicitly increasing slice alignment",
7261 ctx.objErrStage1("implicitly increasing slice alignment",
70407262 \\const Foo = packed struct {
70417263 \\ a: u8,
70427264 \\ b: u32,
......@@ -7057,7 +7279,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
70577279 "tmp.zig:9:26: note: '*[1]u32' has alignment 4",
70587280 });
70597281
7060 cases.add("increase pointer alignment in @ptrCast",
7282 ctx.objErrStage1("increase pointer alignment in @ptrCast",
70617283 \\export fn entry() u32 {
70627284 \\ var bytes: [4]u8 = [_]u8{0x01, 0x02, 0x03, 0x04};
70637285 \\ const ptr = @ptrCast(*u32, &bytes[0]);
......@@ -7069,7 +7291,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
70697291 "tmp.zig:3:26: note: '*u32' has alignment 4",
70707292 });
70717293
7072 cases.add("@alignCast expects pointer or slice",
7294 ctx.objErrStage1("@alignCast expects pointer or slice",
70737295 \\export fn entry() void {
70747296 \\ @alignCast(4, @as(u32, 3));
70757297 \\}
......@@ -7077,7 +7299,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
70777299 "tmp.zig:2:19: error: expected pointer or slice, found 'u32'",
70787300 });
70797301
7080 cases.add("passing an under-aligned function pointer",
7302 ctx.objErrStage1("passing an under-aligned function pointer",
70817303 \\export fn entry() void {
70827304 \\ testImplicitlyDecreaseFnAlign(alignedSmall, 1234);
70837305 \\}
......@@ -7089,7 +7311,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
70897311 "tmp.zig:2:35: error: expected type 'fn() align(8) i32', found 'fn() align(4) i32'",
70907312 });
70917313
7092 cases.add("passing a not-aligned-enough pointer to cmpxchg",
7314 ctx.objErrStage1("passing a not-aligned-enough pointer to cmpxchg",
70937315 \\const AtomicOrder = @import("std").builtin.AtomicOrder;
70947316 \\export fn entry() bool {
70957317 \\ var x: i32 align(1) = 1234;
......@@ -7100,15 +7322,16 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
71007322 "tmp.zig:4:32: error: expected type '*i32', found '*align(1) i32'",
71017323 });
71027324
7103 cases.add("wrong size to an array literal",
7325 ctx.objErrStage1("wrong size to an array literal",
71047326 \\comptime {
71057327 \\ const array = [2]u8{1, 2, 3};
7328 \\ _ = array;
71067329 \\}
71077330 , &[_][]const u8{
71087331 "tmp.zig:2:31: error: index 2 outside array of size 2",
71097332 });
71107333
7111 cases.add("wrong pointer coerced to pointer to opaque {}",
7334 ctx.objErrStage1("wrong pointer coerced to pointer to opaque {}",
71127335 \\const Derp = opaque {};
71137336 \\extern fn bar(d: *Derp) void;
71147337 \\export fn foo() void {
......@@ -7119,49 +7342,57 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
71197342 "tmp.zig:5:9: error: expected type '*Derp', found '*c_void'",
71207343 });
71217344
7122 cases.add("non-const variables of things that require const variables",
7345 ctx.objErrStage1("non-const variables of things that require const variables",
71237346 \\export fn entry1() void {
71247347 \\ var m2 = &2;
7348 \\ _ = m2;
71257349 \\}
71267350 \\export fn entry2() void {
71277351 \\ var a = undefined;
7352 \\ _ = a;
71287353 \\}
71297354 \\export fn entry3() void {
71307355 \\ var b = 1;
7356 \\ _ = b;
71317357 \\}
71327358 \\export fn entry4() void {
71337359 \\ var c = 1.0;
7360 \\ _ = c;
71347361 \\}
71357362 \\export fn entry5() void {
71367363 \\ var d = null;
7364 \\ _ = d;
71377365 \\}
71387366 \\export fn entry6(opaque_: *Opaque) void {
71397367 \\ var e = opaque_.*;
7368 \\ _ = e;
71407369 \\}
71417370 \\export fn entry7() void {
71427371 \\ var f = i32;
7372 \\ _ = f;
71437373 \\}
71447374 \\export fn entry8() void {
71457375 \\ var h = (Foo {}).bar;
7376 \\ _ = h;
71467377 \\}
71477378 \\const Opaque = opaque {};
71487379 \\const Foo = struct {
7149 \\ fn bar(self: *const Foo) void {}
7380 \\ fn bar(self: *const Foo) void {_ = self;}
71507381 \\};
71517382 , &[_][]const u8{
71527383 "tmp.zig:2:4: error: variable of type '*const comptime_int' must be const or comptime",
7153 "tmp.zig:5:4: error: variable of type '(undefined)' must be const or comptime",
7154 "tmp.zig:8:4: error: variable of type 'comptime_int' must be const or comptime",
7155 "tmp.zig:8:4: note: to modify this variable at runtime, it must be given an explicit fixed-size number type",
7156 "tmp.zig:11:4: error: variable of type 'comptime_float' must be const or comptime",
7157 "tmp.zig:11:4: note: to modify this variable at runtime, it must be given an explicit fixed-size number type",
7158 "tmp.zig:14:4: error: variable of type '(null)' must be const or comptime",
7159 "tmp.zig:17:4: error: variable of type 'Opaque' not allowed",
7160 "tmp.zig:20:4: error: variable of type 'type' must be const or comptime",
7161 "tmp.zig:23:4: error: variable of type '(bound fn(*const Foo) void)' must be const or comptime",
7162 });
7163
7164 cases.add("variable with type 'noreturn'",
7384 "tmp.zig:6:4: error: variable of type '(undefined)' must be const or comptime",
7385 "tmp.zig:10:4: error: variable of type 'comptime_int' must be const or comptime",
7386 "tmp.zig:10:4: note: to modify this variable at runtime, it must be given an explicit fixed-size number type",
7387 "tmp.zig:14:4: error: variable of type 'comptime_float' must be const or comptime",
7388 "tmp.zig:14:4: note: to modify this variable at runtime, it must be given an explicit fixed-size number type",
7389 "tmp.zig:18:4: error: variable of type '(null)' must be const or comptime",
7390 "tmp.zig:22:4: error: variable of type 'Opaque' not allowed",
7391 "tmp.zig:26:4: error: variable of type 'type' must be const or comptime",
7392 "tmp.zig:30:4: error: variable of type '(bound fn(*const Foo) void)' must be const or comptime",
7393 });
7394
7395 ctx.objErrStage1("variable with type 'noreturn'",
71657396 \\export fn entry9() void {
71667397 \\ var z: noreturn = return;
71677398 \\}
......@@ -7170,7 +7401,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
71707401 "tmp.zig:2:23: note: control flow is diverted here",
71717402 });
71727403
7173 cases.add("wrong types given to atomic order args in cmpxchg",
7404 ctx.objErrStage1("wrong types given to atomic order args in cmpxchg",
71747405 \\export fn entry() void {
71757406 \\ var x: i32 = 1234;
71767407 \\ while (!@cmpxchgWeak(i32, &x, 1234, 5678, @as(u32, 1234), @as(u32, 1234))) {}
......@@ -7179,7 +7410,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
71797410 "tmp.zig:3:47: error: expected type 'std.builtin.AtomicOrder', found 'u32'",
71807411 });
71817412
7182 cases.add("wrong types given to @export",
7413 ctx.objErrStage1("wrong types given to @export",
71837414 \\fn entry() callconv(.C) void { }
71847415 \\comptime {
71857416 \\ @export(entry, .{.name = "entry", .linkage = @as(u32, 1234) });
......@@ -7188,7 +7419,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
71887419 "tmp.zig:3:59: error: expected type 'std.builtin.GlobalLinkage', found 'comptime_int'",
71897420 });
71907421
7191 cases.add("struct with invalid field",
7422 ctx.objErrStage1("struct with invalid field",
71927423 \\const std = @import("std",);
71937424 \\const Allocator = std.mem.Allocator;
71947425 \\const ArrayList = std.ArrayList;
......@@ -7211,12 +7442,13 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
72117442 \\ .text = MdText.init(&std.testing.allocator),
72127443 \\ .weight = HeaderWeight.H1,
72137444 \\ };
7445 \\ _ = a;
72147446 \\}
72157447 , &[_][]const u8{
72167448 "tmp.zig:14:17: error: use of undeclared identifier 'HeaderValue'",
72177449 });
72187450
7219 cases.add("@setAlignStack outside function",
7451 ctx.objErrStage1("@setAlignStack outside function",
72207452 \\comptime {
72217453 \\ @setAlignStack(16);
72227454 \\}
......@@ -7224,7 +7456,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
72247456 "tmp.zig:2:5: error: @setAlignStack outside function",
72257457 });
72267458
7227 cases.add("@setAlignStack in naked function",
7459 ctx.objErrStage1("@setAlignStack in naked function",
72287460 \\export fn entry() callconv(.Naked) void {
72297461 \\ @setAlignStack(16);
72307462 \\}
......@@ -7232,7 +7464,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
72327464 "tmp.zig:2:5: error: @setAlignStack in naked function",
72337465 });
72347466
7235 cases.add("@setAlignStack in inline function",
7467 ctx.objErrStage1("@setAlignStack in inline function",
72367468 \\export fn entry() void {
72377469 \\ foo();
72387470 \\}
......@@ -7243,7 +7475,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
72437475 "tmp.zig:5:5: error: @setAlignStack in inline function",
72447476 });
72457477
7246 cases.add("@setAlignStack set twice",
7478 ctx.objErrStage1("@setAlignStack set twice",
72477479 \\export fn entry() void {
72487480 \\ @setAlignStack(16);
72497481 \\ @setAlignStack(16);
......@@ -7253,7 +7485,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
72537485 "tmp.zig:2:5: note: first set here",
72547486 });
72557487
7256 cases.add("@setAlignStack too big",
7488 ctx.objErrStage1("@setAlignStack too big",
72577489 \\export fn entry() void {
72587490 \\ @setAlignStack(511 + 1);
72597491 \\}
......@@ -7261,7 +7493,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
72617493 "tmp.zig:2:5: error: attempt to @setAlignStack(512); maximum is 256",
72627494 });
72637495
7264 cases.add("storing runtime value in compile time variable then using it",
7496 ctx.objErrStage1("storing runtime value in compile time variable then using it",
72657497 \\const Mode = @import("std").builtin.Mode;
72667498 \\
72677499 \\fn Free(comptime filename: []const u8) TestCase {
......@@ -7307,7 +7539,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
73077539 "tmp.zig:37:29: error: cannot store runtime value in compile time variable",
73087540 });
73097541
7310 cases.add("invalid legacy unicode escape",
7542 ctx.objErrStage1("invalid legacy unicode escape",
73117543 \\export fn entry() void {
73127544 \\ const a = '\U1234';
73137545 \\}
......@@ -7315,7 +7547,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
73157547 "tmp.zig:2:17: error: invalid character: 'U'",
73167548 });
73177549
7318 cases.add("invalid empty unicode escape",
7550 ctx.objErrStage1("invalid empty unicode escape",
73197551 \\export fn entry() void {
73207552 \\ const a = '\u{}';
73217553 \\}
......@@ -7323,21 +7555,20 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
73237555 "tmp.zig:2:19: error: empty unicode escape sequence",
73247556 });
73257557
7326 cases.add("non-printable invalid character", "\xff\xfe" ++
7327 \\fn test() bool {\r
7328 \\ true\r
7329 \\}
7330 , &[_][]const u8{
7558 ctx.objErrStage1("non-printable invalid character", "\xff\xfe" ++
7559 "fn foo() bool {\r\n" ++
7560 " return true;\r\n" ++
7561 "}\r\n", &[_][]const u8{
73317562 "tmp.zig:1:1: error: invalid character: '\\xff'",
73327563 });
73337564
7334 cases.add("non-printable invalid character with escape alternative", "fn test() bool {\n" ++
7335 "\ttrue\n" ++
7565 ctx.objErrStage1("non-printable invalid character with escape alternative", "fn foo() bool {\n" ++
7566 "\treturn true;\n" ++
73367567 "}\n", &[_][]const u8{
73377568 "tmp.zig:2:1: error: invalid character: '\\t'",
73387569 });
73397570
7340 cases.add("calling var args extern function, passing array instead of pointer",
7571 ctx.objErrStage1("calling var args extern function, passing array instead of pointer",
73417572 \\export fn entry() void {
73427573 \\ foo("hello".*,);
73437574 \\}
......@@ -7346,7 +7577,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
73467577 "tmp.zig:2:16: error: expected type '*const u8', found '[5:0]u8'",
73477578 });
73487579
7349 cases.add("constant inside comptime function has compile error",
7580 ctx.objErrStage1("constant inside comptime function has compile error",
73507581 \\const ContextAllocator = MemoryPool(usize);
73517582 \\
73527583 \\pub fn MemoryPool(comptime T: type) type {
......@@ -7361,12 +7592,12 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
73617592 \\ var allocator: ContextAllocator = undefined;
73627593 \\}
73637594 , &[_][]const u8{
7364 "tmp.zig:4:25: error: aoeu",
7365 "tmp.zig:1:36: note: referenced here",
7366 "tmp.zig:12:20: note: referenced here",
7595 "tmp.zig:4:5: error: unreachable code",
7596 "tmp.zig:4:25: note: control flow is diverted here",
7597 "tmp.zig:12:9: error: unused local variable",
73677598 });
73687599
7369 cases.add("specify enum tag type that is too small",
7600 ctx.objErrStage1("specify enum tag type that is too small",
73707601 \\const Small = enum (u2) {
73717602 \\ One,
73727603 \\ Two,
......@@ -7377,12 +7608,13 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
73777608 \\
73787609 \\export fn entry() void {
73797610 \\ var x = Small.One;
7611 \\ _ = x;
73807612 \\}
73817613 , &[_][]const u8{
73827614 "tmp.zig:6:5: error: enumeration value 4 too large for type 'u2'",
73837615 });
73847616
7385 cases.add("specify non-integer enum tag type",
7617 ctx.objErrStage1("specify non-integer enum tag type",
73867618 \\const Small = enum (f32) {
73877619 \\ One,
73887620 \\ Two,
......@@ -7391,12 +7623,13 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
73917623 \\
73927624 \\export fn entry() void {
73937625 \\ var x = Small.One;
7626 \\ _ = x;
73947627 \\}
73957628 , &[_][]const u8{
73967629 "tmp.zig:1:21: error: expected integer, found 'f32'",
73977630 });
73987631
7399 cases.add("implicitly casting enum to tag type",
7632 ctx.objErrStage1("implicitly casting enum to tag type",
74007633 \\const Small = enum(u2) {
74017634 \\ One,
74027635 \\ Two,
......@@ -7406,12 +7639,13 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
74067639 \\
74077640 \\export fn entry() void {
74087641 \\ var x: u2 = Small.Two;
7642 \\ _ = x;
74097643 \\}
74107644 , &[_][]const u8{
74117645 "tmp.zig:9:22: error: expected type 'u2', found 'Small'",
74127646 });
74137647
7414 cases.add("explicitly casting non tag type to enum",
7648 ctx.objErrStage1("explicitly casting non tag type to enum",
74157649 \\const Small = enum(u2) {
74167650 \\ One,
74177651 \\ Two,
......@@ -7422,24 +7656,26 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
74227656 \\export fn entry() void {
74237657 \\ var y = @as(u3, 3);
74247658 \\ var x = @intToEnum(Small, y);
7659 \\ _ = x;
74257660 \\}
74267661 , &[_][]const u8{
74277662 "tmp.zig:10:31: error: expected type 'u2', found 'u3'",
74287663 });
74297664
7430 cases.add("union fields with value assignments",
7665 ctx.objErrStage1("union fields with value assignments",
74317666 \\const MultipleChoice = union {
74327667 \\ A: i32 = 20,
74337668 \\};
74347669 \\export fn entry() void {
74357670 \\ var x: MultipleChoice = undefined;
7671 \\ _ = x;
74367672 \\}
74377673 , &[_][]const u8{
74387674 "tmp.zig:2:14: error: untagged union field assignment",
74397675 "tmp.zig:1:24: note: consider 'union(enum)' here",
74407676 });
74417677
7442 cases.add("enum with 0 fields",
7678 ctx.objErrStage1("enum with 0 fields",
74437679 \\const Foo = enum {};
74447680 \\export fn entry() usize {
74457681 \\ return @sizeOf(Foo);
......@@ -7448,16 +7684,16 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
74487684 "tmp.zig:1:13: error: enums must have 1 or more fields",
74497685 });
74507686
7451 cases.add("union with 0 fields",
7687 ctx.objErrStage1("union with 0 fields",
74527688 \\const Foo = union {};
74537689 \\export fn entry() usize {
74547690 \\ return @sizeOf(Foo);
74557691 \\}
74567692 , &[_][]const u8{
7457 "tmp.zig:1:13: error: unions must have 1 or more fields",
7693 "tmp.zig:1:13: error: union declarations must have at least one tag",
74587694 });
74597695
7460 cases.add("enum value already taken",
7696 ctx.objErrStage1("enum value already taken",
74617697 \\const MultipleChoice = enum(u32) {
74627698 \\ A = 20,
74637699 \\ B = 40,
......@@ -7467,13 +7703,14 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
74677703 \\};
74687704 \\export fn entry() void {
74697705 \\ var x = MultipleChoice.C;
7706 \\ _ = x;
74707707 \\}
74717708 , &[_][]const u8{
74727709 "tmp.zig:6:5: error: enum tag value 60 already taken",
74737710 "tmp.zig:4:5: note: other occurrence here",
74747711 });
74757712
7476 cases.add("union with specified enum omits field",
7713 ctx.objErrStage1("union with specified enum omits field",
74777714 \\const Letter = enum {
74787715 \\ A,
74797716 \\ B,
......@@ -7491,29 +7728,31 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
74917728 "tmp.zig:4:5: note: declared here",
74927729 });
74937730
7494 cases.add("non-integer tag type to automatic union enum",
7731 ctx.objErrStage1("non-integer tag type to automatic union enum",
74957732 \\const Foo = union(enum(f32)) {
74967733 \\ A: i32,
74977734 \\};
74987735 \\export fn entry() void {
74997736 \\ const x = @typeInfo(Foo).Union.tag_type.?;
7737 \\ _ = x;
75007738 \\}
75017739 , &[_][]const u8{
75027740 "tmp.zig:1:24: error: expected integer tag type, found 'f32'",
75037741 });
75047742
7505 cases.add("non-enum tag type passed to union",
7743 ctx.objErrStage1("non-enum tag type passed to union",
75067744 \\const Foo = union(u32) {
75077745 \\ A: i32,
75087746 \\};
75097747 \\export fn entry() void {
75107748 \\ const x = @typeInfo(Foo).Union.tag_type.?;
7749 \\ _ = x;
75117750 \\}
75127751 , &[_][]const u8{
75137752 "tmp.zig:1:19: error: expected enum tag type, found 'u32'",
75147753 });
75157754
7516 cases.add("union auto-enum value already taken",
7755 ctx.objErrStage1("union auto-enum value already taken",
75177756 \\const MultipleChoice = union(enum(u32)) {
75187757 \\ A = 20,
75197758 \\ B = 40,
......@@ -7523,13 +7762,14 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
75237762 \\};
75247763 \\export fn entry() void {
75257764 \\ var x = MultipleChoice { .C = {} };
7765 \\ _ = x;
75267766 \\}
75277767 , &[_][]const u8{
75287768 "tmp.zig:6:9: error: enum tag value 60 already taken",
75297769 "tmp.zig:4:9: note: other occurrence here",
75307770 });
75317771
7532 cases.add("union enum field does not match enum",
7772 ctx.objErrStage1("union enum field does not match enum",
75337773 \\const Letter = enum {
75347774 \\ A,
75357775 \\ B,
......@@ -7543,13 +7783,14 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
75437783 \\};
75447784 \\export fn entry() void {
75457785 \\ var a = Payload {.A = 1234};
7786 \\ _ = a;
75467787 \\}
75477788 , &[_][]const u8{
75487789 "tmp.zig:10:5: error: enum field not found: 'D'",
75497790 "tmp.zig:1:16: note: enum declared here",
75507791 });
75517792
7552 cases.add("field type supplied in an enum",
7793 ctx.objErrStage1("field type supplied in an enum",
75537794 \\const Letter = enum {
75547795 \\ A: void,
75557796 \\ B,
......@@ -7557,35 +7798,38 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
75577798 \\};
75587799 \\export fn entry() void {
75597800 \\ var b = Letter.B;
7801 \\ _ = b;
75607802 \\}
75617803 , &[_][]const u8{
75627804 "tmp.zig:2:8: error: structs and unions, not enums, support field types",
75637805 "tmp.zig:1:16: note: consider 'union(enum)' here",
75647806 });
75657807
7566 cases.add("struct field missing type",
7808 ctx.objErrStage1("struct field missing type",
75677809 \\const Letter = struct {
75687810 \\ A,
75697811 \\};
75707812 \\export fn entry() void {
75717813 \\ var a = Letter { .A = {} };
7814 \\ _ = a;
75727815 \\}
75737816 , &[_][]const u8{
75747817 "tmp.zig:2:5: error: struct field missing type",
75757818 });
75767819
7577 cases.add("extern union field missing type",
7820 ctx.objErrStage1("extern union field missing type",
75787821 \\const Letter = extern union {
75797822 \\ A,
75807823 \\};
75817824 \\export fn entry() void {
75827825 \\ var a = Letter { .A = {} };
7826 \\ _ = a;
75837827 \\}
75847828 , &[_][]const u8{
75857829 "tmp.zig:2:5: error: union field missing type",
75867830 });
75877831
7588 cases.add("extern union given enum tag type",
7832 ctx.objErrStage1("extern union given enum tag type",
75897833 \\const Letter = enum {
75907834 \\ A,
75917835 \\ B,
......@@ -7598,12 +7842,13 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
75987842 \\};
75997843 \\export fn entry() void {
76007844 \\ var a = Payload { .A = 1234 };
7845 \\ _ = a;
76017846 \\}
76027847 , &[_][]const u8{
76037848 "tmp.zig:6:30: error: extern union does not support enum tag type",
76047849 });
76057850
7606 cases.add("packed union given enum tag type",
7851 ctx.objErrStage1("packed union given enum tag type",
76077852 \\const Letter = enum {
76087853 \\ A,
76097854 \\ B,
......@@ -7616,12 +7861,13 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
76167861 \\};
76177862 \\export fn entry() void {
76187863 \\ var a = Payload { .A = 1234 };
7864 \\ _ = a;
76197865 \\}
76207866 , &[_][]const u8{
76217867 "tmp.zig:6:30: error: packed union does not support enum tag type",
76227868 });
76237869
7624 cases.add("packed union with automatic layout field",
7870 ctx.objErrStage1("packed union with automatic layout field",
76257871 \\const Foo = struct {
76267872 \\ a: u32,
76277873 \\ b: f32,
......@@ -7632,12 +7878,13 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
76327878 \\};
76337879 \\export fn entry() void {
76347880 \\ var a = Payload { .B = true };
7881 \\ _ = a;
76357882 \\}
76367883 , &[_][]const u8{
76377884 "tmp.zig:6:5: error: non-packed, non-extern struct 'Foo' not allowed in packed union; no guaranteed in-memory representation",
76387885 });
76397886
7640 cases.add("switch on union with no attached enum",
7887 ctx.objErrStage1("switch on union with no attached enum",
76417888 \\const Payload = union {
76427889 \\ A: i32,
76437890 \\ B: f64,
......@@ -7658,20 +7905,21 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
76587905 "tmp.zig:1:17: note: consider 'union(enum)' here",
76597906 });
76607907
7661 cases.add("enum in field count range but not matching tag",
7908 ctx.objErrStage1("enum in field count range but not matching tag",
76627909 \\const Foo = enum(u32) {
76637910 \\ A = 10,
76647911 \\ B = 11,
76657912 \\};
76667913 \\export fn entry() void {
76677914 \\ var x = @intToEnum(Foo, 0);
7915 \\ _ = x;
76687916 \\}
76697917 , &[_][]const u8{
76707918 "tmp.zig:6:13: error: enum 'Foo' has no tag matching integer value 0",
76717919 "tmp.zig:1:13: note: 'Foo' declared here",
76727920 });
76737921
7674 cases.add("comptime cast enum to union but field has payload",
7922 ctx.objErrStage1("comptime cast enum to union but field has payload",
76757923 \\const Letter = enum { A, B, C };
76767924 \\const Value = union(Letter) {
76777925 \\ A: i32,
......@@ -7680,13 +7928,14 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
76807928 \\};
76817929 \\export fn entry() void {
76827930 \\ var x: Value = Letter.A;
7931 \\ _ = x;
76837932 \\}
76847933 , &[_][]const u8{
76857934 "tmp.zig:8:26: error: cast to union 'Value' must initialize 'i32' field 'A'",
76867935 "tmp.zig:3:5: note: field 'A' declared here",
76877936 });
76887937
7689 cases.add("runtime cast to union which has non-void fields",
7938 ctx.objErrStage1("runtime cast to union which has non-void fields",
76907939 \\const Letter = enum { A, B, C };
76917940 \\const Value = union(Letter) {
76927941 \\ A: i32,
......@@ -7698,13 +7947,14 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
76987947 \\}
76997948 \\fn foo(l: Letter) void {
77007949 \\ var x: Value = l;
7950 \\ _ = x;
77017951 \\}
77027952 , &[_][]const u8{
77037953 "tmp.zig:11:20: error: runtime cast to union 'Value' which has non-void fields",
77047954 "tmp.zig:3:5: note: field 'A' has type 'i32'",
77057955 });
77067956
7707 cases.add("taking byte offset of void field in struct",
7957 ctx.objErrStage1("taking byte offset of void field in struct",
77087958 \\const Empty = struct {
77097959 \\ val: void,
77107960 \\};
......@@ -7715,7 +7965,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
77157965 "tmp.zig:5:42: error: zero-bit field 'val' in struct 'Empty' has no offset",
77167966 });
77177967
7718 cases.add("taking bit offset of void field in struct",
7968 ctx.objErrStage1("taking bit offset of void field in struct",
77197969 \\const Empty = struct {
77207970 \\ val: void,
77217971 \\};
......@@ -7726,7 +7976,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
77267976 "tmp.zig:5:45: error: zero-bit field 'val' in struct 'Empty' has no offset",
77277977 });
77287978
7729 cases.add("invalid union field access in comptime",
7979 ctx.objErrStage1("invalid union field access in comptime",
77307980 \\const Foo = union {
77317981 \\ Bar: u8,
77327982 \\ Baz: void,
......@@ -7739,7 +7989,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
77397989 "tmp.zig:7:24: error: accessing union field 'Bar' while field 'Baz' is set",
77407990 });
77417991
7742 cases.add("unsupported modifier at start of asm output constraint",
7992 ctx.objErrStage1("unsupported modifier at start of asm output constraint",
77437993 \\export fn foo() void {
77447994 \\ var bar: u32 = 3;
77457995 \\ asm volatile ("" : [baz]"+r"(bar) : : "");
......@@ -7748,7 +7998,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
77487998 "tmp.zig:3:5: error: invalid modifier starting output constraint for 'baz': '+', only '=' is supported. Compiler TODO: see https://github.com/ziglang/zig/issues/215",
77497999 });
77508000
7751 cases.add("comptime_int in asm input",
8001 ctx.objErrStage1("comptime_int in asm input",
77528002 \\export fn foo() void {
77538003 \\ asm volatile ("" : : [bar]"r"(3) : "");
77548004 \\}
......@@ -7756,7 +8006,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
77568006 "tmp.zig:2:35: error: expected sized integer or sized float, found comptime_int",
77578007 });
77588008
7759 cases.add("comptime_float in asm input",
8009 ctx.objErrStage1("comptime_float in asm input",
77608010 \\export fn foo() void {
77618011 \\ asm volatile ("" : : [bar]"r"(3.17) : "");
77628012 \\}
......@@ -7764,7 +8014,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
77648014 "tmp.zig:2:35: error: expected sized integer or sized float, found comptime_float",
77658015 });
77668016
7767 cases.add("runtime assignment to comptime struct type",
8017 ctx.objErrStage1("runtime assignment to comptime struct type",
77688018 \\const Foo = struct {
77698019 \\ Bar: u8,
77708020 \\ Baz: type,
......@@ -7772,12 +8022,13 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
77728022 \\export fn f() void {
77738023 \\ var x: u8 = 0;
77748024 \\ const foo = Foo { .Bar = x, .Baz = u8 };
8025 \\ _ = foo;
77758026 \\}
77768027 , &[_][]const u8{
77778028 "tmp.zig:7:23: error: unable to evaluate constant expression",
77788029 });
77798030
7780 cases.add("runtime assignment to comptime union type",
8031 ctx.objErrStage1("runtime assignment to comptime union type",
77818032 \\const Foo = union {
77828033 \\ Bar: u8,
77838034 \\ Baz: type,
......@@ -7785,16 +8036,18 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
77858036 \\export fn f() void {
77868037 \\ var x: u8 = 0;
77878038 \\ const foo = Foo { .Bar = x };
8039 \\ _ = foo;
77888040 \\}
77898041 , &[_][]const u8{
77908042 "tmp.zig:7:23: error: unable to evaluate constant expression",
77918043 });
77928044
7793 cases.addTest("@shuffle with selected index past first vector length",
8045 ctx.testErrStage1("@shuffle with selected index past first vector length",
77948046 \\export fn entry() void {
77958047 \\ const v: @import("std").meta.Vector(4, u32) = [4]u32{ 10, 11, 12, 13 };
77968048 \\ const x: @import("std").meta.Vector(4, u32) = [4]u32{ 14, 15, 16, 17 };
77978049 \\ var z = @shuffle(u32, v, x, [8]i32{ 0, 1, 2, 3, 7, 6, 5, 4 });
8050 \\ _ = z;
77988051 \\}
77998052 , &[_][]const u8{
78008053 "tmp.zig:4:39: error: mask index '4' has out-of-bounds selection",
......@@ -7802,27 +8055,29 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
78028055 "tmp.zig:4:30: note: selections from the second vector are specified with negative numbers",
78038056 });
78048057
7805 cases.addTest("nested vectors",
8058 ctx.testErrStage1("nested vectors",
78068059 \\export fn entry() void {
78078060 \\ const V1 = @import("std").meta.Vector(4, u8);
78088061 \\ const V2 = @Type(@import("std").builtin.TypeInfo{ .Vector = .{ .len = 4, .child = V1 } });
78098062 \\ var v: V2 = undefined;
8063 \\ _ = v;
78108064 \\}
78118065 , &[_][]const u8{
78128066 "tmp.zig:3:53: error: vector element type must be integer, float, bool, or pointer; '@Vector(4, u8)' is invalid",
78138067 "tmp.zig:3:16: note: referenced here",
78148068 });
78158069
7816 cases.addTest("bad @splat type",
8070 ctx.testErrStage1("bad @splat type",
78178071 \\export fn entry() void {
78188072 \\ const c = 4;
78198073 \\ var v = @splat(4, c);
8074 \\ _ = v;
78208075 \\}
78218076 , &[_][]const u8{
78228077 "tmp.zig:3:23: error: vector element type must be integer, float, bool, or pointer; 'comptime_int' is invalid",
78238078 });
78248079
7825 cases.add("compileLog of tagged enum doesn't crash the compiler",
8080 ctx.objErrStage1("compileLog of tagged enum doesn't crash the compiler",
78268081 \\const Bar = union(enum(u32)) {
78278082 \\ X: i32 = 1
78288083 \\};
......@@ -7838,7 +8093,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
78388093 "tmp.zig:6:5: error: found compile log statement",
78398094 });
78408095
7841 cases.add("attempted implicit cast from *const T to *[1]T",
8096 ctx.objErrStage1("attempted implicit cast from *const T to *[1]T",
78428097 \\export fn entry(byte: u8) void {
78438098 \\ const w: i32 = 1234;
78448099 \\ var x: *const i32 = &w;
......@@ -7850,16 +8105,17 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
78508105 "tmp.zig:4:22: note: cast discards const qualifier",
78518106 });
78528107
7853 cases.add("attempted implicit cast from *const T to []T",
8108 ctx.objErrStage1("attempted implicit cast from *const T to []T",
78548109 \\export fn entry() void {
78558110 \\ const u: u32 = 42;
78568111 \\ const x: []u32 = &u;
8112 \\ _ = x;
78578113 \\}
78588114 , &[_][]const u8{
78598115 "tmp.zig:3:23: error: expected type '[]u32', found '*const u32'",
78608116 });
78618117
7862 cases.add("for loop body expression ignored",
8118 ctx.objErrStage1("for loop body expression ignored",
78638119 \\fn returns() usize {
78648120 \\ return 2;
78658121 \\}
......@@ -7875,7 +8131,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
78758131 "tmp.zig:9:30: error: expression value is ignored",
78768132 });
78778133
7878 cases.add("aligned variable of zero-bit type",
8134 ctx.objErrStage1("aligned variable of zero-bit type",
78798135 \\export fn f() void {
78808136 \\ var s: struct {} align(4) = undefined;
78818137 \\}
......@@ -7883,7 +8139,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
78838139 "tmp.zig:2:5: error: variable 's' of zero-bit type 'struct:2:12' has no in-memory representation, it cannot be aligned",
78848140 });
78858141
7886 cases.add("function returning opaque type",
8142 ctx.objErrStage1("function returning opaque type",
78878143 \\const FooType = opaque {};
78888144 \\export fn bar() !FooType {
78898145 \\ return error.InvalidValue;
......@@ -7901,7 +8157,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
79018157 "tmp.zig:8:18: error: Undefined return type '(undefined)' not allowed",
79028158 });
79038159
7904 cases.add("generic function returning opaque type",
8160 ctx.objErrStage1("generic function returning opaque type",
79058161 \\const FooType = opaque {};
79068162 \\fn generic(comptime T: type) !T {
79078163 \\ return undefined;
......@@ -7925,22 +8181,24 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
79258181 "tmp.zig:2:1: note: function declared here",
79268182 });
79278183
7928 cases.add("function parameter is opaque",
8184 ctx.objErrStage1("function parameter is opaque",
79298185 \\const FooType = opaque {};
79308186 \\export fn entry1() void {
79318187 \\ const someFuncPtr: fn (FooType) void = undefined;
8188 \\ _ = someFuncPtr;
79328189 \\}
79338190 \\
79348191 \\export fn entry2() void {
79358192 \\ const someFuncPtr: fn (@TypeOf(null)) void = undefined;
8193 \\ _ = someFuncPtr;
79368194 \\}
79378195 \\
7938 \\fn foo(p: FooType) void {}
8196 \\fn foo(p: FooType) void {_ = p;}
79398197 \\export fn entry3() void {
79408198 \\ _ = foo;
79418199 \\}
79428200 \\
7943 \\fn bar(p: @TypeOf(null)) void {}
8201 \\fn bar(p: @TypeOf(null)) void {_ = p;}
79448202 \\export fn entry4() void {
79458203 \\ _ = bar;
79468204 \\}
......@@ -7951,30 +8209,34 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
79518209 "tmp.zig:15:11: error: parameter of type '(null)' not allowed",
79528210 });
79538211
7954 cases.add( // fixed bug #2032
8212 ctx.objErrStage1( // fixed bug #2032
79558213 "compile diagnostic string for top level decl type",
79568214 \\export fn entry() void {
79578215 \\ var foo: u32 = @This(){};
8216 \\ _ = foo;
79588217 \\}
79598218 , &[_][]const u8{
79608219 "tmp.zig:2:27: error: type 'u32' does not support array initialization",
79618220 });
79628221
7963 cases.add("issue #2687: coerce from undefined array pointer to slice",
8222 ctx.objErrStage1("issue #2687: coerce from undefined array pointer to slice",
79648223 \\export fn foo1() void {
79658224 \\ const a: *[1]u8 = undefined;
79668225 \\ var b: []u8 = a;
8226 \\ _ = b;
79678227 \\}
79688228 \\export fn foo2() void {
79698229 \\ comptime {
79708230 \\ var a: *[1]u8 = undefined;
79718231 \\ var b: []u8 = a;
8232 \\ _ = b;
79728233 \\ }
79738234 \\}
79748235 \\export fn foo3() void {
79758236 \\ comptime {
79768237 \\ const a: *[1]u8 = undefined;
79778238 \\ var b: []u8 = a;
8239 \\ _ = b;
79788240 \\ }
79798241 \\}
79808242 , &[_][]const u8{
......@@ -7983,14 +8245,16 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
79838245 "tmp.zig:14:23: error: use of undefined value here causes undefined behavior",
79848246 });
79858247
7986 cases.add("issue #3818: bitcast from parray/slice to u16",
8248 ctx.objErrStage1("issue #3818: bitcast from parray/slice to u16",
79878249 \\export fn foo1() void {
79888250 \\ var bytes = [_]u8{1, 2};
79898251 \\ const word: u16 = @bitCast(u16, bytes[0..]);
8252 \\ _ = word;
79908253 \\}
79918254 \\export fn foo2() void {
79928255 \\ var bytes: []const u8 = &[_]u8{1, 2};
79938256 \\ const word: u16 = @bitCast(u16, bytes);
8257 \\ _ = word;
79948258 \\}
79958259 , &[_][]const u8{
79968260 "tmp.zig:3:42: error: unable to @bitCast from pointer type '*[2]u8'",
......@@ -7999,7 +8263,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
79998263 });
80008264
80018265 // issue #7810
8002 cases.add("comptime slice-len increment beyond bounds",
8266 ctx.objErrStage1("comptime slice-len increment beyond bounds",
80038267 \\export fn foo_slice_len_increment_beyond_bounds() void {
80048268 \\ comptime {
80058269 \\ var buf_storage: [8]u8 = undefined;
......@@ -8012,11 +8276,12 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
80128276 ":6:12: error: out of bounds slice",
80138277 });
80148278
8015 cases.add("comptime slice-sentinel is out of bounds (unterminated)",
8279 ctx.objErrStage1("comptime slice-sentinel is out of bounds (unterminated)",
80168280 \\export fn foo_array() void {
80178281 \\ comptime {
80188282 \\ var target = [_]u8{ 'a', 'b', 'c', 'd' } ++ [_]u8{undefined} ** 10;
80198283 \\ const slice = target[0..14 :0];
8284 \\ _ = slice;
80208285 \\ }
80218286 \\}
80228287 \\export fn foo_ptr_array() void {
......@@ -8024,6 +8289,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
80248289 \\ var buf = [_]u8{ 'a', 'b', 'c', 'd' } ++ [_]u8{undefined} ** 10;
80258290 \\ var target = &buf;
80268291 \\ const slice = target[0..14 :0];
8292 \\ _ = slice;
80278293 \\ }
80288294 \\}
80298295 \\export fn foo_vector_ConstPtrSpecialBaseArray() void {
......@@ -8031,6 +8297,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
80318297 \\ var buf = [_]u8{ 'a', 'b', 'c', 'd' } ++ [_]u8{undefined} ** 10;
80328298 \\ var target: [*]u8 = &buf;
80338299 \\ const slice = target[0..14 :0];
8300 \\ _ = slice;
80348301 \\ }
80358302 \\}
80368303 \\export fn foo_vector_ConstPtrSpecialRef() void {
......@@ -8038,6 +8305,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
80388305 \\ var buf = [_]u8{ 'a', 'b', 'c', 'd' } ++ [_]u8{undefined} ** 10;
80398306 \\ var target: [*]u8 = @ptrCast([*]u8, &buf);
80408307 \\ const slice = target[0..14 :0];
8308 \\ _ = slice;
80418309 \\ }
80428310 \\}
80438311 \\export fn foo_cvector_ConstPtrSpecialBaseArray() void {
......@@ -8045,6 +8313,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
80458313 \\ var buf = [_]u8{ 'a', 'b', 'c', 'd' } ++ [_]u8{undefined} ** 10;
80468314 \\ var target: [*c]u8 = &buf;
80478315 \\ const slice = target[0..14 :0];
8316 \\ _ = slice;
80488317 \\ }
80498318 \\}
80508319 \\export fn foo_cvector_ConstPtrSpecialRef() void {
......@@ -8052,6 +8321,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
80528321 \\ var buf = [_]u8{ 'a', 'b', 'c', 'd' } ++ [_]u8{undefined} ** 10;
80538322 \\ var target: [*c]u8 = @ptrCast([*c]u8, &buf);
80548323 \\ const slice = target[0..14 :0];
8324 \\ _ = slice;
80558325 \\ }
80568326 \\}
80578327 \\export fn foo_slice() void {
......@@ -8059,6 +8329,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
80598329 \\ var buf = [_]u8{ 'a', 'b', 'c', 'd' } ++ [_]u8{undefined} ** 10;
80608330 \\ var target: []u8 = &buf;
80618331 \\ const slice = target[0..14 :0];
8332 \\ _ = slice;
80628333 \\ }
80638334 \\}
80648335 , &[_][]const u8{
......@@ -8071,11 +8342,12 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
80718342 ":46:29: error: slice-sentinel is out of bounds",
80728343 });
80738344
8074 cases.add("comptime slice-sentinel is out of bounds (terminated)",
8345 ctx.objErrStage1("comptime slice-sentinel is out of bounds (terminated)",
80758346 \\export fn foo_array() void {
80768347 \\ comptime {
80778348 \\ var target = [_:0]u8{ 'a', 'b', 'c', 'd' } ++ [_]u8{undefined} ** 10;
80788349 \\ const slice = target[0..15 :1];
8350 \\ _ = slice;
80798351 \\ }
80808352 \\}
80818353 \\export fn foo_ptr_array() void {
......@@ -8083,6 +8355,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
80838355 \\ var buf = [_:0]u8{ 'a', 'b', 'c', 'd' } ++ [_]u8{undefined} ** 10;
80848356 \\ var target = &buf;
80858357 \\ const slice = target[0..15 :0];
8358 \\ _ = slice;
80868359 \\ }
80878360 \\}
80888361 \\export fn foo_vector_ConstPtrSpecialBaseArray() void {
......@@ -8090,6 +8363,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
80908363 \\ var buf = [_:0]u8{ 'a', 'b', 'c', 'd' } ++ [_]u8{undefined} ** 10;
80918364 \\ var target: [*]u8 = &buf;
80928365 \\ const slice = target[0..15 :0];
8366 \\ _ = slice;
80938367 \\ }
80948368 \\}
80958369 \\export fn foo_vector_ConstPtrSpecialRef() void {
......@@ -8097,6 +8371,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
80978371 \\ var buf = [_:0]u8{ 'a', 'b', 'c', 'd' } ++ [_]u8{undefined} ** 10;
80988372 \\ var target: [*]u8 = @ptrCast([*]u8, &buf);
80998373 \\ const slice = target[0..15 :0];
8374 \\ _ = slice;
81008375 \\ }
81018376 \\}
81028377 \\export fn foo_cvector_ConstPtrSpecialBaseArray() void {
......@@ -8104,6 +8379,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
81048379 \\ var buf = [_:0]u8{ 'a', 'b', 'c', 'd' } ++ [_]u8{undefined} ** 10;
81058380 \\ var target: [*c]u8 = &buf;
81068381 \\ const slice = target[0..15 :0];
8382 \\ _ = slice;
81078383 \\ }
81088384 \\}
81098385 \\export fn foo_cvector_ConstPtrSpecialRef() void {
......@@ -8111,6 +8387,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
81118387 \\ var buf = [_:0]u8{ 'a', 'b', 'c', 'd' } ++ [_]u8{undefined} ** 10;
81128388 \\ var target: [*c]u8 = @ptrCast([*c]u8, &buf);
81138389 \\ const slice = target[0..15 :0];
8390 \\ _ = slice;
81148391 \\ }
81158392 \\}
81168393 \\export fn foo_slice() void {
......@@ -8118,6 +8395,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
81188395 \\ var buf = [_:0]u8{ 'a', 'b', 'c', 'd' } ++ [_]u8{undefined} ** 10;
81198396 \\ var target: []u8 = &buf;
81208397 \\ const slice = target[0..15 :0];
8398 \\ _ = slice;
81218399 \\ }
81228400 \\}
81238401 , &[_][]const u8{
......@@ -8130,11 +8408,12 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
81308408 ":46:29: error: out of bounds slice",
81318409 });
81328410
8133 cases.add("comptime slice-sentinel does not match memory at target index (unterminated)",
8411 ctx.objErrStage1("comptime slice-sentinel does not match memory at target index (unterminated)",
81348412 \\export fn foo_array() void {
81358413 \\ comptime {
81368414 \\ var target = [_]u8{ 'a', 'b', 'c', 'd' } ++ [_]u8{undefined} ** 10;
81378415 \\ const slice = target[0..3 :0];
8416 \\ _ = slice;
81388417 \\ }
81398418 \\}
81408419 \\export fn foo_ptr_array() void {
......@@ -8142,6 +8421,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
81428421 \\ var buf = [_]u8{ 'a', 'b', 'c', 'd' } ++ [_]u8{undefined} ** 10;
81438422 \\ var target = &buf;
81448423 \\ const slice = target[0..3 :0];
8424 \\ _ = slice;
81458425 \\ }
81468426 \\}
81478427 \\export fn foo_vector_ConstPtrSpecialBaseArray() void {
......@@ -8149,6 +8429,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
81498429 \\ var buf = [_]u8{ 'a', 'b', 'c', 'd' } ++ [_]u8{undefined} ** 10;
81508430 \\ var target: [*]u8 = &buf;
81518431 \\ const slice = target[0..3 :0];
8432 \\ _ = slice;
81528433 \\ }
81538434 \\}
81548435 \\export fn foo_vector_ConstPtrSpecialRef() void {
......@@ -8156,6 +8437,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
81568437 \\ var buf = [_]u8{ 'a', 'b', 'c', 'd' } ++ [_]u8{undefined} ** 10;
81578438 \\ var target: [*]u8 = @ptrCast([*]u8, &buf);
81588439 \\ const slice = target[0..3 :0];
8440 \\ _ = slice;
81598441 \\ }
81608442 \\}
81618443 \\export fn foo_cvector_ConstPtrSpecialBaseArray() void {
......@@ -8163,6 +8445,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
81638445 \\ var buf = [_]u8{ 'a', 'b', 'c', 'd' } ++ [_]u8{undefined} ** 10;
81648446 \\ var target: [*c]u8 = &buf;
81658447 \\ const slice = target[0..3 :0];
8448 \\ _ = slice;
81668449 \\ }
81678450 \\}
81688451 \\export fn foo_cvector_ConstPtrSpecialRef() void {
......@@ -8170,6 +8453,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
81708453 \\ var buf = [_]u8{ 'a', 'b', 'c', 'd' } ++ [_]u8{undefined} ** 10;
81718454 \\ var target: [*c]u8 = @ptrCast([*c]u8, &buf);
81728455 \\ const slice = target[0..3 :0];
8456 \\ _ = slice;
81738457 \\ }
81748458 \\}
81758459 \\export fn foo_slice() void {
......@@ -8177,6 +8461,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
81778461 \\ var buf = [_]u8{ 'a', 'b', 'c', 'd' } ++ [_]u8{undefined} ** 10;
81788462 \\ var target: []u8 = &buf;
81798463 \\ const slice = target[0..3 :0];
8464 \\ _ = slice;
81808465 \\ }
81818466 \\}
81828467 , &[_][]const u8{
......@@ -8189,11 +8474,12 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
81898474 ":46:29: error: slice-sentinel does not match memory at target index",
81908475 });
81918476
8192 cases.add("comptime slice-sentinel does not match memory at target index (terminated)",
8477 ctx.objErrStage1("comptime slice-sentinel does not match memory at target index (terminated)",
81938478 \\export fn foo_array() void {
81948479 \\ comptime {
81958480 \\ var target = [_:0]u8{ 'a', 'b', 'c', 'd' } ++ [_]u8{undefined} ** 10;
81968481 \\ const slice = target[0..3 :0];
8482 \\ _ = slice;
81978483 \\ }
81988484 \\}
81998485 \\export fn foo_ptr_array() void {
......@@ -8201,6 +8487,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
82018487 \\ var buf = [_:0]u8{ 'a', 'b', 'c', 'd' } ++ [_]u8{undefined} ** 10;
82028488 \\ var target = &buf;
82038489 \\ const slice = target[0..3 :0];
8490 \\ _ = slice;
82048491 \\ }
82058492 \\}
82068493 \\export fn foo_vector_ConstPtrSpecialBaseArray() void {
......@@ -8208,6 +8495,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
82088495 \\ var buf = [_:0]u8{ 'a', 'b', 'c', 'd' } ++ [_]u8{undefined} ** 10;
82098496 \\ var target: [*]u8 = &buf;
82108497 \\ const slice = target[0..3 :0];
8498 \\ _ = slice;
82118499 \\ }
82128500 \\}
82138501 \\export fn foo_vector_ConstPtrSpecialRef() void {
......@@ -8215,6 +8503,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
82158503 \\ var buf = [_:0]u8{ 'a', 'b', 'c', 'd' } ++ [_]u8{undefined} ** 10;
82168504 \\ var target: [*]u8 = @ptrCast([*]u8, &buf);
82178505 \\ const slice = target[0..3 :0];
8506 \\ _ = slice;
82188507 \\ }
82198508 \\}
82208509 \\export fn foo_cvector_ConstPtrSpecialBaseArray() void {
......@@ -8222,6 +8511,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
82228511 \\ var buf = [_:0]u8{ 'a', 'b', 'c', 'd' } ++ [_]u8{undefined} ** 10;
82238512 \\ var target: [*c]u8 = &buf;
82248513 \\ const slice = target[0..3 :0];
8514 \\ _ = slice;
82258515 \\ }
82268516 \\}
82278517 \\export fn foo_cvector_ConstPtrSpecialRef() void {
......@@ -8229,6 +8519,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
82298519 \\ var buf = [_:0]u8{ 'a', 'b', 'c', 'd' } ++ [_]u8{undefined} ** 10;
82308520 \\ var target: [*c]u8 = @ptrCast([*c]u8, &buf);
82318521 \\ const slice = target[0..3 :0];
8522 \\ _ = slice;
82328523 \\ }
82338524 \\}
82348525 \\export fn foo_slice() void {
......@@ -8236,6 +8527,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
82368527 \\ var buf = [_:0]u8{ 'a', 'b', 'c', 'd' } ++ [_]u8{undefined} ** 10;
82378528 \\ var target: []u8 = &buf;
82388529 \\ const slice = target[0..3 :0];
8530 \\ _ = slice;
82398531 \\ }
82408532 \\}
82418533 , &[_][]const u8{
......@@ -8248,11 +8540,12 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
82488540 ":46:29: error: slice-sentinel does not match memory at target index",
82498541 });
82508542
8251 cases.add("comptime slice-sentinel does not match target-sentinel",
8543 ctx.objErrStage1("comptime slice-sentinel does not match target-sentinel",
82528544 \\export fn foo_array() void {
82538545 \\ comptime {
82548546 \\ var target = [_:0]u8{ 'a', 'b', 'c', 'd' } ++ [_]u8{undefined} ** 10;
82558547 \\ const slice = target[0..14 :255];
8548 \\ _ = slice;
82568549 \\ }
82578550 \\}
82588551 \\export fn foo_ptr_array() void {
......@@ -8260,6 +8553,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
82608553 \\ var buf = [_:0]u8{ 'a', 'b', 'c', 'd' } ++ [_]u8{undefined} ** 10;
82618554 \\ var target = &buf;
82628555 \\ const slice = target[0..14 :255];
8556 \\ _ = slice;
82638557 \\ }
82648558 \\}
82658559 \\export fn foo_vector_ConstPtrSpecialBaseArray() void {
......@@ -8267,6 +8561,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
82678561 \\ var buf = [_:0]u8{ 'a', 'b', 'c', 'd' } ++ [_]u8{undefined} ** 10;
82688562 \\ var target: [*]u8 = &buf;
82698563 \\ const slice = target[0..14 :255];
8564 \\ _ = slice;
82708565 \\ }
82718566 \\}
82728567 \\export fn foo_vector_ConstPtrSpecialRef() void {
......@@ -8274,6 +8569,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
82748569 \\ var buf = [_:0]u8{ 'a', 'b', 'c', 'd' } ++ [_]u8{undefined} ** 10;
82758570 \\ var target: [*]u8 = @ptrCast([*]u8, &buf);
82768571 \\ const slice = target[0..14 :255];
8572 \\ _ = slice;
82778573 \\ }
82788574 \\}
82798575 \\export fn foo_cvector_ConstPtrSpecialBaseArray() void {
......@@ -8281,6 +8577,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
82818577 \\ var buf = [_:0]u8{ 'a', 'b', 'c', 'd' } ++ [_]u8{undefined} ** 10;
82828578 \\ var target: [*c]u8 = &buf;
82838579 \\ const slice = target[0..14 :255];
8580 \\ _ = slice;
82848581 \\ }
82858582 \\}
82868583 \\export fn foo_cvector_ConstPtrSpecialRef() void {
......@@ -8288,6 +8585,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
82888585 \\ var buf = [_:0]u8{ 'a', 'b', 'c', 'd' } ++ [_]u8{undefined} ** 10;
82898586 \\ var target: [*c]u8 = @ptrCast([*c]u8, &buf);
82908587 \\ const slice = target[0..14 :255];
8588 \\ _ = slice;
82918589 \\ }
82928590 \\}
82938591 \\export fn foo_slice() void {
......@@ -8295,6 +8593,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
82958593 \\ var buf = [_:0]u8{ 'a', 'b', 'c', 'd' } ++ [_]u8{undefined} ** 10;
82968594 \\ var target: []u8 = &buf;
82978595 \\ const slice = target[0..14 :255];
8596 \\ _ = slice;
82988597 \\ }
82998598 \\}
83008599 , &[_][]const u8{
......@@ -8307,7 +8606,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
83078606 ":46:29: error: slice-sentinel does not match target-sentinel",
83088607 });
83098608
8310 cases.add("issue #4207: coerce from non-terminated-slice to terminated-pointer",
8609 ctx.objErrStage1("issue #4207: coerce from non-terminated-slice to terminated-pointer",
83118610 \\export fn foo() [*:0]const u8 {
83128611 \\ var buffer: [64]u8 = undefined;
83138612 \\ return buffer[0..];
......@@ -8317,7 +8616,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
83178616 ":3:18: note: destination pointer requires a terminating '0' sentinel",
83188617 });
83198618
8320 cases.add("issue #5221: invalid struct init type referenced by @typeInfo and passed into function",
8619 ctx.objErrStage1("issue #5221: invalid struct init type referenced by @typeInfo and passed into function",
83218620 \\fn ignore(comptime param: anytype) void {}
83228621 \\
83238622 \\export fn foo() void {
......@@ -8331,7 +8630,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
83318630 ":5:28: error: expected type '[]u8', found '*const [3:0]u8'",
83328631 });
83338632
8334 cases.add("integer underflow error",
8633 ctx.objErrStage1("integer underflow error",
83358634 \\export fn entry() void {
83368635 \\ _ = @intToPtr(*c_void, ~@as(usize, @import("std").math.maxInt(usize)) - 1);
83378636 \\}
......@@ -8339,23 +8638,24 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
83398638 ":2:75: error: operation caused overflow",
83408639 });
83418640
8342 cases.addCase(x: {
8343 var tc = cases.create("align(N) expr function pointers is a compile error",
8641 {
8642 const case = ctx.obj("align(N) expr function pointers is a compile error", .{
8643 .cpu_arch = .wasm32,
8644 .os_tag = .freestanding,
8645 .abi = .none,
8646 });
8647 case.backend = .stage1;
8648
8649 case.addError(
83448650 \\export fn foo() align(1) void {
83458651 \\ return;
83468652 \\}
83478653 , &[_][]const u8{
83488654 "tmp.zig:1:23: error: align(N) expr is not allowed on function prototypes in wasm32/wasm64",
83498655 });
8350 tc.target = std.zig.CrossTarget{
8351 .cpu_arch = .wasm32,
8352 .os_tag = .freestanding,
8353 .abi = .none,
8354 };
8355 break :x tc;
8356 });
8656 }
83578657
8358 cases.add("compare optional to non-optional with invalid types",
8658 ctx.objErrStage1("compare optional to non-optional with invalid types",
83598659 \\export fn inconsistentChildType() void {
83608660 \\ var x: ?i32 = undefined;
83618661 \\ const y: comptime_int = 10;
......@@ -8389,16 +8689,17 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
83898689 ":22:12: note: operator not supported for type '[3]i32'",
83908690 });
83918691
8392 cases.add("slice cannot have its bytes reinterpreted",
8692 ctx.objErrStage1("slice cannot have its bytes reinterpreted",
83938693 \\export fn foo() void {
83948694 \\ const bytes = [1]u8{ 0xfa } ** 16;
83958695 \\ var value = @ptrCast(*const []const u8, &bytes).*;
8696 \\ _ = value;
83968697 \\}
83978698 , &[_][]const u8{
83988699 ":3:52: error: slice '[]const u8' cannot have its bytes reinterpreted",
83998700 });
84008701
8401 cases.add("wasmMemorySize is a compile error in non-Wasm targets",
8702 ctx.objErrStage1("wasmMemorySize is a compile error in non-Wasm targets",
84028703 \\export fn foo() void {
84038704 \\ _ = @wasmMemorySize(0);
84048705 \\ return;
......@@ -8407,7 +8708,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
84078708 "tmp.zig:2:9: error: @wasmMemorySize is a wasm32 feature only",
84088709 });
84098710
8410 cases.add("wasmMemoryGrow is a compile error in non-Wasm targets",
8711 ctx.objErrStage1("wasmMemoryGrow is a compile error in non-Wasm targets",
84118712 \\export fn foo() void {
84128713 \\ _ = @wasmMemoryGrow(0, 1);
84138714 \\ return;
......@@ -8415,7 +8716,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
84158716 , &[_][]const u8{
84168717 "tmp.zig:2:9: error: @wasmMemoryGrow is a wasm32 feature only",
84178718 });
8418 cases.add("Issue #5586: Make unary minus for unsigned types a compile error",
8719 ctx.objErrStage1("Issue #5586: Make unary minus for unsigned types a compile error",
84198720 \\export fn f1(x: u32) u32 {
84208721 \\ const y = -%x;
84218722 \\ return -y;
......@@ -8430,7 +8731,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
84308731 "tmp.zig:8:12: error: negation of type 'u32'",
84318732 });
84328733
8433 cases.add("Issue #5618: coercion of ?*c_void to *c_void must fail.",
8734 ctx.objErrStage1("Issue #5618: coercion of ?*c_void to *c_void must fail.",
84348735 \\export fn foo() void {
84358736 \\ var u: ?*c_void = null;
84368737 \\ var v: *c_void = undefined;
......@@ -8440,15 +8741,16 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
84408741 "tmp.zig:4:9: error: expected type '*c_void', found '?*c_void'",
84418742 });
84428743
8443 cases.add("Issue #6823: don't allow .* to be followed by **",
8744 ctx.objErrStage1("Issue #6823: don't allow .* to be followed by **",
84448745 \\fn foo() void {
84458746 \\ var sequence = "repeat".*** 10;
8747 \\ _ = sequence;
84468748 \\}
84478749 , &[_][]const u8{
84488750 "tmp.zig:2:30: error: `.*` cannot be followed by `*`. Are you missing a space?",
84498751 });
84508752
8451 cases.add("Issue #9165: windows tcp server compilation error",
8753 ctx.objErrStage1("Issue #9165: windows tcp server compilation error",
84528754 \\const std = @import("std");
84538755 \\pub const io_mode = .evented;
84548756 \\pub fn main() !void {
test/stage2/test.zig deleted-1626
......@@ -1,1626 +0,0 @@
1const std = @import("std");
2const TestContext = @import("../../src/test.zig").TestContext;
3
4// Self-hosted has differing levels of support for various architectures. For now we pass explicit
5// target parameters to each test case. At some point we will take this to the next level and have
6// a set of targets that all test cases run on unless specifically overridden. For now, each test
7// case applies to only the specified target.
8
9const linux_x64 = std.zig.CrossTarget{
10 .cpu_arch = .x86_64,
11 .os_tag = .linux,
12};
13
14pub fn addCases(ctx: *TestContext) !void {
15 try @import("cbe.zig").addCases(ctx);
16 try @import("arm.zig").addCases(ctx);
17 try @import("aarch64.zig").addCases(ctx);
18 try @import("llvm.zig").addCases(ctx);
19 try @import("wasm.zig").addCases(ctx);
20 try @import("darwin.zig").addCases(ctx);
21 try @import("riscv64.zig").addCases(ctx);
22
23 {
24 var case = ctx.exe("hello world with updates", linux_x64);
25
26 case.addError("", &[_][]const u8{
27 ":93:9: error: struct 'test_case.test_case' has no member named 'main'",
28 });
29
30 // Incorrect return type
31 case.addError(
32 \\pub export fn _start() noreturn {
33 \\}
34 , &[_][]const u8{":2:1: error: expected noreturn, found void"});
35
36 // Regular old hello world
37 case.addCompareOutput(
38 \\pub export fn _start() noreturn {
39 \\ print();
40 \\
41 \\ exit();
42 \\}
43 \\
44 \\fn print() void {
45 \\ asm volatile ("syscall"
46 \\ :
47 \\ : [number] "{rax}" (1),
48 \\ [arg1] "{rdi}" (1),
49 \\ [arg2] "{rsi}" (@ptrToInt("Hello, World!\n")),
50 \\ [arg3] "{rdx}" (14)
51 \\ : "rcx", "r11", "memory"
52 \\ );
53 \\ return;
54 \\}
55 \\
56 \\fn exit() noreturn {
57 \\ asm volatile ("syscall"
58 \\ :
59 \\ : [number] "{rax}" (231),
60 \\ [arg1] "{rdi}" (0)
61 \\ : "rcx", "r11", "memory"
62 \\ );
63 \\ unreachable;
64 \\}
65 ,
66 "Hello, World!\n",
67 );
68
69 // Convert to pub fn main
70 case.addCompareOutput(
71 \\pub fn main() void {
72 \\ print();
73 \\}
74 \\
75 \\fn print() void {
76 \\ asm volatile ("syscall"
77 \\ :
78 \\ : [number] "{rax}" (1),
79 \\ [arg1] "{rdi}" (1),
80 \\ [arg2] "{rsi}" (@ptrToInt("Hello, World!\n")),
81 \\ [arg3] "{rdx}" (14)
82 \\ : "rcx", "r11", "memory"
83 \\ );
84 \\ return;
85 \\}
86 ,
87 "Hello, World!\n",
88 );
89
90 // Now change the message only
91 case.addCompareOutput(
92 \\pub fn main() void {
93 \\ print();
94 \\}
95 \\
96 \\fn print() void {
97 \\ asm volatile ("syscall"
98 \\ :
99 \\ : [number] "{rax}" (1),
100 \\ [arg1] "{rdi}" (1),
101 \\ [arg2] "{rsi}" (@ptrToInt("What is up? This is a longer message that will force the data to be relocated in virtual address space.\n")),
102 \\ [arg3] "{rdx}" (104)
103 \\ : "rcx", "r11", "memory"
104 \\ );
105 \\ return;
106 \\}
107 ,
108 "What is up? This is a longer message that will force the data to be relocated in virtual address space.\n",
109 );
110 // Now we print it twice.
111 case.addCompareOutput(
112 \\pub fn main() void {
113 \\ print();
114 \\ print();
115 \\}
116 \\
117 \\fn print() void {
118 \\ asm volatile ("syscall"
119 \\ :
120 \\ : [number] "{rax}" (1),
121 \\ [arg1] "{rdi}" (1),
122 \\ [arg2] "{rsi}" (@ptrToInt("What is up? This is a longer message that will force the data to be relocated in virtual address space.\n")),
123 \\ [arg3] "{rdx}" (104)
124 \\ : "rcx", "r11", "memory"
125 \\ );
126 \\ return;
127 \\}
128 ,
129 \\What is up? This is a longer message that will force the data to be relocated in virtual address space.
130 \\What is up? This is a longer message that will force the data to be relocated in virtual address space.
131 \\
132 );
133 }
134
135 {
136 var case = ctx.exe("adding numbers at comptime", linux_x64);
137 case.addCompareOutput(
138 \\pub export fn _start() noreturn {
139 \\ asm volatile ("syscall"
140 \\ :
141 \\ : [number] "{rax}" (1),
142 \\ [arg1] "{rdi}" (1),
143 \\ [arg2] "{rsi}" (@ptrToInt("Hello, World!\n")),
144 \\ [arg3] "{rdx}" (10 + 4)
145 \\ : "rcx", "r11", "memory"
146 \\ );
147 \\ asm volatile ("syscall"
148 \\ :
149 \\ : [number] "{rax}" (@as(usize, 230) + @as(usize, 1)),
150 \\ [arg1] "{rdi}" (0)
151 \\ : "rcx", "r11", "memory"
152 \\ );
153 \\ unreachable;
154 \\}
155 ,
156 "Hello, World!\n",
157 );
158 }
159
160 {
161 var case = ctx.exe("adding numbers at runtime and comptime", linux_x64);
162 case.addCompareOutput(
163 \\pub export fn _start() noreturn {
164 \\ add(3, 4);
165 \\
166 \\ exit();
167 \\}
168 \\
169 \\fn add(a: u32, b: u32) void {
170 \\ if (a + b != 7) unreachable;
171 \\}
172 \\
173 \\fn exit() noreturn {
174 \\ asm volatile ("syscall"
175 \\ :
176 \\ : [number] "{rax}" (231),
177 \\ [arg1] "{rdi}" (0)
178 \\ : "rcx", "r11", "memory"
179 \\ );
180 \\ unreachable;
181 \\}
182 ,
183 "",
184 );
185 // comptime function call
186 case.addCompareOutput(
187 \\pub export fn _start() noreturn {
188 \\ exit();
189 \\}
190 \\
191 \\fn add(a: u32, b: u32) u32 {
192 \\ return a + b;
193 \\}
194 \\
195 \\const x = add(3, 4);
196 \\
197 \\fn exit() noreturn {
198 \\ asm volatile ("syscall"
199 \\ :
200 \\ : [number] "{rax}" (231),
201 \\ [arg1] "{rdi}" (x - 7)
202 \\ : "rcx", "r11", "memory"
203 \\ );
204 \\ unreachable;
205 \\}
206 ,
207 "",
208 );
209 // Inline function call
210 case.addCompareOutput(
211 \\pub export fn _start() noreturn {
212 \\ var x: usize = 3;
213 \\ const y = add(1, 2, x);
214 \\ exit(y - 6);
215 \\}
216 \\
217 \\fn add(a: usize, b: usize, c: usize) callconv(.Inline) usize {
218 \\ return a + b + c;
219 \\}
220 \\
221 \\fn exit(code: usize) noreturn {
222 \\ asm volatile ("syscall"
223 \\ :
224 \\ : [number] "{rax}" (231),
225 \\ [arg1] "{rdi}" (code)
226 \\ : "rcx", "r11", "memory"
227 \\ );
228 \\ unreachable;
229 \\}
230 ,
231 "",
232 );
233 }
234
235 {
236 var case = ctx.exe("subtracting numbers at runtime", linux_x64);
237 case.addCompareOutput(
238 \\pub fn main() void {
239 \\ sub(7, 4);
240 \\}
241 \\
242 \\fn sub(a: u32, b: u32) void {
243 \\ if (a - b != 3) unreachable;
244 \\}
245 ,
246 "",
247 );
248 }
249 {
250 var case = ctx.exe("unused vars", linux_x64);
251 case.addError(
252 \\pub fn main() void {
253 \\ const x = 1;
254 \\}
255 , &.{":2:11: error: unused local constant"});
256 }
257 {
258 var case = ctx.exe("@TypeOf", linux_x64);
259 case.addCompareOutput(
260 \\pub fn main() void {
261 \\ var x: usize = 0;
262 \\ _ = x;
263 \\ const z = @TypeOf(x, @as(u128, 5));
264 \\ assert(z == u128);
265 \\}
266 \\
267 \\pub fn assert(ok: bool) void {
268 \\ if (!ok) unreachable; // assertion failure
269 \\}
270 ,
271 "",
272 );
273 case.addCompareOutput(
274 \\pub fn main() void {
275 \\ const z = @TypeOf(true);
276 \\ assert(z == bool);
277 \\}
278 \\
279 \\pub fn assert(ok: bool) void {
280 \\ if (!ok) unreachable; // assertion failure
281 \\}
282 ,
283 "",
284 );
285 case.addError(
286 \\pub fn main() void {
287 \\ _ = @TypeOf(true, 1);
288 \\}
289 , &[_][]const u8{":2:9: error: incompatible types: 'bool' and 'comptime_int'"});
290 }
291
292 {
293 var case = ctx.exe("multiplying numbers at runtime and comptime", linux_x64);
294 case.addCompareOutput(
295 \\pub export fn _start() noreturn {
296 \\ mul(3, 4);
297 \\
298 \\ exit();
299 \\}
300 \\
301 \\fn mul(a: u32, b: u32) void {
302 \\ if (a * b != 12) unreachable;
303 \\}
304 \\
305 \\fn exit() noreturn {
306 \\ asm volatile ("syscall"
307 \\ :
308 \\ : [number] "{rax}" (231),
309 \\ [arg1] "{rdi}" (0)
310 \\ : "rcx", "r11", "memory"
311 \\ );
312 \\ unreachable;
313 \\}
314 ,
315 "",
316 );
317 // comptime function call
318 case.addCompareOutput(
319 \\pub fn _start() noreturn {
320 \\ exit();
321 \\}
322 \\
323 \\fn mul(a: u32, b: u32) u32 {
324 \\ return a * b;
325 \\}
326 \\
327 \\const x = mul(3, 4);
328 \\
329 \\fn exit() noreturn {
330 \\ asm volatile ("syscall"
331 \\ :
332 \\ : [number] "{rax}" (231),
333 \\ [arg1] "{rdi}" (x - 12)
334 \\ : "rcx", "r11", "memory"
335 \\ );
336 \\ unreachable;
337 \\}
338 ,
339 "",
340 );
341 // Inline function call
342 case.addCompareOutput(
343 \\pub export fn _start() noreturn {
344 \\ var x: usize = 5;
345 \\ const y = mul(2, 3, x);
346 \\ exit(y - 30);
347 \\}
348 \\
349 \\fn mul(a: usize, b: usize, c: usize) callconv(.Inline) usize {
350 \\ return a * b * c;
351 \\}
352 \\
353 \\fn exit(code: usize) noreturn {
354 \\ asm volatile ("syscall"
355 \\ :
356 \\ : [number] "{rax}" (231),
357 \\ [arg1] "{rdi}" (code)
358 \\ : "rcx", "r11", "memory"
359 \\ );
360 \\ unreachable;
361 \\}
362 ,
363 "",
364 );
365 }
366
367 {
368 var case = ctx.exe("assert function", linux_x64);
369 case.addCompareOutput(
370 \\pub fn main() void {
371 \\ add(3, 4);
372 \\}
373 \\
374 \\fn add(a: u32, b: u32) void {
375 \\ assert(a + b == 7);
376 \\}
377 \\
378 \\pub fn assert(ok: bool) void {
379 \\ if (!ok) unreachable; // assertion failure
380 \\}
381 \\
382 \\fn exit() noreturn {
383 \\ asm volatile ("syscall"
384 \\ :
385 \\ : [number] "{rax}" (231),
386 \\ [arg1] "{rdi}" (0)
387 \\ : "rcx", "r11", "memory"
388 \\ );
389 \\ unreachable;
390 \\}
391 ,
392 "",
393 );
394
395 // Tests copying a register. For the `c = a + b`, it has to
396 // preserve both a and b, because they are both used later.
397 case.addCompareOutput(
398 \\pub fn main() void {
399 \\ add(3, 4);
400 \\}
401 \\
402 \\fn add(a: u32, b: u32) void {
403 \\ const c = a + b; // 7
404 \\ const d = a + c; // 10
405 \\ const e = d + b; // 14
406 \\ assert(e == 14);
407 \\}
408 \\
409 \\pub fn assert(ok: bool) void {
410 \\ if (!ok) unreachable; // assertion failure
411 \\}
412 ,
413 "",
414 );
415
416 // More stress on the liveness detection.
417 case.addCompareOutput(
418 \\pub fn main() void {
419 \\ add(3, 4);
420 \\}
421 \\
422 \\fn add(a: u32, b: u32) void {
423 \\ const c = a + b; // 7
424 \\ const d = a + c; // 10
425 \\ const e = d + b; // 14
426 \\ const f = d + e; // 24
427 \\ const g = e + f; // 38
428 \\ const h = f + g; // 62
429 \\ const i = g + h; // 100
430 \\ assert(i == 100);
431 \\}
432 \\
433 \\pub fn assert(ok: bool) void {
434 \\ if (!ok) unreachable; // assertion failure
435 \\}
436 ,
437 "",
438 );
439
440 // Requires a second move. The register allocator should figure out to re-use rax.
441 case.addCompareOutput(
442 \\pub fn main() void {
443 \\ add(3, 4);
444 \\}
445 \\
446 \\fn add(a: u32, b: u32) void {
447 \\ const c = a + b; // 7
448 \\ const d = a + c; // 10
449 \\ const e = d + b; // 14
450 \\ const f = d + e; // 24
451 \\ const g = e + f; // 38
452 \\ const h = f + g; // 62
453 \\ const i = g + h; // 100
454 \\ const j = i + d; // 110
455 \\ assert(j == 110);
456 \\}
457 \\
458 \\pub fn assert(ok: bool) void {
459 \\ if (!ok) unreachable; // assertion failure
460 \\}
461 ,
462 "",
463 );
464
465 // Now we test integer return values.
466 case.addCompareOutput(
467 \\pub fn main() void {
468 \\ assert(add(3, 4) == 7);
469 \\ assert(add(20, 10) == 30);
470 \\}
471 \\
472 \\fn add(a: u32, b: u32) u32 {
473 \\ return a + b;
474 \\}
475 \\
476 \\pub fn assert(ok: bool) void {
477 \\ if (!ok) unreachable; // assertion failure
478 \\}
479 ,
480 "",
481 );
482
483 // Local mutable variables.
484 case.addCompareOutput(
485 \\pub fn main() void {
486 \\ assert(add(3, 4) == 7);
487 \\ assert(add(20, 10) == 30);
488 \\}
489 \\
490 \\fn add(a: u32, b: u32) u32 {
491 \\ var x: u32 = undefined;
492 \\ x = 0;
493 \\ x += a;
494 \\ x += b;
495 \\ return x;
496 \\}
497 \\
498 \\pub fn assert(ok: bool) void {
499 \\ if (!ok) unreachable; // assertion failure
500 \\}
501 ,
502 "",
503 );
504
505 // Optionals
506 case.addCompareOutput(
507 \\pub fn main() void {
508 \\ const a: u32 = 2;
509 \\ const b: ?u32 = a;
510 \\ const c = b.?;
511 \\ if (c != 2) unreachable;
512 \\}
513 ,
514 "",
515 );
516
517 // While loops
518 case.addCompareOutput(
519 \\pub fn main() void {
520 \\ var i: u32 = 0;
521 \\ while (i < 4) : (i += 1) print();
522 \\ assert(i == 4);
523 \\}
524 \\
525 \\fn print() void {
526 \\ asm volatile ("syscall"
527 \\ :
528 \\ : [number] "{rax}" (1),
529 \\ [arg1] "{rdi}" (1),
530 \\ [arg2] "{rsi}" (@ptrToInt("hello\n")),
531 \\ [arg3] "{rdx}" (6)
532 \\ : "rcx", "r11", "memory"
533 \\ );
534 \\ return;
535 \\}
536 \\
537 \\pub fn assert(ok: bool) void {
538 \\ if (!ok) unreachable; // assertion failure
539 \\}
540 ,
541 "hello\nhello\nhello\nhello\n",
542 );
543
544 // inline while requires the condition to be comptime known.
545 case.addError(
546 \\pub fn main() void {
547 \\ var i: u32 = 0;
548 \\ inline while (i < 4) : (i += 1) print();
549 \\ assert(i == 4);
550 \\}
551 \\
552 \\fn print() void {
553 \\ asm volatile ("syscall"
554 \\ :
555 \\ : [number] "{rax}" (1),
556 \\ [arg1] "{rdi}" (1),
557 \\ [arg2] "{rsi}" (@ptrToInt("hello\n")),
558 \\ [arg3] "{rdx}" (6)
559 \\ : "rcx", "r11", "memory"
560 \\ );
561 \\ return;
562 \\}
563 \\
564 \\pub fn assert(ok: bool) void {
565 \\ if (!ok) unreachable; // assertion failure
566 \\}
567 , &[_][]const u8{":3:21: error: unable to resolve comptime value"});
568
569 // Labeled blocks (no conditional branch)
570 case.addCompareOutput(
571 \\pub fn main() void {
572 \\ assert(add(3, 4) == 20);
573 \\}
574 \\
575 \\fn add(a: u32, b: u32) u32 {
576 \\ const x: u32 = blk: {
577 \\ const c = a + b; // 7
578 \\ const d = a + c; // 10
579 \\ const e = d + b; // 14
580 \\ break :blk e;
581 \\ };
582 \\ const y = x + a; // 17
583 \\ const z = y + a; // 20
584 \\ return z;
585 \\}
586 \\
587 \\pub fn assert(ok: bool) void {
588 \\ if (!ok) unreachable; // assertion failure
589 \\}
590 ,
591 "",
592 );
593
594 // This catches a possible bug in the logic for re-using dying operands.
595 case.addCompareOutput(
596 \\pub fn main() void {
597 \\ assert(add(3, 4) == 116);
598 \\}
599 \\
600 \\fn add(a: u32, b: u32) u32 {
601 \\ const x: u32 = blk: {
602 \\ const c = a + b; // 7
603 \\ const d = a + c; // 10
604 \\ const e = d + b; // 14
605 \\ const f = d + e; // 24
606 \\ const g = e + f; // 38
607 \\ const h = f + g; // 62
608 \\ const i = g + h; // 100
609 \\ const j = i + d; // 110
610 \\ break :blk j;
611 \\ };
612 \\ const y = x + a; // 113
613 \\ const z = y + a; // 116
614 \\ return z;
615 \\}
616 \\
617 \\pub fn assert(ok: bool) void {
618 \\ if (!ok) unreachable; // assertion failure
619 \\}
620 ,
621 "",
622 );
623
624 // Spilling registers to the stack.
625 case.addCompareOutput(
626 \\pub fn main() void {
627 \\ assert(add(3, 4) == 1221);
628 \\ assert(mul(3, 4) == 21609);
629 \\}
630 \\
631 \\fn add(a: u32, b: u32) u32 {
632 \\ const x: u32 = blk: {
633 \\ const c = a + b; // 7
634 \\ const d = a + c; // 10
635 \\ const e = d + b; // 14
636 \\ const f = d + e; // 24
637 \\ const g = e + f; // 38
638 \\ const h = f + g; // 62
639 \\ const i = g + h; // 100
640 \\ const j = i + d; // 110
641 \\ const k = i + j; // 210
642 \\ const l = j + k; // 320
643 \\ const m = l + c; // 327
644 \\ const n = m + d; // 337
645 \\ const o = n + e; // 351
646 \\ const p = o + f; // 375
647 \\ const q = p + g; // 413
648 \\ const r = q + h; // 475
649 \\ const s = r + i; // 575
650 \\ const t = s + j; // 685
651 \\ const u = t + k; // 895
652 \\ const v = u + l; // 1215
653 \\ break :blk v;
654 \\ };
655 \\ const y = x + a; // 1218
656 \\ const z = y + a; // 1221
657 \\ return z;
658 \\}
659 \\
660 \\fn mul(a: u32, b: u32) u32 {
661 \\ const x: u32 = blk: {
662 \\ const c = a * a * a * a; // 81
663 \\ const d = a * a * a * b; // 108
664 \\ const e = a * a * b * a; // 108
665 \\ const f = a * a * b * b; // 144
666 \\ const g = a * b * a * a; // 108
667 \\ const h = a * b * a * b; // 144
668 \\ const i = a * b * b * a; // 144
669 \\ const j = a * b * b * b; // 192
670 \\ const k = b * a * a * a; // 108
671 \\ const l = b * a * a * b; // 144
672 \\ const m = b * a * b * a; // 144
673 \\ const n = b * a * b * b; // 192
674 \\ const o = b * b * a * a; // 144
675 \\ const p = b * b * a * b; // 192
676 \\ const q = b * b * b * a; // 192
677 \\ const r = b * b * b * b; // 256
678 \\ const s = c + d + e + f + g + h + i + j + k + l + m + n + o + p + q + r; // 2401
679 \\ break :blk s;
680 \\ };
681 \\ const y = x * a; // 7203
682 \\ const z = y * a; // 21609
683 \\ return z;
684 \\}
685 \\
686 \\pub fn assert(ok: bool) void {
687 \\ if (!ok) unreachable; // assertion failure
688 \\}
689 ,
690 "",
691 );
692
693 // Reusing the registers of dead operands playing nicely with conditional branching.
694 case.addCompareOutput(
695 \\pub fn main() void {
696 \\ assert(add(3, 4) == 791);
697 \\ assert(add(4, 3) == 79);
698 \\}
699 \\
700 \\fn add(a: u32, b: u32) u32 {
701 \\ const x: u32 = if (a < b) blk: {
702 \\ const c = a + b; // 7
703 \\ const d = a + c; // 10
704 \\ const e = d + b; // 14
705 \\ const f = d + e; // 24
706 \\ const g = e + f; // 38
707 \\ const h = f + g; // 62
708 \\ const i = g + h; // 100
709 \\ const j = i + d; // 110
710 \\ const k = i + j; // 210
711 \\ const l = k + c; // 217
712 \\ const m = l + d; // 227
713 \\ const n = m + e; // 241
714 \\ const o = n + f; // 265
715 \\ const p = o + g; // 303
716 \\ const q = p + h; // 365
717 \\ const r = q + i; // 465
718 \\ const s = r + j; // 575
719 \\ const t = s + k; // 785
720 \\ break :blk t;
721 \\ } else blk: {
722 \\ const t = b + b + a; // 10
723 \\ const c = a + t; // 14
724 \\ const d = c + t; // 24
725 \\ const e = d + t; // 34
726 \\ const f = e + t; // 44
727 \\ const g = f + t; // 54
728 \\ const h = c + g; // 68
729 \\ break :blk h + b; // 71
730 \\ };
731 \\ const y = x + a; // 788, 75
732 \\ const z = y + a; // 791, 79
733 \\ return z;
734 \\}
735 \\
736 \\pub fn assert(ok: bool) void {
737 \\ if (!ok) unreachable; // assertion failure
738 \\}
739 ,
740 "",
741 );
742
743 // Character literals and multiline strings.
744 case.addCompareOutput(
745 \\pub fn main() void {
746 \\ const ignore =
747 \\ \\ cool thx
748 \\ \\
749 \\ ;
750 \\ _ = ignore;
751 \\ add('ぁ', '\x03');
752 \\}
753 \\
754 \\fn add(a: u32, b: u32) void {
755 \\ assert(a + b == 12356);
756 \\}
757 \\
758 \\pub fn assert(ok: bool) void {
759 \\ if (!ok) unreachable; // assertion failure
760 \\}
761 ,
762 "",
763 );
764
765 // Global const.
766 case.addCompareOutput(
767 \\pub fn main() void {
768 \\ add(aa, bb);
769 \\}
770 \\
771 \\const aa = 'ぁ';
772 \\const bb = '\x03';
773 \\
774 \\fn add(a: u32, b: u32) void {
775 \\ assert(a + b == 12356);
776 \\}
777 \\
778 \\pub fn assert(ok: bool) void {
779 \\ if (!ok) unreachable; // assertion failure
780 \\}
781 ,
782 "",
783 );
784
785 // Array access.
786 case.addCompareOutput(
787 \\pub fn main() void {
788 \\ assert("hello"[0] == 'h');
789 \\}
790 \\
791 \\pub fn assert(ok: bool) void {
792 \\ if (!ok) unreachable; // assertion failure
793 \\}
794 ,
795 "",
796 );
797
798 // Array access to a global array.
799 case.addCompareOutput(
800 \\const hello = "hello".*;
801 \\pub fn main() void {
802 \\ assert(hello[1] == 'e');
803 \\}
804 \\
805 \\pub fn assert(ok: bool) void {
806 \\ if (!ok) unreachable; // assertion failure
807 \\}
808 ,
809 "",
810 );
811
812 // 64bit set stack
813 case.addCompareOutput(
814 \\pub fn main() void {
815 \\ var i: u64 = 0xFFEEDDCCBBAA9988;
816 \\ assert(i == 0xFFEEDDCCBBAA9988);
817 \\}
818 \\
819 \\pub fn assert(ok: bool) void {
820 \\ if (!ok) unreachable; // assertion failure
821 \\}
822 ,
823 "",
824 );
825
826 // Basic for loop
827 case.addCompareOutput(
828 \\pub fn main() void {
829 \\ for ("hello") |_| print();
830 \\}
831 \\
832 \\fn print() void {
833 \\ asm volatile ("syscall"
834 \\ :
835 \\ : [number] "{rax}" (1),
836 \\ [arg1] "{rdi}" (1),
837 \\ [arg2] "{rsi}" (@ptrToInt("hello\n")),
838 \\ [arg3] "{rdx}" (6)
839 \\ : "rcx", "r11", "memory"
840 \\ );
841 \\ return;
842 \\}
843 ,
844 "hello\nhello\nhello\nhello\nhello\n",
845 );
846 }
847
848 {
849 var case = ctx.exe("basic import", linux_x64);
850 case.addCompareOutput(
851 \\pub fn main() void {
852 \\ @import("print.zig").print();
853 \\}
854 ,
855 "Hello, World!\n",
856 );
857 try case.files.append(.{
858 .src =
859 \\pub fn print() void {
860 \\ asm volatile ("syscall"
861 \\ :
862 \\ : [number] "{rax}" (@as(usize, 1)),
863 \\ [arg1] "{rdi}" (@as(usize, 1)),
864 \\ [arg2] "{rsi}" (@ptrToInt("Hello, World!\n")),
865 \\ [arg3] "{rdx}" (@as(usize, 14))
866 \\ : "rcx", "r11", "memory"
867 \\ );
868 \\ return;
869 \\}
870 ,
871 .path = "print.zig",
872 });
873 }
874 {
875 var case = ctx.exe("redundant comptime", linux_x64);
876 case.addError(
877 \\pub fn main() void {
878 \\ var a: comptime u32 = 0;
879 \\}
880 ,
881 &.{":2:12: error: redundant comptime keyword in already comptime scope"},
882 );
883 case.addError(
884 \\pub fn main() void {
885 \\ comptime {
886 \\ var a: u32 = comptime 0;
887 \\ }
888 \\}
889 ,
890 &.{":3:22: error: redundant comptime keyword in already comptime scope"},
891 );
892 }
893 {
894 var case = ctx.exe("try in comptime in struct in test", linux_x64);
895 case.addError(
896 \\test "@unionInit on union w/ tag but no fields" {
897 \\ const S = struct {
898 \\ comptime {
899 \\ try expect(false);
900 \\ }
901 \\ };
902 \\ _ = S;
903 \\}
904 ,
905 &.{":4:13: error: invalid 'try' outside function scope"},
906 );
907 }
908 {
909 var case = ctx.exe("import private", linux_x64);
910 case.addError(
911 \\pub fn main() void {
912 \\ @import("print.zig").print();
913 \\}
914 ,
915 &.{
916 ":2:25: error: 'print' is not marked 'pub'",
917 "print.zig:2:1: note: declared here",
918 },
919 );
920 try case.files.append(.{
921 .src =
922 \\// dummy comment to make print be on line 2
923 \\fn print() void {
924 \\ asm volatile ("syscall"
925 \\ :
926 \\ : [number] "{rax}" (@as(usize, 1)),
927 \\ [arg1] "{rdi}" (@as(usize, 1)),
928 \\ [arg2] "{rsi}" (@ptrToInt("Hello, World!\n")),
929 \\ [arg3] "{rdx}" (@as(usize, 14))
930 \\ : "rcx", "r11", "memory"
931 \\ );
932 \\ return;
933 \\}
934 ,
935 .path = "print.zig",
936 });
937 }
938
939 ctx.compileError("function redeclaration", linux_x64,
940 \\// dummy comment
941 \\fn entry() void {}
942 \\fn entry() void {}
943 \\
944 \\fn foo() void {
945 \\ var foo = 1234;
946 \\}
947 , &[_][]const u8{
948 ":3:1: error: redeclaration of 'entry'",
949 ":2:1: note: other declaration here",
950 ":6:9: error: local shadows declaration of 'foo'",
951 ":5:1: note: declared here",
952 });
953
954 ctx.compileError("returns in try", linux_x64,
955 \\pub fn main() !void {
956 \\ try a();
957 \\ try b();
958 \\}
959 \\
960 \\pub fn a() !void {
961 \\ defer try b();
962 \\}
963 \\pub fn b() !void {
964 \\ defer return a();
965 \\}
966 , &[_][]const u8{
967 ":7:8: error: try is not allowed inside defer expression",
968 ":10:8: error: cannot return from defer expression",
969 });
970
971 ctx.compileError("ambiguous references", linux_x64,
972 \\const T = struct {
973 \\ const T = struct {
974 \\ fn f() void {
975 \\ _ = T;
976 \\ }
977 \\ };
978 \\};
979 , &.{
980 ":4:17: error: ambiguous reference",
981 ":1:1: note: declared here",
982 ":2:5: note: also declared here",
983 });
984
985 ctx.compileError("inner func accessing outer var", linux_x64,
986 \\pub fn f() void {
987 \\ var bar: bool = true;
988 \\ const S = struct {
989 \\ fn baz() bool {
990 \\ return bar;
991 \\ }
992 \\ };
993 \\ _ = S;
994 \\}
995 , &.{
996 ":5:20: error: 'bar' not accessible from inner function",
997 ":2:9: note: declared here",
998 });
999
1000 ctx.compileError("global variable redeclaration", linux_x64,
1001 \\// dummy comment
1002 \\var foo = false;
1003 \\var foo = true;
1004 , &[_][]const u8{
1005 ":3:1: error: redeclaration of 'foo'",
1006 ":2:1: note: other declaration here",
1007 });
1008
1009 ctx.compileError("compileError", linux_x64,
1010 \\export fn foo() void {
1011 \\ @compileError("this is an error");
1012 \\}
1013 , &[_][]const u8{":2:3: error: this is an error"});
1014
1015 {
1016 var case = ctx.exe("intToPtr", linux_x64);
1017 case.addError(
1018 \\pub fn main() void {
1019 \\ _ = @intToPtr(*u8, 0);
1020 \\}
1021 , &[_][]const u8{
1022 ":2:24: error: pointer type '*u8' does not allow address zero",
1023 });
1024 case.addError(
1025 \\pub fn main() void {
1026 \\ _ = @intToPtr(*u32, 2);
1027 \\}
1028 , &[_][]const u8{
1029 ":2:25: error: pointer type '*u32' requires aligned address",
1030 });
1031 }
1032
1033 {
1034 var case = ctx.obj("variable shadowing", linux_x64);
1035 case.addError(
1036 \\pub fn main() void {
1037 \\ var i: u32 = 10;
1038 \\ var i: u32 = 10;
1039 \\}
1040 , &[_][]const u8{
1041 ":3:9: error: redeclaration of 'i'",
1042 ":2:9: note: previously declared here",
1043 });
1044 case.addError(
1045 \\var testing: i64 = 10;
1046 \\pub fn main() void {
1047 \\ var testing: i64 = 20;
1048 \\}
1049 , &[_][]const u8{
1050 ":3:9: error: local shadows declaration of 'testing'",
1051 ":1:1: note: declared here",
1052 });
1053 case.addError(
1054 \\fn a() type {
1055 \\ return struct {
1056 \\ pub fn b() void {
1057 \\ const c = 6;
1058 \\ const c = 69;
1059 \\ }
1060 \\ };
1061 \\}
1062 , &[_][]const u8{
1063 ":5:19: error: redeclaration of 'c'",
1064 ":4:19: note: previously declared here",
1065 });
1066 }
1067
1068 {
1069 // TODO make the test harness support checking the compile log output too
1070 var case = ctx.obj("@compileLog", linux_x64);
1071 // The other compile error prevents emission of a "found compile log" statement.
1072 case.addError(
1073 \\export fn _start() noreturn {
1074 \\ const b = true;
1075 \\ var f: u32 = 1;
1076 \\ @compileLog(b, 20, f, x);
1077 \\ @compileLog(1000);
1078 \\ var bruh: usize = true;
1079 \\ _ = bruh;
1080 \\ unreachable;
1081 \\}
1082 \\export fn other() void {
1083 \\ @compileLog(1234);
1084 \\}
1085 \\fn x() void {}
1086 , &[_][]const u8{
1087 ":6:23: error: expected usize, found bool",
1088 });
1089
1090 // Now only compile log statements remain. One per Decl.
1091 case.addError(
1092 \\export fn _start() noreturn {
1093 \\ const b = true;
1094 \\ var f: u32 = 1;
1095 \\ @compileLog(b, 20, f, x);
1096 \\ @compileLog(1000);
1097 \\ unreachable;
1098 \\}
1099 \\export fn other() void {
1100 \\ @compileLog(1234);
1101 \\}
1102 \\fn x() void {}
1103 , &[_][]const u8{
1104 ":9:5: error: found compile log statement",
1105 ":4:5: note: also here",
1106 });
1107 }
1108
1109 {
1110 var case = ctx.obj("extern variable has no type", linux_x64);
1111 case.addError(
1112 \\comptime {
1113 \\ _ = foo;
1114 \\}
1115 \\extern var foo: i32;
1116 , &[_][]const u8{":2:9: error: unable to resolve comptime value"});
1117 case.addError(
1118 \\export fn entry() void {
1119 \\ _ = foo;
1120 \\}
1121 \\extern var foo;
1122 , &[_][]const u8{":4:8: error: unable to infer variable type"});
1123 }
1124
1125 {
1126 var case = ctx.exe("break/continue", linux_x64);
1127
1128 // Break out of loop
1129 case.addCompareOutput(
1130 \\pub fn main() void {
1131 \\ while (true) {
1132 \\ break;
1133 \\ }
1134 \\}
1135 ,
1136 "",
1137 );
1138 case.addCompareOutput(
1139 \\pub fn main() void {
1140 \\ foo: while (true) {
1141 \\ break :foo;
1142 \\ }
1143 \\}
1144 ,
1145 "",
1146 );
1147
1148 // Continue in loop
1149 case.addCompareOutput(
1150 \\pub export fn _start() noreturn {
1151 \\ var i: u64 = 0;
1152 \\ while (true) : (i+=1) {
1153 \\ if (i == 4) exit();
1154 \\ continue;
1155 \\ }
1156 \\}
1157 \\
1158 \\fn exit() noreturn {
1159 \\ asm volatile ("syscall"
1160 \\ :
1161 \\ : [number] "{rax}" (231),
1162 \\ [arg1] "{rdi}" (0)
1163 \\ : "rcx", "r11", "memory"
1164 \\ );
1165 \\ unreachable;
1166 \\}
1167 ,
1168 "",
1169 );
1170 case.addCompareOutput(
1171 \\pub export fn _start() noreturn {
1172 \\ var i: u64 = 0;
1173 \\ foo: while (true) : (i+=1) {
1174 \\ if (i == 4) exit();
1175 \\ continue :foo;
1176 \\ }
1177 \\}
1178 \\
1179 \\fn exit() noreturn {
1180 \\ asm volatile ("syscall"
1181 \\ :
1182 \\ : [number] "{rax}" (231),
1183 \\ [arg1] "{rdi}" (0)
1184 \\ : "rcx", "r11", "memory"
1185 \\ );
1186 \\ unreachable;
1187 \\}
1188 ,
1189 "",
1190 );
1191 }
1192
1193 {
1194 var case = ctx.exe("unused labels", linux_x64);
1195 case.addError(
1196 \\comptime {
1197 \\ foo: {}
1198 \\}
1199 , &[_][]const u8{":2:5: error: unused block label"});
1200 case.addError(
1201 \\comptime {
1202 \\ foo: while (true) {}
1203 \\}
1204 , &[_][]const u8{":2:5: error: unused while loop label"});
1205 case.addError(
1206 \\comptime {
1207 \\ foo: for ("foo") |_| {}
1208 \\}
1209 , &[_][]const u8{":2:5: error: unused for loop label"});
1210 case.addError(
1211 \\comptime {
1212 \\ blk: {blk: {}}
1213 \\}
1214 , &[_][]const u8{
1215 ":2:11: error: redefinition of label 'blk'",
1216 ":2:5: note: previous definition is here",
1217 });
1218 }
1219
1220 {
1221 var case = ctx.exe("bad inferred variable type", linux_x64);
1222 case.addError(
1223 \\pub fn main() void {
1224 \\ var x = null;
1225 \\ _ = x;
1226 \\}
1227 , &[_][]const u8{
1228 ":2:9: error: variable of type '@Type(.Null)' must be const or comptime",
1229 });
1230 }
1231
1232 {
1233 var case = ctx.exe("compile error in inline fn call fixed", linux_x64);
1234 case.addError(
1235 \\pub export fn _start() noreturn {
1236 \\ var x: usize = 3;
1237 \\ const y = add(10, 2, x);
1238 \\ exit(y - 6);
1239 \\}
1240 \\
1241 \\fn add(a: usize, b: usize, c: usize) callconv(.Inline) usize {
1242 \\ if (a == 10) @compileError("bad");
1243 \\ return a + b + c;
1244 \\}
1245 \\
1246 \\fn exit(code: usize) noreturn {
1247 \\ asm volatile ("syscall"
1248 \\ :
1249 \\ : [number] "{rax}" (231),
1250 \\ [arg1] "{rdi}" (code)
1251 \\ : "rcx", "r11", "memory"
1252 \\ );
1253 \\ unreachable;
1254 \\}
1255 , &[_][]const u8{":8:18: error: bad"});
1256
1257 case.addCompareOutput(
1258 \\pub export fn _start() noreturn {
1259 \\ var x: usize = 3;
1260 \\ const y = add(1, 2, x);
1261 \\ exit(y - 6);
1262 \\}
1263 \\
1264 \\fn add(a: usize, b: usize, c: usize) callconv(.Inline) usize {
1265 \\ if (a == 10) @compileError("bad");
1266 \\ return a + b + c;
1267 \\}
1268 \\
1269 \\fn exit(code: usize) noreturn {
1270 \\ asm volatile ("syscall"
1271 \\ :
1272 \\ : [number] "{rax}" (231),
1273 \\ [arg1] "{rdi}" (code)
1274 \\ : "rcx", "r11", "memory"
1275 \\ );
1276 \\ unreachable;
1277 \\}
1278 ,
1279 "",
1280 );
1281 }
1282 {
1283 var case = ctx.exe("recursive inline function", linux_x64);
1284 case.addCompareOutput(
1285 \\pub export fn _start() noreturn {
1286 \\ const y = fibonacci(7);
1287 \\ exit(y - 21);
1288 \\}
1289 \\
1290 \\fn fibonacci(n: usize) callconv(.Inline) usize {
1291 \\ if (n <= 2) return n;
1292 \\ return fibonacci(n - 2) + fibonacci(n - 1);
1293 \\}
1294 \\
1295 \\fn exit(code: usize) noreturn {
1296 \\ asm volatile ("syscall"
1297 \\ :
1298 \\ : [number] "{rax}" (231),
1299 \\ [arg1] "{rdi}" (code)
1300 \\ : "rcx", "r11", "memory"
1301 \\ );
1302 \\ unreachable;
1303 \\}
1304 ,
1305 "",
1306 );
1307 // This additionally tests that the compile error reports the correct source location.
1308 // Without storing source locations relative to the owner decl, the compile error
1309 // here would be off by 2 bytes (from the "7" -> "999").
1310 case.addError(
1311 \\pub export fn _start() noreturn {
1312 \\ const y = fibonacci(999);
1313 \\ exit(y - 21);
1314 \\}
1315 \\
1316 \\fn fibonacci(n: usize) callconv(.Inline) usize {
1317 \\ if (n <= 2) return n;
1318 \\ return fibonacci(n - 2) + fibonacci(n - 1);
1319 \\}
1320 \\
1321 \\fn exit(code: usize) noreturn {
1322 \\ asm volatile ("syscall"
1323 \\ :
1324 \\ : [number] "{rax}" (231),
1325 \\ [arg1] "{rdi}" (code)
1326 \\ : "rcx", "r11", "memory"
1327 \\ );
1328 \\ unreachable;
1329 \\}
1330 , &[_][]const u8{":8:21: error: evaluation exceeded 1000 backwards branches"});
1331 }
1332 {
1333 var case = ctx.exe("orelse at comptime", linux_x64);
1334 case.addCompareOutput(
1335 \\pub fn main() void {
1336 \\ const i: ?u64 = 0;
1337 \\ const result = i orelse 5;
1338 \\ assert(result == 0);
1339 \\}
1340 \\fn assert(b: bool) void {
1341 \\ if (!b) unreachable;
1342 \\}
1343 ,
1344 "",
1345 );
1346 case.addCompareOutput(
1347 \\pub fn main() void {
1348 \\ const i: ?u64 = null;
1349 \\ const result = i orelse 5;
1350 \\ assert(result == 5);
1351 \\}
1352 \\fn assert(b: bool) void {
1353 \\ if (!b) unreachable;
1354 \\}
1355 ,
1356 "",
1357 );
1358 }
1359
1360 {
1361 var case = ctx.exe("only 1 function and it gets updated", linux_x64);
1362 case.addCompareOutput(
1363 \\pub export fn _start() noreturn {
1364 \\ asm volatile ("syscall"
1365 \\ :
1366 \\ : [number] "{rax}" (60), // exit
1367 \\ [arg1] "{rdi}" (0)
1368 \\ : "rcx", "r11", "memory"
1369 \\ );
1370 \\ unreachable;
1371 \\}
1372 ,
1373 "",
1374 );
1375 case.addCompareOutput(
1376 \\pub export fn _start() noreturn {
1377 \\ asm volatile ("syscall"
1378 \\ :
1379 \\ : [number] "{rax}" (231), // exit_group
1380 \\ [arg1] "{rdi}" (0)
1381 \\ : "rcx", "r11", "memory"
1382 \\ );
1383 \\ unreachable;
1384 \\}
1385 ,
1386 "",
1387 );
1388 }
1389 {
1390 var case = ctx.exe("passing u0 to function", linux_x64);
1391 case.addCompareOutput(
1392 \\pub fn main() void {
1393 \\ doNothing(0);
1394 \\}
1395 \\fn doNothing(arg: u0) void {
1396 \\ _ = arg;
1397 \\}
1398 ,
1399 "",
1400 );
1401 }
1402 {
1403 var case = ctx.exe("catch at comptime", linux_x64);
1404 case.addCompareOutput(
1405 \\pub fn main() void {
1406 \\ const i: anyerror!u64 = 0;
1407 \\ const caught = i catch 5;
1408 \\ assert(caught == 0);
1409 \\}
1410 \\fn assert(b: bool) void {
1411 \\ if (!b) unreachable;
1412 \\}
1413 ,
1414 "",
1415 );
1416
1417 case.addCompareOutput(
1418 \\pub fn main() void {
1419 \\ const i: anyerror!u64 = error.B;
1420 \\ const caught = i catch 5;
1421 \\ assert(caught == 5);
1422 \\}
1423 \\fn assert(b: bool) void {
1424 \\ if (!b) unreachable;
1425 \\}
1426 ,
1427 "",
1428 );
1429
1430 case.addCompareOutput(
1431 \\pub fn main() void {
1432 \\ const a: anyerror!comptime_int = 42;
1433 \\ const b: *const comptime_int = &(a catch unreachable);
1434 \\ assert(b.* == 42);
1435 \\}
1436 \\fn assert(b: bool) void {
1437 \\ if (!b) unreachable; // assertion failure
1438 \\}
1439 , "");
1440
1441 case.addCompareOutput(
1442 \\pub fn main() void {
1443 \\ const a: anyerror!u32 = error.B;
1444 \\ _ = &(a catch |err| assert(err == error.B));
1445 \\}
1446 \\fn assert(b: bool) void {
1447 \\ if (!b) unreachable;
1448 \\}
1449 , "");
1450
1451 case.addCompareOutput(
1452 \\pub fn main() void {
1453 \\ const a: anyerror!u32 = error.Bar;
1454 \\ a catch |err| assert(err == error.Bar);
1455 \\}
1456 \\fn assert(b: bool) void {
1457 \\ if (!b) unreachable;
1458 \\}
1459 , "");
1460 }
1461 {
1462 var case = ctx.exe("merge error sets", linux_x64);
1463
1464 case.addCompareOutput(
1465 \\pub fn main() void {
1466 \\ const E = error{ A, B, D } || error { A, B, C };
1467 \\ E.A catch {};
1468 \\ E.B catch {};
1469 \\ E.C catch {};
1470 \\ E.D catch {};
1471 \\ const E2 = error { X, Y } || @TypeOf(error.Z);
1472 \\ E2.X catch {};
1473 \\ E2.Y catch {};
1474 \\ E2.Z catch {};
1475 \\ assert(anyerror || error { Z } == anyerror);
1476 \\}
1477 \\fn assert(b: bool) void {
1478 \\ if (!b) unreachable;
1479 \\}
1480 ,
1481 "",
1482 );
1483 }
1484 {
1485 var case = ctx.exe("inline assembly", linux_x64);
1486
1487 case.addError(
1488 \\pub fn main() void {
1489 \\ const number = 1234;
1490 \\ const x = asm volatile ("syscall"
1491 \\ : [o] "{rax}" (-> number)
1492 \\ : [number] "{rax}" (231),
1493 \\ [arg1] "{rdi}" (code)
1494 \\ : "rcx", "r11", "memory"
1495 \\ );
1496 \\ _ = x;
1497 \\}
1498 , &[_][]const u8{":4:27: error: expected type, found comptime_int"});
1499 }
1500 {
1501 var case = ctx.exe("comptime var", linux_x64);
1502
1503 case.addError(
1504 \\pub fn main() void {
1505 \\ var a: u32 = 0;
1506 \\ comptime var b: u32 = 0;
1507 \\ if (a == 0) b = 3;
1508 \\}
1509 , &.{
1510 ":4:21: error: store to comptime variable depends on runtime condition",
1511 ":4:11: note: runtime condition here",
1512 });
1513
1514 case.addError(
1515 \\pub fn main() void {
1516 \\ var a: u32 = 0;
1517 \\ comptime var b: u32 = 0;
1518 \\ switch (a) {
1519 \\ 0 => {},
1520 \\ else => b = 3,
1521 \\ }
1522 \\}
1523 , &.{
1524 ":6:21: error: store to comptime variable depends on runtime condition",
1525 ":4:13: note: runtime condition here",
1526 });
1527
1528 case.addCompareOutput(
1529 \\pub fn main() void {
1530 \\ comptime var len: u32 = 5;
1531 \\ print(len);
1532 \\ len += 9;
1533 \\ print(len);
1534 \\}
1535 \\
1536 \\fn print(len: usize) void {
1537 \\ asm volatile ("syscall"
1538 \\ :
1539 \\ : [number] "{rax}" (1),
1540 \\ [arg1] "{rdi}" (1),
1541 \\ [arg2] "{rsi}" (@ptrToInt("Hello, World!\n")),
1542 \\ [arg3] "{rdx}" (len)
1543 \\ : "rcx", "r11", "memory"
1544 \\ );
1545 \\ return;
1546 \\}
1547 , "HelloHello, World!\n");
1548
1549 case.addError(
1550 \\comptime {
1551 \\ var x: i32 = 1;
1552 \\ x += 1;
1553 \\ if (x != 1) unreachable;
1554 \\}
1555 \\pub fn main() void {}
1556 , &.{":4:17: error: unable to resolve comptime value"});
1557
1558 case.addError(
1559 \\pub fn main() void {
1560 \\ comptime var i: u64 = 0;
1561 \\ while (i < 5) : (i += 1) {}
1562 \\}
1563 , &.{
1564 ":3:24: error: cannot store to comptime variable in non-inline loop",
1565 ":3:5: note: non-inline loop here",
1566 });
1567
1568 case.addCompareOutput(
1569 \\pub fn main() void {
1570 \\ var a: u32 = 0;
1571 \\ if (a == 0) {
1572 \\ comptime var b: u32 = 0;
1573 \\ b = 1;
1574 \\ }
1575 \\}
1576 \\comptime {
1577 \\ var x: i32 = 1;
1578 \\ x += 1;
1579 \\ if (x != 2) unreachable;
1580 \\}
1581 , "");
1582
1583 case.addCompareOutput(
1584 \\pub fn main() void {
1585 \\ comptime var i: u64 = 2;
1586 \\ inline while (i < 6) : (i+=1) {
1587 \\ print(i);
1588 \\ }
1589 \\}
1590 \\fn print(len: usize) void {
1591 \\ asm volatile ("syscall"
1592 \\ :
1593 \\ : [number] "{rax}" (1),
1594 \\ [arg1] "{rdi}" (1),
1595 \\ [arg2] "{rsi}" (@ptrToInt("Hello")),
1596 \\ [arg3] "{rdx}" (len)
1597 \\ : "rcx", "r11", "memory"
1598 \\ );
1599 \\ return;
1600 \\}
1601 , "HeHelHellHello");
1602 }
1603
1604 {
1605 var case = ctx.exe("double ampersand", linux_x64);
1606
1607 case.addError(
1608 \\pub const a = if (true && false) 1 else 2;
1609 , &[_][]const u8{":1:24: error: `&&` is invalid; note that `and` is boolean AND"});
1610
1611 case.addError(
1612 \\pub fn main() void {
1613 \\ const a = true;
1614 \\ const b = false;
1615 \\ _ = a & &b;
1616 \\}
1617 , &[_][]const u8{":4:11: error: incompatible types: 'bool' and '*const bool'"});
1618
1619 case.addCompareOutput(
1620 \\pub fn main() void {
1621 \\ const b: u8 = 1;
1622 \\ _ = &&b;
1623 \\}
1624 , "");
1625 }
1626}
test/tests.zig-321
......@@ -16,7 +16,6 @@ const LibExeObjStep = build.LibExeObjStep;
1616const compare_output = @import("compare_output.zig");
1717const standalone = @import("standalone.zig");
1818const stack_traces = @import("stack_traces.zig");
19const compile_errors = @import("compile_errors.zig");
2019const assemble_and_link = @import("assemble_and_link.zig");
2120const runtime_safety = @import("runtime_safety.zig");
2221const translate_c = @import("translate_c.zig");
......@@ -384,21 +383,6 @@ pub fn addRuntimeSafetyTests(b: *build.Builder, test_filter: ?[]const u8, modes:
384383 return cases.step;
385384}
386385
387pub fn addCompileErrorTests(b: *build.Builder, test_filter: ?[]const u8, modes: []const Mode) *build.Step {
388 const cases = b.allocator.create(CompileErrorContext) catch unreachable;
389 cases.* = CompileErrorContext{
390 .b = b,
391 .step = b.step("test-compile-errors", "Run the compile error tests"),
392 .test_index = 0,
393 .test_filter = test_filter,
394 .modes = modes,
395 };
396
397 compile_errors.addCases(cases);
398
399 return cases.step;
400}
401
402386pub fn addStandaloneTests(b: *build.Builder, test_filter: ?[]const u8, modes: []const Mode, skip_non_native: bool, target: std.zig.CrossTarget) *build.Step {
403387 const cases = b.allocator.create(StandaloneContext) catch unreachable;
404388 cases.* = StandaloneContext{
......@@ -840,304 +824,6 @@ pub const StackTracesContext = struct {
840824 };
841825};
842826
843pub const CompileErrorContext = struct {
844 b: *build.Builder,
845 step: *build.Step,
846 test_index: usize,
847 test_filter: ?[]const u8,
848 modes: []const Mode,
849
850 const TestCase = struct {
851 name: []const u8,
852 sources: ArrayList(SourceFile),
853 expected_errors: ArrayList([]const u8),
854 expect_exact: bool,
855 link_libc: bool,
856 is_exe: bool,
857 is_test: bool,
858 target: CrossTarget = CrossTarget{},
859
860 const SourceFile = struct {
861 filename: []const u8,
862 source: []const u8,
863 };
864
865 pub fn addSourceFile(self: *TestCase, filename: []const u8, source: []const u8) void {
866 self.sources.append(SourceFile{
867 .filename = filename,
868 .source = source,
869 }) catch unreachable;
870 }
871
872 pub fn addExpectedError(self: *TestCase, text: []const u8) void {
873 self.expected_errors.append(text) catch unreachable;
874 }
875 };
876
877 const CompileCmpOutputStep = struct {
878 pub const base_id = .custom;
879
880 step: build.Step,
881 context: *CompileErrorContext,
882 name: []const u8,
883 test_index: usize,
884 case: *const TestCase,
885 build_mode: Mode,
886 write_src: *build.WriteFileStep,
887
888 const ErrLineIter = struct {
889 lines: mem.SplitIterator,
890
891 const source_file = "tmp.zig";
892
893 fn init(input: []const u8) ErrLineIter {
894 return ErrLineIter{ .lines = mem.split(input, "\n") };
895 }
896
897 fn next(self: *ErrLineIter) ?[]const u8 {
898 while (self.lines.next()) |line| {
899 if (mem.indexOf(u8, line, source_file) != null)
900 return line;
901 }
902 return null;
903 }
904 };
905
906 pub fn create(
907 context: *CompileErrorContext,
908 name: []const u8,
909 case: *const TestCase,
910 build_mode: Mode,
911 write_src: *build.WriteFileStep,
912 ) *CompileCmpOutputStep {
913 const allocator = context.b.allocator;
914 const ptr = allocator.create(CompileCmpOutputStep) catch unreachable;
915 ptr.* = CompileCmpOutputStep{
916 .step = build.Step.init(.custom, "CompileCmpOutput", allocator, make),
917 .context = context,
918 .name = name,
919 .test_index = context.test_index,
920 .case = case,
921 .build_mode = build_mode,
922 .write_src = write_src,
923 };
924
925 context.test_index += 1;
926 return ptr;
927 }
928
929 fn make(step: *build.Step) !void {
930 const self = @fieldParentPtr(CompileCmpOutputStep, "step", step);
931 const b = self.context.b;
932
933 var zig_args = ArrayList([]const u8).init(b.allocator);
934 zig_args.append(b.zig_exe) catch unreachable;
935
936 if (self.case.is_exe) {
937 try zig_args.append("build-exe");
938 } else if (self.case.is_test) {
939 try zig_args.append("test");
940 } else {
941 try zig_args.append("build-obj");
942 }
943 const root_src_basename = self.case.sources.items[0].filename;
944 try zig_args.append(self.write_src.getFileSource(root_src_basename).?.getPath(b));
945
946 zig_args.append("--name") catch unreachable;
947 zig_args.append("test") catch unreachable;
948
949 if (!self.case.target.isNative()) {
950 try zig_args.append("-target");
951 try zig_args.append(try self.case.target.zigTriple(b.allocator));
952 }
953
954 zig_args.append("-O") catch unreachable;
955 zig_args.append(@tagName(self.build_mode)) catch unreachable;
956
957 warn("Test {d}/{d} {s}...", .{ self.test_index + 1, self.context.test_index, self.name });
958
959 if (b.verbose) {
960 printInvocation(zig_args.items);
961 }
962
963 const child = std.ChildProcess.init(zig_args.items, b.allocator) catch unreachable;
964 defer child.deinit();
965
966 child.env_map = b.env_map;
967 child.stdin_behavior = .Ignore;
968 child.stdout_behavior = .Pipe;
969 child.stderr_behavior = .Pipe;
970
971 child.spawn() catch |err| debug.panic("Unable to spawn {s}: {s}\n", .{ zig_args.items[0], @errorName(err) });
972
973 var stdout_buf = ArrayList(u8).init(b.allocator);
974 var stderr_buf = ArrayList(u8).init(b.allocator);
975
976 child.stdout.?.reader().readAllArrayList(&stdout_buf, max_stdout_size) catch unreachable;
977 child.stderr.?.reader().readAllArrayList(&stderr_buf, max_stdout_size) catch unreachable;
978
979 const term = child.wait() catch |err| {
980 debug.panic("Unable to spawn {s}: {s}\n", .{ zig_args.items[0], @errorName(err) });
981 };
982 switch (term) {
983 .Exited => |code| {
984 if (code == 0) {
985 printInvocation(zig_args.items);
986 return error.CompilationIncorrectlySucceeded;
987 }
988 },
989 else => {
990 warn("Process {s} terminated unexpectedly\n", .{b.zig_exe});
991 printInvocation(zig_args.items);
992 return error.TestFailed;
993 },
994 }
995
996 const stdout = stdout_buf.items;
997 const stderr = stderr_buf.items;
998
999 if (stdout.len != 0) {
1000 warn(
1001 \\
1002 \\Expected empty stdout, instead found:
1003 \\================================================
1004 \\{s}
1005 \\================================================
1006 \\
1007 , .{stdout});
1008 return error.TestFailed;
1009 }
1010
1011 var ok = true;
1012 if (self.case.expect_exact) {
1013 var err_iter = ErrLineIter.init(stderr);
1014 var i: usize = 0;
1015 ok = while (err_iter.next()) |line| : (i += 1) {
1016 if (i >= self.case.expected_errors.items.len) break false;
1017 const expected = self.case.expected_errors.items[i];
1018 if (mem.indexOf(u8, line, expected) == null) break false;
1019 continue;
1020 } else true;
1021
1022 ok = ok and i == self.case.expected_errors.items.len;
1023
1024 if (!ok) {
1025 warn("\n======== Expected these compile errors: ========\n", .{});
1026 for (self.case.expected_errors.items) |expected| {
1027 warn("{s}\n", .{expected});
1028 }
1029 }
1030 } else {
1031 for (self.case.expected_errors.items) |expected| {
1032 if (mem.indexOf(u8, stderr, expected) == null) {
1033 warn(
1034 \\
1035 \\=========== Expected compile error: ============
1036 \\{s}
1037 \\
1038 , .{expected});
1039 ok = false;
1040 break;
1041 }
1042 }
1043 }
1044
1045 if (!ok) {
1046 warn(
1047 \\================= Full output: =================
1048 \\{s}
1049 \\
1050 , .{stderr});
1051 return error.TestFailed;
1052 }
1053
1054 warn("OK\n", .{});
1055 }
1056 };
1057
1058 pub fn create(
1059 self: *CompileErrorContext,
1060 name: []const u8,
1061 source: []const u8,
1062 expected_lines: []const []const u8,
1063 ) *TestCase {
1064 const tc = self.b.allocator.create(TestCase) catch unreachable;
1065 tc.* = TestCase{
1066 .name = name,
1067 .sources = ArrayList(TestCase.SourceFile).init(self.b.allocator),
1068 .expected_errors = ArrayList([]const u8).init(self.b.allocator),
1069 .expect_exact = false,
1070 .link_libc = false,
1071 .is_exe = false,
1072 .is_test = false,
1073 };
1074
1075 tc.addSourceFile("tmp.zig", source);
1076 var arg_i: usize = 0;
1077 while (arg_i < expected_lines.len) : (arg_i += 1) {
1078 tc.addExpectedError(expected_lines[arg_i]);
1079 }
1080 return tc;
1081 }
1082
1083 pub fn addC(self: *CompileErrorContext, name: []const u8, source: []const u8, expected_lines: []const []const u8) void {
1084 var tc = self.create(name, source, expected_lines);
1085 tc.link_libc = true;
1086 self.addCase(tc);
1087 }
1088
1089 pub fn addExe(
1090 self: *CompileErrorContext,
1091 name: []const u8,
1092 source: []const u8,
1093 expected_lines: []const []const u8,
1094 ) void {
1095 var tc = self.create(name, source, expected_lines);
1096 tc.is_exe = true;
1097 self.addCase(tc);
1098 }
1099
1100 pub fn add(
1101 self: *CompileErrorContext,
1102 name: []const u8,
1103 source: []const u8,
1104 expected_lines: []const []const u8,
1105 ) void {
1106 const tc = self.create(name, source, expected_lines);
1107 self.addCase(tc);
1108 }
1109
1110 pub fn addTest(
1111 self: *CompileErrorContext,
1112 name: []const u8,
1113 source: []const u8,
1114 expected_lines: []const []const u8,
1115 ) void {
1116 const tc = self.create(name, source, expected_lines);
1117 tc.is_test = true;
1118 self.addCase(tc);
1119 }
1120
1121 pub fn addCase(self: *CompileErrorContext, case: *const TestCase) void {
1122 const b = self.b;
1123
1124 const annotated_case_name = fmt.allocPrint(self.b.allocator, "compile-error {s}", .{
1125 case.name,
1126 }) catch unreachable;
1127 if (self.test_filter) |filter| {
1128 if (mem.indexOf(u8, annotated_case_name, filter) == null) return;
1129 }
1130 const write_src = b.addWriteFiles();
1131 for (case.sources.items) |src_file| {
1132 write_src.add(src_file.filename, src_file.source);
1133 }
1134
1135 const compile_and_cmp_errors = CompileCmpOutputStep.create(self, annotated_case_name, case, .Debug, write_src);
1136 compile_and_cmp_errors.step.dependOn(&write_src.step);
1137 self.step.dependOn(&compile_and_cmp_errors.step);
1138 }
1139};
1140
1141827pub const StandaloneContext = struct {
1142828 b: *build.Builder,
1143829 step: *build.Step,
......@@ -1312,13 +998,6 @@ pub const GenHContext = struct {
1312998 }
1313999 };
13141000
1315 fn printInvocation(args: []const []const u8) void {
1316 for (args) |arg| {
1317 warn("{s} ", .{arg});
1318 }
1319 warn("\n", .{});
1320 }
1321
13221001 pub fn create(
13231002 self: *GenHContext,
13241003 filename: []const u8,