authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2021-06-23 18:09:19-04:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2021-06-23 18:09:19-04:00
log31c49ad64ded02b9dde57f5d3ef102a771fa5cf7
treed715ce49093cad93f00dff554b47b3b64a58fdc3
parentfc185a6f71b9bd611a6d808082999a9da0f107e8
parent29314b64bd91be91e8f1d48a1b71eba68569be94
signaturebadge-question-mark Signed by PGP key 4AEE18F83AFDEB23

Merge pull request #9191 from ziglang/stage1-astcheck

run AstGen even when using the stage1 backend

12 files changed, 585 insertions(+), 98 deletions(-)

CMakeLists.txt+6-1
......@@ -91,7 +91,12 @@ set(ZIG_TARGET_MCPU "baseline" CACHE STRING "-mcpu parameter to output binaries
9191set(ZIG_EXECUTABLE "" CACHE STRING "(when cross compiling) path to already-built zig binary")
9292set(ZIG_SINGLE_THREADED off CACHE BOOL "limit the zig compiler to use only 1 thread")
9393set(ZIG_OMIT_STAGE2 off CACHE BOOL "omit the stage2 backend from stage1")
94set(ZIG_ENABLE_LOGGING off CACHE BOOL "enable logging")
94
95if("${CMAKE_BUILD_TYPE}" STREQUAL "Debug")
96 set(ZIG_ENABLE_LOGGING ON CACHE BOOL "enable logging")
97else()
98 set(ZIG_ENABLE_LOGGING OFF CACHE BOOL "enable logging")
99endif()
95100
96101if("${ZIG_TARGET_TRIPLE}" STREQUAL "native")
97102 set(ZIG_USE_LLVM_CONFIG ON CACHE BOOL "use llvm-config to find LLVM libraries")
ci/azure/linux_script+2-2
......@@ -59,9 +59,9 @@ unset CXX
5959
6060make $JOBS install
6161
62# Look for formatting errors and AST errors.
62# Look for non-conforming code formatting.
6363# Formatting errors can be fixed by running `zig fmt` on the files printed here.
64release/bin/zig fmt --check --ast-check ..
64release/bin/zig fmt --check ..
6565
6666# Here we rebuild zig but this time using the Zig binary we just now produced to
6767# build zig1.o rather than relying on the one built with stage0. See
doc/langref.html.in+54-24
......@@ -965,7 +965,7 @@ test "thread local storage" {
965965 thread2.wait();
966966}
967967
968fn testTls(context: void) void {
968fn testTls(_: void) void {
969969 assert(x == 1234);
970970 x += 1;
971971 assert(x == 1235);
......@@ -2502,6 +2502,8 @@ test "struct namespaced variable" {
25022502
25032503 // you can still instantiate an empty struct
25042504 const does_nothing = Empty {};
2505
2506 _ = does_nothing;
25052507}
25062508
25072509// struct field order is determined by the compiler for optimal performance.
......@@ -3026,11 +3028,12 @@ const Foo = enum { a, b, c };
30263028export fn entry(foo: Foo) void { }
30273029 {#code_end#}
30283030 <p>
3029 For a C-ABI-compatible enum, use {#syntax#}extern enum{#endsyntax#}:
3031 For a C-ABI-compatible enum, provide an explicit tag type to
3032 the enum:
30303033 </p>
30313034 {#code_begin|obj#}
3032const Foo = extern enum { a, b, c };
3033export fn entry(foo: Foo) void { }
3035const Foo = enum(c_int) { a, b, c };
3036export fn entry(foo: Foo) void { _ = foo; }
30343037 {#code_end#}
30353038 {#header_close#}
30363039
......@@ -3392,9 +3395,11 @@ test "inside test block" {
33923395test "separate scopes" {
33933396 {
33943397 const pi = 3.14;
3398 _ = pi;
33953399 }
33963400 {
33973401 var pi: bool = true;
3402 _ = pi;
33983403 }
33993404}
34003405 {#code_end#}
......@@ -3432,7 +3437,7 @@ test "switch simple" {
34323437 // Switching on arbitrary expressions is allowed as long as the
34333438 // expression is known at compile-time.
34343439 zz => zz,
3435 comptime blk: {
3440 blk: {
34363441 const d: u32 = 5;
34373442 const e: u32 = 100;
34383443 break :blk d + e;
......@@ -3831,7 +3836,7 @@ test "for basics" {
38313836 // To access the index of iteration, specify a second capture value.
38323837 // This is zero-indexed.
38333838 var sum2: i32 = 0;
3834 for (items) |value, i| {
3839 for (items) |_, i| {
38353840 try expect(@TypeOf(i) == usize);
38363841 sum2 += @intCast(i32, i);
38373842 }
......@@ -3984,7 +3989,7 @@ test "if optional" {
39843989 }
39853990
39863991 const b: ?u32 = null;
3987 if (b) |value| {
3992 if (b) |_| {
39883993 unreachable;
39893994 } else {
39903995 try expect(true);
......@@ -4021,11 +4026,13 @@ test "if error union" {
40214026 if (a) |value| {
40224027 try expect(value == 0);
40234028 } else |err| {
4029 _ = err;
40244030 unreachable;
40254031 }
40264032
40274033 const b: anyerror!u32 = error.BadValue;
40284034 if (b) |value| {
4035 _ = value;
40294036 unreachable;
40304037 } else |err| {
40314038 try expect(err == error.BadValue);
......@@ -4045,13 +4052,13 @@ test "if error union" {
40454052 var c: anyerror!u32 = 3;
40464053 if (c) |*value| {
40474054 value.* = 9;
4048 } else |err| {
4055 } else |_| {
40494056 unreachable;
40504057 }
40514058
40524059 if (c) |value| {
40534060 try expect(value == 9);
4054 } else |err| {
4061 } else |_| {
40554062 unreachable;
40564063 }
40574064}
......@@ -4064,18 +4071,20 @@ test "if error union with optional" {
40644071 if (a) |optional_value| {
40654072 try expect(optional_value.? == 0);
40664073 } else |err| {
4074 _ = err;
40674075 unreachable;
40684076 }
40694077
40704078 const b: anyerror!?u32 = null;
40714079 if (b) |optional_value| {
40724080 try expect(optional_value == null);
4073 } else |err| {
4081 } else |_| {
40744082 unreachable;
40754083 }
40764084
40774085 const c: anyerror!?u32 = error.BadValue;
40784086 if (c) |optional_value| {
4087 _ = optional_value;
40794088 unreachable;
40804089 } else |err| {
40814090 try expect(err == error.BadValue);
......@@ -4087,13 +4096,13 @@ test "if error union with optional" {
40874096 if (optional_value.*) |*value| {
40884097 value.* = 9;
40894098 }
4090 } else |err| {
4099 } else |_| {
40914100 unreachable;
40924101 }
40934102
40944103 if (d) |optional_value| {
40954104 try expect(optional_value.? == 9);
4096 } else |err| {
4105 } else |_| {
40974106 unreachable;
40984107 }
40994108}
......@@ -4246,6 +4255,7 @@ test "type of unreachable" {
42464255 {#code_begin|test#}
42474256fn foo(condition: bool, b: u32) void {
42484257 const a = if (condition) b else return;
4258 _ = a;
42494259 @panic("do something with a");
42504260}
42514261test "noreturn" {
......@@ -4574,7 +4584,7 @@ test "parse u64" {
45744584 {#code_begin|syntax#}
45754585fn doAThing(str: []u8) void {
45764586 const number = parseU64(str, 10) catch 13;
4577 // ...
4587 _ = number; // ...
45784588}
45794589 {#code_end#}
45804590 <p>
......@@ -4589,7 +4599,7 @@ fn doAThing(str: []u8) void {
45894599 {#code_begin|syntax#}
45904600fn doAThing(str: []u8) !void {
45914601 const number = parseU64(str, 10) catch |err| return err;
4592 // ...
4602 _ = number; // ...
45934603}
45944604 {#code_end#}
45954605 <p>
......@@ -4598,7 +4608,7 @@ fn doAThing(str: []u8) !void {
45984608 {#code_begin|syntax#}
45994609fn doAThing(str: []u8) !void {
46004610 const number = try parseU64(str, 10);
4601 // ...
4611 _ = number; // ...
46024612}
46034613 {#code_end#}
46044614 <p>
......@@ -5022,7 +5032,7 @@ extern fn malloc(size: size_t) ?*u8;
50225032
50235033fn doAThing() ?*Foo {
50245034 const ptr = malloc(1234) orelse return null;
5025 // ...
5035 _ = ptr; // ...
50265036}
50275037 {#code_end#}
50285038 <p>
......@@ -5135,6 +5145,7 @@ test "optional pointers" {
51355145test "type coercion - variable declaration" {
51365146 var a: u8 = 1;
51375147 var b: u16 = a;
5148 _ = b;
51385149}
51395150
51405151test "type coercion - function call" {
......@@ -5142,11 +5153,14 @@ test "type coercion - function call" {
51425153 foo(a);
51435154}
51445155
5145fn foo(b: u16) void {}
5156fn foo(b: u16) void {
5157 _ = b;
5158}
51465159
51475160test "type coercion - @as builtin" {
51485161 var a: u8 = 1;
51495162 var b = @as(u16, a);
5163 _ = b;
51505164}
51515165 {#code_end#}
51525166 <p>
......@@ -5174,7 +5188,7 @@ test "type coercion - const qualification" {
51745188 foo(b);
51755189}
51765190
5177fn foo(a: *const i32) void {}
5191fn foo(_: *const i32) void {}
51785192 {#code_end#}
51795193 <p>
51805194 In addition, pointers coerce to const optional pointers:
......@@ -5424,7 +5438,7 @@ test "coercion between unions and enums" {
54245438test "coercion of zero bit types" {
54255439 var x: void = {};
54265440 var y: *void = x;
5427 //var z: void = y; // TODO
5441 _ = y;
54285442}
54295443 {#code_end#}
54305444 {#header_close#}
......@@ -6569,6 +6583,7 @@ var x: i32 = 1;
65696583test "suspend with no resume" {
65706584 var frame = async func();
65716585 try expect(x == 2);
6586 _ = frame;
65726587}
65736588
65746589fn func() void {
......@@ -6800,6 +6815,7 @@ fn amain() !void {
68006815
68016816var global_download_frame: anyframe = undefined;
68026817fn fetchUrl(allocator: *Allocator, url: []const u8) ![]u8 {
6818 _ = url; // this is just an example, we don't actually do it!
68036819 const result = try std.mem.dupe(allocator, u8, "this is the downloaded url contents");
68046820 errdefer allocator.free(result);
68056821 suspend {
......@@ -6811,6 +6827,7 @@ fn fetchUrl(allocator: *Allocator, url: []const u8) ![]u8 {
68116827
68126828var global_file_frame: anyframe = undefined;
68136829fn readFile(allocator: *Allocator, filename: []const u8) ![]u8 {
6830 _ = filename; // this is just an example, we don't actually do it!
68146831 const result = try std.mem.dupe(allocator, u8, "this is the file contents");
68156832 errdefer allocator.free(result);
68166833 suspend {
......@@ -6869,6 +6886,7 @@ fn amain() !void {
68696886}
68706887
68716888fn fetchUrl(allocator: *Allocator, url: []const u8) ![]u8 {
6889 _ = url; // this is just an example, we don't actually do it!
68726890 const result = try std.mem.dupe(allocator, u8, "this is the downloaded url contents");
68736891 errdefer allocator.free(result);
68746892 std.debug.print("fetchUrl returning\n", .{});
......@@ -6876,6 +6894,7 @@ fn fetchUrl(allocator: *Allocator, url: []const u8) ![]u8 {
68766894}
68776895
68786896fn readFile(allocator: *Allocator, filename: []const u8) ![]u8 {
6897 _ = filename; // this is just an example, we don't actually do it!
68796898 const result = try std.mem.dupe(allocator, u8, "this is the file contents");
68806899 errdefer allocator.free(result);
68816900 std.debug.print("readFile returning\n", .{});
......@@ -8584,6 +8603,7 @@ fn List(comptime T: type) type {
85848603test "integer cast panic" {
85858604 var a: u16 = 0xabcd;
85868605 var b: u8 = @intCast(u8, a);
8606 _ = b;
85878607}
85888608 {#code_end#}
85898609 <p>
......@@ -8839,6 +8859,7 @@ comptime {
88398859 {#code_begin|exe_err#}
88408860pub fn main() void {
88418861 var x = foo("hello");
8862 _ = x;
88428863}
88438864
88448865fn foo(x: []const u8) u8 {
......@@ -9107,6 +9128,7 @@ pub fn main() void {
91079128comptime {
91089129 const optional_number: ?i32 = null;
91099130 const number = optional_number.?;
9131 _ = number;
91109132}
91119133 {#code_end#}
91129134 <p>At runtime:</p>
......@@ -9140,6 +9162,7 @@ pub fn main() void {
91409162 {#code_begin|test_err|caught unexpected error 'UnableToReturnNumber'#}
91419163comptime {
91429164 const number = getNumberOrFail() catch unreachable;
9165 _ = number;
91439166}
91449167
91459168fn getNumberOrFail() !i32 {
......@@ -9187,6 +9210,7 @@ comptime {
91879210 const err = error.AnError;
91889211 const number = @errorToInt(err) + 10;
91899212 const invalid_err = @intToError(number);
9213 _ = invalid_err;
91909214}
91919215 {#code_end#}
91929216 <p>At runtime:</p>
......@@ -9197,7 +9221,7 @@ pub fn main() void {
91979221 var err = error.AnError;
91989222 var number = @errorToInt(err) + 500;
91999223 var invalid_err = @intToError(number);
9200 std.debug.print("value: {}\n", .{number});
9224 std.debug.print("value: {}\n", .{invalid_err});
92019225}
92029226 {#code_end#}
92039227 {#header_close#}
......@@ -9212,6 +9236,7 @@ const Foo = enum {
92129236comptime {
92139237 const a: u2 = 3;
92149238 const b = @intToEnum(Foo, a);
9239 _ = b;
92159240}
92169241 {#code_end#}
92179242 <p>At runtime:</p>
......@@ -9396,6 +9421,7 @@ comptime {
93969421pub fn main() void {
93979422 var opt_ptr: ?*i32 = null;
93989423 var ptr = @ptrCast(*i32, opt_ptr);
9424 _ = ptr;
93999425}
94009426 {#code_end#}
94019427 {#header_close#}
......@@ -9523,7 +9549,9 @@ pub fn main() !void {
95239549 This is why it is an error to pass a string literal to a mutable slice, like this:
95249550 </p>
95259551 {#code_begin|test_err|expected type '[]u8'#}
9526fn foo(s: []u8) void {}
9552fn foo(s: []u8) void {
9553 _ = s;
9554}
95279555
95289556test "string literal to mutable slice" {
95299557 foo("hello");
......@@ -9531,7 +9559,9 @@ test "string literal to mutable slice" {
95319559 {#code_end#}
95329560 <p>However if you make the slice constant, then it works:</p>
95339561 {#code_begin|test|strlit#}
9534fn foo(s: []const u8) void {}
9562fn foo(s: []const u8) void {
9563 _ = s;
9564}
95359565
95369566test "string literal to constant slice" {
95379567 foo("hello");
......@@ -10476,7 +10506,7 @@ coding style.
1047610506 </p>
1047710507 {#header_close#}
1047810508 {#header_open|Examples#}
10479 {#code_begin|syntax#}
10509 <pre>{#syntax#}
1048010510const namespace_name = @import("dir_name/file_name.zig");
1048110511const TypeName = @import("dir_name/TypeName.zig");
1048210512var global_var: i32 = undefined;
......@@ -10520,7 +10550,7 @@ const XmlParser = struct {
1052010550
1052110551// The initials BE (Big Endian) are just another word in Zig identifier names.
1052210552fn readU32Be() u32 {}
10523 {#code_end#}
10553 {#endsyntax#}</pre>
1052410554 <p>
1052510555 See the Zig Standard Library for more examples.
1052610556 </p>
src/AstGen.zig+4
......@@ -3558,6 +3558,10 @@ fn structDeclInner(
35583558 const field_name = try astgen.identAsString(member.ast.name_token);
35593559 fields_data.appendAssumeCapacity(field_name);
35603560
3561 if (member.ast.type_expr == 0) {
3562 return astgen.failTok(member.ast.name_token, "struct field missing type", .{});
3563 }
3564
35613565 const field_type: Zir.Inst.Ref = if (node_tags[member.ast.type_expr] == .@"anytype")
35623566 .none
35633567 else
src/Compilation.zig+89-59
......@@ -342,6 +342,7 @@ pub const AllErrors = struct {
342342 const stderr = stderr_file.writer();
343343 switch (msg) {
344344 .src => |src| {
345 try stderr.writeByteNTimes(' ', indent);
345346 ttyconf.setColor(stderr, .Bold);
346347 try stderr.print("{s}:{d}:{d}: ", .{
347348 src.src_path,
......@@ -349,7 +350,6 @@ pub const AllErrors = struct {
349350 src.column + 1,
350351 });
351352 ttyconf.setColor(stderr, color);
352 try stderr.writeByteNTimes(' ', indent);
353353 try stderr.writeAll(kind);
354354 ttyconf.setColor(stderr, .Reset);
355355 ttyconf.setColor(stderr, .Bold);
......@@ -731,6 +731,7 @@ fn addPackageTableToCacheHash(
731731 hash: *Cache.HashHelper,
732732 arena: *std.heap.ArenaAllocator,
733733 pkg_table: Package.Table,
734 seen_table: *std.AutoHashMap(*Package, void),
734735 hash_type: union(enum) { path_bytes, files: *Cache.Manifest },
735736) (error{OutOfMemory} || std.os.GetCwdError)!void {
736737 const allocator = &arena.allocator;
......@@ -755,6 +756,8 @@ fn addPackageTableToCacheHash(
755756 }.lessThan);
756757
757758 for (packages) |pkg| {
759 if ((try seen_table.getOrPut(pkg.value)).found_existing) continue;
760
758761 // Finally insert the package name and path to the cache hash.
759762 hash.addBytes(pkg.key);
760763 switch (hash_type) {
......@@ -770,7 +773,7 @@ fn addPackageTableToCacheHash(
770773 },
771774 }
772775 // Recurse to handle the package's dependencies
773 try addPackageTableToCacheHash(hash, arena, pkg.value.table, hash_type);
776 try addPackageTableToCacheHash(hash, arena, pkg.value.table, seen_table, hash_type);
774777 }
775778}
776779
......@@ -1116,7 +1119,8 @@ pub fn create(gpa: *Allocator, options: InitOptions) !*Compilation {
11161119 {
11171120 var local_arena = std.heap.ArenaAllocator.init(gpa);
11181121 defer local_arena.deinit();
1119 try addPackageTableToCacheHash(&hash, &local_arena, root_pkg.table, .path_bytes);
1122 var seen_table = std.AutoHashMap(*Package, void).init(&local_arena.allocator);
1123 try addPackageTableToCacheHash(&hash, &local_arena, root_pkg.table, &seen_table, .path_bytes);
11201124 }
11211125 hash.add(valgrind);
11221126 hash.add(single_threaded);
......@@ -1137,36 +1141,32 @@ pub fn create(gpa: *Allocator, options: InitOptions) !*Compilation {
11371141 artifact_sub_dir,
11381142 };
11391143
1140 // If we rely on stage1, we must not redundantly add these packages.
1141 const use_stage1 = build_options.is_stage1 and use_llvm;
1142 if (!use_stage1) {
1143 const builtin_pkg = try Package.createWithDir(
1144 gpa,
1145 zig_cache_artifact_directory,
1146 null,
1147 "builtin.zig",
1148 );
1149 errdefer builtin_pkg.destroy(gpa);
1144 const builtin_pkg = try Package.createWithDir(
1145 gpa,
1146 zig_cache_artifact_directory,
1147 null,
1148 "builtin.zig",
1149 );
1150 errdefer builtin_pkg.destroy(gpa);
11501151
1151 const std_pkg = try Package.createWithDir(
1152 gpa,
1153 options.zig_lib_directory,
1154 "std",
1155 "std.zig",
1156 );
1157 errdefer std_pkg.destroy(gpa);
1152 const std_pkg = try Package.createWithDir(
1153 gpa,
1154 options.zig_lib_directory,
1155 "std",
1156 "std.zig",
1157 );
1158 errdefer std_pkg.destroy(gpa);
11581159
1159 try root_pkg.addAndAdopt(gpa, "builtin", builtin_pkg);
1160 try root_pkg.add(gpa, "root", root_pkg);
1161 try root_pkg.addAndAdopt(gpa, "std", std_pkg);
1160 try root_pkg.addAndAdopt(gpa, "builtin", builtin_pkg);
1161 try root_pkg.add(gpa, "root", root_pkg);
1162 try root_pkg.addAndAdopt(gpa, "std", std_pkg);
11621163
1163 try std_pkg.add(gpa, "builtin", builtin_pkg);
1164 try std_pkg.add(gpa, "root", root_pkg);
1165 try std_pkg.add(gpa, "std", std_pkg);
1164 try std_pkg.add(gpa, "builtin", builtin_pkg);
1165 try std_pkg.add(gpa, "root", root_pkg);
1166 try std_pkg.add(gpa, "std", std_pkg);
11661167
1167 try builtin_pkg.add(gpa, "std", std_pkg);
1168 try builtin_pkg.add(gpa, "builtin", builtin_pkg);
1169 }
1168 try builtin_pkg.add(gpa, "std", std_pkg);
1169 try builtin_pkg.add(gpa, "builtin", builtin_pkg);
11701170
11711171 // Pre-open the directory handles for cached ZIR code so that it does not need
11721172 // to redundantly happen for each AstGen operation.
......@@ -1625,30 +1625,39 @@ pub fn update(self: *Compilation) !void {
16251625 // Add a Job for each C object.
16261626 try self.c_object_work_queue.ensureUnusedCapacity(self.c_object_table.count());
16271627 for (self.c_object_table.keys()) |key| {
1628 assert(@ptrToInt(key) != 0xaaaa_aaaa_aaaa_aaaa);
16291628 self.c_object_work_queue.writeItemAssumeCapacity(key);
16301629 }
16311630
16321631 const use_stage1 = build_options.omit_stage2 or
16331632 (build_options.is_stage1 and self.bin_file.options.use_llvm);
1634 if (!use_stage1) {
1635 if (self.bin_file.options.module) |module| {
1636 module.compile_log_text.shrinkAndFree(module.gpa, 0);
1637 module.generation += 1;
1638
1639 // Make sure std.zig is inside the import_table. We unconditionally need
1640 // it for start.zig.
1641 const std_pkg = module.root_pkg.table.get("std").?;
1642 _ = try module.importPkg(std_pkg);
1643
1644 // Put a work item in for every known source file to detect if
1645 // it changed, and, if so, re-compute ZIR and then queue the job
1646 // to update it.
1647 try self.astgen_work_queue.ensureUnusedCapacity(module.import_table.count());
1648 for (module.import_table.values()) |value| {
1649 self.astgen_work_queue.writeItemAssumeCapacity(value);
1650 }
1633 if (self.bin_file.options.module) |module| {
1634 module.compile_log_text.shrinkAndFree(module.gpa, 0);
1635 module.generation += 1;
1636
1637 // Make sure std.zig is inside the import_table. We unconditionally need
1638 // it for start.zig.
1639 const std_pkg = module.root_pkg.table.get("std").?;
1640 _ = try module.importPkg(std_pkg);
1641
1642 // Normally we rely on importing std to in turn import the root source file
1643 // in the start code, but when using the stage1 backend that won't happen,
1644 // so in order to run AstGen on the root source file we put it into the
1645 // import_table here.
1646 if (use_stage1) {
1647 _ = try module.importPkg(module.root_pkg);
1648 }
1649
1650 // Put a work item in for every known source file to detect if
1651 // it changed, and, if so, re-compute ZIR and then queue the job
1652 // to update it.
1653 // We still want AstGen work items for stage1 so that we expose compile errors
1654 // that are implemented in stage2 but not stage1.
1655 try self.astgen_work_queue.ensureUnusedCapacity(module.import_table.count());
1656 for (module.import_table.values()) |value| {
1657 self.astgen_work_queue.writeItemAssumeCapacity(value);
1658 }
16511659
1660 if (!use_stage1) {
16521661 try self.work_queue.writeItem(.{ .analyze_pkg = std_pkg });
16531662 }
16541663 }
......@@ -1915,7 +1924,7 @@ pub fn performAllTheWork(self: *Compilation) error{ TimerUnsupported, OutOfMemor
19151924 // (at least for now) single-threaded main work queue. However, C object compilation
19161925 // only needs to be finished by the end of this function.
19171926
1918 var zir_prog_node = main_progress_node.start("AstGen", self.astgen_work_queue.count);
1927 var zir_prog_node = main_progress_node.start("AST Lowering", self.astgen_work_queue.count);
19191928 defer zir_prog_node.end();
19201929
19211930 var c_obj_prog_node = main_progress_node.start("Compile C Objects", self.c_source_files.len);
......@@ -1936,7 +1945,6 @@ pub fn performAllTheWork(self: *Compilation) error{ TimerUnsupported, OutOfMemor
19361945 }
19371946
19381947 while (self.c_object_work_queue.readItem()) |c_object| {
1939 assert(@ptrToInt(c_object) != 0xaaaa_aaaa_aaaa_aaaa);
19401948 self.work_queue_wait_group.start();
19411949 try self.thread_pool.spawn(workerUpdateCObject, .{
19421950 self, c_object, &c_obj_prog_node, &self.work_queue_wait_group,
......@@ -1944,9 +1952,13 @@ pub fn performAllTheWork(self: *Compilation) error{ TimerUnsupported, OutOfMemor
19441952 }
19451953 }
19461954
1947 // Iterate over all the files and look for outdated and deleted declarations.
1948 if (self.bin_file.options.module) |mod| {
1949 try mod.processOutdatedAndDeletedDecls();
1955 const use_stage1 = build_options.omit_stage2 or
1956 (build_options.is_stage1 and self.bin_file.options.use_llvm);
1957 if (!use_stage1) {
1958 // Iterate over all the files and look for outdated and deleted declarations.
1959 if (self.bin_file.options.module) |mod| {
1960 try mod.processOutdatedAndDeletedDecls();
1961 }
19501962 }
19511963
19521964 while (self.work_queue.readItem()) |work_item| switch (work_item) {
......@@ -2319,6 +2331,9 @@ fn workerAstGenFile(
23192331 break :blk mod.importFile(file, import_path) catch continue;
23202332 };
23212333 if (import_result.is_new) {
2334 log.debug("AstGen of {s} has import '{s}'; queuing AstGen of {s}", .{
2335 file.sub_file_path, import_path, import_result.file.sub_file_path,
2336 });
23222337 wg.start();
23232338 comp.thread_pool.spawn(workerAstGenFile, .{
23242339 comp, import_result.file, prog_node, wg,
......@@ -2540,13 +2555,23 @@ fn reportRetryableAstGenError(
25402555
25412556 file.status = .retryable_failure;
25422557
2543 const err_msg = try Module.ErrorMsg.create(gpa, .{
2558 const src_loc: Module.SrcLoc = .{
25442559 .file_scope = file,
25452560 .parent_decl_node = 0,
25462561 .lazy = .entire_file,
2547 }, "unable to load {s}: {s}", .{
2548 file.sub_file_path, @errorName(err),
2549 });
2562 };
2563
2564 const err_msg = if (file.pkg.root_src_directory.path) |dir_path|
2565 try Module.ErrorMsg.create(
2566 gpa,
2567 src_loc,
2568 "unable to load {s}" ++ std.fs.path.sep_str ++ "{s}: {s}",
2569 .{ dir_path, file.sub_file_path, @errorName(err) },
2570 )
2571 else
2572 try Module.ErrorMsg.create(gpa, src_loc, "unable to load {s}: {s}", .{
2573 file.sub_file_path, @errorName(err),
2574 });
25502575 errdefer err_msg.destroy(gpa);
25512576
25522577 {
......@@ -3830,9 +3855,8 @@ fn updateStage1Module(comp: *Compilation, main_progress_node: *std.Progress.Node
38303855
38313856 _ = try man.addFile(main_zig_file, null);
38323857 {
3833 var local_arena = std.heap.ArenaAllocator.init(comp.gpa);
3834 defer local_arena.deinit();
3835 try addPackageTableToCacheHash(&man.hash, &local_arena, mod.root_pkg.table, .{ .files = &man });
3858 var seen_table = std.AutoHashMap(*Package, void).init(&arena_allocator.allocator);
3859 try addPackageTableToCacheHash(&man.hash, &arena_allocator, mod.root_pkg.table, &seen_table, .{ .files = &man });
38363860 }
38373861 man.hash.add(comp.bin_file.options.valgrind);
38383862 man.hash.add(comp.bin_file.options.single_threaded);
......@@ -4103,6 +4127,12 @@ fn createStage1Pkg(
41034127 var children = std.ArrayList(*stage1.Pkg).init(arena);
41044128 var it = pkg.table.iterator();
41054129 while (it.next()) |entry| {
4130 if (mem.eql(u8, entry.key_ptr.*, "std") or
4131 mem.eql(u8, entry.key_ptr.*, "builtin") or
4132 mem.eql(u8, entry.key_ptr.*, "root"))
4133 {
4134 continue;
4135 }
41064136 try children.append(try createStage1Pkg(arena, entry.key_ptr.*, entry.value_ptr.*, child_pkg));
41074137 }
41084138 break :blk children.items;
src/Module.zig+3
......@@ -3185,6 +3185,9 @@ pub fn importFile(
31853185 if (cur_file.pkg.table.get(import_string)) |pkg| {
31863186 return mod.importPkg(pkg);
31873187 }
3188 if (!mem.endsWith(u8, import_string, ".zig")) {
3189 return error.PackageNotFound;
3190 }
31883191 const gpa = mod.gpa;
31893192
31903193 // The resolved path is used as the key in the import table, to detect if
src/codegen/x86_64.zig-1
......@@ -4,7 +4,6 @@ const mem = std.mem;
44const assert = std.debug.assert;
55const ArrayList = std.ArrayList;
66const Allocator = std.mem.Allocator;
7const Type = @import("../Type.zig");
87const DW = std.dwarf;
98
109// zig fmt: off
src/translate_c.zig+20-4
......@@ -151,6 +151,12 @@ const Scope = struct {
151151 return true;
152152 return scope.base.parent.?.contains(name);
153153 }
154
155 fn discardVariable(scope: *Block, c: *Context, name: []const u8) Error!void {
156 const name_node = try Tag.identifier.create(c.arena, name);
157 const discard = try Tag.discard.create(c.arena, name_node);
158 try scope.statements.append(discard);
159 }
154160 };
155161
156162 const Root = struct {
......@@ -625,6 +631,7 @@ fn visitFnDecl(c: *Context, fn_decl: *const clang.FunctionDecl) Error!void {
625631 const redecl_node = try Tag.arg_redecl.create(c.arena, .{ .actual = mangled_param_name, .mangled = arg_name });
626632 try block_scope.statements.append(redecl_node);
627633 }
634 try block_scope.discardVariable(c, mangled_param_name);
628635
629636 param_id += 1;
630637 }
......@@ -827,6 +834,7 @@ fn transTypeDef(c: *Context, scope: *Scope, typedef_decl: *const clang.TypedefNa
827834 try addTopLevelDecl(c, name, node);
828835 } else {
829836 try scope.appendNode(node);
837 try bs.discardVariable(c, name);
830838 }
831839}
832840
......@@ -1077,6 +1085,7 @@ fn transRecordDecl(c: *Context, scope: *Scope, record_decl: *const clang.RecordD
10771085 try c.alias_list.append(.{ .alias = bare_name, .name = name });
10781086 } else {
10791087 try scope.appendNode(Node.initPayload(&payload.base));
1088 try bs.discardVariable(c, name);
10801089 }
10811090}
10821091
......@@ -1128,8 +1137,10 @@ fn transEnumDecl(c: *Context, scope: *Scope, enum_decl: *const clang.EnumDecl) E
11281137 });
11291138 if (toplevel)
11301139 try addTopLevelDecl(c, enum_val_name, enum_const_def)
1131 else
1140 else {
11321141 try scope.appendNode(enum_const_def);
1142 try bs.discardVariable(c, enum_val_name);
1143 }
11331144 }
11341145
11351146 const int_type = enum_decl.getIntegerType();
......@@ -1168,6 +1179,7 @@ fn transEnumDecl(c: *Context, scope: *Scope, enum_decl: *const clang.EnumDecl) E
11681179 try c.alias_list.append(.{ .alias = bare_name, .name = name });
11691180 } else {
11701181 try scope.appendNode(Node.initPayload(&payload.base));
1182 try bs.discardVariable(c, name);
11711183 }
11721184}
11731185
......@@ -1352,11 +1364,10 @@ fn makeShuffleMask(c: *Context, scope: *Scope, expr: *const clang.ShuffleVectorE
13521364 init.* = converted_index;
13531365 }
13541366
1355 const mask_init = try Tag.array_init.create(c.arena, .{
1367 return Tag.array_init.create(c.arena, .{
13561368 .cond = mask_type,
13571369 .cases = init_list,
13581370 });
1359 return Tag.@"comptime".create(c.arena, mask_init);
13601371}
13611372
13621373/// @typeInfo(@TypeOf(vec_node)).Vector.<field>
......@@ -1766,6 +1777,7 @@ fn transDeclStmtOne(
17661777 node = try Tag.static_local_var.create(c.arena, .{ .name = mangled_name, .init = node });
17671778 }
17681779 try block_scope.statements.append(node);
1780 try block_scope.discardVariable(c, mangled_name);
17691781
17701782 const cleanup_attr = var_decl.getCleanupAttribute();
17711783 if (cleanup_attr) |fn_decl| {
......@@ -4903,6 +4915,10 @@ fn transMacroDefine(c: *Context, m: *MacroCtx) ParseError!void {
49034915 const scope = &c.global_scope.base;
49044916
49054917 const init_node = try parseCExpr(c, m, scope);
4918 if (init_node.castTag(.identifier)) |ident_node| {
4919 if (mem.eql(u8, "_", ident_node.data))
4920 return m.fail(c, "unable to translate C expr: illegal identifier _", .{});
4921 }
49064922 const last = m.next().?;
49074923 if (last != .Eof and last != .Nl)
49084924 return m.fail(c, "unable to translate C expr: unexpected token .{s}", .{@tagName(last)});
......@@ -4933,7 +4949,7 @@ fn transMacroFnDefine(c: *Context, m: *MacroCtx) ParseError!void {
49334949 .name = mangled_name,
49344950 .type = Tag.@"anytype".init(),
49354951 });
4936
4952 try block_scope.discardVariable(c, mangled_name);
49374953 if (m.peek().? != .Comma) break;
49384954 _ = m.next();
49394955 }
test/cli.zig+2
......@@ -116,6 +116,8 @@ fn testGodboltApi(zig_exe: []const u8, dir_path: []const u8) anyerror!void {
116116 \\}
117117 \\extern fn zig_panic() noreturn;
118118 \\pub fn panic(msg: []const u8, error_return_trace: ?*@import("std").builtin.StackTrace) noreturn {
119 \\ _ = msg;
120 \\ _ = error_return_trace;
119121 \\ zig_panic();
120122 \\}
121123 );
test/compare_output.zig+16-2
......@@ -10,6 +10,8 @@ pub fn addCases(cases: *tests.CompareOutputContext) void {
1010 \\ @cInclude("stdio.h");
1111 \\});
1212 \\pub export fn main(argc: c_int, argv: [*][*]u8) c_int {
13 \\ _ = argc;
14 \\ _ = argv;
1315 \\ _ = c.puts("Hello, world!");
1416 \\ return 0;
1517 \\}
......@@ -143,6 +145,8 @@ pub fn addCases(cases: *tests.CompareOutputContext) void {
143145 \\});
144146 \\
145147 \\pub export fn main(argc: c_int, argv: [*][*]u8) c_int {
148 \\ _ = argc;
149 \\ _ = argv;
146150 \\ if (is_windows) {
147151 \\ // we want actual \n, not \r\n
148152 \\ _ = c._setmode(1, c._O_BINARY);
......@@ -265,8 +269,10 @@ pub fn addCases(cases: *tests.CompareOutputContext) void {
265269 \\const y : u16 = 5678;
266270 \\pub fn main() void {
267271 \\ var x_local : i32 = print_ok(x);
272 \\ _ = x_local;
268273 \\}
269274 \\fn print_ok(val: @TypeOf(x)) @TypeOf(foo) {
275 \\ _ = val;
270276 \\ const stdout = io.getStdOut().writer();
271277 \\ stdout.print("OK\n", .{}) catch unreachable;
272278 \\ return 0;
......@@ -318,6 +324,8 @@ pub fn addCases(cases: *tests.CompareOutputContext) void {
318324 \\});
319325 \\
320326 \\pub export fn main(argc: c_int, argv: [*][*]u8) c_int {
327 \\ _ = argc;
328 \\ _ = argv;
321329 \\ if (is_windows) {
322330 \\ // we want actual \n, not \r\n
323331 \\ _ = c._setmode(1, c._O_BINARY);
......@@ -337,13 +345,19 @@ pub fn addCases(cases: *tests.CompareOutputContext) void {
337345 \\const Foo = struct {
338346 \\ field1: Bar,
339347 \\
340 \\ fn method(a: *const Foo) bool { return true; }
348 \\ fn method(a: *const Foo) bool {
349 \\ _ = a;
350 \\ return true;
351 \\ }
341352 \\};
342353 \\
343354 \\const Bar = struct {
344355 \\ field2: i32,
345356 \\
346 \\ fn method(b: *const Bar) bool { return true; }
357 \\ fn method(b: *const Bar) bool {
358 \\ _ = b;
359 \\ return true;
360 \\ }
347361 \\};
348362 \\
349363 \\pub fn main() void {
test/runtime_safety.zig+160-5
......@@ -4,6 +4,7 @@ pub fn addCases(cases: *tests.CompareOutputContext) void {
44 {
55 const check_panic_msg =
66 \\pub fn panic(message: []const u8, stack_trace: ?*std.builtin.StackTrace) noreturn {
7 \\ _ = stack_trace;
78 \\ if (std.mem.eql(u8, message, "reached unreachable code")) {
89 \\ std.process.exit(126); // good
910 \\ }
......@@ -45,6 +46,7 @@ pub fn addCases(cases: *tests.CompareOutputContext) void {
4546 {
4647 const check_panic_msg =
4748 \\pub fn panic(message: []const u8, stack_trace: ?*std.builtin.StackTrace) noreturn {
49 \\ _ = stack_trace;
4850 \\ if (std.mem.eql(u8, message, "invalid enum value")) {
4951 \\ std.process.exit(126); // good
5052 \\ }
......@@ -62,6 +64,7 @@ pub fn addCases(cases: *tests.CompareOutputContext) void {
6264 \\ var e: E = undefined;
6365 \\ @memset(@ptrCast([*]u8, &e), 0x55, @sizeOf(E));
6466 \\ var n = @tagName(e);
67 \\ _ = n;
6568 \\}
6669 );
6770
......@@ -76,6 +79,7 @@ pub fn addCases(cases: *tests.CompareOutputContext) void {
7679 \\ @memset(@ptrCast([*]u8, &u), 0x55, @sizeOf(U));
7780 \\ var t: @typeInfo(U).Union.tag_type.? = u;
7881 \\ var n = @tagName(t);
82 \\ _ = n;
7983 \\}
8084 );
8185 }
......@@ -83,6 +87,7 @@ pub fn addCases(cases: *tests.CompareOutputContext) void {
8387 {
8488 const check_panic_msg =
8589 \\pub fn panic(message: []const u8, stack_trace: ?*std.builtin.StackTrace) noreturn {
90 \\ _ = stack_trace;
8691 \\ if (std.mem.eql(u8, message, "index out of bounds")) {
8792 \\ std.process.exit(126); // good
8893 \\ }
......@@ -96,6 +101,7 @@ pub fn addCases(cases: *tests.CompareOutputContext) void {
96101 \\pub fn main() void {
97102 \\ var buf = [4]u8{'a','b','c',0};
98103 \\ const slice = buf[0..4 :0];
104 \\ _ = slice;
99105 \\}
100106 );
101107 cases.addRuntimeSafety("slicing operator with sentinel",
......@@ -104,6 +110,7 @@ pub fn addCases(cases: *tests.CompareOutputContext) void {
104110 \\pub fn main() void {
105111 \\ var buf = [4]u8{'a','b','c',0};
106112 \\ const slice = buf[0..:0];
113 \\ _ = slice;
107114 \\}
108115 );
109116 cases.addRuntimeSafety("slicing operator with sentinel",
......@@ -112,6 +119,7 @@ pub fn addCases(cases: *tests.CompareOutputContext) void {
112119 \\pub fn main() void {
113120 \\ var buf_zero = [0]u8{};
114121 \\ const slice = buf_zero[0..0 :0];
122 \\ _ = slice;
115123 \\}
116124 );
117125 cases.addRuntimeSafety("slicing operator with sentinel",
......@@ -120,6 +128,7 @@ pub fn addCases(cases: *tests.CompareOutputContext) void {
120128 \\pub fn main() void {
121129 \\ var buf_zero = [0]u8{};
122130 \\ const slice = buf_zero[0..:0];
131 \\ _ = slice;
123132 \\}
124133 );
125134 cases.addRuntimeSafety("slicing operator with sentinel",
......@@ -129,6 +138,7 @@ pub fn addCases(cases: *tests.CompareOutputContext) void {
129138 \\ var buf_sentinel = [2:0]u8{'a','b'};
130139 \\ @ptrCast(*[3]u8, &buf_sentinel)[2] = 0;
131140 \\ const slice = buf_sentinel[0..3 :0];
141 \\ _ = slice;
132142 \\}
133143 );
134144 cases.addRuntimeSafety("slicing operator with sentinel",
......@@ -137,6 +147,7 @@ pub fn addCases(cases: *tests.CompareOutputContext) void {
137147 \\pub fn main() void {
138148 \\ var buf_slice: []const u8 = &[3]u8{ 'a', 'b', 0 };
139149 \\ const slice = buf_slice[0..3 :0];
150 \\ _ = slice;
140151 \\}
141152 );
142153 cases.addRuntimeSafety("slicing operator with sentinel",
......@@ -145,6 +156,7 @@ pub fn addCases(cases: *tests.CompareOutputContext) void {
145156 \\pub fn main() void {
146157 \\ var buf_slice: []const u8 = &[3]u8{ 'a', 'b', 0 };
147158 \\ const slice = buf_slice[0.. :0];
159 \\ _ = slice;
148160 \\}
149161 );
150162 }
......@@ -153,6 +165,7 @@ pub fn addCases(cases: *tests.CompareOutputContext) void {
153165 \\const std = @import("std");
154166 \\const V = @import("std").meta.Vector;
155167 \\pub fn panic(message: []const u8, stack_trace: ?*std.builtin.StackTrace) noreturn {
168 \\ _ = stack_trace;
156169 \\ if (std.mem.eql(u8, message, "integer cast truncated bits")) {
157170 \\ std.process.exit(126); // good
158171 \\ }
......@@ -161,6 +174,7 @@ pub fn addCases(cases: *tests.CompareOutputContext) void {
161174 \\pub fn main() void {
162175 \\ var x = @splat(4, @as(u32, 0xdeadbeef));
163176 \\ var y = @intCast(V(4, u16), x);
177 \\ _ = y;
164178 \\}
165179 );
166180
......@@ -168,6 +182,7 @@ pub fn addCases(cases: *tests.CompareOutputContext) void {
168182 \\const std = @import("std");
169183 \\const V = @import("std").meta.Vector;
170184 \\pub fn panic(message: []const u8, stack_trace: ?*std.builtin.StackTrace) noreturn {
185 \\ _ = stack_trace;
171186 \\ if (std.mem.eql(u8, message, "integer cast truncated bits")) {
172187 \\ std.process.exit(126); // good
173188 \\ }
......@@ -176,6 +191,7 @@ pub fn addCases(cases: *tests.CompareOutputContext) void {
176191 \\pub fn main() void {
177192 \\ var x = @splat(4, @as(u32, 0x80000000));
178193 \\ var y = @intCast(V(4, i32), x);
194 \\ _ = y;
179195 \\}
180196 );
181197
......@@ -183,6 +199,7 @@ pub fn addCases(cases: *tests.CompareOutputContext) void {
183199 \\const std = @import("std");
184200 \\const V = @import("std").meta.Vector;
185201 \\pub fn panic(message: []const u8, stack_trace: ?*std.builtin.StackTrace) noreturn {
202 \\ _ = stack_trace;
186203 \\ if (std.mem.eql(u8, message, "attempt to cast negative value to unsigned integer")) {
187204 \\ std.process.exit(126); // good
188205 \\ }
......@@ -191,12 +208,14 @@ pub fn addCases(cases: *tests.CompareOutputContext) void {
191208 \\pub fn main() void {
192209 \\ var x = @splat(4, @as(i32, -2147483647));
193210 \\ var y = @intCast(V(4, u32), x);
211 \\ _ = y;
194212 \\}
195213 );
196214
197215 cases.addRuntimeSafety("shift left by huge amount",
198216 \\const std = @import("std");
199217 \\pub fn panic(message: []const u8, stack_trace: ?*std.builtin.StackTrace) noreturn {
218 \\ _ = stack_trace;
200219 \\ if (std.mem.eql(u8, message, "shift amount is greater than the type size")) {
201220 \\ std.process.exit(126); // good
202221 \\ }
......@@ -206,12 +225,14 @@ pub fn addCases(cases: *tests.CompareOutputContext) void {
206225 \\ var x: u24 = 42;
207226 \\ var y: u5 = 24;
208227 \\ var z = x >> y;
228 \\ _ = z;
209229 \\}
210230 );
211231
212232 cases.addRuntimeSafety("shift right by huge amount",
213233 \\const std = @import("std");
214234 \\pub fn panic(message: []const u8, stack_trace: ?*std.builtin.StackTrace) noreturn {
235 \\ _ = stack_trace;
215236 \\ if (std.mem.eql(u8, message, "shift amount is greater than the type size")) {
216237 \\ std.process.exit(126); // good
217238 \\ }
......@@ -221,12 +242,14 @@ pub fn addCases(cases: *tests.CompareOutputContext) void {
221242 \\ var x: u24 = 42;
222243 \\ var y: u5 = 24;
223244 \\ var z = x << y;
245 \\ _ = z;
224246 \\}
225247 );
226248
227249 cases.addRuntimeSafety("slice sentinel mismatch - optional pointers",
228250 \\const std = @import("std");
229251 \\pub fn panic(message: []const u8, stack_trace: ?*std.builtin.StackTrace) noreturn {
252 \\ _ = stack_trace;
230253 \\ if (std.mem.eql(u8, message, "sentinel mismatch")) {
231254 \\ std.process.exit(126); // good
232255 \\ }
......@@ -235,12 +258,14 @@ pub fn addCases(cases: *tests.CompareOutputContext) void {
235258 \\pub fn main() void {
236259 \\ var buf: [4]?*i32 = undefined;
237260 \\ const slice = buf[0..3 :null];
261 \\ _ = slice;
238262 \\}
239263 );
240264
241265 cases.addRuntimeSafety("slice sentinel mismatch - floats",
242266 \\const std = @import("std");
243267 \\pub fn panic(message: []const u8, stack_trace: ?*std.builtin.StackTrace) noreturn {
268 \\ _ = stack_trace;
244269 \\ if (std.mem.eql(u8, message, "sentinel mismatch")) {
245270 \\ std.process.exit(126); // good
246271 \\ }
......@@ -249,12 +274,14 @@ pub fn addCases(cases: *tests.CompareOutputContext) void {
249274 \\pub fn main() void {
250275 \\ var buf: [4]f32 = undefined;
251276 \\ const slice = buf[0..3 :1.2];
277 \\ _ = slice;
252278 \\}
253279 );
254280
255281 cases.addRuntimeSafety("pointer slice sentinel mismatch",
256282 \\const std = @import("std");
257283 \\pub fn panic(message: []const u8, stack_trace: ?*std.builtin.StackTrace) noreturn {
284 \\ _ = stack_trace;
258285 \\ if (std.mem.eql(u8, message, "sentinel mismatch")) {
259286 \\ std.process.exit(126); // good
260287 \\ }
......@@ -264,12 +291,14 @@ pub fn addCases(cases: *tests.CompareOutputContext) void {
264291 \\ var buf: [4]u8 = undefined;
265292 \\ const ptr: [*]u8 = &buf;
266293 \\ const slice = ptr[0..3 :0];
294 \\ _ = slice;
267295 \\}
268296 );
269297
270298 cases.addRuntimeSafety("slice slice sentinel mismatch",
271299 \\const std = @import("std");
272300 \\pub fn panic(message: []const u8, stack_trace: ?*std.builtin.StackTrace) noreturn {
301 \\ _ = stack_trace;
273302 \\ if (std.mem.eql(u8, message, "sentinel mismatch")) {
274303 \\ std.process.exit(126); // good
275304 \\ }
......@@ -279,12 +308,14 @@ pub fn addCases(cases: *tests.CompareOutputContext) void {
279308 \\ var buf: [4]u8 = undefined;
280309 \\ const slice = buf[0..];
281310 \\ const slice2 = slice[0..3 :0];
311 \\ _ = slice2;
282312 \\}
283313 );
284314
285315 cases.addRuntimeSafety("array slice sentinel mismatch",
286316 \\const std = @import("std");
287317 \\pub fn panic(message: []const u8, stack_trace: ?*std.builtin.StackTrace) noreturn {
318 \\ _ = stack_trace;
288319 \\ if (std.mem.eql(u8, message, "sentinel mismatch")) {
289320 \\ std.process.exit(126); // good
290321 \\ }
......@@ -293,12 +324,14 @@ pub fn addCases(cases: *tests.CompareOutputContext) void {
293324 \\pub fn main() void {
294325 \\ var buf: [4]u8 = undefined;
295326 \\ const slice = buf[0..3 :0];
327 \\ _ = slice;
296328 \\}
297329 );
298330
299331 cases.addRuntimeSafety("intToPtr with misaligned address",
300332 \\const std = @import("std");
301333 \\pub fn panic(message: []const u8, stack_trace: ?*std.builtin.StackTrace) noreturn {
334 \\ _ = stack_trace;
302335 \\ if (std.mem.eql(u8, message, "incorrect alignment")) {
303336 \\ std.os.exit(126); // good
304337 \\ }
......@@ -307,16 +340,20 @@ pub fn addCases(cases: *tests.CompareOutputContext) void {
307340 \\pub fn main() void {
308341 \\ var x: usize = 5;
309342 \\ var y = @intToPtr([*]align(4) u8, x);
343 \\ _ = y;
310344 \\}
311345 );
312346
313347 cases.addRuntimeSafety("resuming a non-suspended function which never been suspended",
314348 \\const std = @import("std");
315349 \\pub fn panic(message: []const u8, stack_trace: ?*std.builtin.StackTrace) noreturn {
350 \\ _ = message;
351 \\ _ = stack_trace;
316352 \\ std.os.exit(126);
317353 \\}
318354 \\fn foo() void {
319355 \\ var f = async bar(@frame());
356 \\ _ = f;
320357 \\ std.os.exit(0);
321358 \\}
322359 \\
......@@ -335,6 +372,8 @@ pub fn addCases(cases: *tests.CompareOutputContext) void {
335372 cases.addRuntimeSafety("resuming a non-suspended function which has been suspended and resumed",
336373 \\const std = @import("std");
337374 \\pub fn panic(message: []const u8, stack_trace: ?*std.builtin.StackTrace) noreturn {
375 \\ _ = message;
376 \\ _ = stack_trace;
338377 \\ std.os.exit(126);
339378 \\}
340379 \\fn foo() void {
......@@ -342,6 +381,7 @@ pub fn addCases(cases: *tests.CompareOutputContext) void {
342381 \\ global_frame = @frame();
343382 \\ }
344383 \\ var f = async bar(@frame());
384 \\ _ = f;
345385 \\ std.os.exit(0);
346386 \\}
347387 \\
......@@ -363,6 +403,8 @@ pub fn addCases(cases: *tests.CompareOutputContext) void {
363403 cases.addRuntimeSafety("nosuspend function call, callee suspends",
364404 \\const std = @import("std");
365405 \\pub fn panic(message: []const u8, stack_trace: ?*std.builtin.StackTrace) noreturn {
406 \\ _ = message;
407 \\ _ = stack_trace;
366408 \\ std.os.exit(126);
367409 \\}
368410 \\pub fn main() void {
......@@ -379,6 +421,8 @@ pub fn addCases(cases: *tests.CompareOutputContext) void {
379421 cases.addRuntimeSafety("awaiting twice",
380422 \\const std = @import("std");
381423 \\pub fn panic(message: []const u8, stack_trace: ?*std.builtin.StackTrace) noreturn {
424 \\ _ = message;
425 \\ _ = stack_trace;
382426 \\ std.os.exit(126);
383427 \\}
384428 \\var frame: anyframe = undefined;
......@@ -404,12 +448,15 @@ pub fn addCases(cases: *tests.CompareOutputContext) void {
404448 cases.addRuntimeSafety("@asyncCall with too small a frame",
405449 \\const std = @import("std");
406450 \\pub fn panic(message: []const u8, stack_trace: ?*std.builtin.StackTrace) noreturn {
451 \\ _ = message;
452 \\ _ = stack_trace;
407453 \\ std.os.exit(126);
408454 \\}
409455 \\pub fn main() void {
410456 \\ var bytes: [1]u8 align(16) = undefined;
411457 \\ var ptr = other;
412458 \\ var frame = @asyncCall(&bytes, {}, ptr, .{});
459 \\ _ = frame;
413460 \\}
414461 \\fn other() callconv(.Async) void {
415462 \\ suspend {}
......@@ -419,6 +466,8 @@ pub fn addCases(cases: *tests.CompareOutputContext) void {
419466 cases.addRuntimeSafety("resuming a function which is awaiting a frame",
420467 \\const std = @import("std");
421468 \\pub fn panic(message: []const u8, stack_trace: ?*std.builtin.StackTrace) noreturn {
469 \\ _ = message;
470 \\ _ = stack_trace;
422471 \\ std.os.exit(126);
423472 \\}
424473 \\pub fn main() void {
......@@ -437,6 +486,8 @@ pub fn addCases(cases: *tests.CompareOutputContext) void {
437486 cases.addRuntimeSafety("resuming a function which is awaiting a call",
438487 \\const std = @import("std");
439488 \\pub fn panic(message: []const u8, stack_trace: ?*std.builtin.StackTrace) noreturn {
489 \\ _ = message;
490 \\ _ = stack_trace;
440491 \\ std.os.exit(126);
441492 \\}
442493 \\pub fn main() void {
......@@ -454,6 +505,8 @@ pub fn addCases(cases: *tests.CompareOutputContext) void {
454505 cases.addRuntimeSafety("invalid resume of async function",
455506 \\const std = @import("std");
456507 \\pub fn panic(message: []const u8, stack_trace: ?*std.builtin.StackTrace) noreturn {
508 \\ _ = message;
509 \\ _ = stack_trace;
457510 \\ std.os.exit(126);
458511 \\}
459512 \\pub fn main() void {
......@@ -469,61 +522,78 @@ pub fn addCases(cases: *tests.CompareOutputContext) void {
469522 cases.addRuntimeSafety(".? operator on null pointer",
470523 \\const std = @import("std");
471524 \\pub fn panic(message: []const u8, stack_trace: ?*std.builtin.StackTrace) noreturn {
525 \\ _ = message;
526 \\ _ = stack_trace;
472527 \\ std.os.exit(126);
473528 \\}
474529 \\pub fn main() void {
475530 \\ var ptr: ?*i32 = null;
476531 \\ var b = ptr.?;
532 \\ _ = b;
477533 \\}
478534 );
479535
480536 cases.addRuntimeSafety(".? operator on C pointer",
481537 \\const std = @import("std");
482538 \\pub fn panic(message: []const u8, stack_trace: ?*std.builtin.StackTrace) noreturn {
539 \\ _ = message;
540 \\ _ = stack_trace;
483541 \\ std.os.exit(126);
484542 \\}
485543 \\pub fn main() void {
486544 \\ var ptr: [*c]i32 = null;
487545 \\ var b = ptr.?;
546 \\ _ = b;
488547 \\}
489548 );
490549
491550 cases.addRuntimeSafety("@intToPtr address zero to non-optional pointer",
492551 \\const std = @import("std");
493552 \\pub fn panic(message: []const u8, stack_trace: ?*std.builtin.StackTrace) noreturn {
553 \\ _ = message;
554 \\ _ = stack_trace;
494555 \\ std.os.exit(126);
495556 \\}
496557 \\pub fn main() void {
497558 \\ var zero: usize = 0;
498559 \\ var b = @intToPtr(*i32, zero);
560 \\ _ = b;
499561 \\}
500562 );
501563
502564 cases.addRuntimeSafety("@intToPtr address zero to non-optional byte-aligned pointer",
503565 \\const std = @import("std");
504566 \\pub fn panic(message: []const u8, stack_trace: ?*std.builtin.StackTrace) noreturn {
567 \\ _ = message;
568 \\ _ = stack_trace;
505569 \\ std.os.exit(126);
506570 \\}
507571 \\pub fn main() void {
508572 \\ var zero: usize = 0;
509573 \\ var b = @intToPtr(*u8, zero);
574 \\ _ = b;
510575 \\}
511576 );
512577
513578 cases.addRuntimeSafety("pointer casting null to non-optional pointer",
514579 \\const std = @import("std");
515580 \\pub fn panic(message: []const u8, stack_trace: ?*std.builtin.StackTrace) noreturn {
581 \\ _ = message;
582 \\ _ = stack_trace;
516583 \\ std.os.exit(126);
517584 \\}
518585 \\pub fn main() void {
519586 \\ var c_ptr: [*c]u8 = 0;
520587 \\ var zig_ptr: *u8 = c_ptr;
588 \\ _ = zig_ptr;
521589 \\}
522590 );
523591
524592 cases.addRuntimeSafety("@intToEnum - no matching tag value",
525593 \\const std = @import("std");
526594 \\pub fn panic(message: []const u8, stack_trace: ?*std.builtin.StackTrace) noreturn {
595 \\ _ = message;
596 \\ _ = stack_trace;
527597 \\ std.os.exit(126);
528598 \\}
529599 \\const Foo = enum {
......@@ -537,12 +607,14 @@ pub fn addCases(cases: *tests.CompareOutputContext) void {
537607 \\fn bar(a: u2) Foo {
538608 \\ return @intToEnum(Foo, a);
539609 \\}
540 \\fn baz(a: Foo) void {}
610 \\fn baz(_: Foo) void {}
541611 );
542612
543613 cases.addRuntimeSafety("@floatToInt cannot fit - negative to unsigned",
544614 \\const std = @import("std");
545615 \\pub fn panic(message: []const u8, stack_trace: ?*std.builtin.StackTrace) noreturn {
616 \\ _ = message;
617 \\ _ = stack_trace;
546618 \\ std.os.exit(126);
547619 \\}
548620 \\pub fn main() void {
......@@ -551,12 +623,14 @@ pub fn addCases(cases: *tests.CompareOutputContext) void {
551623 \\fn bar(a: f32) u8 {
552624 \\ return @floatToInt(u8, a);
553625 \\}
554 \\fn baz(a: u8) void { }
626 \\fn baz(_: u8) void { }
555627 );
556628
557629 cases.addRuntimeSafety("@floatToInt cannot fit - negative out of range",
558630 \\const std = @import("std");
559631 \\pub fn panic(message: []const u8, stack_trace: ?*std.builtin.StackTrace) noreturn {
632 \\ _ = message;
633 \\ _ = stack_trace;
560634 \\ std.os.exit(126);
561635 \\}
562636 \\pub fn main() void {
......@@ -565,12 +639,14 @@ pub fn addCases(cases: *tests.CompareOutputContext) void {
565639 \\fn bar(a: f32) i8 {
566640 \\ return @floatToInt(i8, a);
567641 \\}
568 \\fn baz(a: i8) void { }
642 \\fn baz(_: i8) void { }
569643 );
570644
571645 cases.addRuntimeSafety("@floatToInt cannot fit - positive out of range",
572646 \\const std = @import("std");
573647 \\pub fn panic(message: []const u8, stack_trace: ?*std.builtin.StackTrace) noreturn {
648 \\ _ = message;
649 \\ _ = stack_trace;
574650 \\ std.os.exit(126);
575651 \\}
576652 \\pub fn main() void {
......@@ -579,12 +655,14 @@ pub fn addCases(cases: *tests.CompareOutputContext) void {
579655 \\fn bar(a: f32) u8 {
580656 \\ return @floatToInt(u8, a);
581657 \\}
582 \\fn baz(a: u8) void { }
658 \\fn baz(_: u8) void { }
583659 );
584660
585661 cases.addRuntimeSafety("calling panic",
586662 \\const std = @import("std");
587663 \\pub fn panic(message: []const u8, stack_trace: ?*std.builtin.StackTrace) noreturn {
664 \\ _ = message;
665 \\ _ = stack_trace;
588666 \\ std.os.exit(126);
589667 \\}
590668 \\pub fn main() void {
......@@ -595,6 +673,8 @@ pub fn addCases(cases: *tests.CompareOutputContext) void {
595673 cases.addRuntimeSafety("out of bounds slice access",
596674 \\const std = @import("std");
597675 \\pub fn panic(message: []const u8, stack_trace: ?*std.builtin.StackTrace) noreturn {
676 \\ _ = message;
677 \\ _ = stack_trace;
598678 \\ std.os.exit(126);
599679 \\}
600680 \\pub fn main() void {
......@@ -604,12 +684,14 @@ pub fn addCases(cases: *tests.CompareOutputContext) void {
604684 \\fn bar(a: []const i32) i32 {
605685 \\ return a[4];
606686 \\}
607 \\fn baz(a: i32) void { }
687 \\fn baz(_: i32) void { }
608688 );
609689
610690 cases.addRuntimeSafety("integer addition overflow",
611691 \\const std = @import("std");
612692 \\pub fn panic(message: []const u8, stack_trace: ?*std.builtin.StackTrace) noreturn {
693 \\ _ = message;
694 \\ _ = stack_trace;
613695 \\ std.os.exit(126);
614696 \\}
615697 \\pub fn main() !void {
......@@ -624,12 +706,15 @@ pub fn addCases(cases: *tests.CompareOutputContext) void {
624706 cases.addRuntimeSafety("vector integer addition overflow",
625707 \\const std = @import("std");
626708 \\pub fn panic(message: []const u8, stack_trace: ?*std.builtin.StackTrace) noreturn {
709 \\ _ = message;
710 \\ _ = stack_trace;
627711 \\ std.os.exit(126);
628712 \\}
629713 \\pub fn main() void {
630714 \\ var a: std.meta.Vector(4, i32) = [_]i32{ 1, 2, 2147483643, 4 };
631715 \\ var b: std.meta.Vector(4, i32) = [_]i32{ 5, 6, 7, 8 };
632716 \\ const x = add(a, b);
717 \\ _ = x;
633718 \\}
634719 \\fn add(a: std.meta.Vector(4, i32), b: std.meta.Vector(4, i32)) std.meta.Vector(4, i32) {
635720 \\ return a + b;
......@@ -639,12 +724,15 @@ pub fn addCases(cases: *tests.CompareOutputContext) void {
639724 cases.addRuntimeSafety("vector integer subtraction overflow",
640725 \\const std = @import("std");
641726 \\pub fn panic(message: []const u8, stack_trace: ?*std.builtin.StackTrace) noreturn {
727 \\ _ = message;
728 \\ _ = stack_trace;
642729 \\ std.os.exit(126);
643730 \\}
644731 \\pub fn main() void {
645732 \\ var a: std.meta.Vector(4, u32) = [_]u32{ 1, 2, 8, 4 };
646733 \\ var b: std.meta.Vector(4, u32) = [_]u32{ 5, 6, 7, 8 };
647734 \\ const x = sub(b, a);
735 \\ _ = x;
648736 \\}
649737 \\fn sub(a: std.meta.Vector(4, u32), b: std.meta.Vector(4, u32)) std.meta.Vector(4, u32) {
650738 \\ return a - b;
......@@ -654,12 +742,15 @@ pub fn addCases(cases: *tests.CompareOutputContext) void {
654742 cases.addRuntimeSafety("vector integer multiplication overflow",
655743 \\const std = @import("std");
656744 \\pub fn panic(message: []const u8, stack_trace: ?*std.builtin.StackTrace) noreturn {
745 \\ _ = message;
746 \\ _ = stack_trace;
657747 \\ std.os.exit(126);
658748 \\}
659749 \\pub fn main() void {
660750 \\ var a: std.meta.Vector(4, u8) = [_]u8{ 1, 2, 200, 4 };
661751 \\ var b: std.meta.Vector(4, u8) = [_]u8{ 5, 6, 2, 8 };
662752 \\ const x = mul(b, a);
753 \\ _ = x;
663754 \\}
664755 \\fn mul(a: std.meta.Vector(4, u8), b: std.meta.Vector(4, u8)) std.meta.Vector(4, u8) {
665756 \\ return a * b;
......@@ -669,11 +760,14 @@ pub fn addCases(cases: *tests.CompareOutputContext) void {
669760 cases.addRuntimeSafety("vector integer negation overflow",
670761 \\const std = @import("std");
671762 \\pub fn panic(message: []const u8, stack_trace: ?*std.builtin.StackTrace) noreturn {
763 \\ _ = message;
764 \\ _ = stack_trace;
672765 \\ std.os.exit(126);
673766 \\}
674767 \\pub fn main() void {
675768 \\ var a: std.meta.Vector(4, i16) = [_]i16{ 1, -32768, 200, 4 };
676769 \\ const x = neg(a);
770 \\ _ = x;
677771 \\}
678772 \\fn neg(a: std.meta.Vector(4, i16)) std.meta.Vector(4, i16) {
679773 \\ return -a;
......@@ -683,6 +777,8 @@ pub fn addCases(cases: *tests.CompareOutputContext) void {
683777 cases.addRuntimeSafety("integer subtraction overflow",
684778 \\const std = @import("std");
685779 \\pub fn panic(message: []const u8, stack_trace: ?*std.builtin.StackTrace) noreturn {
780 \\ _ = message;
781 \\ _ = stack_trace;
686782 \\ std.os.exit(126);
687783 \\}
688784 \\pub fn main() !void {
......@@ -697,6 +793,8 @@ pub fn addCases(cases: *tests.CompareOutputContext) void {
697793 cases.addRuntimeSafety("integer multiplication overflow",
698794 \\const std = @import("std");
699795 \\pub fn panic(message: []const u8, stack_trace: ?*std.builtin.StackTrace) noreturn {
796 \\ _ = message;
797 \\ _ = stack_trace;
700798 \\ std.os.exit(126);
701799 \\}
702800 \\pub fn main() !void {
......@@ -711,6 +809,8 @@ pub fn addCases(cases: *tests.CompareOutputContext) void {
711809 cases.addRuntimeSafety("integer negation overflow",
712810 \\const std = @import("std");
713811 \\pub fn panic(message: []const u8, stack_trace: ?*std.builtin.StackTrace) noreturn {
812 \\ _ = message;
813 \\ _ = stack_trace;
714814 \\ std.os.exit(126);
715815 \\}
716816 \\pub fn main() !void {
......@@ -725,6 +825,8 @@ pub fn addCases(cases: *tests.CompareOutputContext) void {
725825 cases.addRuntimeSafety("signed integer division overflow",
726826 \\const std = @import("std");
727827 \\pub fn panic(message: []const u8, stack_trace: ?*std.builtin.StackTrace) noreturn {
828 \\ _ = message;
829 \\ _ = stack_trace;
728830 \\ std.os.exit(126);
729831 \\}
730832 \\pub fn main() !void {
......@@ -739,6 +841,8 @@ pub fn addCases(cases: *tests.CompareOutputContext) void {
739841 cases.addRuntimeSafety("signed integer division overflow - vectors",
740842 \\const std = @import("std");
741843 \\pub fn panic(message: []const u8, stack_trace: ?*std.builtin.StackTrace) noreturn {
844 \\ _ = message;
845 \\ _ = stack_trace;
742846 \\ std.os.exit(126);
743847 \\}
744848 \\pub fn main() !void {
......@@ -755,6 +859,8 @@ pub fn addCases(cases: *tests.CompareOutputContext) void {
755859 cases.addRuntimeSafety("signed shift left overflow",
756860 \\const std = @import("std");
757861 \\pub fn panic(message: []const u8, stack_trace: ?*std.builtin.StackTrace) noreturn {
862 \\ _ = message;
863 \\ _ = stack_trace;
758864 \\ std.os.exit(126);
759865 \\}
760866 \\pub fn main() !void {
......@@ -769,6 +875,8 @@ pub fn addCases(cases: *tests.CompareOutputContext) void {
769875 cases.addRuntimeSafety("unsigned shift left overflow",
770876 \\const std = @import("std");
771877 \\pub fn panic(message: []const u8, stack_trace: ?*std.builtin.StackTrace) noreturn {
878 \\ _ = message;
879 \\ _ = stack_trace;
772880 \\ std.os.exit(126);
773881 \\}
774882 \\pub fn main() !void {
......@@ -783,6 +891,8 @@ pub fn addCases(cases: *tests.CompareOutputContext) void {
783891 cases.addRuntimeSafety("signed shift right overflow",
784892 \\const std = @import("std");
785893 \\pub fn panic(message: []const u8, stack_trace: ?*std.builtin.StackTrace) noreturn {
894 \\ _ = message;
895 \\ _ = stack_trace;
786896 \\ std.os.exit(126);
787897 \\}
788898 \\pub fn main() !void {
......@@ -797,6 +907,8 @@ pub fn addCases(cases: *tests.CompareOutputContext) void {
797907 cases.addRuntimeSafety("unsigned shift right overflow",
798908 \\const std = @import("std");
799909 \\pub fn panic(message: []const u8, stack_trace: ?*std.builtin.StackTrace) noreturn {
910 \\ _ = message;
911 \\ _ = stack_trace;
800912 \\ std.os.exit(126);
801913 \\}
802914 \\pub fn main() !void {
......@@ -811,10 +923,13 @@ pub fn addCases(cases: *tests.CompareOutputContext) void {
811923 cases.addRuntimeSafety("integer division by zero",
812924 \\const std = @import("std");
813925 \\pub fn panic(message: []const u8, stack_trace: ?*std.builtin.StackTrace) noreturn {
926 \\ _ = message;
927 \\ _ = stack_trace;
814928 \\ std.os.exit(126);
815929 \\}
816930 \\pub fn main() void {
817931 \\ const x = div0(999, 0);
932 \\ _ = x;
818933 \\}
819934 \\fn div0(a: i32, b: i32) i32 {
820935 \\ return @divTrunc(a, b);
......@@ -824,12 +939,15 @@ pub fn addCases(cases: *tests.CompareOutputContext) void {
824939 cases.addRuntimeSafety("integer division by zero - vectors",
825940 \\const std = @import("std");
826941 \\pub fn panic(message: []const u8, stack_trace: ?*std.builtin.StackTrace) noreturn {
942 \\ _ = message;
943 \\ _ = stack_trace;
827944 \\ std.os.exit(126);
828945 \\}
829946 \\pub fn main() void {
830947 \\ var a: std.meta.Vector(4, i32) = [4]i32{111, 222, 333, 444};
831948 \\ var b: std.meta.Vector(4, i32) = [4]i32{111, 0, 333, 444};
832949 \\ const x = div0(a, b);
950 \\ _ = x;
833951 \\}
834952 \\fn div0(a: std.meta.Vector(4, i32), b: std.meta.Vector(4, i32)) std.meta.Vector(4, i32) {
835953 \\ return @divTrunc(a, b);
......@@ -839,6 +957,8 @@ pub fn addCases(cases: *tests.CompareOutputContext) void {
839957 cases.addRuntimeSafety("exact division failure",
840958 \\const std = @import("std");
841959 \\pub fn panic(message: []const u8, stack_trace: ?*std.builtin.StackTrace) noreturn {
960 \\ _ = message;
961 \\ _ = stack_trace;
842962 \\ std.os.exit(126);
843963 \\}
844964 \\pub fn main() !void {
......@@ -853,12 +973,15 @@ pub fn addCases(cases: *tests.CompareOutputContext) void {
853973 cases.addRuntimeSafety("exact division failure - vectors",
854974 \\const std = @import("std");
855975 \\pub fn panic(message: []const u8, stack_trace: ?*std.builtin.StackTrace) noreturn {
976 \\ _ = message;
977 \\ _ = stack_trace;
856978 \\ std.os.exit(126);
857979 \\}
858980 \\pub fn main() !void {
859981 \\ var a: std.meta.Vector(4, i32) = [4]i32{111, 222, 333, 444};
860982 \\ var b: std.meta.Vector(4, i32) = [4]i32{111, 222, 333, 441};
861983 \\ const x = divExact(a, b);
984 \\ _ = x;
862985 \\}
863986 \\fn divExact(a: std.meta.Vector(4, i32), b: std.meta.Vector(4, i32)) std.meta.Vector(4, i32) {
864987 \\ return @divExact(a, b);
......@@ -868,6 +991,8 @@ pub fn addCases(cases: *tests.CompareOutputContext) void {
868991 cases.addRuntimeSafety("cast []u8 to bigger slice of wrong size",
869992 \\const std = @import("std");
870993 \\pub fn panic(message: []const u8, stack_trace: ?*std.builtin.StackTrace) noreturn {
994 \\ _ = message;
995 \\ _ = stack_trace;
871996 \\ std.os.exit(126);
872997 \\}
873998 \\pub fn main() !void {
......@@ -882,6 +1007,8 @@ pub fn addCases(cases: *tests.CompareOutputContext) void {
8821007 cases.addRuntimeSafety("value does not fit in shortening cast",
8831008 \\const std = @import("std");
8841009 \\pub fn panic(message: []const u8, stack_trace: ?*std.builtin.StackTrace) noreturn {
1010 \\ _ = message;
1011 \\ _ = stack_trace;
8851012 \\ std.os.exit(126);
8861013 \\}
8871014 \\pub fn main() !void {
......@@ -896,6 +1023,8 @@ pub fn addCases(cases: *tests.CompareOutputContext) void {
8961023 cases.addRuntimeSafety("value does not fit in shortening cast - u0",
8971024 \\const std = @import("std");
8981025 \\pub fn panic(message: []const u8, stack_trace: ?*std.builtin.StackTrace) noreturn {
1026 \\ _ = message;
1027 \\ _ = stack_trace;
8991028 \\ std.os.exit(126);
9001029 \\}
9011030 \\pub fn main() !void {
......@@ -910,6 +1039,8 @@ pub fn addCases(cases: *tests.CompareOutputContext) void {
9101039 cases.addRuntimeSafety("signed integer not fitting in cast to unsigned integer",
9111040 \\const std = @import("std");
9121041 \\pub fn panic(message: []const u8, stack_trace: ?*std.builtin.StackTrace) noreturn {
1042 \\ _ = message;
1043 \\ _ = stack_trace;
9131044 \\ std.os.exit(126);
9141045 \\}
9151046 \\pub fn main() !void {
......@@ -924,28 +1055,35 @@ pub fn addCases(cases: *tests.CompareOutputContext) void {
9241055 cases.addRuntimeSafety("signed integer not fitting in cast to unsigned integer - widening",
9251056 \\const std = @import("std");
9261057 \\pub fn panic(message: []const u8, stack_trace: ?*std.builtin.StackTrace) noreturn {
1058 \\ _ = message;
1059 \\ _ = stack_trace;
9271060 \\ std.os.exit(126);
9281061 \\}
9291062 \\pub fn main() void {
9301063 \\ var value: c_short = -1;
9311064 \\ var casted = @intCast(u32, value);
1065 \\ _ = casted;
9321066 \\}
9331067 );
9341068
9351069 cases.addRuntimeSafety("unsigned integer not fitting in cast to signed integer - same bit count",
9361070 \\const std = @import("std");
9371071 \\pub fn panic(message: []const u8, stack_trace: ?*std.builtin.StackTrace) noreturn {
1072 \\ _ = message;
1073 \\ _ = stack_trace;
9381074 \\ std.os.exit(126);
9391075 \\}
9401076 \\pub fn main() void {
9411077 \\ var value: u8 = 245;
9421078 \\ var casted = @intCast(i8, value);
1079 \\ _ = casted;
9431080 \\}
9441081 );
9451082
9461083 cases.addRuntimeSafety("unwrap error",
9471084 \\const std = @import("std");
9481085 \\pub fn panic(message: []const u8, stack_trace: ?*std.builtin.StackTrace) noreturn {
1086 \\ _ = stack_trace;
9491087 \\ if (std.mem.eql(u8, message, "attempt to unwrap error: Whatever")) {
9501088 \\ std.os.exit(126); // good
9511089 \\ }
......@@ -962,6 +1100,8 @@ pub fn addCases(cases: *tests.CompareOutputContext) void {
9621100 cases.addRuntimeSafety("cast integer to global error and no code matches",
9631101 \\const std = @import("std");
9641102 \\pub fn panic(message: []const u8, stack_trace: ?*std.builtin.StackTrace) noreturn {
1103 \\ _ = message;
1104 \\ _ = stack_trace;
9651105 \\ std.os.exit(126);
9661106 \\}
9671107 \\pub fn main() void {
......@@ -975,6 +1115,8 @@ pub fn addCases(cases: *tests.CompareOutputContext) void {
9751115 cases.addRuntimeSafety("@errSetCast error not present in destination",
9761116 \\const std = @import("std");
9771117 \\pub fn panic(message: []const u8, stack_trace: ?*std.builtin.StackTrace) noreturn {
1118 \\ _ = message;
1119 \\ _ = stack_trace;
9781120 \\ std.os.exit(126);
9791121 \\}
9801122 \\const Set1 = error{A, B};
......@@ -990,6 +1132,8 @@ pub fn addCases(cases: *tests.CompareOutputContext) void {
9901132 cases.addRuntimeSafety("@alignCast misaligned",
9911133 \\const std = @import("std");
9921134 \\pub fn panic(message: []const u8, stack_trace: ?*std.builtin.StackTrace) noreturn {
1135 \\ _ = message;
1136 \\ _ = stack_trace;
9931137 \\ std.os.exit(126);
9941138 \\}
9951139 \\pub fn main() !void {
......@@ -1007,6 +1151,8 @@ pub fn addCases(cases: *tests.CompareOutputContext) void {
10071151 cases.addRuntimeSafety("bad union field access",
10081152 \\const std = @import("std");
10091153 \\pub fn panic(message: []const u8, stack_trace: ?*std.builtin.StackTrace) noreturn {
1154 \\ _ = message;
1155 \\ _ = stack_trace;
10101156 \\ std.os.exit(126);
10111157 \\}
10121158 \\
......@@ -1031,6 +1177,8 @@ pub fn addCases(cases: *tests.CompareOutputContext) void {
10311177 cases.addRuntimeSafety("@intCast to u0",
10321178 \\const std = @import("std");
10331179 \\pub fn panic(message: []const u8, stack_trace: ?*std.builtin.StackTrace) noreturn {
1180 \\ _ = message;
1181 \\ _ = stack_trace;
10341182 \\ std.os.exit(126);
10351183 \\}
10361184 \\
......@@ -1040,6 +1188,7 @@ pub fn addCases(cases: *tests.CompareOutputContext) void {
10401188 \\
10411189 \\fn bar(one: u1, not_zero: i32) void {
10421190 \\ var x = one << @intCast(u0, not_zero);
1191 \\ _ = x;
10431192 \\}
10441193 );
10451194
......@@ -1049,6 +1198,8 @@ pub fn addCases(cases: *tests.CompareOutputContext) void {
10491198 \\const std = @import("std");
10501199 \\
10511200 \\pub fn panic(message: []const u8, stack_trace: ?*std.builtin.StackTrace) noreturn {
1201 \\ _ = message;
1202 \\ _ = stack_trace;
10521203 \\ std.os.exit(126);
10531204 \\}
10541205 \\
......@@ -1058,6 +1209,7 @@ pub fn addCases(cases: *tests.CompareOutputContext) void {
10581209 \\ const p = nonFailing();
10591210 \\ resume p;
10601211 \\ const p2 = async printTrace(p);
1212 \\ _ = p2;
10611213 \\}
10621214 \\
10631215 \\fn nonFailing() anyframe->anyerror!void {
......@@ -1084,12 +1236,15 @@ pub fn addCases(cases: *tests.CompareOutputContext) void {
10841236 cases.addRuntimeSafety("slicing null C pointer",
10851237 \\const std = @import("std");
10861238 \\pub fn panic(message: []const u8, stack_trace: ?*std.builtin.StackTrace) noreturn {
1239 \\ _ = message;
1240 \\ _ = stack_trace;
10871241 \\ std.os.exit(126);
10881242 \\}
10891243 \\
10901244 \\pub fn main() void {
10911245 \\ var ptr: [*c]const u32 = null;
10921246 \\ var slice = ptr[0..3];
1247 \\ _ = slice;
10931248 \\}
10941249 );
10951250}
test/translate_c.zig+229
......@@ -12,9 +12,11 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
1212 , &[_][]const u8{
1313 \\pub export fn foo(arg_x: c_ulong) c_ulong {
1414 \\ var x = arg_x;
15 \\ _ = x;
1516 \\ const union_unnamed_1 = extern union {
1617 \\ _x: c_ulong,
1718 \\ };
19 \\ _ = union_unnamed_1;
1820 \\ return (union_unnamed_1{
1921 \\ ._x = x,
2022 \\ })._x;
......@@ -54,8 +56,10 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
5456 \\pub export fn foo() void {
5557 \\ while (true) if (true) {
5658 \\ var a: c_int = 1;
59 \\ _ = a;
5760 \\ } else {
5861 \\ var b: c_int = 2;
62 \\ _ = b;
5963 \\ };
6064 \\ if (true) if (true) {};
6165 \\}
......@@ -71,6 +75,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
7175 \\pub extern fn bar(...) c_int;
7276 \\pub export fn foo() void {
7377 \\ var a: c_int = undefined;
78 \\ _ = a;
7479 \\ if (a != 0) a = 2 else _ = bar();
7580 \\}
7681 });
......@@ -123,22 +128,26 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
123128 \\ B: c_int,
124129 \\ C: c_int,
125130 \\ };
131 \\ _ = struct_Foo;
126132 \\ var a: struct_Foo = struct_Foo{
127133 \\ .A = @as(c_int, 0),
128134 \\ .B = 0,
129135 \\ .C = 0,
130136 \\ };
137 \\ _ = a;
131138 \\ {
132139 \\ const struct_Foo_1 = extern struct {
133140 \\ A: c_int,
134141 \\ B: c_int,
135142 \\ C: c_int,
136143 \\ };
144 \\ _ = struct_Foo_1;
137145 \\ var a_2: struct_Foo_1 = struct_Foo_1{
138146 \\ .A = @as(c_int, 0),
139147 \\ .B = 0,
140148 \\ .C = 0,
141149 \\ };
150 \\ _ = a_2;
142151 \\ }
143152 \\}
144153 });
......@@ -167,20 +176,26 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
167176 \\ B: c_int,
168177 \\ C: c_int,
169178 \\ };
179 \\ _ = union_unnamed_1;
170180 \\ const Foo = union_unnamed_1;
181 \\ _ = Foo;
171182 \\ var a: Foo = Foo{
172183 \\ .A = @as(c_int, 0),
173184 \\ };
185 \\ _ = a;
174186 \\ {
175187 \\ const union_unnamed_2 = extern union {
176188 \\ A: c_int,
177189 \\ B: c_int,
178190 \\ C: c_int,
179191 \\ };
192 \\ _ = union_unnamed_2;
180193 \\ const Foo_1 = union_unnamed_2;
194 \\ _ = Foo_1;
181195 \\ var a_2: Foo_1 = Foo_1{
182196 \\ .A = @as(c_int, 0),
183197 \\ };
198 \\ _ = a_2;
184199 \\ }
185200 \\}
186201 });
......@@ -190,6 +205,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
190205 \\#define MEM_PHYSICAL_TO_K0(x) (void*)((uint32_t)(x) + SYS_BASE_CACHED)
191206 , &[_][]const u8{
192207 \\pub inline fn MEM_PHYSICAL_TO_K0(x: anytype) ?*c_void {
208 \\ _ = x;
193209 \\ return @import("std").zig.c_translation.cast(?*c_void, @import("std").zig.c_translation.cast(u32, x) + SYS_BASE_CACHED);
194210 \\}
195211 });
......@@ -231,6 +247,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
231247 \\pub const VALUE = ((((@as(c_int, 1) + (@as(c_int, 2) * @as(c_int, 3))) + (@as(c_int, 4) * @as(c_int, 5))) + @as(c_int, 6)) << @as(c_int, 7)) | @boolToInt(@as(c_int, 8) == @as(c_int, 9));
232248 ,
233249 \\pub inline fn _AL_READ3BYTES(p: anytype) @TypeOf((@import("std").zig.c_translation.cast([*c]u8, p).* | ((@import("std").zig.c_translation.cast([*c]u8, p) + @as(c_int, 1)).* << @as(c_int, 8))) | ((@import("std").zig.c_translation.cast([*c]u8, p) + @as(c_int, 2)).* << @as(c_int, 16))) {
250 \\ _ = p;
234251 \\ return (@import("std").zig.c_translation.cast([*c]u8, p).* | ((@import("std").zig.c_translation.cast([*c]u8, p) + @as(c_int, 1)).* << @as(c_int, 8))) | ((@import("std").zig.c_translation.cast([*c]u8, p) + @as(c_int, 2)).* << @as(c_int, 16));
235252 \\}
236253 });
......@@ -246,6 +263,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
246263 \\ const bar_1 = struct {
247264 \\ threadlocal var static: c_int = 2;
248265 \\ };
266 \\ _ = bar_1;
249267 \\ return 0;
250268 \\}
251269 });
......@@ -264,6 +282,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
264282 \\}
265283 \\pub export fn bar() c_int {
266284 \\ var a: c_int = 2;
285 \\ _ = a;
267286 \\ return 0;
268287 \\}
269288 \\pub export fn baz() c_int {
......@@ -278,6 +297,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
278297 , &[_][]const u8{
279298 \\pub export fn main() void {
280299 \\ var a: c_int = @bitCast(c_int, @truncate(c_uint, @alignOf(c_int)));
300 \\ _ = a;
281301 \\}
282302 });
283303
......@@ -308,6 +328,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
308328 \\pub const Color = struct_Color;
309329 ,
310330 \\pub inline fn CLITERAL(type_1: anytype) @TypeOf(type_1) {
331 \\ _ = type_1;
311332 \\ return type_1;
312333 \\}
313334 ,
......@@ -325,6 +346,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
325346 \\};
326347 ,
327348 \\pub inline fn A(_x: anytype) MyCStruct {
349 \\ _ = _x;
328350 \\ return @import("std").mem.zeroInit(MyCStruct, .{
329351 \\ .x = _x,
330352 \\ });
......@@ -355,6 +377,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
355377 \\#define __ferror_unlocked_body(_fp) (((_fp)->_flags & _IO_ERR_SEEN) != 0)
356378 , &[_][]const u8{
357379 \\pub inline fn __ferror_unlocked_body(_fp: anytype) @TypeOf((_fp.*._flags & _IO_ERR_SEEN) != @as(c_int, 0)) {
380 \\ _ = _fp;
358381 \\ return (_fp.*._flags & _IO_ERR_SEEN) != @as(c_int, 0);
359382 \\}
360383 });
......@@ -364,6 +387,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
364387 \\#define BAR 1 && 2 > 4
365388 , &[_][]const u8{
366389 \\pub inline fn FOO(x: anytype) @TypeOf(@boolToInt(x >= @as(c_int, 0)) + @boolToInt(x >= @as(c_int, 0))) {
390 \\ _ = x;
367391 \\ return @boolToInt(x >= @as(c_int, 0)) + @boolToInt(x >= @as(c_int, 0));
368392 \\}
369393 ,
......@@ -426,6 +450,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
426450 \\};
427451 ,
428452 \\pub inline fn bar(x: anytype) @TypeOf(baz(@as(c_int, 1), @as(c_int, 2))) {
453 \\ _ = x;
429454 \\ return blk: {
430455 \\ _ = &x;
431456 \\ _ = @as(c_int, 3);
......@@ -555,6 +580,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
555580 \\};
556581 \\pub export fn foo(arg_x: [*c]outer) void {
557582 \\ var x = arg_x;
583 \\ _ = x;
558584 \\ x.*.unnamed_0.unnamed_0.y = @bitCast(c_int, @as(c_uint, x.*.unnamed_0.x));
559585 \\}
560586 });
......@@ -641,7 +667,9 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
641667 \\pub const struct_opaque_2 = opaque {};
642668 \\pub export fn function(arg_opaque_1: ?*struct_opaque) void {
643669 \\ var opaque_1 = arg_opaque_1;
670 \\ _ = opaque_1;
644671 \\ var cast: ?*struct_opaque_2 = @ptrCast(?*struct_opaque_2, opaque_1);
672 \\ _ = cast;
645673 \\}
646674 });
647675
......@@ -673,6 +701,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
673701 \\pub export fn my_fn() align(128) void {}
674702 \\pub export fn other_fn() void {
675703 \\ var ARR: [16]u8 align(16) = undefined;
704 \\ _ = ARR;
676705 \\}
677706 });
678707
......@@ -708,11 +737,17 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
708737 , &[_][]const u8{
709738 \\pub export fn foo() void {
710739 \\ var a: c_int = undefined;
740 \\ _ = a;
711741 \\ var b: u8 = 123;
742 \\ _ = b;
712743 \\ const c: c_int = undefined;
744 \\ _ = c;
713745 \\ const d: c_uint = @bitCast(c_uint, @as(c_int, 440));
746 \\ _ = d;
714747 \\ var e: c_int = 10;
748 \\ _ = e;
715749 \\ var f: c_uint = 10;
750 \\ _ = f;
716751 \\}
717752 });
718753
......@@ -728,6 +763,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
728763 , &[_][]const u8{
729764 \\pub export fn foo() void {
730765 \\ var a: c_int = undefined;
766 \\ _ = a;
731767 \\ _ = @as(c_int, 1);
732768 \\ _ = "hey";
733769 \\ _ = @as(c_int, 1) + @as(c_int, 1);
......@@ -771,6 +807,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
771807 \\ const v2 = struct {
772808 \\ const static: [5:0]u8 = "2.2.2".*;
773809 \\ };
810 \\ _ = v2;
774811 \\}
775812 });
776813
......@@ -812,7 +849,9 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
812849 \\pub extern fn foo() void;
813850 \\pub export fn bar() void {
814851 \\ var func_ptr: ?*c_void = @ptrCast(?*c_void, foo);
852 \\ _ = func_ptr;
815853 \\ var typed_func_ptr: ?fn () callconv(.C) void = @intToPtr(?fn () callconv(.C) void, @intCast(c_ulong, @ptrToInt(func_ptr)));
854 \\ _ = typed_func_ptr;
816855 \\}
817856 });
818857
......@@ -842,8 +881,11 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
842881 , &[_][]const u8{
843882 \\pub export fn s() c_int {
844883 \\ var a: c_int = undefined;
884 \\ _ = a;
845885 \\ var b: c_int = undefined;
886 \\ _ = b;
846887 \\ var c: c_int = undefined;
888 \\ _ = c;
847889 \\ c = a + b;
848890 \\ c = a - b;
849891 \\ c = a * b;
......@@ -853,8 +895,11 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
853895 \\}
854896 \\pub export fn u() c_uint {
855897 \\ var a: c_uint = undefined;
898 \\ _ = a;
856899 \\ var b: c_uint = undefined;
900 \\ _ = b;
857901 \\ var c: c_uint = undefined;
902 \\ _ = c;
858903 \\ c = a +% b;
859904 \\ c = a -% b;
860905 \\ c = a *% b;
......@@ -1218,6 +1263,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
12181263 \\pub export fn foo() void {
12191264 \\ var a: c_int = undefined;
12201265 \\ _ = a;
1266 \\ _ = a;
12211267 \\}
12221268 });
12231269
......@@ -1229,6 +1275,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
12291275 , &[_][]const u8{
12301276 \\pub export fn foo() ?*c_void {
12311277 \\ var x: [*c]c_ushort = undefined;
1278 \\ _ = x;
12321279 \\ return @ptrCast(?*c_void, x);
12331280 \\}
12341281 });
......@@ -1285,6 +1332,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
12851332 \\pub export fn foo() void {
12861333 \\ {
12871334 \\ var i: c_int = 0;
1335 \\ _ = i;
12881336 \\ while (i != 0) : (i += 1) {}
12891337 \\ }
12901338 \\}
......@@ -1308,6 +1356,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
13081356 , &[_][]const u8{
13091357 \\pub export fn foo() void {
13101358 \\ var i: c_int = undefined;
1359 \\ _ = i;
13111360 \\ {
13121361 \\ i = 3;
13131362 \\ while (i != 0) : (i -= 1) {}
......@@ -1351,6 +1400,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
13511400 , &[_][]const u8{
13521401 \\pub export fn ptrcast() [*c]f32 {
13531402 \\ var a: [*c]c_int = undefined;
1403 \\ _ = a;
13541404 \\ return @ptrCast([*c]f32, @alignCast(@import("std").meta.alignment(f32), a));
13551405 \\}
13561406 });
......@@ -1374,17 +1424,26 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
13741424 , &[_][]const u8{
13751425 \\pub export fn test_ptr_cast() void {
13761426 \\ var p: ?*c_void = undefined;
1427 \\ _ = p;
13771428 \\ {
13781429 \\ var to_char: [*c]u8 = @ptrCast([*c]u8, @alignCast(@import("std").meta.alignment(u8), p));
1430 \\ _ = to_char;
13791431 \\ var to_short: [*c]c_short = @ptrCast([*c]c_short, @alignCast(@import("std").meta.alignment(c_short), p));
1432 \\ _ = to_short;
13801433 \\ var to_int: [*c]c_int = @ptrCast([*c]c_int, @alignCast(@import("std").meta.alignment(c_int), p));
1434 \\ _ = to_int;
13811435 \\ var to_longlong: [*c]c_longlong = @ptrCast([*c]c_longlong, @alignCast(@import("std").meta.alignment(c_longlong), p));
1436 \\ _ = to_longlong;
13821437 \\ }
13831438 \\ {
13841439 \\ var to_char: [*c]u8 = @ptrCast([*c]u8, @alignCast(@import("std").meta.alignment(u8), p));
1440 \\ _ = to_char;
13851441 \\ var to_short: [*c]c_short = @ptrCast([*c]c_short, @alignCast(@import("std").meta.alignment(c_short), p));
1442 \\ _ = to_short;
13861443 \\ var to_int: [*c]c_int = @ptrCast([*c]c_int, @alignCast(@import("std").meta.alignment(c_int), p));
1444 \\ _ = to_int;
13871445 \\ var to_longlong: [*c]c_longlong = @ptrCast([*c]c_longlong, @alignCast(@import("std").meta.alignment(c_longlong), p));
1446 \\ _ = to_longlong;
13881447 \\ }
13891448 \\}
13901449 });
......@@ -1402,8 +1461,11 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
14021461 , &[_][]const u8{
14031462 \\pub export fn while_none_bool() c_int {
14041463 \\ var a: c_int = undefined;
1464 \\ _ = a;
14051465 \\ var b: f32 = undefined;
1466 \\ _ = b;
14061467 \\ var c: ?*c_void = undefined;
1468 \\ _ = c;
14071469 \\ while (a != 0) return 0;
14081470 \\ while (b != 0) return 1;
14091471 \\ while (c != null) return 2;
......@@ -1424,8 +1486,11 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
14241486 , &[_][]const u8{
14251487 \\pub export fn for_none_bool() c_int {
14261488 \\ var a: c_int = undefined;
1489 \\ _ = a;
14271490 \\ var b: f32 = undefined;
1491 \\ _ = b;
14281492 \\ var c: ?*c_void = undefined;
1493 \\ _ = c;
14291494 \\ while (a != 0) return 0;
14301495 \\ while (b != 0) return 1;
14311496 \\ while (c != null) return 2;
......@@ -1462,6 +1527,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
14621527 , &[_][]const u8{
14631528 \\pub export fn foo() void {
14641529 \\ var x: [*c]c_int = undefined;
1530 \\ _ = x;
14651531 \\ x.* = 1;
14661532 \\}
14671533 });
......@@ -1475,7 +1541,9 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
14751541 , &[_][]const u8{
14761542 \\pub export fn foo() c_int {
14771543 \\ var x: c_int = 1234;
1544 \\ _ = x;
14781545 \\ var ptr: [*c]c_int = &x;
1546 \\ _ = ptr;
14791547 \\ return ptr.*;
14801548 \\}
14811549 });
......@@ -1488,6 +1556,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
14881556 , &[_][]const u8{
14891557 \\pub export fn foo() c_int {
14901558 \\ var x: c_int = undefined;
1559 \\ _ = x;
14911560 \\ return ~x;
14921561 \\}
14931562 });
......@@ -1505,8 +1574,11 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
15051574 , &[_][]const u8{
15061575 \\pub export fn foo() c_int {
15071576 \\ var a: c_int = undefined;
1577 \\ _ = a;
15081578 \\ var b: f32 = undefined;
1579 \\ _ = b;
15091580 \\ var c: ?*c_void = undefined;
1581 \\ _ = c;
15101582 \\ return @boolToInt(!(a == @as(c_int, 0)));
15111583 \\ return @boolToInt(!(a != 0));
15121584 \\ return @boolToInt(!(b != 0));
......@@ -1628,9 +1700,11 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
16281700 \\ var arr: [10]u8 = [1]u8{
16291701 \\ 1,
16301702 \\ } ++ [1]u8{0} ** 9;
1703 \\ _ = arr;
16311704 \\ var arr1: [10][*c]u8 = [1][*c]u8{
16321705 \\ null,
16331706 \\ } ++ [1][*c]u8{null} ** 9;
1707 \\ _ = arr1;
16341708 \\}
16351709 });
16361710
......@@ -1817,10 +1891,13 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
18171891 \\pub extern var c: c_int;
18181892 ,
18191893 \\pub inline fn BASIC(c_1: anytype) @TypeOf(c_1 * @as(c_int, 2)) {
1894 \\ _ = c_1;
18201895 \\ return c_1 * @as(c_int, 2);
18211896 \\}
18221897 ,
18231898 \\pub inline fn FOO(L: anytype, b: anytype) @TypeOf(L + b) {
1899 \\ _ = L;
1900 \\ _ = b;
18241901 \\ return L + b;
18251902 \\}
18261903 ,
......@@ -1872,13 +1949,18 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
18721949 \\pub var c: c_int = 4;
18731950 \\pub export fn foo(arg_c_1: u8) void {
18741951 \\ var c_1 = arg_c_1;
1952 \\ _ = c_1;
18751953 \\ var a_2: c_int = undefined;
1954 \\ _ = a_2;
18761955 \\ var b_3: u8 = 123;
1956 \\ _ = b_3;
18771957 \\ b_3 = @bitCast(u8, @truncate(i8, a_2));
18781958 \\ {
18791959 \\ var d: c_int = 5;
1960 \\ _ = d;
18801961 \\ }
18811962 \\ var d: c_uint = @bitCast(c_uint, @as(c_int, 440));
1963 \\ _ = d;
18821964 \\}
18831965 });
18841966
......@@ -1912,7 +1994,9 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
19121994 , &[_][]const u8{
19131995 \\pub export fn foo() void {
19141996 \\ var a: c_int = undefined;
1997 \\ _ = a;
19151998 \\ var b: c_int = undefined;
1999 \\ _ = b;
19162000 \\ a = blk: {
19172001 \\ const tmp = @as(c_int, 2);
19182002 \\ b = tmp;
......@@ -1942,11 +2026,13 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
19422026 , &[_][]const u8{
19432027 \\pub export fn foo() c_int {
19442028 \\ var a: c_int = 5;
2029 \\ _ = a;
19452030 \\ while (true) {
19462031 \\ a = 2;
19472032 \\ }
19482033 \\ while (true) {
19492034 \\ var a_1: c_int = 4;
2035 \\ _ = a_1;
19502036 \\ a_1 = 9;
19512037 \\ return blk: {
19522038 \\ _ = @as(c_int, 6);
......@@ -1955,6 +2041,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
19552041 \\ }
19562042 \\ while (true) {
19572043 \\ var a_1: c_int = 2;
2044 \\ _ = a_1;
19582045 \\ a_1 = 12;
19592046 \\ }
19602047 \\ while (true) {
......@@ -1976,9 +2063,12 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
19762063 \\pub export fn foo() void {
19772064 \\ {
19782065 \\ var i: c_int = 2;
2066 \\ _ = i;
19792067 \\ var b: c_int = 4;
2068 \\ _ = b;
19802069 \\ while ((i + @as(c_int, 2)) != 0) : (i = 2) {
19812070 \\ var a: c_int = 2;
2071 \\ _ = a;
19822072 \\ _ = blk: {
19832073 \\ _ = blk_1: {
19842074 \\ a = 6;
......@@ -1989,6 +2079,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
19892079 \\ }
19902080 \\ }
19912081 \\ var i: u8 = 2;
2082 \\ _ = i;
19922083 \\}
19932084 });
19942085
......@@ -2061,7 +2152,9 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
20612152 , &[_][]const u8{
20622153 \\pub export fn switch_fn(arg_i: c_int) void {
20632154 \\ var i = arg_i;
2155 \\ _ = i;
20642156 \\ var res: c_int = 0;
2157 \\ _ = res;
20652158 \\ while (true) {
20662159 \\ switch (i) {
20672160 \\ @as(c_int, 0) => {
......@@ -2150,7 +2243,9 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
21502243 , &[_][]const u8{
21512244 \\pub export fn max(arg_a: c_int) void {
21522245 \\ var a = arg_a;
2246 \\ _ = a;
21532247 \\ var tmp: c_int = undefined;
2248 \\ _ = tmp;
21542249 \\ tmp = a;
21552250 \\ a = tmp;
21562251 \\}
......@@ -2164,8 +2259,11 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
21642259 , &[_][]const u8{
21652260 \\pub export fn max(arg_a: c_int) void {
21662261 \\ var a = arg_a;
2262 \\ _ = a;
21672263 \\ var b: c_int = undefined;
2264 \\ _ = b;
21682265 \\ var c: c_int = undefined;
2266 \\ _ = c;
21692267 \\ c = blk: {
21702268 \\ const tmp = a;
21712269 \\ b = tmp;
......@@ -2194,6 +2292,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
21942292 , &[_][]const u8{
21952293 \\pub export fn float_to_int(arg_a: f32) c_int {
21962294 \\ var a = arg_a;
2295 \\ _ = a;
21972296 \\ return @floatToInt(c_int, a);
21982297 \\}
21992298 });
......@@ -2217,16 +2316,27 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
22172316 , &[_][]const u8{
22182317 \\pub export fn escapes() [*c]const u8 {
22192318 \\ var a: u8 = '\'';
2319 \\ _ = a;
22202320 \\ var b: u8 = '\\';
2321 \\ _ = b;
22212322 \\ var c: u8 = '\x07';
2323 \\ _ = c;
22222324 \\ var d: u8 = '\x08';
2325 \\ _ = d;
22232326 \\ var e: u8 = '\x0c';
2327 \\ _ = e;
22242328 \\ var f: u8 = '\n';
2329 \\ _ = f;
22252330 \\ var g: u8 = '\r';
2331 \\ _ = g;
22262332 \\ var h: u8 = '\t';
2333 \\ _ = h;
22272334 \\ var i: u8 = '\x0b';
2335 \\ _ = i;
22282336 \\ var j: u8 = '\x00';
2337 \\ _ = j;
22292338 \\ var k: u8 = '"';
2339 \\ _ = k;
22302340 \\ return "'\\\x07\x08\x0c\n\r\t\x0b\x00\"";
22312341 \\}
22322342 });
......@@ -2246,11 +2356,13 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
22462356 , &[_][]const u8{
22472357 \\pub export fn foo() void {
22482358 \\ var a: c_int = 2;
2359 \\ _ = a;
22492360 \\ while (true) {
22502361 \\ a = a - @as(c_int, 1);
22512362 \\ if (!(a != 0)) break;
22522363 \\ }
22532364 \\ var b: c_int = 2;
2365 \\ _ = b;
22542366 \\ while (true) {
22552367 \\ b = b - @as(c_int, 1);
22562368 \\ if (!(b != 0)) break;
......@@ -2291,21 +2403,37 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
22912403 \\pub const SomeTypedef = c_int;
22922404 \\pub export fn and_or_non_bool(arg_a: c_int, arg_b: f32, arg_c: ?*c_void) c_int {
22932405 \\ var a = arg_a;
2406 \\ _ = a;
22942407 \\ var b = arg_b;
2408 \\ _ = b;
22952409 \\ var c = arg_c;
2410 \\ _ = c;
22962411 \\ var d: enum_Foo = @bitCast(c_uint, FooA);
2412 \\ _ = d;
22972413 \\ var e: c_int = @boolToInt((a != 0) and (b != 0));
2414 \\ _ = e;
22982415 \\ var f: c_int = @boolToInt((b != 0) and (c != null));
2416 \\ _ = f;
22992417 \\ var g: c_int = @boolToInt((a != 0) and (c != null));
2418 \\ _ = g;
23002419 \\ var h: c_int = @boolToInt((a != 0) or (b != 0));
2420 \\ _ = h;
23012421 \\ var i: c_int = @boolToInt((b != 0) or (c != null));
2422 \\ _ = i;
23022423 \\ var j: c_int = @boolToInt((a != 0) or (c != null));
2424 \\ _ = j;
23032425 \\ var k: c_int = @boolToInt((a != 0) or (@bitCast(c_int, d) != 0));
2426 \\ _ = k;
23042427 \\ var l: c_int = @boolToInt((@bitCast(c_int, d) != 0) and (b != 0));
2428 \\ _ = l;
23052429 \\ var m: c_int = @boolToInt((c != null) or (d != 0));
2430 \\ _ = m;
23062431 \\ var td: SomeTypedef = 44;
2432 \\ _ = td;
23072433 \\ var o: c_int = @boolToInt((td != 0) or (b != 0));
2434 \\ _ = o;
23082435 \\ var p: c_int = @boolToInt((c != null) and (td != 0));
2436 \\ _ = p;
23092437 \\ return (((((((((e + f) + g) + h) + i) + j) + k) + l) + m) + o) + p;
23102438 \\}
23112439 ,
......@@ -2345,7 +2473,9 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
23452473 , &[_][]const u8{
23462474 \\pub export fn max(arg_a: c_int, arg_b: c_int) c_int {
23472475 \\ var a = arg_a;
2476 \\ _ = a;
23482477 \\ var b = arg_b;
2478 \\ _ = b;
23492479 \\ return (a & b) ^ (a | b);
23502480 \\}
23512481 });
......@@ -2364,14 +2494,23 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
23642494 , &[_][]const u8{
23652495 \\pub export fn test_comparisons(arg_a: c_int, arg_b: c_int) c_int {
23662496 \\ var a = arg_a;
2497 \\ _ = a;
23672498 \\ var b = arg_b;
2499 \\ _ = b;
23682500 \\ var c: c_int = @boolToInt(a < b);
2501 \\ _ = c;
23692502 \\ var d: c_int = @boolToInt(a > b);
2503 \\ _ = d;
23702504 \\ var e: c_int = @boolToInt(a <= b);
2505 \\ _ = e;
23712506 \\ var f: c_int = @boolToInt(a >= b);
2507 \\ _ = f;
23722508 \\ var g: c_int = @boolToInt(c < d);
2509 \\ _ = g;
23732510 \\ var h: c_int = @boolToInt(e < f);
2511 \\ _ = h;
23742512 \\ var i: c_int = @boolToInt(g < h);
2513 \\ _ = i;
23752514 \\ return i;
23762515 \\}
23772516 });
......@@ -2387,7 +2526,9 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
23872526 , &[_][]const u8{
23882527 \\pub export fn max(arg_a: c_int, arg_b: c_int) c_int {
23892528 \\ var a = arg_a;
2529 \\ _ = a;
23902530 \\ var b = arg_b;
2531 \\ _ = b;
23912532 \\ if (a == b) return a;
23922533 \\ if (a != b) return b;
23932534 \\ return a;
......@@ -2404,6 +2545,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
24042545 \\pub const yes = [*c]u8;
24052546 \\pub export fn foo() void {
24062547 \\ var a: yes = undefined;
2548 \\ _ = a;
24072549 \\ if (a != null) {
24082550 \\ _ = @as(c_int, 2);
24092551 \\ }
......@@ -2423,6 +2565,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
24232565 \\ return blk: {
24242566 \\ var a: c_int = 1;
24252567 \\ _ = a;
2568 \\ _ = a;
24262569 \\ break :blk a;
24272570 \\ };
24282571 \\}
......@@ -2448,6 +2591,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
24482591 \\pub export var b: f32 = 2.0;
24492592 \\pub export fn foo() void {
24502593 \\ var c: [*c]struct_Foo = undefined;
2594 \\ _ = c;
24512595 \\ _ = a.b;
24522596 \\ _ = c.*.b;
24532597 \\}
......@@ -2467,6 +2611,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
24672611 \\pub export var array: [100]c_int = [1]c_int{0} ** 100;
24682612 \\pub export fn foo(arg_index: c_int) c_int {
24692613 \\ var index = arg_index;
2614 \\ _ = index;
24702615 \\ return array[@intCast(c_uint, index)];
24712616 \\}
24722617 ,
......@@ -2481,7 +2626,9 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
24812626 , &[_][]const u8{
24822627 \\pub export fn foo() void {
24832628 \\ var a: [10]c_int = undefined;
2629 \\ _ = a;
24842630 \\ var i: c_int = 0;
2631 \\ _ = i;
24852632 \\ a[@intCast(c_uint, i)] = 0;
24862633 \\}
24872634 });
......@@ -2494,7 +2641,9 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
24942641 , &[_][]const u8{
24952642 \\pub export fn foo() void {
24962643 \\ var a: [10]c_longlong = undefined;
2644 \\ _ = a;
24972645 \\ var i: c_longlong = 0;
2646 \\ _ = i;
24982647 \\ a[@intCast(usize, i)] = 0;
24992648 \\}
25002649 });
......@@ -2507,7 +2656,9 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
25072656 , &[_][]const u8{
25082657 \\pub export fn foo() void {
25092658 \\ var a: [10]c_uint = undefined;
2659 \\ _ = a;
25102660 \\ var i: c_uint = 0;
2661 \\ _ = i;
25112662 \\ a[i] = 0;
25122663 \\}
25132664 });
......@@ -2516,6 +2667,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
25162667 \\#define CALL(arg) bar(arg)
25172668 , &[_][]const u8{
25182669 \\pub inline fn CALL(arg: anytype) @TypeOf(bar(arg)) {
2670 \\ _ = arg;
25192671 \\ return bar(arg);
25202672 \\}
25212673 });
......@@ -2524,6 +2676,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
25242676 \\#define CALL(arg) bar()
25252677 , &[_][]const u8{
25262678 \\pub inline fn CALL(arg: anytype) @TypeOf(bar()) {
2679 \\ _ = arg;
25272680 \\ return bar();
25282681 \\}
25292682 });
......@@ -2539,7 +2692,9 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
25392692 , &[_][]const u8{
25402693 \\pub export fn max(arg_a: c_int, arg_b: c_int) c_int {
25412694 \\ var a = arg_a;
2695 \\ _ = a;
25422696 \\ var b = arg_b;
2697 \\ _ = b;
25432698 \\ if ((a < b) or (a == b)) return b;
25442699 \\ if ((a >= b) and (a == b)) return a;
25452700 \\ return a;
......@@ -2561,7 +2716,9 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
25612716 , &[_][]const u8{
25622717 \\pub export fn max(arg_a: c_int, arg_b: c_int) c_int {
25632718 \\ var a = arg_a;
2719 \\ _ = a;
25642720 \\ var b = arg_b;
2721 \\ _ = b;
25652722 \\ if (a < b) return b;
25662723 \\ if (a < b) return b else return a;
25672724 \\ if (a < b) {} else {}
......@@ -2582,12 +2739,14 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
25822739 \\pub export fn foo() void {
25832740 \\ if (true) {
25842741 \\ var a: c_int = 2;
2742 \\ _ = a;
25852743 \\ }
25862744 \\ if ((blk: {
25872745 \\ _ = @as(c_int, 2);
25882746 \\ break :blk @as(c_int, 5);
25892747 \\ }) != 0) {
25902748 \\ var a: c_int = 2;
2749 \\ _ = a;
25912750 \\ }
25922751 \\}
25932752 });
......@@ -2610,9 +2769,13 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
26102769 \\;
26112770 \\pub export fn if_none_bool(arg_a: c_int, arg_b: f32, arg_c: ?*c_void, arg_d: enum_SomeEnum) c_int {
26122771 \\ var a = arg_a;
2772 \\ _ = a;
26132773 \\ var b = arg_b;
2774 \\ _ = b;
26142775 \\ var c = arg_c;
2776 \\ _ = c;
26152777 \\ var d = arg_d;
2778 \\ _ = d;
26162779 \\ if (a != 0) return 0;
26172780 \\ if (b != 0) return 1;
26182781 \\ if (c != null) return 2;
......@@ -2640,6 +2803,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
26402803 , &[_][]const u8{
26412804 \\pub export fn abs(arg_a: c_int) c_int {
26422805 \\ var a = arg_a;
2806 \\ _ = a;
26432807 \\ return if (a < @as(c_int, 0)) -a else a;
26442808 \\}
26452809 });
......@@ -2660,16 +2824,19 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
26602824 , &[_][]const u8{
26612825 \\pub export fn foo1(arg_a: c_uint) c_uint {
26622826 \\ var a = arg_a;
2827 \\ _ = a;
26632828 \\ a +%= 1;
26642829 \\ return a;
26652830 \\}
26662831 \\pub export fn foo2(arg_a: c_int) c_int {
26672832 \\ var a = arg_a;
2833 \\ _ = a;
26682834 \\ a += 1;
26692835 \\ return a;
26702836 \\}
26712837 \\pub export fn foo3(arg_a: [*c]c_int) [*c]c_int {
26722838 \\ var a = arg_a;
2839 \\ _ = a;
26732840 \\ a += 1;
26742841 \\ return a;
26752842 \\}
......@@ -2695,7 +2862,9 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
26952862 \\}
26962863 \\pub export fn bar() void {
26972864 \\ var f: ?fn () callconv(.C) void = foo;
2865 \\ _ = f;
26982866 \\ var b: ?fn () callconv(.C) c_int = baz;
2867 \\ _ = b;
26992868 \\ f.?();
27002869 \\ f.?();
27012870 \\ foo();
......@@ -2721,7 +2890,9 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
27212890 , &[_][]const u8{
27222891 \\pub export fn foo() void {
27232892 \\ var i: c_int = 0;
2893 \\ _ = i;
27242894 \\ var u: c_uint = 0;
2895 \\ _ = u;
27252896 \\ i += 1;
27262897 \\ i -= 1;
27272898 \\ u +%= 1;
......@@ -2760,7 +2931,9 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
27602931 , &[_][]const u8{
27612932 \\pub export fn log2(arg_a: c_uint) c_int {
27622933 \\ var a = arg_a;
2934 \\ _ = a;
27632935 \\ var i: c_int = 0;
2936 \\ _ = i;
27642937 \\ while (a > @bitCast(c_uint, @as(c_int, 0))) {
27652938 \\ a >>= @intCast(@import("std").math.Log2Int(c_int), @as(c_int, 1));
27662939 \\ }
......@@ -2780,7 +2953,9 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
27802953 , &[_][]const u8{
27812954 \\pub export fn log2(arg_a: u32) c_int {
27822955 \\ var a = arg_a;
2956 \\ _ = a;
27832957 \\ var i: c_int = 0;
2958 \\ _ = i;
27842959 \\ while (a > @bitCast(c_uint, @as(c_int, 0))) {
27852960 \\ a >>= @intCast(@import("std").math.Log2Int(c_int), @as(c_int, 1));
27862961 \\ }
......@@ -2808,7 +2983,9 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
28082983 , &[_][]const u8{
28092984 \\pub export fn foo() void {
28102985 \\ var a: c_int = 0;
2986 \\ _ = a;
28112987 \\ var b: c_uint = 0;
2988 \\ _ = b;
28122989 \\ a += blk: {
28132990 \\ const ref = &a;
28142991 \\ ref.* += @as(c_int, 1);
......@@ -2887,6 +3064,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
28873064 , &[_][]const u8{
28883065 \\pub export fn foo() void {
28893066 \\ var a: c_uint = 0;
3067 \\ _ = a;
28903068 \\ a +%= blk: {
28913069 \\ const ref = &a;
28923070 \\ ref.* +%= @bitCast(c_uint, @as(c_int, 1));
......@@ -2946,7 +3124,9 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
29463124 , &[_][]const u8{
29473125 \\pub export fn foo() void {
29483126 \\ var i: c_int = 0;
3127 \\ _ = i;
29493128 \\ var u: c_uint = 0;
3129 \\ _ = u;
29503130 \\ i += 1;
29513131 \\ i -= 1;
29523132 \\ u +%= 1;
......@@ -3041,6 +3221,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
30413221 \\pub fn bar() callconv(.C) void {}
30423222 \\pub export fn foo(arg_baz: ?fn () callconv(.C) [*c]c_int) void {
30433223 \\ var baz = arg_baz;
3224 \\ _ = baz;
30443225 \\ bar();
30453226 \\ _ = baz.?();
30463227 \\}
......@@ -3082,6 +3263,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
30823263 \\#define BAZ (uint32_t)(2)
30833264 , &[_][]const u8{
30843265 \\pub inline fn FOO(bar: anytype) @TypeOf(baz(@import("std").zig.c_translation.cast(?*c_void, baz))) {
3266 \\ _ = bar;
30853267 \\ return baz(@import("std").zig.c_translation.cast(?*c_void, baz));
30863268 \\}
30873269 ,
......@@ -3122,10 +3304,14 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
31223304 \\#define MAX(a, b) ((b) > (a) ? (b) : (a))
31233305 , &[_][]const u8{
31243306 \\pub inline fn MIN(a: anytype, b: anytype) @TypeOf(if (b < a) b else a) {
3307 \\ _ = a;
3308 \\ _ = b;
31253309 \\ return if (b < a) b else a;
31263310 \\}
31273311 ,
31283312 \\pub inline fn MAX(a: anytype, b: anytype) @TypeOf(if (b > a) b else a) {
3313 \\ _ = a;
3314 \\ _ = b;
31293315 \\ return if (b > a) b else a;
31303316 \\}
31313317 });
......@@ -3137,7 +3323,9 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
31373323 , &[_][]const u8{
31383324 \\pub export fn foo(arg_p: [*c]c_int, arg_x: c_int) c_int {
31393325 \\ var p = arg_p;
3326 \\ _ = p;
31403327 \\ var x = arg_x;
3328 \\ _ = x;
31413329 \\ return blk: {
31423330 \\ const tmp = x;
31433331 \\ (blk_1: {
......@@ -3164,6 +3352,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
31643352 \\}
31653353 \\pub export fn bar(arg_x: c_long) c_ushort {
31663354 \\ var x = arg_x;
3355 \\ _ = x;
31673356 \\ return @bitCast(c_ushort, @truncate(c_short, x));
31683357 \\}
31693358 });
......@@ -3176,6 +3365,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
31763365 , &[_][]const u8{
31773366 \\pub export fn foo(arg_bar_1: c_int) void {
31783367 \\ var bar_1 = arg_bar_1;
3368 \\ _ = bar_1;
31793369 \\ bar_1 = 2;
31803370 \\}
31813371 \\pub export var bar: c_int = 4;
......@@ -3189,6 +3379,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
31893379 , &[_][]const u8{
31903380 \\pub export fn foo(arg_bar_1: c_int) void {
31913381 \\ var bar_1 = arg_bar_1;
3382 \\ _ = bar_1;
31923383 \\ bar_1 = 2;
31933384 \\}
31943385 ,
......@@ -3218,13 +3409,16 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
32183409 , &[_][]const u8{
32193410 \\pub export fn foo(arg_a: [*c]c_int) void {
32203411 \\ var a = arg_a;
3412 \\ _ = a;
32213413 \\}
32223414 \\pub export fn bar(arg_a: [*c]const c_int) void {
32233415 \\ var a = arg_a;
3416 \\ _ = a;
32243417 \\ foo(@intToPtr([*c]c_int, @ptrToInt(a)));
32253418 \\}
32263419 \\pub export fn baz(arg_a: [*c]volatile c_int) void {
32273420 \\ var a = arg_a;
3421 \\ _ = a;
32283422 \\ foo(@intToPtr([*c]c_int, @ptrToInt(a)));
32293423 \\}
32303424 });
......@@ -3239,9 +3433,13 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
32393433 , &[_][]const u8{
32403434 \\pub export fn foo(arg_x: bool) bool {
32413435 \\ var x = arg_x;
3436 \\ _ = x;
32423437 \\ var a: bool = @as(c_int, @boolToInt(x)) != @as(c_int, 1);
3438 \\ _ = a;
32433439 \\ var b: bool = @as(c_int, @boolToInt(a)) != @as(c_int, 0);
3440 \\ _ = b;
32443441 \\ var c: bool = @ptrToInt(foo) != 0;
3442 \\ _ = c;
32453443 \\ return foo(@as(c_int, @boolToInt(c)) != @as(c_int, @boolToInt(b)));
32463444 \\}
32473445 });
......@@ -3252,7 +3450,9 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
32523450 \\}
32533451 , &[_][]const u8{
32543452 \\pub export fn max(x: c_int, arg_y: c_int) c_int {
3453 \\ _ = x;
32553454 \\ var y = arg_y;
3455 \\ _ = y;
32563456 \\ return if (x > y) x else y;
32573457 \\}
32583458 });
......@@ -3313,6 +3513,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
33133513 \\
33143514 , &[_][]const u8{
33153515 \\pub inline fn DefaultScreen(dpy: anytype) @TypeOf(@import("std").zig.c_translation.cast(_XPrivDisplay, dpy).*.default_screen) {
3516 \\ _ = dpy;
33163517 \\ return @import("std").zig.c_translation.cast(_XPrivDisplay, dpy).*.default_screen;
33173518 \\}
33183519 });
......@@ -3532,6 +3733,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
35323733 \\ const foo = struct {
35333734 \\ var static: struct_FOO = @import("std").mem.zeroes(struct_FOO);
35343735 \\ };
3736 \\ _ = foo;
35353737 \\ return foo.static.x;
35363738 \\}
35373739 });
......@@ -3544,4 +3746,31 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
35443746 \\pub const MAP_FAILED = @import("std").zig.c_translation.cast(?*c_void, -@as(c_int, 1));
35453747 \\pub const INVALID_HANDLE_VALUE = @import("std").zig.c_translation.cast(?*c_void, @import("std").zig.c_translation.cast(LONG_PTR, -@as(c_int, 1)));
35463748 });
3749
3750 cases.add("discard local variables and function parameters",
3751 \\#define FOO(A, B) (A) + (B)
3752 \\int bar(int x, int y) {
3753 \\ return x;
3754 \\}
3755 , &[_][]const u8{
3756 \\pub export fn bar(arg_x: c_int, arg_y: c_int) c_int {
3757 \\ var x = arg_x;
3758 \\ _ = x;
3759 \\ var y = arg_y;
3760 \\ _ = y;
3761 \\ return x;
3762 \\}
3763 ,
3764 \\pub inline fn FOO(A: anytype, B: anytype) @TypeOf(A + B) {
3765 \\ _ = A;
3766 \\ _ = B;
3767 \\ return A + B;
3768 \\}
3769 });
3770
3771 cases.add("Don't allow underscore identifier in macros",
3772 \\#define FOO _
3773 , &[_][]const u8{
3774 \\pub const FOO = @compileError("unable to translate C expr: illegal identifier _");
3775 });
35473776}