diff options
| author | Andrew Kelley <andrew@ziglang.org> | 2023-02-18 09:33:27 -0700 |
|---|---|---|
| committer | Andrew Kelley <andrew@ziglang.org> | 2023-02-18 09:33:27 -0700 |
| commit | efdc94c10712f610e7de5e49fd9cd6f88b4bbbae (patch) | |
| tree | 4b66ec30176843b0efd87b73199c75aa2fba675d /test | |
| parent | 06df842e4d313e81444063803deff306602e0a17 (diff) | |
| parent | c32171991b25b323cd68ff96c294bf5a6fa753b8 (diff) | |
| download | zig-efdc94c10712f610e7de5e49fd9cd6f88b4bbbae.tar.gz zig-efdc94c10712f610e7de5e49fd9cd6f88b4bbbae.zip | |
Merge remote-tracking branch 'origin/master' into llvm16
Diffstat (limited to 'test')
23 files changed, 312 insertions, 36 deletions
diff --git a/test/behavior/cast.zig b/test/behavior/cast.zig index dbb4c07f64..16f3c6e2dd 100644 --- a/test/behavior/cast.zig +++ b/test/behavior/cast.zig @@ -1541,3 +1541,30 @@ test "single item pointer to pointer to array to slice" { const z1 = @as([]const i32, @as(*[1]i32, &x)); try expect(z1[0] == 1234); } + +test "peer type resolution forms error union" { + if (builtin.zig_backend == .stage2_wasm) return error.SkipZigTest; // TODO + + var foo: i32 = 123; + const result = if (foo < 0) switch (-foo) { + 0 => unreachable, + 42 => error.AccessDenied, + else => unreachable, + } else @intCast(u32, foo); + try expect(try result == 123); +} + +test "@constCast without a result location" { + const x: i32 = 1234; + const y = @constCast(&x); + try expect(@TypeOf(y) == *i32); + try expect(y.* == 1234); +} + +test "@volatileCast without a result location" { + var x: i32 = 1234; + var y: *volatile i32 = &x; + const z = @volatileCast(y); + try expect(@TypeOf(z) == *i32); + try expect(z.* == 1234); +} diff --git a/test/cases/compile_errors/function_returning_opaque_type.zig b/test/cases/compile_errors/function_returning_opaque_type.zig index caf5d74d40..26d70c86c1 100644 --- a/test/cases/compile_errors/function_returning_opaque_type.zig +++ b/test/cases/compile_errors/function_returning_opaque_type.zig @@ -1,11 +1,11 @@ const FooType = opaque {}; -export fn bar() !FooType { +export fn bar() FooType { return error.InvalidValue; } -export fn bav() !@TypeOf(null) { +export fn bav() @TypeOf(null) { return error.InvalidValue; } -export fn baz() !@TypeOf(undefined) { +export fn baz() @TypeOf(undefined) { return error.InvalidValue; } @@ -13,7 +13,7 @@ export fn baz() !@TypeOf(undefined) { // backend=stage2 // target=native // -// :2:18: error: opaque return type 'tmp.FooType' not allowed +// :2:17: error: opaque return type 'tmp.FooType' not allowed // :1:17: note: opaque declared here -// :5:18: error: return type '@TypeOf(null)' not allowed -// :8:18: error: return type '@TypeOf(undefined)' not allowed +// :5:17: error: return type '@TypeOf(null)' not allowed +// :8:17: error: return type '@TypeOf(undefined)' not allowed diff --git a/test/cases/compile_errors/invalid_error_union_payload_type.zig b/test/cases/compile_errors/invalid_error_union_payload_type.zig index f8646d9450..244eb765ec 100644 --- a/test/cases/compile_errors/invalid_error_union_payload_type.zig +++ b/test/cases/compile_errors/invalid_error_union_payload_type.zig @@ -4,6 +4,12 @@ comptime { comptime { _ = anyerror!anyerror; } +fn someFunction() !anyerror { + return error.C; +} +comptime { + _ = someFunction; +} // error // backend=stage2 @@ -11,3 +17,4 @@ comptime { // // :2:18: error: error union with payload of opaque type 'anyopaque' not allowed // :5:18: error: error union with payload of error set type 'anyerror' not allowed +// :7:20: error: error union with payload of error set type 'anyerror' not allowed diff --git a/test/cases/compile_errors/invalid_qualcast.zig b/test/cases/compile_errors/invalid_qualcast.zig deleted file mode 100644 index 20b223b727..0000000000 --- a/test/cases/compile_errors/invalid_qualcast.zig +++ /dev/null @@ -1,12 +0,0 @@ -pub export fn entry() void { - var a: [*:0]const volatile u16 = undefined; - _ = @qualCast([*]u16, a); -} - -// error -// backend=stage2 -// target=native -// -// :3:9: error: '@qualCast' can only modify 'const' and 'volatile' qualifiers -// :3:9: note: expected type '[*]const volatile u16' -// :3:9: note: got type '[*:0]const volatile u16' diff --git a/test/cases/compile_errors/ptrCast_discards_const_qualifier.zig b/test/cases/compile_errors/ptrCast_discards_const_qualifier.zig index eedef01234..f27f5f4f93 100644 --- a/test/cases/compile_errors/ptrCast_discards_const_qualifier.zig +++ b/test/cases/compile_errors/ptrCast_discards_const_qualifier.zig @@ -9,4 +9,4 @@ export fn entry() void { // target=native // // :3:15: error: cast discards const qualifier -// :3:15: note: consider using '@qualCast' +// :3:15: note: consider using '@constCast' diff --git a/test/link/macho/uuid/build.zig b/test/link/macho/uuid/build.zig index 6a68263fbf..5a8c14ae37 100644 --- a/test/link/macho/uuid/build.zig +++ b/test/link/macho/uuid/build.zig @@ -1,4 +1,8 @@ const std = @import("std"); +const Builder = std.Build.Builder; +const CompileStep = std.Build.CompileStep; +const FileSource = std.Build.FileSource; +const Step = std.Build.Step; pub fn build(b: *std.Build) void { const test_step = b.step("test", "Test"); @@ -10,18 +14,18 @@ pub fn build(b: *std.Build) void { .os_tag = .macos, }; - testUuid(b, test_step, .ReleaseSafe, aarch64_macos, "675bb6ba8e5d3d3191f7936d7168f0e9"); - testUuid(b, test_step, .ReleaseFast, aarch64_macos, "675bb6ba8e5d3d3191f7936d7168f0e9"); - testUuid(b, test_step, .ReleaseSmall, aarch64_macos, "675bb6ba8e5d3d3191f7936d7168f0e9"); + testUuid(b, test_step, .ReleaseSafe, aarch64_macos); + testUuid(b, test_step, .ReleaseFast, aarch64_macos); + testUuid(b, test_step, .ReleaseSmall, aarch64_macos); const x86_64_macos = std.zig.CrossTarget{ .cpu_arch = .x86_64, .os_tag = .macos, }; - testUuid(b, test_step, .ReleaseSafe, x86_64_macos, "5b7071b4587c3071b0d2352fadce0e48"); - testUuid(b, test_step, .ReleaseFast, x86_64_macos, "5b7071b4587c3071b0d2352fadce0e48"); - testUuid(b, test_step, .ReleaseSmall, x86_64_macos, "4b58f2583c383169bbe3a716bd240048"); + testUuid(b, test_step, .ReleaseSafe, x86_64_macos); + testUuid(b, test_step, .ReleaseFast, x86_64_macos); + testUuid(b, test_step, .ReleaseSmall, x86_64_macos); } fn testUuid( @@ -29,25 +33,23 @@ fn testUuid( test_step: *std.Build.Step, optimize: std.builtin.OptimizeMode, target: std.zig.CrossTarget, - comptime exp: []const u8, ) void { // The calculated UUID value is independent of debug info and so it should // stay the same across builds. { const dylib = simpleDylib(b, optimize, target); - const check_dylib = dylib.checkObject(.macho); - check_dylib.checkStart("cmd UUID"); - check_dylib.checkNext("uuid " ++ exp); - test_step.dependOn(&check_dylib.step); + const install_step = installWithRename(dylib, "test1.dylib"); + install_step.step.dependOn(&dylib.step); } { const dylib = simpleDylib(b, optimize, target); dylib.strip = true; - const check_dylib = dylib.checkObject(.macho); - check_dylib.checkStart("cmd UUID"); - check_dylib.checkNext("uuid " ++ exp); - test_step.dependOn(&check_dylib.step); + const install_step = installWithRename(dylib, "test2.dylib"); + install_step.step.dependOn(&dylib.step); } + + const cmp_step = CompareUuid.create(b, "test1.dylib", "test2.dylib"); + test_step.dependOn(&cmp_step.step); } fn simpleDylib( @@ -65,3 +67,118 @@ fn simpleDylib( dylib.linkLibC(); return dylib; } + +fn installWithRename(cs: *CompileStep, name: []const u8) *InstallWithRename { + const step = InstallWithRename.create(cs.builder, cs.getOutputSource(), name); + cs.builder.getInstallStep().dependOn(&step.step); + return step; +} + +const InstallWithRename = struct { + pub const base_id = .custom; + + step: Step, + builder: *Builder, + source: FileSource, + name: []const u8, + + pub fn create( + builder: *Builder, + source: FileSource, + name: []const u8, + ) *InstallWithRename { + const self = builder.allocator.create(InstallWithRename) catch @panic("OOM"); + self.* = InstallWithRename{ + .builder = builder, + .step = Step.init(.custom, builder.fmt("install and rename: {s} -> {s}", .{ + source.getDisplayName(), + name, + }), builder.allocator, make), + .source = source, + .name = builder.dupe(name), + }; + return self; + } + + fn make(step: *Step) anyerror!void { + const self = @fieldParentPtr(InstallWithRename, "step", step); + const source_path = self.source.getPath(self.builder); + const target_path = self.builder.getInstallPath(.lib, self.name); + self.builder.updateFile(source_path, target_path) catch |err| { + std.log.err("Unable to rename: {s} -> {s}", .{ source_path, target_path }); + return err; + }; + } +}; + +const CompareUuid = struct { + pub const base_id = .custom; + + step: Step, + builder: *Builder, + lhs: []const u8, + rhs: []const u8, + + pub fn create(builder: *Builder, lhs: []const u8, rhs: []const u8) *CompareUuid { + const self = builder.allocator.create(CompareUuid) catch @panic("OOM"); + self.* = CompareUuid{ + .builder = builder, + .step = Step.init( + .custom, + builder.fmt("compare uuid: {s} and {s}", .{ + lhs, + rhs, + }), + builder.allocator, + make, + ), + .lhs = lhs, + .rhs = rhs, + }; + return self; + } + + fn make(step: *Step) anyerror!void { + const self = @fieldParentPtr(CompareUuid, "step", step); + const gpa = self.builder.allocator; + + var lhs_uuid: [16]u8 = undefined; + const lhs_path = self.builder.getInstallPath(.lib, self.lhs); + try parseUuid(gpa, lhs_path, &lhs_uuid); + + var rhs_uuid: [16]u8 = undefined; + const rhs_path = self.builder.getInstallPath(.lib, self.rhs); + try parseUuid(gpa, rhs_path, &rhs_uuid); + + try std.testing.expectEqualStrings(&lhs_uuid, &rhs_uuid); + } + + fn parseUuid(gpa: std.mem.Allocator, path: []const u8, uuid: *[16]u8) anyerror!void { + const max_bytes: usize = 20 * 1024 * 1024; + const data = try std.fs.cwd().readFileAllocOptions( + gpa, + path, + max_bytes, + null, + @alignOf(u64), + null, + ); + var stream = std.io.fixedBufferStream(data); + const reader = stream.reader(); + + const hdr = try reader.readStruct(std.macho.mach_header_64); + if (hdr.magic != std.macho.MH_MAGIC_64) { + return error.InvalidMagicNumber; + } + + var it = std.macho.LoadCommandIterator{ + .ncmds = hdr.ncmds, + .buffer = data[@sizeOf(std.macho.mach_header_64)..][0..hdr.sizeofcmds], + }; + const cmd = while (it.next()) |cmd| switch (cmd.cmd()) { + .UUID => break cmd.cast(std.macho.uuid_command).?, + else => {}, + } else return error.UuidLoadCommandNotFound; + std.mem.copy(u8, uuid, &cmd.uuid); + } +}; diff --git a/test/standalone.zig b/test/standalone.zig index c0cb9ff02b..81eb1b0042 100644 --- a/test/standalone.zig +++ b/test/standalone.zig @@ -27,6 +27,7 @@ pub fn addCases(cases: *tests.StandaloneContext) void { cases.add("test/standalone/noreturn_call/inline.zig"); cases.add("test/standalone/noreturn_call/as_arg.zig"); cases.addBuildFile("test/standalone/test_runner_path/build.zig", .{ .requires_stage2 = true }); + cases.addBuildFile("test/standalone/issue_13970/build.zig", .{}); cases.addBuildFile("test/standalone/main_pkg_path/build.zig", .{}); cases.addBuildFile("test/standalone/shared_library/build.zig", .{}); cases.addBuildFile("test/standalone/mix_o_files/build.zig", .{}); @@ -102,4 +103,5 @@ pub fn addCases(cases: *tests.StandaloneContext) void { cases.addBuildFile("test/standalone/issue_13030/build.zig", .{ .build_modes = true }); cases.addBuildFile("test/standalone/emit_asm_and_bin/build.zig", .{}); cases.addBuildFile("test/standalone/issue_12588/build.zig", .{}); + cases.addBuildFile("test/standalone/embed_generated_file/build.zig", .{}); } diff --git a/test/standalone/embed_generated_file/bootloader.zig b/test/standalone/embed_generated_file/bootloader.zig new file mode 100644 index 0000000000..dc79a847f4 --- /dev/null +++ b/test/standalone/embed_generated_file/bootloader.zig @@ -0,0 +1 @@ +pub export fn _start() void {} diff --git a/test/standalone/embed_generated_file/build.zig b/test/standalone/embed_generated_file/build.zig new file mode 100644 index 0000000000..3b17ff0b8f --- /dev/null +++ b/test/standalone/embed_generated_file/build.zig @@ -0,0 +1,28 @@ +const std = @import("std"); + +pub fn build(b: *std.Build) void { + const target = b.standardTargetOptions(.{}); + const optimize = b.standardOptimizeOption(.{}); + + const bootloader = b.addExecutable(.{ + .name = "bootloader", + .root_source_file = .{ .path = "bootloader.zig" }, + .target = .{ + .cpu_arch = .x86, + .os_tag = .freestanding, + }, + .optimize = .ReleaseSmall, + }); + + const exe = b.addTest(.{ + .root_source_file = .{ .path = "main.zig" }, + .target = target, + .optimize = optimize, + }); + exe.addAnonymousModule("bootloader.elf", .{ + .source_file = bootloader.getOutputSource(), + }); + + const test_step = b.step("test", "Test the program"); + test_step.dependOn(&exe.step); +} diff --git a/test/standalone/embed_generated_file/main.zig b/test/standalone/embed_generated_file/main.zig new file mode 100644 index 0000000000..31f6e8c628 --- /dev/null +++ b/test/standalone/embed_generated_file/main.zig @@ -0,0 +1,8 @@ +const std = @import("std"); +const blah = @embedFile("bootloader.elf"); + +test { + comptime { + std.debug.assert(std.mem.eql(u8, blah[1..][0..3], "ELF")); + } +} diff --git a/test/standalone/issue_13970/build.zig b/test/standalone/issue_13970/build.zig new file mode 100644 index 0000000000..f5e07d8903 --- /dev/null +++ b/test/standalone/issue_13970/build.zig @@ -0,0 +1,21 @@ +const std = @import("std"); + +pub fn build(b: *std.Build) void { + const test1 = b.addTest(.{ + .root_source_file = .{ .path = "test_root/empty.zig" }, + }); + const test2 = b.addTest(.{ + .root_source_file = .{ .path = "src/empty.zig" }, + }); + const test3 = b.addTest(.{ + .root_source_file = .{ .path = "empty.zig" }, + }); + test1.setTestRunner("src/main.zig"); + test2.setTestRunner("src/main.zig"); + test3.setTestRunner("src/main.zig"); + + const test_step = b.step("test", "Test package path resolution of custom test runner"); + test_step.dependOn(&test1.step); + test_step.dependOn(&test2.step); + test_step.dependOn(&test3.step); +} diff --git a/test/standalone/issue_13970/empty.zig b/test/standalone/issue_13970/empty.zig new file mode 100644 index 0000000000..e69de29bb2 --- /dev/null +++ b/test/standalone/issue_13970/empty.zig diff --git a/test/standalone/issue_13970/src/empty.zig b/test/standalone/issue_13970/src/empty.zig new file mode 100644 index 0000000000..e69de29bb2 --- /dev/null +++ b/test/standalone/issue_13970/src/empty.zig diff --git a/test/standalone/issue_13970/src/main.zig b/test/standalone/issue_13970/src/main.zig new file mode 100644 index 0000000000..d71320d46a --- /dev/null +++ b/test/standalone/issue_13970/src/main.zig @@ -0,0 +1,8 @@ +const std = @import("std"); +const package = @import("package.zig"); +const root = @import("root"); +const builtin = @import("builtin"); + +pub fn main() !void { + _ = package.decl; +} diff --git a/test/standalone/issue_13970/src/package.zig b/test/standalone/issue_13970/src/package.zig new file mode 100644 index 0000000000..4f6037b38d --- /dev/null +++ b/test/standalone/issue_13970/src/package.zig @@ -0,0 +1 @@ +pub const decl = 0; diff --git a/test/standalone/issue_13970/test_root/empty.zig b/test/standalone/issue_13970/test_root/empty.zig new file mode 100644 index 0000000000..e69de29bb2 --- /dev/null +++ b/test/standalone/issue_13970/test_root/empty.zig diff --git a/test/standalone/test_runner_module_imports/build.zig b/test/standalone/test_runner_module_imports/build.zig new file mode 100644 index 0000000000..973365e495 --- /dev/null +++ b/test/standalone/test_runner_module_imports/build.zig @@ -0,0 +1,19 @@ +const std = @import("std"); + +pub fn build(b: *std.Build) void { + const t = b.addTest(.{ + .root_source_file = .{ .path = "src/main.zig" }, + }); + t.setTestRunner("test_runner/main.zig"); + + const module1 = b.createModule(.{ .source_file = .{ .path = "module1/main.zig" } }); + const module2 = b.createModule(.{ + .source_file = .{ .path = "module2/main.zig" }, + .dependencies = &.{.{ .name = "module1", .module = module1 }}, + }); + + t.addModule("module2", module2); + + const test_step = b.step("test", "Run unit tests"); + test_step.dependOn(&t.step); +} diff --git a/test/standalone/test_runner_module_imports/module1/main.zig b/test/standalone/test_runner_module_imports/module1/main.zig new file mode 100644 index 0000000000..883c61ac7e --- /dev/null +++ b/test/standalone/test_runner_module_imports/module1/main.zig @@ -0,0 +1 @@ +pub const decl: usize = 1234567890; diff --git a/test/standalone/test_runner_module_imports/module2/main.zig b/test/standalone/test_runner_module_imports/module2/main.zig new file mode 100644 index 0000000000..f8bb21d7df --- /dev/null +++ b/test/standalone/test_runner_module_imports/module2/main.zig @@ -0,0 +1 @@ +pub const mod1 = @import("module1"); diff --git a/test/standalone/test_runner_module_imports/src/main.zig b/test/standalone/test_runner_module_imports/src/main.zig new file mode 100644 index 0000000000..9d5d5795b6 --- /dev/null +++ b/test/standalone/test_runner_module_imports/src/main.zig @@ -0,0 +1,6 @@ +const mod2 = @import("module2"); +const std = @import("std"); + +test { + try std.testing.expectEqual(@as(usize, 1234567890), mod2.mod1.decl); +} diff --git a/test/standalone/test_runner_module_imports/test_runner/main.zig b/test/standalone/test_runner_module_imports/test_runner/main.zig new file mode 100644 index 0000000000..13ce91b88f --- /dev/null +++ b/test/standalone/test_runner_module_imports/test_runner/main.zig @@ -0,0 +1,9 @@ +const std = @import("std"); +const mod2 = @import("module2"); + +pub fn main() !void { + try std.testing.expectEqual(@as(usize, 1234567890), mod2.mod1.decl); + for (@import("builtin").test_functions) |test_fn| { + try test_fn.func(); + } +} diff --git a/test/tests.zig b/test/tests.zig index 94030ce851..d3ebe5a046 100644 --- a/test/tests.zig +++ b/test/tests.zig @@ -570,7 +570,7 @@ pub fn addCliTests(b: *std.Build, test_filter: ?[]const u8, optimize_modes: []co const run_cmd = exe.run(); run_cmd.addArgs(&[_][]const u8{ fs.realpathAlloc(b.allocator, b.zig_exe) catch unreachable, - b.pathFromRoot(b.cache_root), + b.pathFromRoot(b.cache_root.path orelse "."), }); step.dependOn(&run_cmd.step); @@ -1059,7 +1059,7 @@ pub const StandaloneContext = struct { } var zig_args = ArrayList([]const u8).init(b.allocator); - const rel_zig_exe = fs.path.relative(b.allocator, b.build_root, b.zig_exe) catch unreachable; + const rel_zig_exe = fs.path.relative(b.allocator, b.build_root.path orelse ".", b.zig_exe) catch unreachable; zig_args.append(rel_zig_exe) catch unreachable; zig_args.append("build") catch unreachable; diff --git a/test/translate_c.zig b/test/translate_c.zig index d2db895a5a..92dc3038c0 100644 --- a/test/translate_c.zig +++ b/test/translate_c.zig @@ -3916,4 +3916,36 @@ pub fn addCases(cases: *tests.TranslateCContext) void { \\ }) != 0) {} \\} }); + + if (builtin.os.tag == .windows) { + cases.add("Pointer subtraction with typedef", + \\typedef char* S; + \\void foo() { + \\ S a, b; + \\ long long c = a - b; + \\} + , &[_][]const u8{ + \\pub export fn foo() void { + \\ var a: S = undefined; + \\ var b: S = undefined; + \\ var c: c_longlong = @divExact(@bitCast(c_longlong, @ptrToInt(a) -% @ptrToInt(b)), @sizeOf(u8)); + \\ _ = @TypeOf(c); + \\} + }); + } else { + cases.add("Pointer subtraction with typedef", + \\typedef char* S; + \\void foo() { + \\ S a, b; + \\ long c = a - b; + \\} + , &[_][]const u8{ + \\pub export fn foo() void { + \\ var a: S = undefined; + \\ var b: S = undefined; + \\ var c: c_long = @divExact(@bitCast(c_long, @ptrToInt(a) -% @ptrToInt(b)), @sizeOf(u8)); + \\ _ = @TypeOf(c); + \\} + }); + } } |
