| author | |
| committer | |
| log | 31c49ad64ded02b9dde57f5d3ef102a771fa5cf7 |
| tree | d715ce49093cad93f00dff554b47b3b64a58fdc3 |
| parent | fc185a6f71b9bd611a6d808082999a9da0f107e8 |
| parent | 29314b64bd91be91e8f1d48a1b71eba68569be94 |
| signature |
run AstGen even when using the stage1 backend12 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 |
| 91 | 91 | set(ZIG_EXECUTABLE "" CACHE STRING "(when cross compiling) path to already-built zig binary") |
| 92 | 92 | set(ZIG_SINGLE_THREADED off CACHE BOOL "limit the zig compiler to use only 1 thread") |
| 93 | 93 | set(ZIG_OMIT_STAGE2 off CACHE BOOL "omit the stage2 backend from stage1") |
| 94 | set(ZIG_ENABLE_LOGGING off CACHE BOOL "enable logging") | |
| 94 | ||
| 95 | if("${CMAKE_BUILD_TYPE}" STREQUAL "Debug") | |
| 96 | set(ZIG_ENABLE_LOGGING ON CACHE BOOL "enable logging") | |
| 97 | else() | |
| 98 | set(ZIG_ENABLE_LOGGING OFF CACHE BOOL "enable logging") | |
| 99 | endif() | |
| 95 | 100 | |
| 96 | 101 | if("${ZIG_TARGET_TRIPLE}" STREQUAL "native") |
| 97 | 102 | 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 |
| 59 | 59 | |
| 60 | 60 | make $JOBS install |
| 61 | 61 | |
| 62 | # Look for formatting errors and AST errors. | |
| 62 | # Look for non-conforming code formatting. | |
| 63 | 63 | # Formatting errors can be fixed by running `zig fmt` on the files printed here. |
| 64 | release/bin/zig fmt --check --ast-check .. | |
| 64 | release/bin/zig fmt --check .. | |
| 65 | 65 | |
| 66 | 66 | # Here we rebuild zig but this time using the Zig binary we just now produced to |
| 67 | 67 | # 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" { |
| 965 | 965 | thread2.wait(); |
| 966 | 966 | } |
| 967 | 967 | |
| 968 | fn testTls(context: void) void { | |
| 968 | fn testTls(_: void) void { | |
| 969 | 969 | assert(x == 1234); |
| 970 | 970 | x += 1; |
| 971 | 971 | assert(x == 1235); |
| ... | ... | @@ -2502,6 +2502,8 @@ test "struct namespaced variable" { |
| 2502 | 2502 | |
| 2503 | 2503 | // you can still instantiate an empty struct |
| 2504 | 2504 | const does_nothing = Empty {}; |
| 2505 | ||
| 2506 | _ = does_nothing; | |
| 2505 | 2507 | } |
| 2506 | 2508 | |
| 2507 | 2509 | // struct field order is determined by the compiler for optimal performance. |
| ... | ... | @@ -3026,11 +3028,12 @@ const Foo = enum { a, b, c }; |
| 3026 | 3028 | export fn entry(foo: Foo) void { } |
| 3027 | 3029 | {#code_end#} |
| 3028 | 3030 | <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: | |
| 3030 | 3033 | </p> |
| 3031 | 3034 | {#code_begin|obj#} |
| 3032 | const Foo = extern enum { a, b, c }; | |
| 3033 | export fn entry(foo: Foo) void { } | |
| 3035 | const Foo = enum(c_int) { a, b, c }; | |
| 3036 | export fn entry(foo: Foo) void { _ = foo; } | |
| 3034 | 3037 | {#code_end#} |
| 3035 | 3038 | {#header_close#} |
| 3036 | 3039 | |
| ... | ... | @@ -3392,9 +3395,11 @@ test "inside test block" { |
| 3392 | 3395 | test "separate scopes" { |
| 3393 | 3396 | { |
| 3394 | 3397 | const pi = 3.14; |
| 3398 | _ = pi; | |
| 3395 | 3399 | } |
| 3396 | 3400 | { |
| 3397 | 3401 | var pi: bool = true; |
| 3402 | _ = pi; | |
| 3398 | 3403 | } |
| 3399 | 3404 | } |
| 3400 | 3405 | {#code_end#} |
| ... | ... | @@ -3432,7 +3437,7 @@ test "switch simple" { |
| 3432 | 3437 | // Switching on arbitrary expressions is allowed as long as the |
| 3433 | 3438 | // expression is known at compile-time. |
| 3434 | 3439 | zz => zz, |
| 3435 | comptime blk: { | |
| 3440 | blk: { | |
| 3436 | 3441 | const d: u32 = 5; |
| 3437 | 3442 | const e: u32 = 100; |
| 3438 | 3443 | break :blk d + e; |
| ... | ... | @@ -3831,7 +3836,7 @@ test "for basics" { |
| 3831 | 3836 | // To access the index of iteration, specify a second capture value. |
| 3832 | 3837 | // This is zero-indexed. |
| 3833 | 3838 | var sum2: i32 = 0; |
| 3834 | for (items) |value, i| { | |
| 3839 | for (items) |_, i| { | |
| 3835 | 3840 | try expect(@TypeOf(i) == usize); |
| 3836 | 3841 | sum2 += @intCast(i32, i); |
| 3837 | 3842 | } |
| ... | ... | @@ -3984,7 +3989,7 @@ test "if optional" { |
| 3984 | 3989 | } |
| 3985 | 3990 | |
| 3986 | 3991 | const b: ?u32 = null; |
| 3987 | if (b) |value| { | |
| 3992 | if (b) |_| { | |
| 3988 | 3993 | unreachable; |
| 3989 | 3994 | } else { |
| 3990 | 3995 | try expect(true); |
| ... | ... | @@ -4021,11 +4026,13 @@ test "if error union" { |
| 4021 | 4026 | if (a) |value| { |
| 4022 | 4027 | try expect(value == 0); |
| 4023 | 4028 | } else |err| { |
| 4029 | _ = err; | |
| 4024 | 4030 | unreachable; |
| 4025 | 4031 | } |
| 4026 | 4032 | |
| 4027 | 4033 | const b: anyerror!u32 = error.BadValue; |
| 4028 | 4034 | if (b) |value| { |
| 4035 | _ = value; | |
| 4029 | 4036 | unreachable; |
| 4030 | 4037 | } else |err| { |
| 4031 | 4038 | try expect(err == error.BadValue); |
| ... | ... | @@ -4045,13 +4052,13 @@ test "if error union" { |
| 4045 | 4052 | var c: anyerror!u32 = 3; |
| 4046 | 4053 | if (c) |*value| { |
| 4047 | 4054 | value.* = 9; |
| 4048 | } else |err| { | |
| 4055 | } else |_| { | |
| 4049 | 4056 | unreachable; |
| 4050 | 4057 | } |
| 4051 | 4058 | |
| 4052 | 4059 | if (c) |value| { |
| 4053 | 4060 | try expect(value == 9); |
| 4054 | } else |err| { | |
| 4061 | } else |_| { | |
| 4055 | 4062 | unreachable; |
| 4056 | 4063 | } |
| 4057 | 4064 | } |
| ... | ... | @@ -4064,18 +4071,20 @@ test "if error union with optional" { |
| 4064 | 4071 | if (a) |optional_value| { |
| 4065 | 4072 | try expect(optional_value.? == 0); |
| 4066 | 4073 | } else |err| { |
| 4074 | _ = err; | |
| 4067 | 4075 | unreachable; |
| 4068 | 4076 | } |
| 4069 | 4077 | |
| 4070 | 4078 | const b: anyerror!?u32 = null; |
| 4071 | 4079 | if (b) |optional_value| { |
| 4072 | 4080 | try expect(optional_value == null); |
| 4073 | } else |err| { | |
| 4081 | } else |_| { | |
| 4074 | 4082 | unreachable; |
| 4075 | 4083 | } |
| 4076 | 4084 | |
| 4077 | 4085 | const c: anyerror!?u32 = error.BadValue; |
| 4078 | 4086 | if (c) |optional_value| { |
| 4087 | _ = optional_value; | |
| 4079 | 4088 | unreachable; |
| 4080 | 4089 | } else |err| { |
| 4081 | 4090 | try expect(err == error.BadValue); |
| ... | ... | @@ -4087,13 +4096,13 @@ test "if error union with optional" { |
| 4087 | 4096 | if (optional_value.*) |*value| { |
| 4088 | 4097 | value.* = 9; |
| 4089 | 4098 | } |
| 4090 | } else |err| { | |
| 4099 | } else |_| { | |
| 4091 | 4100 | unreachable; |
| 4092 | 4101 | } |
| 4093 | 4102 | |
| 4094 | 4103 | if (d) |optional_value| { |
| 4095 | 4104 | try expect(optional_value.? == 9); |
| 4096 | } else |err| { | |
| 4105 | } else |_| { | |
| 4097 | 4106 | unreachable; |
| 4098 | 4107 | } |
| 4099 | 4108 | } |
| ... | ... | @@ -4246,6 +4255,7 @@ test "type of unreachable" { |
| 4246 | 4255 | {#code_begin|test#} |
| 4247 | 4256 | fn foo(condition: bool, b: u32) void { |
| 4248 | 4257 | const a = if (condition) b else return; |
| 4258 | _ = a; | |
| 4249 | 4259 | @panic("do something with a"); |
| 4250 | 4260 | } |
| 4251 | 4261 | test "noreturn" { |
| ... | ... | @@ -4574,7 +4584,7 @@ test "parse u64" { |
| 4574 | 4584 | {#code_begin|syntax#} |
| 4575 | 4585 | fn doAThing(str: []u8) void { |
| 4576 | 4586 | const number = parseU64(str, 10) catch 13; |
| 4577 | // ... | |
| 4587 | _ = number; // ... | |
| 4578 | 4588 | } |
| 4579 | 4589 | {#code_end#} |
| 4580 | 4590 | <p> |
| ... | ... | @@ -4589,7 +4599,7 @@ fn doAThing(str: []u8) void { |
| 4589 | 4599 | {#code_begin|syntax#} |
| 4590 | 4600 | fn doAThing(str: []u8) !void { |
| 4591 | 4601 | const number = parseU64(str, 10) catch |err| return err; |
| 4592 | // ... | |
| 4602 | _ = number; // ... | |
| 4593 | 4603 | } |
| 4594 | 4604 | {#code_end#} |
| 4595 | 4605 | <p> |
| ... | ... | @@ -4598,7 +4608,7 @@ fn doAThing(str: []u8) !void { |
| 4598 | 4608 | {#code_begin|syntax#} |
| 4599 | 4609 | fn doAThing(str: []u8) !void { |
| 4600 | 4610 | const number = try parseU64(str, 10); |
| 4601 | // ... | |
| 4611 | _ = number; // ... | |
| 4602 | 4612 | } |
| 4603 | 4613 | {#code_end#} |
| 4604 | 4614 | <p> |
| ... | ... | @@ -5022,7 +5032,7 @@ extern fn malloc(size: size_t) ?*u8; |
| 5022 | 5032 | |
| 5023 | 5033 | fn doAThing() ?*Foo { |
| 5024 | 5034 | const ptr = malloc(1234) orelse return null; |
| 5025 | // ... | |
| 5035 | _ = ptr; // ... | |
| 5026 | 5036 | } |
| 5027 | 5037 | {#code_end#} |
| 5028 | 5038 | <p> |
| ... | ... | @@ -5135,6 +5145,7 @@ test "optional pointers" { |
| 5135 | 5145 | test "type coercion - variable declaration" { |
| 5136 | 5146 | var a: u8 = 1; |
| 5137 | 5147 | var b: u16 = a; |
| 5148 | _ = b; | |
| 5138 | 5149 | } |
| 5139 | 5150 | |
| 5140 | 5151 | test "type coercion - function call" { |
| ... | ... | @@ -5142,11 +5153,14 @@ test "type coercion - function call" { |
| 5142 | 5153 | foo(a); |
| 5143 | 5154 | } |
| 5144 | 5155 | |
| 5145 | fn foo(b: u16) void {} | |
| 5156 | fn foo(b: u16) void { | |
| 5157 | _ = b; | |
| 5158 | } | |
| 5146 | 5159 | |
| 5147 | 5160 | test "type coercion - @as builtin" { |
| 5148 | 5161 | var a: u8 = 1; |
| 5149 | 5162 | var b = @as(u16, a); |
| 5163 | _ = b; | |
| 5150 | 5164 | } |
| 5151 | 5165 | {#code_end#} |
| 5152 | 5166 | <p> |
| ... | ... | @@ -5174,7 +5188,7 @@ test "type coercion - const qualification" { |
| 5174 | 5188 | foo(b); |
| 5175 | 5189 | } |
| 5176 | 5190 | |
| 5177 | fn foo(a: *const i32) void {} | |
| 5191 | fn foo(_: *const i32) void {} | |
| 5178 | 5192 | {#code_end#} |
| 5179 | 5193 | <p> |
| 5180 | 5194 | In addition, pointers coerce to const optional pointers: |
| ... | ... | @@ -5424,7 +5438,7 @@ test "coercion between unions and enums" { |
| 5424 | 5438 | test "coercion of zero bit types" { |
| 5425 | 5439 | var x: void = {}; |
| 5426 | 5440 | var y: *void = x; |
| 5427 | //var z: void = y; // TODO | |
| 5441 | _ = y; | |
| 5428 | 5442 | } |
| 5429 | 5443 | {#code_end#} |
| 5430 | 5444 | {#header_close#} |
| ... | ... | @@ -6569,6 +6583,7 @@ var x: i32 = 1; |
| 6569 | 6583 | test "suspend with no resume" { |
| 6570 | 6584 | var frame = async func(); |
| 6571 | 6585 | try expect(x == 2); |
| 6586 | _ = frame; | |
| 6572 | 6587 | } |
| 6573 | 6588 | |
| 6574 | 6589 | fn func() void { |
| ... | ... | @@ -6800,6 +6815,7 @@ fn amain() !void { |
| 6800 | 6815 | |
| 6801 | 6816 | var global_download_frame: anyframe = undefined; |
| 6802 | 6817 | fn fetchUrl(allocator: *Allocator, url: []const u8) ![]u8 { |
| 6818 | _ = url; // this is just an example, we don't actually do it! | |
| 6803 | 6819 | const result = try std.mem.dupe(allocator, u8, "this is the downloaded url contents"); |
| 6804 | 6820 | errdefer allocator.free(result); |
| 6805 | 6821 | suspend { |
| ... | ... | @@ -6811,6 +6827,7 @@ fn fetchUrl(allocator: *Allocator, url: []const u8) ![]u8 { |
| 6811 | 6827 | |
| 6812 | 6828 | var global_file_frame: anyframe = undefined; |
| 6813 | 6829 | fn readFile(allocator: *Allocator, filename: []const u8) ![]u8 { |
| 6830 | _ = filename; // this is just an example, we don't actually do it! | |
| 6814 | 6831 | const result = try std.mem.dupe(allocator, u8, "this is the file contents"); |
| 6815 | 6832 | errdefer allocator.free(result); |
| 6816 | 6833 | suspend { |
| ... | ... | @@ -6869,6 +6886,7 @@ fn amain() !void { |
| 6869 | 6886 | } |
| 6870 | 6887 | |
| 6871 | 6888 | fn fetchUrl(allocator: *Allocator, url: []const u8) ![]u8 { |
| 6889 | _ = url; // this is just an example, we don't actually do it! | |
| 6872 | 6890 | const result = try std.mem.dupe(allocator, u8, "this is the downloaded url contents"); |
| 6873 | 6891 | errdefer allocator.free(result); |
| 6874 | 6892 | std.debug.print("fetchUrl returning\n", .{}); |
| ... | ... | @@ -6876,6 +6894,7 @@ fn fetchUrl(allocator: *Allocator, url: []const u8) ![]u8 { |
| 6876 | 6894 | } |
| 6877 | 6895 | |
| 6878 | 6896 | fn readFile(allocator: *Allocator, filename: []const u8) ![]u8 { |
| 6897 | _ = filename; // this is just an example, we don't actually do it! | |
| 6879 | 6898 | const result = try std.mem.dupe(allocator, u8, "this is the file contents"); |
| 6880 | 6899 | errdefer allocator.free(result); |
| 6881 | 6900 | std.debug.print("readFile returning\n", .{}); |
| ... | ... | @@ -8584,6 +8603,7 @@ fn List(comptime T: type) type { |
| 8584 | 8603 | test "integer cast panic" { |
| 8585 | 8604 | var a: u16 = 0xabcd; |
| 8586 | 8605 | var b: u8 = @intCast(u8, a); |
| 8606 | _ = b; | |
| 8587 | 8607 | } |
| 8588 | 8608 | {#code_end#} |
| 8589 | 8609 | <p> |
| ... | ... | @@ -8839,6 +8859,7 @@ comptime { |
| 8839 | 8859 | {#code_begin|exe_err#} |
| 8840 | 8860 | pub fn main() void { |
| 8841 | 8861 | var x = foo("hello"); |
| 8862 | _ = x; | |
| 8842 | 8863 | } |
| 8843 | 8864 | |
| 8844 | 8865 | fn foo(x: []const u8) u8 { |
| ... | ... | @@ -9107,6 +9128,7 @@ pub fn main() void { |
| 9107 | 9128 | comptime { |
| 9108 | 9129 | const optional_number: ?i32 = null; |
| 9109 | 9130 | const number = optional_number.?; |
| 9131 | _ = number; | |
| 9110 | 9132 | } |
| 9111 | 9133 | {#code_end#} |
| 9112 | 9134 | <p>At runtime:</p> |
| ... | ... | @@ -9140,6 +9162,7 @@ pub fn main() void { |
| 9140 | 9162 | {#code_begin|test_err|caught unexpected error 'UnableToReturnNumber'#} |
| 9141 | 9163 | comptime { |
| 9142 | 9164 | const number = getNumberOrFail() catch unreachable; |
| 9165 | _ = number; | |
| 9143 | 9166 | } |
| 9144 | 9167 | |
| 9145 | 9168 | fn getNumberOrFail() !i32 { |
| ... | ... | @@ -9187,6 +9210,7 @@ comptime { |
| 9187 | 9210 | const err = error.AnError; |
| 9188 | 9211 | const number = @errorToInt(err) + 10; |
| 9189 | 9212 | const invalid_err = @intToError(number); |
| 9213 | _ = invalid_err; | |
| 9190 | 9214 | } |
| 9191 | 9215 | {#code_end#} |
| 9192 | 9216 | <p>At runtime:</p> |
| ... | ... | @@ -9197,7 +9221,7 @@ pub fn main() void { |
| 9197 | 9221 | var err = error.AnError; |
| 9198 | 9222 | var number = @errorToInt(err) + 500; |
| 9199 | 9223 | var invalid_err = @intToError(number); |
| 9200 | std.debug.print("value: {}\n", .{number}); | |
| 9224 | std.debug.print("value: {}\n", .{invalid_err}); | |
| 9201 | 9225 | } |
| 9202 | 9226 | {#code_end#} |
| 9203 | 9227 | {#header_close#} |
| ... | ... | @@ -9212,6 +9236,7 @@ const Foo = enum { |
| 9212 | 9236 | comptime { |
| 9213 | 9237 | const a: u2 = 3; |
| 9214 | 9238 | const b = @intToEnum(Foo, a); |
| 9239 | _ = b; | |
| 9215 | 9240 | } |
| 9216 | 9241 | {#code_end#} |
| 9217 | 9242 | <p>At runtime:</p> |
| ... | ... | @@ -9396,6 +9421,7 @@ comptime { |
| 9396 | 9421 | pub fn main() void { |
| 9397 | 9422 | var opt_ptr: ?*i32 = null; |
| 9398 | 9423 | var ptr = @ptrCast(*i32, opt_ptr); |
| 9424 | _ = ptr; | |
| 9399 | 9425 | } |
| 9400 | 9426 | {#code_end#} |
| 9401 | 9427 | {#header_close#} |
| ... | ... | @@ -9523,7 +9549,9 @@ pub fn main() !void { |
| 9523 | 9549 | This is why it is an error to pass a string literal to a mutable slice, like this: |
| 9524 | 9550 | </p> |
| 9525 | 9551 | {#code_begin|test_err|expected type '[]u8'#} |
| 9526 | fn foo(s: []u8) void {} | |
| 9552 | fn foo(s: []u8) void { | |
| 9553 | _ = s; | |
| 9554 | } | |
| 9527 | 9555 | |
| 9528 | 9556 | test "string literal to mutable slice" { |
| 9529 | 9557 | foo("hello"); |
| ... | ... | @@ -9531,7 +9559,9 @@ test "string literal to mutable slice" { |
| 9531 | 9559 | {#code_end#} |
| 9532 | 9560 | <p>However if you make the slice constant, then it works:</p> |
| 9533 | 9561 | {#code_begin|test|strlit#} |
| 9534 | fn foo(s: []const u8) void {} | |
| 9562 | fn foo(s: []const u8) void { | |
| 9563 | _ = s; | |
| 9564 | } | |
| 9535 | 9565 | |
| 9536 | 9566 | test "string literal to constant slice" { |
| 9537 | 9567 | foo("hello"); |
| ... | ... | @@ -10476,7 +10506,7 @@ coding style. |
| 10476 | 10506 | </p> |
| 10477 | 10507 | {#header_close#} |
| 10478 | 10508 | {#header_open|Examples#} |
| 10479 | {#code_begin|syntax#} | |
| 10509 | <pre>{#syntax#} | |
| 10480 | 10510 | const namespace_name = @import("dir_name/file_name.zig"); |
| 10481 | 10511 | const TypeName = @import("dir_name/TypeName.zig"); |
| 10482 | 10512 | var global_var: i32 = undefined; |
| ... | ... | @@ -10520,7 +10550,7 @@ const XmlParser = struct { |
| 10520 | 10550 | |
| 10521 | 10551 | // The initials BE (Big Endian) are just another word in Zig identifier names. |
| 10522 | 10552 | fn readU32Be() u32 {} |
| 10523 | {#code_end#} | |
| 10553 | {#endsyntax#}</pre> | |
| 10524 | 10554 | <p> |
| 10525 | 10555 | See the Zig Standard Library for more examples. |
| 10526 | 10556 | </p> |
src/AstGen.zig+4| ... | ... | @@ -3558,6 +3558,10 @@ fn structDeclInner( |
| 3558 | 3558 | const field_name = try astgen.identAsString(member.ast.name_token); |
| 3559 | 3559 | fields_data.appendAssumeCapacity(field_name); |
| 3560 | 3560 | |
| 3561 | if (member.ast.type_expr == 0) { | |
| 3562 | return astgen.failTok(member.ast.name_token, "struct field missing type", .{}); | |
| 3563 | } | |
| 3564 | ||
| 3561 | 3565 | const field_type: Zir.Inst.Ref = if (node_tags[member.ast.type_expr] == .@"anytype") |
| 3562 | 3566 | .none |
| 3563 | 3567 | else |
src/Compilation.zig+89-59| ... | ... | @@ -342,6 +342,7 @@ pub const AllErrors = struct { |
| 342 | 342 | const stderr = stderr_file.writer(); |
| 343 | 343 | switch (msg) { |
| 344 | 344 | .src => |src| { |
| 345 | try stderr.writeByteNTimes(' ', indent); | |
| 345 | 346 | ttyconf.setColor(stderr, .Bold); |
| 346 | 347 | try stderr.print("{s}:{d}:{d}: ", .{ |
| 347 | 348 | src.src_path, |
| ... | ... | @@ -349,7 +350,6 @@ pub const AllErrors = struct { |
| 349 | 350 | src.column + 1, |
| 350 | 351 | }); |
| 351 | 352 | ttyconf.setColor(stderr, color); |
| 352 | try stderr.writeByteNTimes(' ', indent); | |
| 353 | 353 | try stderr.writeAll(kind); |
| 354 | 354 | ttyconf.setColor(stderr, .Reset); |
| 355 | 355 | ttyconf.setColor(stderr, .Bold); |
| ... | ... | @@ -731,6 +731,7 @@ fn addPackageTableToCacheHash( |
| 731 | 731 | hash: *Cache.HashHelper, |
| 732 | 732 | arena: *std.heap.ArenaAllocator, |
| 733 | 733 | pkg_table: Package.Table, |
| 734 | seen_table: *std.AutoHashMap(*Package, void), | |
| 734 | 735 | hash_type: union(enum) { path_bytes, files: *Cache.Manifest }, |
| 735 | 736 | ) (error{OutOfMemory} || std.os.GetCwdError)!void { |
| 736 | 737 | const allocator = &arena.allocator; |
| ... | ... | @@ -755,6 +756,8 @@ fn addPackageTableToCacheHash( |
| 755 | 756 | }.lessThan); |
| 756 | 757 | |
| 757 | 758 | for (packages) |pkg| { |
| 759 | if ((try seen_table.getOrPut(pkg.value)).found_existing) continue; | |
| 760 | ||
| 758 | 761 | // Finally insert the package name and path to the cache hash. |
| 759 | 762 | hash.addBytes(pkg.key); |
| 760 | 763 | switch (hash_type) { |
| ... | ... | @@ -770,7 +773,7 @@ fn addPackageTableToCacheHash( |
| 770 | 773 | }, |
| 771 | 774 | } |
| 772 | 775 | // 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); | |
| 774 | 777 | } |
| 775 | 778 | } |
| 776 | 779 | |
| ... | ... | @@ -1116,7 +1119,8 @@ pub fn create(gpa: *Allocator, options: InitOptions) !*Compilation { |
| 1116 | 1119 | { |
| 1117 | 1120 | var local_arena = std.heap.ArenaAllocator.init(gpa); |
| 1118 | 1121 | 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); | |
| 1120 | 1124 | } |
| 1121 | 1125 | hash.add(valgrind); |
| 1122 | 1126 | hash.add(single_threaded); |
| ... | ... | @@ -1137,36 +1141,32 @@ pub fn create(gpa: *Allocator, options: InitOptions) !*Compilation { |
| 1137 | 1141 | artifact_sub_dir, |
| 1138 | 1142 | }; |
| 1139 | 1143 | |
| 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); | |
| 1150 | 1151 | |
| 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); | |
| 1158 | 1159 | |
| 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); | |
| 1162 | 1163 | |
| 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); | |
| 1166 | 1167 | |
| 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); | |
| 1170 | 1170 | |
| 1171 | 1171 | // Pre-open the directory handles for cached ZIR code so that it does not need |
| 1172 | 1172 | // to redundantly happen for each AstGen operation. |
| ... | ... | @@ -1625,30 +1625,39 @@ pub fn update(self: *Compilation) !void { |
| 1625 | 1625 | // Add a Job for each C object. |
| 1626 | 1626 | try self.c_object_work_queue.ensureUnusedCapacity(self.c_object_table.count()); |
| 1627 | 1627 | for (self.c_object_table.keys()) |key| { |
| 1628 | assert(@ptrToInt(key) != 0xaaaa_aaaa_aaaa_aaaa); | |
| 1629 | 1628 | self.c_object_work_queue.writeItemAssumeCapacity(key); |
| 1630 | 1629 | } |
| 1631 | 1630 | |
| 1632 | 1631 | const use_stage1 = build_options.omit_stage2 or |
| 1633 | 1632 | (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 | } | |
| 1651 | 1659 | |
| 1660 | if (!use_stage1) { | |
| 1652 | 1661 | try self.work_queue.writeItem(.{ .analyze_pkg = std_pkg }); |
| 1653 | 1662 | } |
| 1654 | 1663 | } |
| ... | ... | @@ -1915,7 +1924,7 @@ pub fn performAllTheWork(self: *Compilation) error{ TimerUnsupported, OutOfMemor |
| 1915 | 1924 | // (at least for now) single-threaded main work queue. However, C object compilation |
| 1916 | 1925 | // only needs to be finished by the end of this function. |
| 1917 | 1926 | |
| 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); | |
| 1919 | 1928 | defer zir_prog_node.end(); |
| 1920 | 1929 | |
| 1921 | 1930 | 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 |
| 1936 | 1945 | } |
| 1937 | 1946 | |
| 1938 | 1947 | while (self.c_object_work_queue.readItem()) |c_object| { |
| 1939 | assert(@ptrToInt(c_object) != 0xaaaa_aaaa_aaaa_aaaa); | |
| 1940 | 1948 | self.work_queue_wait_group.start(); |
| 1941 | 1949 | try self.thread_pool.spawn(workerUpdateCObject, .{ |
| 1942 | 1950 | self, c_object, &c_obj_prog_node, &self.work_queue_wait_group, |
| ... | ... | @@ -1944,9 +1952,13 @@ pub fn performAllTheWork(self: *Compilation) error{ TimerUnsupported, OutOfMemor |
| 1944 | 1952 | } |
| 1945 | 1953 | } |
| 1946 | 1954 | |
| 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 | } | |
| 1950 | 1962 | } |
| 1951 | 1963 | |
| 1952 | 1964 | while (self.work_queue.readItem()) |work_item| switch (work_item) { |
| ... | ... | @@ -2319,6 +2331,9 @@ fn workerAstGenFile( |
| 2319 | 2331 | break :blk mod.importFile(file, import_path) catch continue; |
| 2320 | 2332 | }; |
| 2321 | 2333 | 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 | }); | |
| 2322 | 2337 | wg.start(); |
| 2323 | 2338 | comp.thread_pool.spawn(workerAstGenFile, .{ |
| 2324 | 2339 | comp, import_result.file, prog_node, wg, |
| ... | ... | @@ -2540,13 +2555,23 @@ fn reportRetryableAstGenError( |
| 2540 | 2555 | |
| 2541 | 2556 | file.status = .retryable_failure; |
| 2542 | 2557 | |
| 2543 | const err_msg = try Module.ErrorMsg.create(gpa, .{ | |
| 2558 | const src_loc: Module.SrcLoc = .{ | |
| 2544 | 2559 | .file_scope = file, |
| 2545 | 2560 | .parent_decl_node = 0, |
| 2546 | 2561 | .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 | }); | |
| 2550 | 2575 | errdefer err_msg.destroy(gpa); |
| 2551 | 2576 | |
| 2552 | 2577 | { |
| ... | ... | @@ -3830,9 +3855,8 @@ fn updateStage1Module(comp: *Compilation, main_progress_node: *std.Progress.Node |
| 3830 | 3855 | |
| 3831 | 3856 | _ = try man.addFile(main_zig_file, null); |
| 3832 | 3857 | { |
| 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 }); | |
| 3836 | 3860 | } |
| 3837 | 3861 | man.hash.add(comp.bin_file.options.valgrind); |
| 3838 | 3862 | man.hash.add(comp.bin_file.options.single_threaded); |
| ... | ... | @@ -4103,6 +4127,12 @@ fn createStage1Pkg( |
| 4103 | 4127 | var children = std.ArrayList(*stage1.Pkg).init(arena); |
| 4104 | 4128 | var it = pkg.table.iterator(); |
| 4105 | 4129 | 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 | } | |
| 4106 | 4136 | try children.append(try createStage1Pkg(arena, entry.key_ptr.*, entry.value_ptr.*, child_pkg)); |
| 4107 | 4137 | } |
| 4108 | 4138 | break :blk children.items; |
src/Module.zig+3| ... | ... | @@ -3185,6 +3185,9 @@ pub fn importFile( |
| 3185 | 3185 | if (cur_file.pkg.table.get(import_string)) |pkg| { |
| 3186 | 3186 | return mod.importPkg(pkg); |
| 3187 | 3187 | } |
| 3188 | if (!mem.endsWith(u8, import_string, ".zig")) { | |
| 3189 | return error.PackageNotFound; | |
| 3190 | } | |
| 3188 | 3191 | const gpa = mod.gpa; |
| 3189 | 3192 | |
| 3190 | 3193 | // 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; |
| 4 | 4 | const assert = std.debug.assert; |
| 5 | 5 | const ArrayList = std.ArrayList; |
| 6 | 6 | const Allocator = std.mem.Allocator; |
| 7 | const Type = @import("../Type.zig"); | |
| 8 | 7 | const DW = std.dwarf; |
| 9 | 8 | |
| 10 | 9 | // zig fmt: off |
src/translate_c.zig+20-4| ... | ... | @@ -151,6 +151,12 @@ const Scope = struct { |
| 151 | 151 | return true; |
| 152 | 152 | return scope.base.parent.?.contains(name); |
| 153 | 153 | } |
| 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 | } | |
| 154 | 160 | }; |
| 155 | 161 | |
| 156 | 162 | const Root = struct { |
| ... | ... | @@ -625,6 +631,7 @@ fn visitFnDecl(c: *Context, fn_decl: *const clang.FunctionDecl) Error!void { |
| 625 | 631 | const redecl_node = try Tag.arg_redecl.create(c.arena, .{ .actual = mangled_param_name, .mangled = arg_name }); |
| 626 | 632 | try block_scope.statements.append(redecl_node); |
| 627 | 633 | } |
| 634 | try block_scope.discardVariable(c, mangled_param_name); | |
| 628 | 635 | |
| 629 | 636 | param_id += 1; |
| 630 | 637 | } |
| ... | ... | @@ -827,6 +834,7 @@ fn transTypeDef(c: *Context, scope: *Scope, typedef_decl: *const clang.TypedefNa |
| 827 | 834 | try addTopLevelDecl(c, name, node); |
| 828 | 835 | } else { |
| 829 | 836 | try scope.appendNode(node); |
| 837 | try bs.discardVariable(c, name); | |
| 830 | 838 | } |
| 831 | 839 | } |
| 832 | 840 | |
| ... | ... | @@ -1077,6 +1085,7 @@ fn transRecordDecl(c: *Context, scope: *Scope, record_decl: *const clang.RecordD |
| 1077 | 1085 | try c.alias_list.append(.{ .alias = bare_name, .name = name }); |
| 1078 | 1086 | } else { |
| 1079 | 1087 | try scope.appendNode(Node.initPayload(&payload.base)); |
| 1088 | try bs.discardVariable(c, name); | |
| 1080 | 1089 | } |
| 1081 | 1090 | } |
| 1082 | 1091 | |
| ... | ... | @@ -1128,8 +1137,10 @@ fn transEnumDecl(c: *Context, scope: *Scope, enum_decl: *const clang.EnumDecl) E |
| 1128 | 1137 | }); |
| 1129 | 1138 | if (toplevel) |
| 1130 | 1139 | try addTopLevelDecl(c, enum_val_name, enum_const_def) |
| 1131 | else | |
| 1140 | else { | |
| 1132 | 1141 | try scope.appendNode(enum_const_def); |
| 1142 | try bs.discardVariable(c, enum_val_name); | |
| 1143 | } | |
| 1133 | 1144 | } |
| 1134 | 1145 | |
| 1135 | 1146 | const int_type = enum_decl.getIntegerType(); |
| ... | ... | @@ -1168,6 +1179,7 @@ fn transEnumDecl(c: *Context, scope: *Scope, enum_decl: *const clang.EnumDecl) E |
| 1168 | 1179 | try c.alias_list.append(.{ .alias = bare_name, .name = name }); |
| 1169 | 1180 | } else { |
| 1170 | 1181 | try scope.appendNode(Node.initPayload(&payload.base)); |
| 1182 | try bs.discardVariable(c, name); | |
| 1171 | 1183 | } |
| 1172 | 1184 | } |
| 1173 | 1185 | |
| ... | ... | @@ -1352,11 +1364,10 @@ fn makeShuffleMask(c: *Context, scope: *Scope, expr: *const clang.ShuffleVectorE |
| 1352 | 1364 | init.* = converted_index; |
| 1353 | 1365 | } |
| 1354 | 1366 | |
| 1355 | const mask_init = try Tag.array_init.create(c.arena, .{ | |
| 1367 | return Tag.array_init.create(c.arena, .{ | |
| 1356 | 1368 | .cond = mask_type, |
| 1357 | 1369 | .cases = init_list, |
| 1358 | 1370 | }); |
| 1359 | return Tag.@"comptime".create(c.arena, mask_init); | |
| 1360 | 1371 | } |
| 1361 | 1372 | |
| 1362 | 1373 | /// @typeInfo(@TypeOf(vec_node)).Vector.<field> |
| ... | ... | @@ -1766,6 +1777,7 @@ fn transDeclStmtOne( |
| 1766 | 1777 | node = try Tag.static_local_var.create(c.arena, .{ .name = mangled_name, .init = node }); |
| 1767 | 1778 | } |
| 1768 | 1779 | try block_scope.statements.append(node); |
| 1780 | try block_scope.discardVariable(c, mangled_name); | |
| 1769 | 1781 | |
| 1770 | 1782 | const cleanup_attr = var_decl.getCleanupAttribute(); |
| 1771 | 1783 | if (cleanup_attr) |fn_decl| { |
| ... | ... | @@ -4903,6 +4915,10 @@ fn transMacroDefine(c: *Context, m: *MacroCtx) ParseError!void { |
| 4903 | 4915 | const scope = &c.global_scope.base; |
| 4904 | 4916 | |
| 4905 | 4917 | 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 | } | |
| 4906 | 4922 | const last = m.next().?; |
| 4907 | 4923 | if (last != .Eof and last != .Nl) |
| 4908 | 4924 | 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 { |
| 4933 | 4949 | .name = mangled_name, |
| 4934 | 4950 | .type = Tag.@"anytype".init(), |
| 4935 | 4951 | }); |
| 4936 | ||
| 4952 | try block_scope.discardVariable(c, mangled_name); | |
| 4937 | 4953 | if (m.peek().? != .Comma) break; |
| 4938 | 4954 | _ = m.next(); |
| 4939 | 4955 | } |
test/cli.zig+2| ... | ... | @@ -116,6 +116,8 @@ fn testGodboltApi(zig_exe: []const u8, dir_path: []const u8) anyerror!void { |
| 116 | 116 | \\} |
| 117 | 117 | \\extern fn zig_panic() noreturn; |
| 118 | 118 | \\pub fn panic(msg: []const u8, error_return_trace: ?*@import("std").builtin.StackTrace) noreturn { |
| 119 | \\ _ = msg; | |
| 120 | \\ _ = error_return_trace; | |
| 119 | 121 | \\ zig_panic(); |
| 120 | 122 | \\} |
| 121 | 123 | ); |
test/compare_output.zig+16-2| ... | ... | @@ -10,6 +10,8 @@ pub fn addCases(cases: *tests.CompareOutputContext) void { |
| 10 | 10 | \\ @cInclude("stdio.h"); |
| 11 | 11 | \\}); |
| 12 | 12 | \\pub export fn main(argc: c_int, argv: [*][*]u8) c_int { |
| 13 | \\ _ = argc; | |
| 14 | \\ _ = argv; | |
| 13 | 15 | \\ _ = c.puts("Hello, world!"); |
| 14 | 16 | \\ return 0; |
| 15 | 17 | \\} |
| ... | ... | @@ -143,6 +145,8 @@ pub fn addCases(cases: *tests.CompareOutputContext) void { |
| 143 | 145 | \\}); |
| 144 | 146 | \\ |
| 145 | 147 | \\pub export fn main(argc: c_int, argv: [*][*]u8) c_int { |
| 148 | \\ _ = argc; | |
| 149 | \\ _ = argv; | |
| 146 | 150 | \\ if (is_windows) { |
| 147 | 151 | \\ // we want actual \n, not \r\n |
| 148 | 152 | \\ _ = c._setmode(1, c._O_BINARY); |
| ... | ... | @@ -265,8 +269,10 @@ pub fn addCases(cases: *tests.CompareOutputContext) void { |
| 265 | 269 | \\const y : u16 = 5678; |
| 266 | 270 | \\pub fn main() void { |
| 267 | 271 | \\ var x_local : i32 = print_ok(x); |
| 272 | \\ _ = x_local; | |
| 268 | 273 | \\} |
| 269 | 274 | \\fn print_ok(val: @TypeOf(x)) @TypeOf(foo) { |
| 275 | \\ _ = val; | |
| 270 | 276 | \\ const stdout = io.getStdOut().writer(); |
| 271 | 277 | \\ stdout.print("OK\n", .{}) catch unreachable; |
| 272 | 278 | \\ return 0; |
| ... | ... | @@ -318,6 +324,8 @@ pub fn addCases(cases: *tests.CompareOutputContext) void { |
| 318 | 324 | \\}); |
| 319 | 325 | \\ |
| 320 | 326 | \\pub export fn main(argc: c_int, argv: [*][*]u8) c_int { |
| 327 | \\ _ = argc; | |
| 328 | \\ _ = argv; | |
| 321 | 329 | \\ if (is_windows) { |
| 322 | 330 | \\ // we want actual \n, not \r\n |
| 323 | 331 | \\ _ = c._setmode(1, c._O_BINARY); |
| ... | ... | @@ -337,13 +345,19 @@ pub fn addCases(cases: *tests.CompareOutputContext) void { |
| 337 | 345 | \\const Foo = struct { |
| 338 | 346 | \\ field1: Bar, |
| 339 | 347 | \\ |
| 340 | \\ fn method(a: *const Foo) bool { return true; } | |
| 348 | \\ fn method(a: *const Foo) bool { | |
| 349 | \\ _ = a; | |
| 350 | \\ return true; | |
| 351 | \\ } | |
| 341 | 352 | \\}; |
| 342 | 353 | \\ |
| 343 | 354 | \\const Bar = struct { |
| 344 | 355 | \\ field2: i32, |
| 345 | 356 | \\ |
| 346 | \\ fn method(b: *const Bar) bool { return true; } | |
| 357 | \\ fn method(b: *const Bar) bool { | |
| 358 | \\ _ = b; | |
| 359 | \\ return true; | |
| 360 | \\ } | |
| 347 | 361 | \\}; |
| 348 | 362 | \\ |
| 349 | 363 | \\pub fn main() void { |
test/runtime_safety.zig+160-5| ... | ... | @@ -4,6 +4,7 @@ pub fn addCases(cases: *tests.CompareOutputContext) void { |
| 4 | 4 | { |
| 5 | 5 | const check_panic_msg = |
| 6 | 6 | \\pub fn panic(message: []const u8, stack_trace: ?*std.builtin.StackTrace) noreturn { |
| 7 | \\ _ = stack_trace; | |
| 7 | 8 | \\ if (std.mem.eql(u8, message, "reached unreachable code")) { |
| 8 | 9 | \\ std.process.exit(126); // good |
| 9 | 10 | \\ } |
| ... | ... | @@ -45,6 +46,7 @@ pub fn addCases(cases: *tests.CompareOutputContext) void { |
| 45 | 46 | { |
| 46 | 47 | const check_panic_msg = |
| 47 | 48 | \\pub fn panic(message: []const u8, stack_trace: ?*std.builtin.StackTrace) noreturn { |
| 49 | \\ _ = stack_trace; | |
| 48 | 50 | \\ if (std.mem.eql(u8, message, "invalid enum value")) { |
| 49 | 51 | \\ std.process.exit(126); // good |
| 50 | 52 | \\ } |
| ... | ... | @@ -62,6 +64,7 @@ pub fn addCases(cases: *tests.CompareOutputContext) void { |
| 62 | 64 | \\ var e: E = undefined; |
| 63 | 65 | \\ @memset(@ptrCast([*]u8, &e), 0x55, @sizeOf(E)); |
| 64 | 66 | \\ var n = @tagName(e); |
| 67 | \\ _ = n; | |
| 65 | 68 | \\} |
| 66 | 69 | ); |
| 67 | 70 | |
| ... | ... | @@ -76,6 +79,7 @@ pub fn addCases(cases: *tests.CompareOutputContext) void { |
| 76 | 79 | \\ @memset(@ptrCast([*]u8, &u), 0x55, @sizeOf(U)); |
| 77 | 80 | \\ var t: @typeInfo(U).Union.tag_type.? = u; |
| 78 | 81 | \\ var n = @tagName(t); |
| 82 | \\ _ = n; | |
| 79 | 83 | \\} |
| 80 | 84 | ); |
| 81 | 85 | } |
| ... | ... | @@ -83,6 +87,7 @@ pub fn addCases(cases: *tests.CompareOutputContext) void { |
| 83 | 87 | { |
| 84 | 88 | const check_panic_msg = |
| 85 | 89 | \\pub fn panic(message: []const u8, stack_trace: ?*std.builtin.StackTrace) noreturn { |
| 90 | \\ _ = stack_trace; | |
| 86 | 91 | \\ if (std.mem.eql(u8, message, "index out of bounds")) { |
| 87 | 92 | \\ std.process.exit(126); // good |
| 88 | 93 | \\ } |
| ... | ... | @@ -96,6 +101,7 @@ pub fn addCases(cases: *tests.CompareOutputContext) void { |
| 96 | 101 | \\pub fn main() void { |
| 97 | 102 | \\ var buf = [4]u8{'a','b','c',0}; |
| 98 | 103 | \\ const slice = buf[0..4 :0]; |
| 104 | \\ _ = slice; | |
| 99 | 105 | \\} |
| 100 | 106 | ); |
| 101 | 107 | cases.addRuntimeSafety("slicing operator with sentinel", |
| ... | ... | @@ -104,6 +110,7 @@ pub fn addCases(cases: *tests.CompareOutputContext) void { |
| 104 | 110 | \\pub fn main() void { |
| 105 | 111 | \\ var buf = [4]u8{'a','b','c',0}; |
| 106 | 112 | \\ const slice = buf[0..:0]; |
| 113 | \\ _ = slice; | |
| 107 | 114 | \\} |
| 108 | 115 | ); |
| 109 | 116 | cases.addRuntimeSafety("slicing operator with sentinel", |
| ... | ... | @@ -112,6 +119,7 @@ pub fn addCases(cases: *tests.CompareOutputContext) void { |
| 112 | 119 | \\pub fn main() void { |
| 113 | 120 | \\ var buf_zero = [0]u8{}; |
| 114 | 121 | \\ const slice = buf_zero[0..0 :0]; |
| 122 | \\ _ = slice; | |
| 115 | 123 | \\} |
| 116 | 124 | ); |
| 117 | 125 | cases.addRuntimeSafety("slicing operator with sentinel", |
| ... | ... | @@ -120,6 +128,7 @@ pub fn addCases(cases: *tests.CompareOutputContext) void { |
| 120 | 128 | \\pub fn main() void { |
| 121 | 129 | \\ var buf_zero = [0]u8{}; |
| 122 | 130 | \\ const slice = buf_zero[0..:0]; |
| 131 | \\ _ = slice; | |
| 123 | 132 | \\} |
| 124 | 133 | ); |
| 125 | 134 | cases.addRuntimeSafety("slicing operator with sentinel", |
| ... | ... | @@ -129,6 +138,7 @@ pub fn addCases(cases: *tests.CompareOutputContext) void { |
| 129 | 138 | \\ var buf_sentinel = [2:0]u8{'a','b'}; |
| 130 | 139 | \\ @ptrCast(*[3]u8, &buf_sentinel)[2] = 0; |
| 131 | 140 | \\ const slice = buf_sentinel[0..3 :0]; |
| 141 | \\ _ = slice; | |
| 132 | 142 | \\} |
| 133 | 143 | ); |
| 134 | 144 | cases.addRuntimeSafety("slicing operator with sentinel", |
| ... | ... | @@ -137,6 +147,7 @@ pub fn addCases(cases: *tests.CompareOutputContext) void { |
| 137 | 147 | \\pub fn main() void { |
| 138 | 148 | \\ var buf_slice: []const u8 = &[3]u8{ 'a', 'b', 0 }; |
| 139 | 149 | \\ const slice = buf_slice[0..3 :0]; |
| 150 | \\ _ = slice; | |
| 140 | 151 | \\} |
| 141 | 152 | ); |
| 142 | 153 | cases.addRuntimeSafety("slicing operator with sentinel", |
| ... | ... | @@ -145,6 +156,7 @@ pub fn addCases(cases: *tests.CompareOutputContext) void { |
| 145 | 156 | \\pub fn main() void { |
| 146 | 157 | \\ var buf_slice: []const u8 = &[3]u8{ 'a', 'b', 0 }; |
| 147 | 158 | \\ const slice = buf_slice[0.. :0]; |
| 159 | \\ _ = slice; | |
| 148 | 160 | \\} |
| 149 | 161 | ); |
| 150 | 162 | } |
| ... | ... | @@ -153,6 +165,7 @@ pub fn addCases(cases: *tests.CompareOutputContext) void { |
| 153 | 165 | \\const std = @import("std"); |
| 154 | 166 | \\const V = @import("std").meta.Vector; |
| 155 | 167 | \\pub fn panic(message: []const u8, stack_trace: ?*std.builtin.StackTrace) noreturn { |
| 168 | \\ _ = stack_trace; | |
| 156 | 169 | \\ if (std.mem.eql(u8, message, "integer cast truncated bits")) { |
| 157 | 170 | \\ std.process.exit(126); // good |
| 158 | 171 | \\ } |
| ... | ... | @@ -161,6 +174,7 @@ pub fn addCases(cases: *tests.CompareOutputContext) void { |
| 161 | 174 | \\pub fn main() void { |
| 162 | 175 | \\ var x = @splat(4, @as(u32, 0xdeadbeef)); |
| 163 | 176 | \\ var y = @intCast(V(4, u16), x); |
| 177 | \\ _ = y; | |
| 164 | 178 | \\} |
| 165 | 179 | ); |
| 166 | 180 | |
| ... | ... | @@ -168,6 +182,7 @@ pub fn addCases(cases: *tests.CompareOutputContext) void { |
| 168 | 182 | \\const std = @import("std"); |
| 169 | 183 | \\const V = @import("std").meta.Vector; |
| 170 | 184 | \\pub fn panic(message: []const u8, stack_trace: ?*std.builtin.StackTrace) noreturn { |
| 185 | \\ _ = stack_trace; | |
| 171 | 186 | \\ if (std.mem.eql(u8, message, "integer cast truncated bits")) { |
| 172 | 187 | \\ std.process.exit(126); // good |
| 173 | 188 | \\ } |
| ... | ... | @@ -176,6 +191,7 @@ pub fn addCases(cases: *tests.CompareOutputContext) void { |
| 176 | 191 | \\pub fn main() void { |
| 177 | 192 | \\ var x = @splat(4, @as(u32, 0x80000000)); |
| 178 | 193 | \\ var y = @intCast(V(4, i32), x); |
| 194 | \\ _ = y; | |
| 179 | 195 | \\} |
| 180 | 196 | ); |
| 181 | 197 | |
| ... | ... | @@ -183,6 +199,7 @@ pub fn addCases(cases: *tests.CompareOutputContext) void { |
| 183 | 199 | \\const std = @import("std"); |
| 184 | 200 | \\const V = @import("std").meta.Vector; |
| 185 | 201 | \\pub fn panic(message: []const u8, stack_trace: ?*std.builtin.StackTrace) noreturn { |
| 202 | \\ _ = stack_trace; | |
| 186 | 203 | \\ if (std.mem.eql(u8, message, "attempt to cast negative value to unsigned integer")) { |
| 187 | 204 | \\ std.process.exit(126); // good |
| 188 | 205 | \\ } |
| ... | ... | @@ -191,12 +208,14 @@ pub fn addCases(cases: *tests.CompareOutputContext) void { |
| 191 | 208 | \\pub fn main() void { |
| 192 | 209 | \\ var x = @splat(4, @as(i32, -2147483647)); |
| 193 | 210 | \\ var y = @intCast(V(4, u32), x); |
| 211 | \\ _ = y; | |
| 194 | 212 | \\} |
| 195 | 213 | ); |
| 196 | 214 | |
| 197 | 215 | cases.addRuntimeSafety("shift left by huge amount", |
| 198 | 216 | \\const std = @import("std"); |
| 199 | 217 | \\pub fn panic(message: []const u8, stack_trace: ?*std.builtin.StackTrace) noreturn { |
| 218 | \\ _ = stack_trace; | |
| 200 | 219 | \\ if (std.mem.eql(u8, message, "shift amount is greater than the type size")) { |
| 201 | 220 | \\ std.process.exit(126); // good |
| 202 | 221 | \\ } |
| ... | ... | @@ -206,12 +225,14 @@ pub fn addCases(cases: *tests.CompareOutputContext) void { |
| 206 | 225 | \\ var x: u24 = 42; |
| 207 | 226 | \\ var y: u5 = 24; |
| 208 | 227 | \\ var z = x >> y; |
| 228 | \\ _ = z; | |
| 209 | 229 | \\} |
| 210 | 230 | ); |
| 211 | 231 | |
| 212 | 232 | cases.addRuntimeSafety("shift right by huge amount", |
| 213 | 233 | \\const std = @import("std"); |
| 214 | 234 | \\pub fn panic(message: []const u8, stack_trace: ?*std.builtin.StackTrace) noreturn { |
| 235 | \\ _ = stack_trace; | |
| 215 | 236 | \\ if (std.mem.eql(u8, message, "shift amount is greater than the type size")) { |
| 216 | 237 | \\ std.process.exit(126); // good |
| 217 | 238 | \\ } |
| ... | ... | @@ -221,12 +242,14 @@ pub fn addCases(cases: *tests.CompareOutputContext) void { |
| 221 | 242 | \\ var x: u24 = 42; |
| 222 | 243 | \\ var y: u5 = 24; |
| 223 | 244 | \\ var z = x << y; |
| 245 | \\ _ = z; | |
| 224 | 246 | \\} |
| 225 | 247 | ); |
| 226 | 248 | |
| 227 | 249 | cases.addRuntimeSafety("slice sentinel mismatch - optional pointers", |
| 228 | 250 | \\const std = @import("std"); |
| 229 | 251 | \\pub fn panic(message: []const u8, stack_trace: ?*std.builtin.StackTrace) noreturn { |
| 252 | \\ _ = stack_trace; | |
| 230 | 253 | \\ if (std.mem.eql(u8, message, "sentinel mismatch")) { |
| 231 | 254 | \\ std.process.exit(126); // good |
| 232 | 255 | \\ } |
| ... | ... | @@ -235,12 +258,14 @@ pub fn addCases(cases: *tests.CompareOutputContext) void { |
| 235 | 258 | \\pub fn main() void { |
| 236 | 259 | \\ var buf: [4]?*i32 = undefined; |
| 237 | 260 | \\ const slice = buf[0..3 :null]; |
| 261 | \\ _ = slice; | |
| 238 | 262 | \\} |
| 239 | 263 | ); |
| 240 | 264 | |
| 241 | 265 | cases.addRuntimeSafety("slice sentinel mismatch - floats", |
| 242 | 266 | \\const std = @import("std"); |
| 243 | 267 | \\pub fn panic(message: []const u8, stack_trace: ?*std.builtin.StackTrace) noreturn { |
| 268 | \\ _ = stack_trace; | |
| 244 | 269 | \\ if (std.mem.eql(u8, message, "sentinel mismatch")) { |
| 245 | 270 | \\ std.process.exit(126); // good |
| 246 | 271 | \\ } |
| ... | ... | @@ -249,12 +274,14 @@ pub fn addCases(cases: *tests.CompareOutputContext) void { |
| 249 | 274 | \\pub fn main() void { |
| 250 | 275 | \\ var buf: [4]f32 = undefined; |
| 251 | 276 | \\ const slice = buf[0..3 :1.2]; |
| 277 | \\ _ = slice; | |
| 252 | 278 | \\} |
| 253 | 279 | ); |
| 254 | 280 | |
| 255 | 281 | cases.addRuntimeSafety("pointer slice sentinel mismatch", |
| 256 | 282 | \\const std = @import("std"); |
| 257 | 283 | \\pub fn panic(message: []const u8, stack_trace: ?*std.builtin.StackTrace) noreturn { |
| 284 | \\ _ = stack_trace; | |
| 258 | 285 | \\ if (std.mem.eql(u8, message, "sentinel mismatch")) { |
| 259 | 286 | \\ std.process.exit(126); // good |
| 260 | 287 | \\ } |
| ... | ... | @@ -264,12 +291,14 @@ pub fn addCases(cases: *tests.CompareOutputContext) void { |
| 264 | 291 | \\ var buf: [4]u8 = undefined; |
| 265 | 292 | \\ const ptr: [*]u8 = &buf; |
| 266 | 293 | \\ const slice = ptr[0..3 :0]; |
| 294 | \\ _ = slice; | |
| 267 | 295 | \\} |
| 268 | 296 | ); |
| 269 | 297 | |
| 270 | 298 | cases.addRuntimeSafety("slice slice sentinel mismatch", |
| 271 | 299 | \\const std = @import("std"); |
| 272 | 300 | \\pub fn panic(message: []const u8, stack_trace: ?*std.builtin.StackTrace) noreturn { |
| 301 | \\ _ = stack_trace; | |
| 273 | 302 | \\ if (std.mem.eql(u8, message, "sentinel mismatch")) { |
| 274 | 303 | \\ std.process.exit(126); // good |
| 275 | 304 | \\ } |
| ... | ... | @@ -279,12 +308,14 @@ pub fn addCases(cases: *tests.CompareOutputContext) void { |
| 279 | 308 | \\ var buf: [4]u8 = undefined; |
| 280 | 309 | \\ const slice = buf[0..]; |
| 281 | 310 | \\ const slice2 = slice[0..3 :0]; |
| 311 | \\ _ = slice2; | |
| 282 | 312 | \\} |
| 283 | 313 | ); |
| 284 | 314 | |
| 285 | 315 | cases.addRuntimeSafety("array slice sentinel mismatch", |
| 286 | 316 | \\const std = @import("std"); |
| 287 | 317 | \\pub fn panic(message: []const u8, stack_trace: ?*std.builtin.StackTrace) noreturn { |
| 318 | \\ _ = stack_trace; | |
| 288 | 319 | \\ if (std.mem.eql(u8, message, "sentinel mismatch")) { |
| 289 | 320 | \\ std.process.exit(126); // good |
| 290 | 321 | \\ } |
| ... | ... | @@ -293,12 +324,14 @@ pub fn addCases(cases: *tests.CompareOutputContext) void { |
| 293 | 324 | \\pub fn main() void { |
| 294 | 325 | \\ var buf: [4]u8 = undefined; |
| 295 | 326 | \\ const slice = buf[0..3 :0]; |
| 327 | \\ _ = slice; | |
| 296 | 328 | \\} |
| 297 | 329 | ); |
| 298 | 330 | |
| 299 | 331 | cases.addRuntimeSafety("intToPtr with misaligned address", |
| 300 | 332 | \\const std = @import("std"); |
| 301 | 333 | \\pub fn panic(message: []const u8, stack_trace: ?*std.builtin.StackTrace) noreturn { |
| 334 | \\ _ = stack_trace; | |
| 302 | 335 | \\ if (std.mem.eql(u8, message, "incorrect alignment")) { |
| 303 | 336 | \\ std.os.exit(126); // good |
| 304 | 337 | \\ } |
| ... | ... | @@ -307,16 +340,20 @@ pub fn addCases(cases: *tests.CompareOutputContext) void { |
| 307 | 340 | \\pub fn main() void { |
| 308 | 341 | \\ var x: usize = 5; |
| 309 | 342 | \\ var y = @intToPtr([*]align(4) u8, x); |
| 343 | \\ _ = y; | |
| 310 | 344 | \\} |
| 311 | 345 | ); |
| 312 | 346 | |
| 313 | 347 | cases.addRuntimeSafety("resuming a non-suspended function which never been suspended", |
| 314 | 348 | \\const std = @import("std"); |
| 315 | 349 | \\pub fn panic(message: []const u8, stack_trace: ?*std.builtin.StackTrace) noreturn { |
| 350 | \\ _ = message; | |
| 351 | \\ _ = stack_trace; | |
| 316 | 352 | \\ std.os.exit(126); |
| 317 | 353 | \\} |
| 318 | 354 | \\fn foo() void { |
| 319 | 355 | \\ var f = async bar(@frame()); |
| 356 | \\ _ = f; | |
| 320 | 357 | \\ std.os.exit(0); |
| 321 | 358 | \\} |
| 322 | 359 | \\ |
| ... | ... | @@ -335,6 +372,8 @@ pub fn addCases(cases: *tests.CompareOutputContext) void { |
| 335 | 372 | cases.addRuntimeSafety("resuming a non-suspended function which has been suspended and resumed", |
| 336 | 373 | \\const std = @import("std"); |
| 337 | 374 | \\pub fn panic(message: []const u8, stack_trace: ?*std.builtin.StackTrace) noreturn { |
| 375 | \\ _ = message; | |
| 376 | \\ _ = stack_trace; | |
| 338 | 377 | \\ std.os.exit(126); |
| 339 | 378 | \\} |
| 340 | 379 | \\fn foo() void { |
| ... | ... | @@ -342,6 +381,7 @@ pub fn addCases(cases: *tests.CompareOutputContext) void { |
| 342 | 381 | \\ global_frame = @frame(); |
| 343 | 382 | \\ } |
| 344 | 383 | \\ var f = async bar(@frame()); |
| 384 | \\ _ = f; | |
| 345 | 385 | \\ std.os.exit(0); |
| 346 | 386 | \\} |
| 347 | 387 | \\ |
| ... | ... | @@ -363,6 +403,8 @@ pub fn addCases(cases: *tests.CompareOutputContext) void { |
| 363 | 403 | cases.addRuntimeSafety("nosuspend function call, callee suspends", |
| 364 | 404 | \\const std = @import("std"); |
| 365 | 405 | \\pub fn panic(message: []const u8, stack_trace: ?*std.builtin.StackTrace) noreturn { |
| 406 | \\ _ = message; | |
| 407 | \\ _ = stack_trace; | |
| 366 | 408 | \\ std.os.exit(126); |
| 367 | 409 | \\} |
| 368 | 410 | \\pub fn main() void { |
| ... | ... | @@ -379,6 +421,8 @@ pub fn addCases(cases: *tests.CompareOutputContext) void { |
| 379 | 421 | cases.addRuntimeSafety("awaiting twice", |
| 380 | 422 | \\const std = @import("std"); |
| 381 | 423 | \\pub fn panic(message: []const u8, stack_trace: ?*std.builtin.StackTrace) noreturn { |
| 424 | \\ _ = message; | |
| 425 | \\ _ = stack_trace; | |
| 382 | 426 | \\ std.os.exit(126); |
| 383 | 427 | \\} |
| 384 | 428 | \\var frame: anyframe = undefined; |
| ... | ... | @@ -404,12 +448,15 @@ pub fn addCases(cases: *tests.CompareOutputContext) void { |
| 404 | 448 | cases.addRuntimeSafety("@asyncCall with too small a frame", |
| 405 | 449 | \\const std = @import("std"); |
| 406 | 450 | \\pub fn panic(message: []const u8, stack_trace: ?*std.builtin.StackTrace) noreturn { |
| 451 | \\ _ = message; | |
| 452 | \\ _ = stack_trace; | |
| 407 | 453 | \\ std.os.exit(126); |
| 408 | 454 | \\} |
| 409 | 455 | \\pub fn main() void { |
| 410 | 456 | \\ var bytes: [1]u8 align(16) = undefined; |
| 411 | 457 | \\ var ptr = other; |
| 412 | 458 | \\ var frame = @asyncCall(&bytes, {}, ptr, .{}); |
| 459 | \\ _ = frame; | |
| 413 | 460 | \\} |
| 414 | 461 | \\fn other() callconv(.Async) void { |
| 415 | 462 | \\ suspend {} |
| ... | ... | @@ -419,6 +466,8 @@ pub fn addCases(cases: *tests.CompareOutputContext) void { |
| 419 | 466 | cases.addRuntimeSafety("resuming a function which is awaiting a frame", |
| 420 | 467 | \\const std = @import("std"); |
| 421 | 468 | \\pub fn panic(message: []const u8, stack_trace: ?*std.builtin.StackTrace) noreturn { |
| 469 | \\ _ = message; | |
| 470 | \\ _ = stack_trace; | |
| 422 | 471 | \\ std.os.exit(126); |
| 423 | 472 | \\} |
| 424 | 473 | \\pub fn main() void { |
| ... | ... | @@ -437,6 +486,8 @@ pub fn addCases(cases: *tests.CompareOutputContext) void { |
| 437 | 486 | cases.addRuntimeSafety("resuming a function which is awaiting a call", |
| 438 | 487 | \\const std = @import("std"); |
| 439 | 488 | \\pub fn panic(message: []const u8, stack_trace: ?*std.builtin.StackTrace) noreturn { |
| 489 | \\ _ = message; | |
| 490 | \\ _ = stack_trace; | |
| 440 | 491 | \\ std.os.exit(126); |
| 441 | 492 | \\} |
| 442 | 493 | \\pub fn main() void { |
| ... | ... | @@ -454,6 +505,8 @@ pub fn addCases(cases: *tests.CompareOutputContext) void { |
| 454 | 505 | cases.addRuntimeSafety("invalid resume of async function", |
| 455 | 506 | \\const std = @import("std"); |
| 456 | 507 | \\pub fn panic(message: []const u8, stack_trace: ?*std.builtin.StackTrace) noreturn { |
| 508 | \\ _ = message; | |
| 509 | \\ _ = stack_trace; | |
| 457 | 510 | \\ std.os.exit(126); |
| 458 | 511 | \\} |
| 459 | 512 | \\pub fn main() void { |
| ... | ... | @@ -469,61 +522,78 @@ pub fn addCases(cases: *tests.CompareOutputContext) void { |
| 469 | 522 | cases.addRuntimeSafety(".? operator on null pointer", |
| 470 | 523 | \\const std = @import("std"); |
| 471 | 524 | \\pub fn panic(message: []const u8, stack_trace: ?*std.builtin.StackTrace) noreturn { |
| 525 | \\ _ = message; | |
| 526 | \\ _ = stack_trace; | |
| 472 | 527 | \\ std.os.exit(126); |
| 473 | 528 | \\} |
| 474 | 529 | \\pub fn main() void { |
| 475 | 530 | \\ var ptr: ?*i32 = null; |
| 476 | 531 | \\ var b = ptr.?; |
| 532 | \\ _ = b; | |
| 477 | 533 | \\} |
| 478 | 534 | ); |
| 479 | 535 | |
| 480 | 536 | cases.addRuntimeSafety(".? operator on C pointer", |
| 481 | 537 | \\const std = @import("std"); |
| 482 | 538 | \\pub fn panic(message: []const u8, stack_trace: ?*std.builtin.StackTrace) noreturn { |
| 539 | \\ _ = message; | |
| 540 | \\ _ = stack_trace; | |
| 483 | 541 | \\ std.os.exit(126); |
| 484 | 542 | \\} |
| 485 | 543 | \\pub fn main() void { |
| 486 | 544 | \\ var ptr: [*c]i32 = null; |
| 487 | 545 | \\ var b = ptr.?; |
| 546 | \\ _ = b; | |
| 488 | 547 | \\} |
| 489 | 548 | ); |
| 490 | 549 | |
| 491 | 550 | cases.addRuntimeSafety("@intToPtr address zero to non-optional pointer", |
| 492 | 551 | \\const std = @import("std"); |
| 493 | 552 | \\pub fn panic(message: []const u8, stack_trace: ?*std.builtin.StackTrace) noreturn { |
| 553 | \\ _ = message; | |
| 554 | \\ _ = stack_trace; | |
| 494 | 555 | \\ std.os.exit(126); |
| 495 | 556 | \\} |
| 496 | 557 | \\pub fn main() void { |
| 497 | 558 | \\ var zero: usize = 0; |
| 498 | 559 | \\ var b = @intToPtr(*i32, zero); |
| 560 | \\ _ = b; | |
| 499 | 561 | \\} |
| 500 | 562 | ); |
| 501 | 563 | |
| 502 | 564 | cases.addRuntimeSafety("@intToPtr address zero to non-optional byte-aligned pointer", |
| 503 | 565 | \\const std = @import("std"); |
| 504 | 566 | \\pub fn panic(message: []const u8, stack_trace: ?*std.builtin.StackTrace) noreturn { |
| 567 | \\ _ = message; | |
| 568 | \\ _ = stack_trace; | |
| 505 | 569 | \\ std.os.exit(126); |
| 506 | 570 | \\} |
| 507 | 571 | \\pub fn main() void { |
| 508 | 572 | \\ var zero: usize = 0; |
| 509 | 573 | \\ var b = @intToPtr(*u8, zero); |
| 574 | \\ _ = b; | |
| 510 | 575 | \\} |
| 511 | 576 | ); |
| 512 | 577 | |
| 513 | 578 | cases.addRuntimeSafety("pointer casting null to non-optional pointer", |
| 514 | 579 | \\const std = @import("std"); |
| 515 | 580 | \\pub fn panic(message: []const u8, stack_trace: ?*std.builtin.StackTrace) noreturn { |
| 581 | \\ _ = message; | |
| 582 | \\ _ = stack_trace; | |
| 516 | 583 | \\ std.os.exit(126); |
| 517 | 584 | \\} |
| 518 | 585 | \\pub fn main() void { |
| 519 | 586 | \\ var c_ptr: [*c]u8 = 0; |
| 520 | 587 | \\ var zig_ptr: *u8 = c_ptr; |
| 588 | \\ _ = zig_ptr; | |
| 521 | 589 | \\} |
| 522 | 590 | ); |
| 523 | 591 | |
| 524 | 592 | cases.addRuntimeSafety("@intToEnum - no matching tag value", |
| 525 | 593 | \\const std = @import("std"); |
| 526 | 594 | \\pub fn panic(message: []const u8, stack_trace: ?*std.builtin.StackTrace) noreturn { |
| 595 | \\ _ = message; | |
| 596 | \\ _ = stack_trace; | |
| 527 | 597 | \\ std.os.exit(126); |
| 528 | 598 | \\} |
| 529 | 599 | \\const Foo = enum { |
| ... | ... | @@ -537,12 +607,14 @@ pub fn addCases(cases: *tests.CompareOutputContext) void { |
| 537 | 607 | \\fn bar(a: u2) Foo { |
| 538 | 608 | \\ return @intToEnum(Foo, a); |
| 539 | 609 | \\} |
| 540 | \\fn baz(a: Foo) void {} | |
| 610 | \\fn baz(_: Foo) void {} | |
| 541 | 611 | ); |
| 542 | 612 | |
| 543 | 613 | cases.addRuntimeSafety("@floatToInt cannot fit - negative to unsigned", |
| 544 | 614 | \\const std = @import("std"); |
| 545 | 615 | \\pub fn panic(message: []const u8, stack_trace: ?*std.builtin.StackTrace) noreturn { |
| 616 | \\ _ = message; | |
| 617 | \\ _ = stack_trace; | |
| 546 | 618 | \\ std.os.exit(126); |
| 547 | 619 | \\} |
| 548 | 620 | \\pub fn main() void { |
| ... | ... | @@ -551,12 +623,14 @@ pub fn addCases(cases: *tests.CompareOutputContext) void { |
| 551 | 623 | \\fn bar(a: f32) u8 { |
| 552 | 624 | \\ return @floatToInt(u8, a); |
| 553 | 625 | \\} |
| 554 | \\fn baz(a: u8) void { } | |
| 626 | \\fn baz(_: u8) void { } | |
| 555 | 627 | ); |
| 556 | 628 | |
| 557 | 629 | cases.addRuntimeSafety("@floatToInt cannot fit - negative out of range", |
| 558 | 630 | \\const std = @import("std"); |
| 559 | 631 | \\pub fn panic(message: []const u8, stack_trace: ?*std.builtin.StackTrace) noreturn { |
| 632 | \\ _ = message; | |
| 633 | \\ _ = stack_trace; | |
| 560 | 634 | \\ std.os.exit(126); |
| 561 | 635 | \\} |
| 562 | 636 | \\pub fn main() void { |
| ... | ... | @@ -565,12 +639,14 @@ pub fn addCases(cases: *tests.CompareOutputContext) void { |
| 565 | 639 | \\fn bar(a: f32) i8 { |
| 566 | 640 | \\ return @floatToInt(i8, a); |
| 567 | 641 | \\} |
| 568 | \\fn baz(a: i8) void { } | |
| 642 | \\fn baz(_: i8) void { } | |
| 569 | 643 | ); |
| 570 | 644 | |
| 571 | 645 | cases.addRuntimeSafety("@floatToInt cannot fit - positive out of range", |
| 572 | 646 | \\const std = @import("std"); |
| 573 | 647 | \\pub fn panic(message: []const u8, stack_trace: ?*std.builtin.StackTrace) noreturn { |
| 648 | \\ _ = message; | |
| 649 | \\ _ = stack_trace; | |
| 574 | 650 | \\ std.os.exit(126); |
| 575 | 651 | \\} |
| 576 | 652 | \\pub fn main() void { |
| ... | ... | @@ -579,12 +655,14 @@ pub fn addCases(cases: *tests.CompareOutputContext) void { |
| 579 | 655 | \\fn bar(a: f32) u8 { |
| 580 | 656 | \\ return @floatToInt(u8, a); |
| 581 | 657 | \\} |
| 582 | \\fn baz(a: u8) void { } | |
| 658 | \\fn baz(_: u8) void { } | |
| 583 | 659 | ); |
| 584 | 660 | |
| 585 | 661 | cases.addRuntimeSafety("calling panic", |
| 586 | 662 | \\const std = @import("std"); |
| 587 | 663 | \\pub fn panic(message: []const u8, stack_trace: ?*std.builtin.StackTrace) noreturn { |
| 664 | \\ _ = message; | |
| 665 | \\ _ = stack_trace; | |
| 588 | 666 | \\ std.os.exit(126); |
| 589 | 667 | \\} |
| 590 | 668 | \\pub fn main() void { |
| ... | ... | @@ -595,6 +673,8 @@ pub fn addCases(cases: *tests.CompareOutputContext) void { |
| 595 | 673 | cases.addRuntimeSafety("out of bounds slice access", |
| 596 | 674 | \\const std = @import("std"); |
| 597 | 675 | \\pub fn panic(message: []const u8, stack_trace: ?*std.builtin.StackTrace) noreturn { |
| 676 | \\ _ = message; | |
| 677 | \\ _ = stack_trace; | |
| 598 | 678 | \\ std.os.exit(126); |
| 599 | 679 | \\} |
| 600 | 680 | \\pub fn main() void { |
| ... | ... | @@ -604,12 +684,14 @@ pub fn addCases(cases: *tests.CompareOutputContext) void { |
| 604 | 684 | \\fn bar(a: []const i32) i32 { |
| 605 | 685 | \\ return a[4]; |
| 606 | 686 | \\} |
| 607 | \\fn baz(a: i32) void { } | |
| 687 | \\fn baz(_: i32) void { } | |
| 608 | 688 | ); |
| 609 | 689 | |
| 610 | 690 | cases.addRuntimeSafety("integer addition overflow", |
| 611 | 691 | \\const std = @import("std"); |
| 612 | 692 | \\pub fn panic(message: []const u8, stack_trace: ?*std.builtin.StackTrace) noreturn { |
| 693 | \\ _ = message; | |
| 694 | \\ _ = stack_trace; | |
| 613 | 695 | \\ std.os.exit(126); |
| 614 | 696 | \\} |
| 615 | 697 | \\pub fn main() !void { |
| ... | ... | @@ -624,12 +706,15 @@ pub fn addCases(cases: *tests.CompareOutputContext) void { |
| 624 | 706 | cases.addRuntimeSafety("vector integer addition overflow", |
| 625 | 707 | \\const std = @import("std"); |
| 626 | 708 | \\pub fn panic(message: []const u8, stack_trace: ?*std.builtin.StackTrace) noreturn { |
| 709 | \\ _ = message; | |
| 710 | \\ _ = stack_trace; | |
| 627 | 711 | \\ std.os.exit(126); |
| 628 | 712 | \\} |
| 629 | 713 | \\pub fn main() void { |
| 630 | 714 | \\ var a: std.meta.Vector(4, i32) = [_]i32{ 1, 2, 2147483643, 4 }; |
| 631 | 715 | \\ var b: std.meta.Vector(4, i32) = [_]i32{ 5, 6, 7, 8 }; |
| 632 | 716 | \\ const x = add(a, b); |
| 717 | \\ _ = x; | |
| 633 | 718 | \\} |
| 634 | 719 | \\fn add(a: std.meta.Vector(4, i32), b: std.meta.Vector(4, i32)) std.meta.Vector(4, i32) { |
| 635 | 720 | \\ return a + b; |
| ... | ... | @@ -639,12 +724,15 @@ pub fn addCases(cases: *tests.CompareOutputContext) void { |
| 639 | 724 | cases.addRuntimeSafety("vector integer subtraction overflow", |
| 640 | 725 | \\const std = @import("std"); |
| 641 | 726 | \\pub fn panic(message: []const u8, stack_trace: ?*std.builtin.StackTrace) noreturn { |
| 727 | \\ _ = message; | |
| 728 | \\ _ = stack_trace; | |
| 642 | 729 | \\ std.os.exit(126); |
| 643 | 730 | \\} |
| 644 | 731 | \\pub fn main() void { |
| 645 | 732 | \\ var a: std.meta.Vector(4, u32) = [_]u32{ 1, 2, 8, 4 }; |
| 646 | 733 | \\ var b: std.meta.Vector(4, u32) = [_]u32{ 5, 6, 7, 8 }; |
| 647 | 734 | \\ const x = sub(b, a); |
| 735 | \\ _ = x; | |
| 648 | 736 | \\} |
| 649 | 737 | \\fn sub(a: std.meta.Vector(4, u32), b: std.meta.Vector(4, u32)) std.meta.Vector(4, u32) { |
| 650 | 738 | \\ return a - b; |
| ... | ... | @@ -654,12 +742,15 @@ pub fn addCases(cases: *tests.CompareOutputContext) void { |
| 654 | 742 | cases.addRuntimeSafety("vector integer multiplication overflow", |
| 655 | 743 | \\const std = @import("std"); |
| 656 | 744 | \\pub fn panic(message: []const u8, stack_trace: ?*std.builtin.StackTrace) noreturn { |
| 745 | \\ _ = message; | |
| 746 | \\ _ = stack_trace; | |
| 657 | 747 | \\ std.os.exit(126); |
| 658 | 748 | \\} |
| 659 | 749 | \\pub fn main() void { |
| 660 | 750 | \\ var a: std.meta.Vector(4, u8) = [_]u8{ 1, 2, 200, 4 }; |
| 661 | 751 | \\ var b: std.meta.Vector(4, u8) = [_]u8{ 5, 6, 2, 8 }; |
| 662 | 752 | \\ const x = mul(b, a); |
| 753 | \\ _ = x; | |
| 663 | 754 | \\} |
| 664 | 755 | \\fn mul(a: std.meta.Vector(4, u8), b: std.meta.Vector(4, u8)) std.meta.Vector(4, u8) { |
| 665 | 756 | \\ return a * b; |
| ... | ... | @@ -669,11 +760,14 @@ pub fn addCases(cases: *tests.CompareOutputContext) void { |
| 669 | 760 | cases.addRuntimeSafety("vector integer negation overflow", |
| 670 | 761 | \\const std = @import("std"); |
| 671 | 762 | \\pub fn panic(message: []const u8, stack_trace: ?*std.builtin.StackTrace) noreturn { |
| 763 | \\ _ = message; | |
| 764 | \\ _ = stack_trace; | |
| 672 | 765 | \\ std.os.exit(126); |
| 673 | 766 | \\} |
| 674 | 767 | \\pub fn main() void { |
| 675 | 768 | \\ var a: std.meta.Vector(4, i16) = [_]i16{ 1, -32768, 200, 4 }; |
| 676 | 769 | \\ const x = neg(a); |
| 770 | \\ _ = x; | |
| 677 | 771 | \\} |
| 678 | 772 | \\fn neg(a: std.meta.Vector(4, i16)) std.meta.Vector(4, i16) { |
| 679 | 773 | \\ return -a; |
| ... | ... | @@ -683,6 +777,8 @@ pub fn addCases(cases: *tests.CompareOutputContext) void { |
| 683 | 777 | cases.addRuntimeSafety("integer subtraction overflow", |
| 684 | 778 | \\const std = @import("std"); |
| 685 | 779 | \\pub fn panic(message: []const u8, stack_trace: ?*std.builtin.StackTrace) noreturn { |
| 780 | \\ _ = message; | |
| 781 | \\ _ = stack_trace; | |
| 686 | 782 | \\ std.os.exit(126); |
| 687 | 783 | \\} |
| 688 | 784 | \\pub fn main() !void { |
| ... | ... | @@ -697,6 +793,8 @@ pub fn addCases(cases: *tests.CompareOutputContext) void { |
| 697 | 793 | cases.addRuntimeSafety("integer multiplication overflow", |
| 698 | 794 | \\const std = @import("std"); |
| 699 | 795 | \\pub fn panic(message: []const u8, stack_trace: ?*std.builtin.StackTrace) noreturn { |
| 796 | \\ _ = message; | |
| 797 | \\ _ = stack_trace; | |
| 700 | 798 | \\ std.os.exit(126); |
| 701 | 799 | \\} |
| 702 | 800 | \\pub fn main() !void { |
| ... | ... | @@ -711,6 +809,8 @@ pub fn addCases(cases: *tests.CompareOutputContext) void { |
| 711 | 809 | cases.addRuntimeSafety("integer negation overflow", |
| 712 | 810 | \\const std = @import("std"); |
| 713 | 811 | \\pub fn panic(message: []const u8, stack_trace: ?*std.builtin.StackTrace) noreturn { |
| 812 | \\ _ = message; | |
| 813 | \\ _ = stack_trace; | |
| 714 | 814 | \\ std.os.exit(126); |
| 715 | 815 | \\} |
| 716 | 816 | \\pub fn main() !void { |
| ... | ... | @@ -725,6 +825,8 @@ pub fn addCases(cases: *tests.CompareOutputContext) void { |
| 725 | 825 | cases.addRuntimeSafety("signed integer division overflow", |
| 726 | 826 | \\const std = @import("std"); |
| 727 | 827 | \\pub fn panic(message: []const u8, stack_trace: ?*std.builtin.StackTrace) noreturn { |
| 828 | \\ _ = message; | |
| 829 | \\ _ = stack_trace; | |
| 728 | 830 | \\ std.os.exit(126); |
| 729 | 831 | \\} |
| 730 | 832 | \\pub fn main() !void { |
| ... | ... | @@ -739,6 +841,8 @@ pub fn addCases(cases: *tests.CompareOutputContext) void { |
| 739 | 841 | cases.addRuntimeSafety("signed integer division overflow - vectors", |
| 740 | 842 | \\const std = @import("std"); |
| 741 | 843 | \\pub fn panic(message: []const u8, stack_trace: ?*std.builtin.StackTrace) noreturn { |
| 844 | \\ _ = message; | |
| 845 | \\ _ = stack_trace; | |
| 742 | 846 | \\ std.os.exit(126); |
| 743 | 847 | \\} |
| 744 | 848 | \\pub fn main() !void { |
| ... | ... | @@ -755,6 +859,8 @@ pub fn addCases(cases: *tests.CompareOutputContext) void { |
| 755 | 859 | cases.addRuntimeSafety("signed shift left overflow", |
| 756 | 860 | \\const std = @import("std"); |
| 757 | 861 | \\pub fn panic(message: []const u8, stack_trace: ?*std.builtin.StackTrace) noreturn { |
| 862 | \\ _ = message; | |
| 863 | \\ _ = stack_trace; | |
| 758 | 864 | \\ std.os.exit(126); |
| 759 | 865 | \\} |
| 760 | 866 | \\pub fn main() !void { |
| ... | ... | @@ -769,6 +875,8 @@ pub fn addCases(cases: *tests.CompareOutputContext) void { |
| 769 | 875 | cases.addRuntimeSafety("unsigned shift left overflow", |
| 770 | 876 | \\const std = @import("std"); |
| 771 | 877 | \\pub fn panic(message: []const u8, stack_trace: ?*std.builtin.StackTrace) noreturn { |
| 878 | \\ _ = message; | |
| 879 | \\ _ = stack_trace; | |
| 772 | 880 | \\ std.os.exit(126); |
| 773 | 881 | \\} |
| 774 | 882 | \\pub fn main() !void { |
| ... | ... | @@ -783,6 +891,8 @@ pub fn addCases(cases: *tests.CompareOutputContext) void { |
| 783 | 891 | cases.addRuntimeSafety("signed shift right overflow", |
| 784 | 892 | \\const std = @import("std"); |
| 785 | 893 | \\pub fn panic(message: []const u8, stack_trace: ?*std.builtin.StackTrace) noreturn { |
| 894 | \\ _ = message; | |
| 895 | \\ _ = stack_trace; | |
| 786 | 896 | \\ std.os.exit(126); |
| 787 | 897 | \\} |
| 788 | 898 | \\pub fn main() !void { |
| ... | ... | @@ -797,6 +907,8 @@ pub fn addCases(cases: *tests.CompareOutputContext) void { |
| 797 | 907 | cases.addRuntimeSafety("unsigned shift right overflow", |
| 798 | 908 | \\const std = @import("std"); |
| 799 | 909 | \\pub fn panic(message: []const u8, stack_trace: ?*std.builtin.StackTrace) noreturn { |
| 910 | \\ _ = message; | |
| 911 | \\ _ = stack_trace; | |
| 800 | 912 | \\ std.os.exit(126); |
| 801 | 913 | \\} |
| 802 | 914 | \\pub fn main() !void { |
| ... | ... | @@ -811,10 +923,13 @@ pub fn addCases(cases: *tests.CompareOutputContext) void { |
| 811 | 923 | cases.addRuntimeSafety("integer division by zero", |
| 812 | 924 | \\const std = @import("std"); |
| 813 | 925 | \\pub fn panic(message: []const u8, stack_trace: ?*std.builtin.StackTrace) noreturn { |
| 926 | \\ _ = message; | |
| 927 | \\ _ = stack_trace; | |
| 814 | 928 | \\ std.os.exit(126); |
| 815 | 929 | \\} |
| 816 | 930 | \\pub fn main() void { |
| 817 | 931 | \\ const x = div0(999, 0); |
| 932 | \\ _ = x; | |
| 818 | 933 | \\} |
| 819 | 934 | \\fn div0(a: i32, b: i32) i32 { |
| 820 | 935 | \\ return @divTrunc(a, b); |
| ... | ... | @@ -824,12 +939,15 @@ pub fn addCases(cases: *tests.CompareOutputContext) void { |
| 824 | 939 | cases.addRuntimeSafety("integer division by zero - vectors", |
| 825 | 940 | \\const std = @import("std"); |
| 826 | 941 | \\pub fn panic(message: []const u8, stack_trace: ?*std.builtin.StackTrace) noreturn { |
| 942 | \\ _ = message; | |
| 943 | \\ _ = stack_trace; | |
| 827 | 944 | \\ std.os.exit(126); |
| 828 | 945 | \\} |
| 829 | 946 | \\pub fn main() void { |
| 830 | 947 | \\ var a: std.meta.Vector(4, i32) = [4]i32{111, 222, 333, 444}; |
| 831 | 948 | \\ var b: std.meta.Vector(4, i32) = [4]i32{111, 0, 333, 444}; |
| 832 | 949 | \\ const x = div0(a, b); |
| 950 | \\ _ = x; | |
| 833 | 951 | \\} |
| 834 | 952 | \\fn div0(a: std.meta.Vector(4, i32), b: std.meta.Vector(4, i32)) std.meta.Vector(4, i32) { |
| 835 | 953 | \\ return @divTrunc(a, b); |
| ... | ... | @@ -839,6 +957,8 @@ pub fn addCases(cases: *tests.CompareOutputContext) void { |
| 839 | 957 | cases.addRuntimeSafety("exact division failure", |
| 840 | 958 | \\const std = @import("std"); |
| 841 | 959 | \\pub fn panic(message: []const u8, stack_trace: ?*std.builtin.StackTrace) noreturn { |
| 960 | \\ _ = message; | |
| 961 | \\ _ = stack_trace; | |
| 842 | 962 | \\ std.os.exit(126); |
| 843 | 963 | \\} |
| 844 | 964 | \\pub fn main() !void { |
| ... | ... | @@ -853,12 +973,15 @@ pub fn addCases(cases: *tests.CompareOutputContext) void { |
| 853 | 973 | cases.addRuntimeSafety("exact division failure - vectors", |
| 854 | 974 | \\const std = @import("std"); |
| 855 | 975 | \\pub fn panic(message: []const u8, stack_trace: ?*std.builtin.StackTrace) noreturn { |
| 976 | \\ _ = message; | |
| 977 | \\ _ = stack_trace; | |
| 856 | 978 | \\ std.os.exit(126); |
| 857 | 979 | \\} |
| 858 | 980 | \\pub fn main() !void { |
| 859 | 981 | \\ var a: std.meta.Vector(4, i32) = [4]i32{111, 222, 333, 444}; |
| 860 | 982 | \\ var b: std.meta.Vector(4, i32) = [4]i32{111, 222, 333, 441}; |
| 861 | 983 | \\ const x = divExact(a, b); |
| 984 | \\ _ = x; | |
| 862 | 985 | \\} |
| 863 | 986 | \\fn divExact(a: std.meta.Vector(4, i32), b: std.meta.Vector(4, i32)) std.meta.Vector(4, i32) { |
| 864 | 987 | \\ return @divExact(a, b); |
| ... | ... | @@ -868,6 +991,8 @@ pub fn addCases(cases: *tests.CompareOutputContext) void { |
| 868 | 991 | cases.addRuntimeSafety("cast []u8 to bigger slice of wrong size", |
| 869 | 992 | \\const std = @import("std"); |
| 870 | 993 | \\pub fn panic(message: []const u8, stack_trace: ?*std.builtin.StackTrace) noreturn { |
| 994 | \\ _ = message; | |
| 995 | \\ _ = stack_trace; | |
| 871 | 996 | \\ std.os.exit(126); |
| 872 | 997 | \\} |
| 873 | 998 | \\pub fn main() !void { |
| ... | ... | @@ -882,6 +1007,8 @@ pub fn addCases(cases: *tests.CompareOutputContext) void { |
| 882 | 1007 | cases.addRuntimeSafety("value does not fit in shortening cast", |
| 883 | 1008 | \\const std = @import("std"); |
| 884 | 1009 | \\pub fn panic(message: []const u8, stack_trace: ?*std.builtin.StackTrace) noreturn { |
| 1010 | \\ _ = message; | |
| 1011 | \\ _ = stack_trace; | |
| 885 | 1012 | \\ std.os.exit(126); |
| 886 | 1013 | \\} |
| 887 | 1014 | \\pub fn main() !void { |
| ... | ... | @@ -896,6 +1023,8 @@ pub fn addCases(cases: *tests.CompareOutputContext) void { |
| 896 | 1023 | cases.addRuntimeSafety("value does not fit in shortening cast - u0", |
| 897 | 1024 | \\const std = @import("std"); |
| 898 | 1025 | \\pub fn panic(message: []const u8, stack_trace: ?*std.builtin.StackTrace) noreturn { |
| 1026 | \\ _ = message; | |
| 1027 | \\ _ = stack_trace; | |
| 899 | 1028 | \\ std.os.exit(126); |
| 900 | 1029 | \\} |
| 901 | 1030 | \\pub fn main() !void { |
| ... | ... | @@ -910,6 +1039,8 @@ pub fn addCases(cases: *tests.CompareOutputContext) void { |
| 910 | 1039 | cases.addRuntimeSafety("signed integer not fitting in cast to unsigned integer", |
| 911 | 1040 | \\const std = @import("std"); |
| 912 | 1041 | \\pub fn panic(message: []const u8, stack_trace: ?*std.builtin.StackTrace) noreturn { |
| 1042 | \\ _ = message; | |
| 1043 | \\ _ = stack_trace; | |
| 913 | 1044 | \\ std.os.exit(126); |
| 914 | 1045 | \\} |
| 915 | 1046 | \\pub fn main() !void { |
| ... | ... | @@ -924,28 +1055,35 @@ pub fn addCases(cases: *tests.CompareOutputContext) void { |
| 924 | 1055 | cases.addRuntimeSafety("signed integer not fitting in cast to unsigned integer - widening", |
| 925 | 1056 | \\const std = @import("std"); |
| 926 | 1057 | \\pub fn panic(message: []const u8, stack_trace: ?*std.builtin.StackTrace) noreturn { |
| 1058 | \\ _ = message; | |
| 1059 | \\ _ = stack_trace; | |
| 927 | 1060 | \\ std.os.exit(126); |
| 928 | 1061 | \\} |
| 929 | 1062 | \\pub fn main() void { |
| 930 | 1063 | \\ var value: c_short = -1; |
| 931 | 1064 | \\ var casted = @intCast(u32, value); |
| 1065 | \\ _ = casted; | |
| 932 | 1066 | \\} |
| 933 | 1067 | ); |
| 934 | 1068 | |
| 935 | 1069 | cases.addRuntimeSafety("unsigned integer not fitting in cast to signed integer - same bit count", |
| 936 | 1070 | \\const std = @import("std"); |
| 937 | 1071 | \\pub fn panic(message: []const u8, stack_trace: ?*std.builtin.StackTrace) noreturn { |
| 1072 | \\ _ = message; | |
| 1073 | \\ _ = stack_trace; | |
| 938 | 1074 | \\ std.os.exit(126); |
| 939 | 1075 | \\} |
| 940 | 1076 | \\pub fn main() void { |
| 941 | 1077 | \\ var value: u8 = 245; |
| 942 | 1078 | \\ var casted = @intCast(i8, value); |
| 1079 | \\ _ = casted; | |
| 943 | 1080 | \\} |
| 944 | 1081 | ); |
| 945 | 1082 | |
| 946 | 1083 | cases.addRuntimeSafety("unwrap error", |
| 947 | 1084 | \\const std = @import("std"); |
| 948 | 1085 | \\pub fn panic(message: []const u8, stack_trace: ?*std.builtin.StackTrace) noreturn { |
| 1086 | \\ _ = stack_trace; | |
| 949 | 1087 | \\ if (std.mem.eql(u8, message, "attempt to unwrap error: Whatever")) { |
| 950 | 1088 | \\ std.os.exit(126); // good |
| 951 | 1089 | \\ } |
| ... | ... | @@ -962,6 +1100,8 @@ pub fn addCases(cases: *tests.CompareOutputContext) void { |
| 962 | 1100 | cases.addRuntimeSafety("cast integer to global error and no code matches", |
| 963 | 1101 | \\const std = @import("std"); |
| 964 | 1102 | \\pub fn panic(message: []const u8, stack_trace: ?*std.builtin.StackTrace) noreturn { |
| 1103 | \\ _ = message; | |
| 1104 | \\ _ = stack_trace; | |
| 965 | 1105 | \\ std.os.exit(126); |
| 966 | 1106 | \\} |
| 967 | 1107 | \\pub fn main() void { |
| ... | ... | @@ -975,6 +1115,8 @@ pub fn addCases(cases: *tests.CompareOutputContext) void { |
| 975 | 1115 | cases.addRuntimeSafety("@errSetCast error not present in destination", |
| 976 | 1116 | \\const std = @import("std"); |
| 977 | 1117 | \\pub fn panic(message: []const u8, stack_trace: ?*std.builtin.StackTrace) noreturn { |
| 1118 | \\ _ = message; | |
| 1119 | \\ _ = stack_trace; | |
| 978 | 1120 | \\ std.os.exit(126); |
| 979 | 1121 | \\} |
| 980 | 1122 | \\const Set1 = error{A, B}; |
| ... | ... | @@ -990,6 +1132,8 @@ pub fn addCases(cases: *tests.CompareOutputContext) void { |
| 990 | 1132 | cases.addRuntimeSafety("@alignCast misaligned", |
| 991 | 1133 | \\const std = @import("std"); |
| 992 | 1134 | \\pub fn panic(message: []const u8, stack_trace: ?*std.builtin.StackTrace) noreturn { |
| 1135 | \\ _ = message; | |
| 1136 | \\ _ = stack_trace; | |
| 993 | 1137 | \\ std.os.exit(126); |
| 994 | 1138 | \\} |
| 995 | 1139 | \\pub fn main() !void { |
| ... | ... | @@ -1007,6 +1151,8 @@ pub fn addCases(cases: *tests.CompareOutputContext) void { |
| 1007 | 1151 | cases.addRuntimeSafety("bad union field access", |
| 1008 | 1152 | \\const std = @import("std"); |
| 1009 | 1153 | \\pub fn panic(message: []const u8, stack_trace: ?*std.builtin.StackTrace) noreturn { |
| 1154 | \\ _ = message; | |
| 1155 | \\ _ = stack_trace; | |
| 1010 | 1156 | \\ std.os.exit(126); |
| 1011 | 1157 | \\} |
| 1012 | 1158 | \\ |
| ... | ... | @@ -1031,6 +1177,8 @@ pub fn addCases(cases: *tests.CompareOutputContext) void { |
| 1031 | 1177 | cases.addRuntimeSafety("@intCast to u0", |
| 1032 | 1178 | \\const std = @import("std"); |
| 1033 | 1179 | \\pub fn panic(message: []const u8, stack_trace: ?*std.builtin.StackTrace) noreturn { |
| 1180 | \\ _ = message; | |
| 1181 | \\ _ = stack_trace; | |
| 1034 | 1182 | \\ std.os.exit(126); |
| 1035 | 1183 | \\} |
| 1036 | 1184 | \\ |
| ... | ... | @@ -1040,6 +1188,7 @@ pub fn addCases(cases: *tests.CompareOutputContext) void { |
| 1040 | 1188 | \\ |
| 1041 | 1189 | \\fn bar(one: u1, not_zero: i32) void { |
| 1042 | 1190 | \\ var x = one << @intCast(u0, not_zero); |
| 1191 | \\ _ = x; | |
| 1043 | 1192 | \\} |
| 1044 | 1193 | ); |
| 1045 | 1194 | |
| ... | ... | @@ -1049,6 +1198,8 @@ pub fn addCases(cases: *tests.CompareOutputContext) void { |
| 1049 | 1198 | \\const std = @import("std"); |
| 1050 | 1199 | \\ |
| 1051 | 1200 | \\pub fn panic(message: []const u8, stack_trace: ?*std.builtin.StackTrace) noreturn { |
| 1201 | \\ _ = message; | |
| 1202 | \\ _ = stack_trace; | |
| 1052 | 1203 | \\ std.os.exit(126); |
| 1053 | 1204 | \\} |
| 1054 | 1205 | \\ |
| ... | ... | @@ -1058,6 +1209,7 @@ pub fn addCases(cases: *tests.CompareOutputContext) void { |
| 1058 | 1209 | \\ const p = nonFailing(); |
| 1059 | 1210 | \\ resume p; |
| 1060 | 1211 | \\ const p2 = async printTrace(p); |
| 1212 | \\ _ = p2; | |
| 1061 | 1213 | \\} |
| 1062 | 1214 | \\ |
| 1063 | 1215 | \\fn nonFailing() anyframe->anyerror!void { |
| ... | ... | @@ -1084,12 +1236,15 @@ pub fn addCases(cases: *tests.CompareOutputContext) void { |
| 1084 | 1236 | cases.addRuntimeSafety("slicing null C pointer", |
| 1085 | 1237 | \\const std = @import("std"); |
| 1086 | 1238 | \\pub fn panic(message: []const u8, stack_trace: ?*std.builtin.StackTrace) noreturn { |
| 1239 | \\ _ = message; | |
| 1240 | \\ _ = stack_trace; | |
| 1087 | 1241 | \\ std.os.exit(126); |
| 1088 | 1242 | \\} |
| 1089 | 1243 | \\ |
| 1090 | 1244 | \\pub fn main() void { |
| 1091 | 1245 | \\ var ptr: [*c]const u32 = null; |
| 1092 | 1246 | \\ var slice = ptr[0..3]; |
| 1247 | \\ _ = slice; | |
| 1093 | 1248 | \\} |
| 1094 | 1249 | ); |
| 1095 | 1250 | } |
test/translate_c.zig+229| ... | ... | @@ -12,9 +12,11 @@ pub fn addCases(cases: *tests.TranslateCContext) void { |
| 12 | 12 | , &[_][]const u8{ |
| 13 | 13 | \\pub export fn foo(arg_x: c_ulong) c_ulong { |
| 14 | 14 | \\ var x = arg_x; |
| 15 | \\ _ = x; | |
| 15 | 16 | \\ const union_unnamed_1 = extern union { |
| 16 | 17 | \\ _x: c_ulong, |
| 17 | 18 | \\ }; |
| 19 | \\ _ = union_unnamed_1; | |
| 18 | 20 | \\ return (union_unnamed_1{ |
| 19 | 21 | \\ ._x = x, |
| 20 | 22 | \\ })._x; |
| ... | ... | @@ -54,8 +56,10 @@ pub fn addCases(cases: *tests.TranslateCContext) void { |
| 54 | 56 | \\pub export fn foo() void { |
| 55 | 57 | \\ while (true) if (true) { |
| 56 | 58 | \\ var a: c_int = 1; |
| 59 | \\ _ = a; | |
| 57 | 60 | \\ } else { |
| 58 | 61 | \\ var b: c_int = 2; |
| 62 | \\ _ = b; | |
| 59 | 63 | \\ }; |
| 60 | 64 | \\ if (true) if (true) {}; |
| 61 | 65 | \\} |
| ... | ... | @@ -71,6 +75,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void { |
| 71 | 75 | \\pub extern fn bar(...) c_int; |
| 72 | 76 | \\pub export fn foo() void { |
| 73 | 77 | \\ var a: c_int = undefined; |
| 78 | \\ _ = a; | |
| 74 | 79 | \\ if (a != 0) a = 2 else _ = bar(); |
| 75 | 80 | \\} |
| 76 | 81 | }); |
| ... | ... | @@ -123,22 +128,26 @@ pub fn addCases(cases: *tests.TranslateCContext) void { |
| 123 | 128 | \\ B: c_int, |
| 124 | 129 | \\ C: c_int, |
| 125 | 130 | \\ }; |
| 131 | \\ _ = struct_Foo; | |
| 126 | 132 | \\ var a: struct_Foo = struct_Foo{ |
| 127 | 133 | \\ .A = @as(c_int, 0), |
| 128 | 134 | \\ .B = 0, |
| 129 | 135 | \\ .C = 0, |
| 130 | 136 | \\ }; |
| 137 | \\ _ = a; | |
| 131 | 138 | \\ { |
| 132 | 139 | \\ const struct_Foo_1 = extern struct { |
| 133 | 140 | \\ A: c_int, |
| 134 | 141 | \\ B: c_int, |
| 135 | 142 | \\ C: c_int, |
| 136 | 143 | \\ }; |
| 144 | \\ _ = struct_Foo_1; | |
| 137 | 145 | \\ var a_2: struct_Foo_1 = struct_Foo_1{ |
| 138 | 146 | \\ .A = @as(c_int, 0), |
| 139 | 147 | \\ .B = 0, |
| 140 | 148 | \\ .C = 0, |
| 141 | 149 | \\ }; |
| 150 | \\ _ = a_2; | |
| 142 | 151 | \\ } |
| 143 | 152 | \\} |
| 144 | 153 | }); |
| ... | ... | @@ -167,20 +176,26 @@ pub fn addCases(cases: *tests.TranslateCContext) void { |
| 167 | 176 | \\ B: c_int, |
| 168 | 177 | \\ C: c_int, |
| 169 | 178 | \\ }; |
| 179 | \\ _ = union_unnamed_1; | |
| 170 | 180 | \\ const Foo = union_unnamed_1; |
| 181 | \\ _ = Foo; | |
| 171 | 182 | \\ var a: Foo = Foo{ |
| 172 | 183 | \\ .A = @as(c_int, 0), |
| 173 | 184 | \\ }; |
| 185 | \\ _ = a; | |
| 174 | 186 | \\ { |
| 175 | 187 | \\ const union_unnamed_2 = extern union { |
| 176 | 188 | \\ A: c_int, |
| 177 | 189 | \\ B: c_int, |
| 178 | 190 | \\ C: c_int, |
| 179 | 191 | \\ }; |
| 192 | \\ _ = union_unnamed_2; | |
| 180 | 193 | \\ const Foo_1 = union_unnamed_2; |
| 194 | \\ _ = Foo_1; | |
| 181 | 195 | \\ var a_2: Foo_1 = Foo_1{ |
| 182 | 196 | \\ .A = @as(c_int, 0), |
| 183 | 197 | \\ }; |
| 198 | \\ _ = a_2; | |
| 184 | 199 | \\ } |
| 185 | 200 | \\} |
| 186 | 201 | }); |
| ... | ... | @@ -190,6 +205,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void { |
| 190 | 205 | \\#define MEM_PHYSICAL_TO_K0(x) (void*)((uint32_t)(x) + SYS_BASE_CACHED) |
| 191 | 206 | , &[_][]const u8{ |
| 192 | 207 | \\pub inline fn MEM_PHYSICAL_TO_K0(x: anytype) ?*c_void { |
| 208 | \\ _ = x; | |
| 193 | 209 | \\ return @import("std").zig.c_translation.cast(?*c_void, @import("std").zig.c_translation.cast(u32, x) + SYS_BASE_CACHED); |
| 194 | 210 | \\} |
| 195 | 211 | }); |
| ... | ... | @@ -231,6 +247,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void { |
| 231 | 247 | \\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)); |
| 232 | 248 | , |
| 233 | 249 | \\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; | |
| 234 | 251 | \\ 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)); |
| 235 | 252 | \\} |
| 236 | 253 | }); |
| ... | ... | @@ -246,6 +263,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void { |
| 246 | 263 | \\ const bar_1 = struct { |
| 247 | 264 | \\ threadlocal var static: c_int = 2; |
| 248 | 265 | \\ }; |
| 266 | \\ _ = bar_1; | |
| 249 | 267 | \\ return 0; |
| 250 | 268 | \\} |
| 251 | 269 | }); |
| ... | ... | @@ -264,6 +282,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void { |
| 264 | 282 | \\} |
| 265 | 283 | \\pub export fn bar() c_int { |
| 266 | 284 | \\ var a: c_int = 2; |
| 285 | \\ _ = a; | |
| 267 | 286 | \\ return 0; |
| 268 | 287 | \\} |
| 269 | 288 | \\pub export fn baz() c_int { |
| ... | ... | @@ -278,6 +297,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void { |
| 278 | 297 | , &[_][]const u8{ |
| 279 | 298 | \\pub export fn main() void { |
| 280 | 299 | \\ var a: c_int = @bitCast(c_int, @truncate(c_uint, @alignOf(c_int))); |
| 300 | \\ _ = a; | |
| 281 | 301 | \\} |
| 282 | 302 | }); |
| 283 | 303 | |
| ... | ... | @@ -308,6 +328,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void { |
| 308 | 328 | \\pub const Color = struct_Color; |
| 309 | 329 | , |
| 310 | 330 | \\pub inline fn CLITERAL(type_1: anytype) @TypeOf(type_1) { |
| 331 | \\ _ = type_1; | |
| 311 | 332 | \\ return type_1; |
| 312 | 333 | \\} |
| 313 | 334 | , |
| ... | ... | @@ -325,6 +346,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void { |
| 325 | 346 | \\}; |
| 326 | 347 | , |
| 327 | 348 | \\pub inline fn A(_x: anytype) MyCStruct { |
| 349 | \\ _ = _x; | |
| 328 | 350 | \\ return @import("std").mem.zeroInit(MyCStruct, .{ |
| 329 | 351 | \\ .x = _x, |
| 330 | 352 | \\ }); |
| ... | ... | @@ -355,6 +377,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void { |
| 355 | 377 | \\#define __ferror_unlocked_body(_fp) (((_fp)->_flags & _IO_ERR_SEEN) != 0) |
| 356 | 378 | , &[_][]const u8{ |
| 357 | 379 | \\pub inline fn __ferror_unlocked_body(_fp: anytype) @TypeOf((_fp.*._flags & _IO_ERR_SEEN) != @as(c_int, 0)) { |
| 380 | \\ _ = _fp; | |
| 358 | 381 | \\ return (_fp.*._flags & _IO_ERR_SEEN) != @as(c_int, 0); |
| 359 | 382 | \\} |
| 360 | 383 | }); |
| ... | ... | @@ -364,6 +387,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void { |
| 364 | 387 | \\#define BAR 1 && 2 > 4 |
| 365 | 388 | , &[_][]const u8{ |
| 366 | 389 | \\pub inline fn FOO(x: anytype) @TypeOf(@boolToInt(x >= @as(c_int, 0)) + @boolToInt(x >= @as(c_int, 0))) { |
| 390 | \\ _ = x; | |
| 367 | 391 | \\ return @boolToInt(x >= @as(c_int, 0)) + @boolToInt(x >= @as(c_int, 0)); |
| 368 | 392 | \\} |
| 369 | 393 | , |
| ... | ... | @@ -426,6 +450,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void { |
| 426 | 450 | \\}; |
| 427 | 451 | , |
| 428 | 452 | \\pub inline fn bar(x: anytype) @TypeOf(baz(@as(c_int, 1), @as(c_int, 2))) { |
| 453 | \\ _ = x; | |
| 429 | 454 | \\ return blk: { |
| 430 | 455 | \\ _ = &x; |
| 431 | 456 | \\ _ = @as(c_int, 3); |
| ... | ... | @@ -555,6 +580,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void { |
| 555 | 580 | \\}; |
| 556 | 581 | \\pub export fn foo(arg_x: [*c]outer) void { |
| 557 | 582 | \\ var x = arg_x; |
| 583 | \\ _ = x; | |
| 558 | 584 | \\ x.*.unnamed_0.unnamed_0.y = @bitCast(c_int, @as(c_uint, x.*.unnamed_0.x)); |
| 559 | 585 | \\} |
| 560 | 586 | }); |
| ... | ... | @@ -641,7 +667,9 @@ pub fn addCases(cases: *tests.TranslateCContext) void { |
| 641 | 667 | \\pub const struct_opaque_2 = opaque {}; |
| 642 | 668 | \\pub export fn function(arg_opaque_1: ?*struct_opaque) void { |
| 643 | 669 | \\ var opaque_1 = arg_opaque_1; |
| 670 | \\ _ = opaque_1; | |
| 644 | 671 | \\ var cast: ?*struct_opaque_2 = @ptrCast(?*struct_opaque_2, opaque_1); |
| 672 | \\ _ = cast; | |
| 645 | 673 | \\} |
| 646 | 674 | }); |
| 647 | 675 | |
| ... | ... | @@ -673,6 +701,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void { |
| 673 | 701 | \\pub export fn my_fn() align(128) void {} |
| 674 | 702 | \\pub export fn other_fn() void { |
| 675 | 703 | \\ var ARR: [16]u8 align(16) = undefined; |
| 704 | \\ _ = ARR; | |
| 676 | 705 | \\} |
| 677 | 706 | }); |
| 678 | 707 | |
| ... | ... | @@ -708,11 +737,17 @@ pub fn addCases(cases: *tests.TranslateCContext) void { |
| 708 | 737 | , &[_][]const u8{ |
| 709 | 738 | \\pub export fn foo() void { |
| 710 | 739 | \\ var a: c_int = undefined; |
| 740 | \\ _ = a; | |
| 711 | 741 | \\ var b: u8 = 123; |
| 742 | \\ _ = b; | |
| 712 | 743 | \\ const c: c_int = undefined; |
| 744 | \\ _ = c; | |
| 713 | 745 | \\ const d: c_uint = @bitCast(c_uint, @as(c_int, 440)); |
| 746 | \\ _ = d; | |
| 714 | 747 | \\ var e: c_int = 10; |
| 748 | \\ _ = e; | |
| 715 | 749 | \\ var f: c_uint = 10; |
| 750 | \\ _ = f; | |
| 716 | 751 | \\} |
| 717 | 752 | }); |
| 718 | 753 | |
| ... | ... | @@ -728,6 +763,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void { |
| 728 | 763 | , &[_][]const u8{ |
| 729 | 764 | \\pub export fn foo() void { |
| 730 | 765 | \\ var a: c_int = undefined; |
| 766 | \\ _ = a; | |
| 731 | 767 | \\ _ = @as(c_int, 1); |
| 732 | 768 | \\ _ = "hey"; |
| 733 | 769 | \\ _ = @as(c_int, 1) + @as(c_int, 1); |
| ... | ... | @@ -771,6 +807,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void { |
| 771 | 807 | \\ const v2 = struct { |
| 772 | 808 | \\ const static: [5:0]u8 = "2.2.2".*; |
| 773 | 809 | \\ }; |
| 810 | \\ _ = v2; | |
| 774 | 811 | \\} |
| 775 | 812 | }); |
| 776 | 813 | |
| ... | ... | @@ -812,7 +849,9 @@ pub fn addCases(cases: *tests.TranslateCContext) void { |
| 812 | 849 | \\pub extern fn foo() void; |
| 813 | 850 | \\pub export fn bar() void { |
| 814 | 851 | \\ var func_ptr: ?*c_void = @ptrCast(?*c_void, foo); |
| 852 | \\ _ = func_ptr; | |
| 815 | 853 | \\ var typed_func_ptr: ?fn () callconv(.C) void = @intToPtr(?fn () callconv(.C) void, @intCast(c_ulong, @ptrToInt(func_ptr))); |
| 854 | \\ _ = typed_func_ptr; | |
| 816 | 855 | \\} |
| 817 | 856 | }); |
| 818 | 857 | |
| ... | ... | @@ -842,8 +881,11 @@ pub fn addCases(cases: *tests.TranslateCContext) void { |
| 842 | 881 | , &[_][]const u8{ |
| 843 | 882 | \\pub export fn s() c_int { |
| 844 | 883 | \\ var a: c_int = undefined; |
| 884 | \\ _ = a; | |
| 845 | 885 | \\ var b: c_int = undefined; |
| 886 | \\ _ = b; | |
| 846 | 887 | \\ var c: c_int = undefined; |
| 888 | \\ _ = c; | |
| 847 | 889 | \\ c = a + b; |
| 848 | 890 | \\ c = a - b; |
| 849 | 891 | \\ c = a * b; |
| ... | ... | @@ -853,8 +895,11 @@ pub fn addCases(cases: *tests.TranslateCContext) void { |
| 853 | 895 | \\} |
| 854 | 896 | \\pub export fn u() c_uint { |
| 855 | 897 | \\ var a: c_uint = undefined; |
| 898 | \\ _ = a; | |
| 856 | 899 | \\ var b: c_uint = undefined; |
| 900 | \\ _ = b; | |
| 857 | 901 | \\ var c: c_uint = undefined; |
| 902 | \\ _ = c; | |
| 858 | 903 | \\ c = a +% b; |
| 859 | 904 | \\ c = a -% b; |
| 860 | 905 | \\ c = a *% b; |
| ... | ... | @@ -1218,6 +1263,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void { |
| 1218 | 1263 | \\pub export fn foo() void { |
| 1219 | 1264 | \\ var a: c_int = undefined; |
| 1220 | 1265 | \\ _ = a; |
| 1266 | \\ _ = a; | |
| 1221 | 1267 | \\} |
| 1222 | 1268 | }); |
| 1223 | 1269 | |
| ... | ... | @@ -1229,6 +1275,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void { |
| 1229 | 1275 | , &[_][]const u8{ |
| 1230 | 1276 | \\pub export fn foo() ?*c_void { |
| 1231 | 1277 | \\ var x: [*c]c_ushort = undefined; |
| 1278 | \\ _ = x; | |
| 1232 | 1279 | \\ return @ptrCast(?*c_void, x); |
| 1233 | 1280 | \\} |
| 1234 | 1281 | }); |
| ... | ... | @@ -1285,6 +1332,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void { |
| 1285 | 1332 | \\pub export fn foo() void { |
| 1286 | 1333 | \\ { |
| 1287 | 1334 | \\ var i: c_int = 0; |
| 1335 | \\ _ = i; | |
| 1288 | 1336 | \\ while (i != 0) : (i += 1) {} |
| 1289 | 1337 | \\ } |
| 1290 | 1338 | \\} |
| ... | ... | @@ -1308,6 +1356,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void { |
| 1308 | 1356 | , &[_][]const u8{ |
| 1309 | 1357 | \\pub export fn foo() void { |
| 1310 | 1358 | \\ var i: c_int = undefined; |
| 1359 | \\ _ = i; | |
| 1311 | 1360 | \\ { |
| 1312 | 1361 | \\ i = 3; |
| 1313 | 1362 | \\ while (i != 0) : (i -= 1) {} |
| ... | ... | @@ -1351,6 +1400,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void { |
| 1351 | 1400 | , &[_][]const u8{ |
| 1352 | 1401 | \\pub export fn ptrcast() [*c]f32 { |
| 1353 | 1402 | \\ var a: [*c]c_int = undefined; |
| 1403 | \\ _ = a; | |
| 1354 | 1404 | \\ return @ptrCast([*c]f32, @alignCast(@import("std").meta.alignment(f32), a)); |
| 1355 | 1405 | \\} |
| 1356 | 1406 | }); |
| ... | ... | @@ -1374,17 +1424,26 @@ pub fn addCases(cases: *tests.TranslateCContext) void { |
| 1374 | 1424 | , &[_][]const u8{ |
| 1375 | 1425 | \\pub export fn test_ptr_cast() void { |
| 1376 | 1426 | \\ var p: ?*c_void = undefined; |
| 1427 | \\ _ = p; | |
| 1377 | 1428 | \\ { |
| 1378 | 1429 | \\ var to_char: [*c]u8 = @ptrCast([*c]u8, @alignCast(@import("std").meta.alignment(u8), p)); |
| 1430 | \\ _ = to_char; | |
| 1379 | 1431 | \\ var to_short: [*c]c_short = @ptrCast([*c]c_short, @alignCast(@import("std").meta.alignment(c_short), p)); |
| 1432 | \\ _ = to_short; | |
| 1380 | 1433 | \\ var to_int: [*c]c_int = @ptrCast([*c]c_int, @alignCast(@import("std").meta.alignment(c_int), p)); |
| 1434 | \\ _ = to_int; | |
| 1381 | 1435 | \\ var to_longlong: [*c]c_longlong = @ptrCast([*c]c_longlong, @alignCast(@import("std").meta.alignment(c_longlong), p)); |
| 1436 | \\ _ = to_longlong; | |
| 1382 | 1437 | \\ } |
| 1383 | 1438 | \\ { |
| 1384 | 1439 | \\ var to_char: [*c]u8 = @ptrCast([*c]u8, @alignCast(@import("std").meta.alignment(u8), p)); |
| 1440 | \\ _ = to_char; | |
| 1385 | 1441 | \\ var to_short: [*c]c_short = @ptrCast([*c]c_short, @alignCast(@import("std").meta.alignment(c_short), p)); |
| 1442 | \\ _ = to_short; | |
| 1386 | 1443 | \\ var to_int: [*c]c_int = @ptrCast([*c]c_int, @alignCast(@import("std").meta.alignment(c_int), p)); |
| 1444 | \\ _ = to_int; | |
| 1387 | 1445 | \\ var to_longlong: [*c]c_longlong = @ptrCast([*c]c_longlong, @alignCast(@import("std").meta.alignment(c_longlong), p)); |
| 1446 | \\ _ = to_longlong; | |
| 1388 | 1447 | \\ } |
| 1389 | 1448 | \\} |
| 1390 | 1449 | }); |
| ... | ... | @@ -1402,8 +1461,11 @@ pub fn addCases(cases: *tests.TranslateCContext) void { |
| 1402 | 1461 | , &[_][]const u8{ |
| 1403 | 1462 | \\pub export fn while_none_bool() c_int { |
| 1404 | 1463 | \\ var a: c_int = undefined; |
| 1464 | \\ _ = a; | |
| 1405 | 1465 | \\ var b: f32 = undefined; |
| 1466 | \\ _ = b; | |
| 1406 | 1467 | \\ var c: ?*c_void = undefined; |
| 1468 | \\ _ = c; | |
| 1407 | 1469 | \\ while (a != 0) return 0; |
| 1408 | 1470 | \\ while (b != 0) return 1; |
| 1409 | 1471 | \\ while (c != null) return 2; |
| ... | ... | @@ -1424,8 +1486,11 @@ pub fn addCases(cases: *tests.TranslateCContext) void { |
| 1424 | 1486 | , &[_][]const u8{ |
| 1425 | 1487 | \\pub export fn for_none_bool() c_int { |
| 1426 | 1488 | \\ var a: c_int = undefined; |
| 1489 | \\ _ = a; | |
| 1427 | 1490 | \\ var b: f32 = undefined; |
| 1491 | \\ _ = b; | |
| 1428 | 1492 | \\ var c: ?*c_void = undefined; |
| 1493 | \\ _ = c; | |
| 1429 | 1494 | \\ while (a != 0) return 0; |
| 1430 | 1495 | \\ while (b != 0) return 1; |
| 1431 | 1496 | \\ while (c != null) return 2; |
| ... | ... | @@ -1462,6 +1527,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void { |
| 1462 | 1527 | , &[_][]const u8{ |
| 1463 | 1528 | \\pub export fn foo() void { |
| 1464 | 1529 | \\ var x: [*c]c_int = undefined; |
| 1530 | \\ _ = x; | |
| 1465 | 1531 | \\ x.* = 1; |
| 1466 | 1532 | \\} |
| 1467 | 1533 | }); |
| ... | ... | @@ -1475,7 +1541,9 @@ pub fn addCases(cases: *tests.TranslateCContext) void { |
| 1475 | 1541 | , &[_][]const u8{ |
| 1476 | 1542 | \\pub export fn foo() c_int { |
| 1477 | 1543 | \\ var x: c_int = 1234; |
| 1544 | \\ _ = x; | |
| 1478 | 1545 | \\ var ptr: [*c]c_int = &x; |
| 1546 | \\ _ = ptr; | |
| 1479 | 1547 | \\ return ptr.*; |
| 1480 | 1548 | \\} |
| 1481 | 1549 | }); |
| ... | ... | @@ -1488,6 +1556,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void { |
| 1488 | 1556 | , &[_][]const u8{ |
| 1489 | 1557 | \\pub export fn foo() c_int { |
| 1490 | 1558 | \\ var x: c_int = undefined; |
| 1559 | \\ _ = x; | |
| 1491 | 1560 | \\ return ~x; |
| 1492 | 1561 | \\} |
| 1493 | 1562 | }); |
| ... | ... | @@ -1505,8 +1574,11 @@ pub fn addCases(cases: *tests.TranslateCContext) void { |
| 1505 | 1574 | , &[_][]const u8{ |
| 1506 | 1575 | \\pub export fn foo() c_int { |
| 1507 | 1576 | \\ var a: c_int = undefined; |
| 1577 | \\ _ = a; | |
| 1508 | 1578 | \\ var b: f32 = undefined; |
| 1579 | \\ _ = b; | |
| 1509 | 1580 | \\ var c: ?*c_void = undefined; |
| 1581 | \\ _ = c; | |
| 1510 | 1582 | \\ return @boolToInt(!(a == @as(c_int, 0))); |
| 1511 | 1583 | \\ return @boolToInt(!(a != 0)); |
| 1512 | 1584 | \\ return @boolToInt(!(b != 0)); |
| ... | ... | @@ -1628,9 +1700,11 @@ pub fn addCases(cases: *tests.TranslateCContext) void { |
| 1628 | 1700 | \\ var arr: [10]u8 = [1]u8{ |
| 1629 | 1701 | \\ 1, |
| 1630 | 1702 | \\ } ++ [1]u8{0} ** 9; |
| 1703 | \\ _ = arr; | |
| 1631 | 1704 | \\ var arr1: [10][*c]u8 = [1][*c]u8{ |
| 1632 | 1705 | \\ null, |
| 1633 | 1706 | \\ } ++ [1][*c]u8{null} ** 9; |
| 1707 | \\ _ = arr1; | |
| 1634 | 1708 | \\} |
| 1635 | 1709 | }); |
| 1636 | 1710 | |
| ... | ... | @@ -1817,10 +1891,13 @@ pub fn addCases(cases: *tests.TranslateCContext) void { |
| 1817 | 1891 | \\pub extern var c: c_int; |
| 1818 | 1892 | , |
| 1819 | 1893 | \\pub inline fn BASIC(c_1: anytype) @TypeOf(c_1 * @as(c_int, 2)) { |
| 1894 | \\ _ = c_1; | |
| 1820 | 1895 | \\ return c_1 * @as(c_int, 2); |
| 1821 | 1896 | \\} |
| 1822 | 1897 | , |
| 1823 | 1898 | \\pub inline fn FOO(L: anytype, b: anytype) @TypeOf(L + b) { |
| 1899 | \\ _ = L; | |
| 1900 | \\ _ = b; | |
| 1824 | 1901 | \\ return L + b; |
| 1825 | 1902 | \\} |
| 1826 | 1903 | , |
| ... | ... | @@ -1872,13 +1949,18 @@ pub fn addCases(cases: *tests.TranslateCContext) void { |
| 1872 | 1949 | \\pub var c: c_int = 4; |
| 1873 | 1950 | \\pub export fn foo(arg_c_1: u8) void { |
| 1874 | 1951 | \\ var c_1 = arg_c_1; |
| 1952 | \\ _ = c_1; | |
| 1875 | 1953 | \\ var a_2: c_int = undefined; |
| 1954 | \\ _ = a_2; | |
| 1876 | 1955 | \\ var b_3: u8 = 123; |
| 1956 | \\ _ = b_3; | |
| 1877 | 1957 | \\ b_3 = @bitCast(u8, @truncate(i8, a_2)); |
| 1878 | 1958 | \\ { |
| 1879 | 1959 | \\ var d: c_int = 5; |
| 1960 | \\ _ = d; | |
| 1880 | 1961 | \\ } |
| 1881 | 1962 | \\ var d: c_uint = @bitCast(c_uint, @as(c_int, 440)); |
| 1963 | \\ _ = d; | |
| 1882 | 1964 | \\} |
| 1883 | 1965 | }); |
| 1884 | 1966 | |
| ... | ... | @@ -1912,7 +1994,9 @@ pub fn addCases(cases: *tests.TranslateCContext) void { |
| 1912 | 1994 | , &[_][]const u8{ |
| 1913 | 1995 | \\pub export fn foo() void { |
| 1914 | 1996 | \\ var a: c_int = undefined; |
| 1997 | \\ _ = a; | |
| 1915 | 1998 | \\ var b: c_int = undefined; |
| 1999 | \\ _ = b; | |
| 1916 | 2000 | \\ a = blk: { |
| 1917 | 2001 | \\ const tmp = @as(c_int, 2); |
| 1918 | 2002 | \\ b = tmp; |
| ... | ... | @@ -1942,11 +2026,13 @@ pub fn addCases(cases: *tests.TranslateCContext) void { |
| 1942 | 2026 | , &[_][]const u8{ |
| 1943 | 2027 | \\pub export fn foo() c_int { |
| 1944 | 2028 | \\ var a: c_int = 5; |
| 2029 | \\ _ = a; | |
| 1945 | 2030 | \\ while (true) { |
| 1946 | 2031 | \\ a = 2; |
| 1947 | 2032 | \\ } |
| 1948 | 2033 | \\ while (true) { |
| 1949 | 2034 | \\ var a_1: c_int = 4; |
| 2035 | \\ _ = a_1; | |
| 1950 | 2036 | \\ a_1 = 9; |
| 1951 | 2037 | \\ return blk: { |
| 1952 | 2038 | \\ _ = @as(c_int, 6); |
| ... | ... | @@ -1955,6 +2041,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void { |
| 1955 | 2041 | \\ } |
| 1956 | 2042 | \\ while (true) { |
| 1957 | 2043 | \\ var a_1: c_int = 2; |
| 2044 | \\ _ = a_1; | |
| 1958 | 2045 | \\ a_1 = 12; |
| 1959 | 2046 | \\ } |
| 1960 | 2047 | \\ while (true) { |
| ... | ... | @@ -1976,9 +2063,12 @@ pub fn addCases(cases: *tests.TranslateCContext) void { |
| 1976 | 2063 | \\pub export fn foo() void { |
| 1977 | 2064 | \\ { |
| 1978 | 2065 | \\ var i: c_int = 2; |
| 2066 | \\ _ = i; | |
| 1979 | 2067 | \\ var b: c_int = 4; |
| 2068 | \\ _ = b; | |
| 1980 | 2069 | \\ while ((i + @as(c_int, 2)) != 0) : (i = 2) { |
| 1981 | 2070 | \\ var a: c_int = 2; |
| 2071 | \\ _ = a; | |
| 1982 | 2072 | \\ _ = blk: { |
| 1983 | 2073 | \\ _ = blk_1: { |
| 1984 | 2074 | \\ a = 6; |
| ... | ... | @@ -1989,6 +2079,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void { |
| 1989 | 2079 | \\ } |
| 1990 | 2080 | \\ } |
| 1991 | 2081 | \\ var i: u8 = 2; |
| 2082 | \\ _ = i; | |
| 1992 | 2083 | \\} |
| 1993 | 2084 | }); |
| 1994 | 2085 | |
| ... | ... | @@ -2061,7 +2152,9 @@ pub fn addCases(cases: *tests.TranslateCContext) void { |
| 2061 | 2152 | , &[_][]const u8{ |
| 2062 | 2153 | \\pub export fn switch_fn(arg_i: c_int) void { |
| 2063 | 2154 | \\ var i = arg_i; |
| 2155 | \\ _ = i; | |
| 2064 | 2156 | \\ var res: c_int = 0; |
| 2157 | \\ _ = res; | |
| 2065 | 2158 | \\ while (true) { |
| 2066 | 2159 | \\ switch (i) { |
| 2067 | 2160 | \\ @as(c_int, 0) => { |
| ... | ... | @@ -2150,7 +2243,9 @@ pub fn addCases(cases: *tests.TranslateCContext) void { |
| 2150 | 2243 | , &[_][]const u8{ |
| 2151 | 2244 | \\pub export fn max(arg_a: c_int) void { |
| 2152 | 2245 | \\ var a = arg_a; |
| 2246 | \\ _ = a; | |
| 2153 | 2247 | \\ var tmp: c_int = undefined; |
| 2248 | \\ _ = tmp; | |
| 2154 | 2249 | \\ tmp = a; |
| 2155 | 2250 | \\ a = tmp; |
| 2156 | 2251 | \\} |
| ... | ... | @@ -2164,8 +2259,11 @@ pub fn addCases(cases: *tests.TranslateCContext) void { |
| 2164 | 2259 | , &[_][]const u8{ |
| 2165 | 2260 | \\pub export fn max(arg_a: c_int) void { |
| 2166 | 2261 | \\ var a = arg_a; |
| 2262 | \\ _ = a; | |
| 2167 | 2263 | \\ var b: c_int = undefined; |
| 2264 | \\ _ = b; | |
| 2168 | 2265 | \\ var c: c_int = undefined; |
| 2266 | \\ _ = c; | |
| 2169 | 2267 | \\ c = blk: { |
| 2170 | 2268 | \\ const tmp = a; |
| 2171 | 2269 | \\ b = tmp; |
| ... | ... | @@ -2194,6 +2292,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void { |
| 2194 | 2292 | , &[_][]const u8{ |
| 2195 | 2293 | \\pub export fn float_to_int(arg_a: f32) c_int { |
| 2196 | 2294 | \\ var a = arg_a; |
| 2295 | \\ _ = a; | |
| 2197 | 2296 | \\ return @floatToInt(c_int, a); |
| 2198 | 2297 | \\} |
| 2199 | 2298 | }); |
| ... | ... | @@ -2217,16 +2316,27 @@ pub fn addCases(cases: *tests.TranslateCContext) void { |
| 2217 | 2316 | , &[_][]const u8{ |
| 2218 | 2317 | \\pub export fn escapes() [*c]const u8 { |
| 2219 | 2318 | \\ var a: u8 = '\''; |
| 2319 | \\ _ = a; | |
| 2220 | 2320 | \\ var b: u8 = '\\'; |
| 2321 | \\ _ = b; | |
| 2221 | 2322 | \\ var c: u8 = '\x07'; |
| 2323 | \\ _ = c; | |
| 2222 | 2324 | \\ var d: u8 = '\x08'; |
| 2325 | \\ _ = d; | |
| 2223 | 2326 | \\ var e: u8 = '\x0c'; |
| 2327 | \\ _ = e; | |
| 2224 | 2328 | \\ var f: u8 = '\n'; |
| 2329 | \\ _ = f; | |
| 2225 | 2330 | \\ var g: u8 = '\r'; |
| 2331 | \\ _ = g; | |
| 2226 | 2332 | \\ var h: u8 = '\t'; |
| 2333 | \\ _ = h; | |
| 2227 | 2334 | \\ var i: u8 = '\x0b'; |
| 2335 | \\ _ = i; | |
| 2228 | 2336 | \\ var j: u8 = '\x00'; |
| 2337 | \\ _ = j; | |
| 2229 | 2338 | \\ var k: u8 = '"'; |
| 2339 | \\ _ = k; | |
| 2230 | 2340 | \\ return "'\\\x07\x08\x0c\n\r\t\x0b\x00\""; |
| 2231 | 2341 | \\} |
| 2232 | 2342 | }); |
| ... | ... | @@ -2246,11 +2356,13 @@ pub fn addCases(cases: *tests.TranslateCContext) void { |
| 2246 | 2356 | , &[_][]const u8{ |
| 2247 | 2357 | \\pub export fn foo() void { |
| 2248 | 2358 | \\ var a: c_int = 2; |
| 2359 | \\ _ = a; | |
| 2249 | 2360 | \\ while (true) { |
| 2250 | 2361 | \\ a = a - @as(c_int, 1); |
| 2251 | 2362 | \\ if (!(a != 0)) break; |
| 2252 | 2363 | \\ } |
| 2253 | 2364 | \\ var b: c_int = 2; |
| 2365 | \\ _ = b; | |
| 2254 | 2366 | \\ while (true) { |
| 2255 | 2367 | \\ b = b - @as(c_int, 1); |
| 2256 | 2368 | \\ if (!(b != 0)) break; |
| ... | ... | @@ -2291,21 +2403,37 @@ pub fn addCases(cases: *tests.TranslateCContext) void { |
| 2291 | 2403 | \\pub const SomeTypedef = c_int; |
| 2292 | 2404 | \\pub export fn and_or_non_bool(arg_a: c_int, arg_b: f32, arg_c: ?*c_void) c_int { |
| 2293 | 2405 | \\ var a = arg_a; |
| 2406 | \\ _ = a; | |
| 2294 | 2407 | \\ var b = arg_b; |
| 2408 | \\ _ = b; | |
| 2295 | 2409 | \\ var c = arg_c; |
| 2410 | \\ _ = c; | |
| 2296 | 2411 | \\ var d: enum_Foo = @bitCast(c_uint, FooA); |
| 2412 | \\ _ = d; | |
| 2297 | 2413 | \\ var e: c_int = @boolToInt((a != 0) and (b != 0)); |
| 2414 | \\ _ = e; | |
| 2298 | 2415 | \\ var f: c_int = @boolToInt((b != 0) and (c != null)); |
| 2416 | \\ _ = f; | |
| 2299 | 2417 | \\ var g: c_int = @boolToInt((a != 0) and (c != null)); |
| 2418 | \\ _ = g; | |
| 2300 | 2419 | \\ var h: c_int = @boolToInt((a != 0) or (b != 0)); |
| 2420 | \\ _ = h; | |
| 2301 | 2421 | \\ var i: c_int = @boolToInt((b != 0) or (c != null)); |
| 2422 | \\ _ = i; | |
| 2302 | 2423 | \\ var j: c_int = @boolToInt((a != 0) or (c != null)); |
| 2424 | \\ _ = j; | |
| 2303 | 2425 | \\ var k: c_int = @boolToInt((a != 0) or (@bitCast(c_int, d) != 0)); |
| 2426 | \\ _ = k; | |
| 2304 | 2427 | \\ var l: c_int = @boolToInt((@bitCast(c_int, d) != 0) and (b != 0)); |
| 2428 | \\ _ = l; | |
| 2305 | 2429 | \\ var m: c_int = @boolToInt((c != null) or (d != 0)); |
| 2430 | \\ _ = m; | |
| 2306 | 2431 | \\ var td: SomeTypedef = 44; |
| 2432 | \\ _ = td; | |
| 2307 | 2433 | \\ var o: c_int = @boolToInt((td != 0) or (b != 0)); |
| 2434 | \\ _ = o; | |
| 2308 | 2435 | \\ var p: c_int = @boolToInt((c != null) and (td != 0)); |
| 2436 | \\ _ = p; | |
| 2309 | 2437 | \\ return (((((((((e + f) + g) + h) + i) + j) + k) + l) + m) + o) + p; |
| 2310 | 2438 | \\} |
| 2311 | 2439 | , |
| ... | ... | @@ -2345,7 +2473,9 @@ pub fn addCases(cases: *tests.TranslateCContext) void { |
| 2345 | 2473 | , &[_][]const u8{ |
| 2346 | 2474 | \\pub export fn max(arg_a: c_int, arg_b: c_int) c_int { |
| 2347 | 2475 | \\ var a = arg_a; |
| 2476 | \\ _ = a; | |
| 2348 | 2477 | \\ var b = arg_b; |
| 2478 | \\ _ = b; | |
| 2349 | 2479 | \\ return (a & b) ^ (a | b); |
| 2350 | 2480 | \\} |
| 2351 | 2481 | }); |
| ... | ... | @@ -2364,14 +2494,23 @@ pub fn addCases(cases: *tests.TranslateCContext) void { |
| 2364 | 2494 | , &[_][]const u8{ |
| 2365 | 2495 | \\pub export fn test_comparisons(arg_a: c_int, arg_b: c_int) c_int { |
| 2366 | 2496 | \\ var a = arg_a; |
| 2497 | \\ _ = a; | |
| 2367 | 2498 | \\ var b = arg_b; |
| 2499 | \\ _ = b; | |
| 2368 | 2500 | \\ var c: c_int = @boolToInt(a < b); |
| 2501 | \\ _ = c; | |
| 2369 | 2502 | \\ var d: c_int = @boolToInt(a > b); |
| 2503 | \\ _ = d; | |
| 2370 | 2504 | \\ var e: c_int = @boolToInt(a <= b); |
| 2505 | \\ _ = e; | |
| 2371 | 2506 | \\ var f: c_int = @boolToInt(a >= b); |
| 2507 | \\ _ = f; | |
| 2372 | 2508 | \\ var g: c_int = @boolToInt(c < d); |
| 2509 | \\ _ = g; | |
| 2373 | 2510 | \\ var h: c_int = @boolToInt(e < f); |
| 2511 | \\ _ = h; | |
| 2374 | 2512 | \\ var i: c_int = @boolToInt(g < h); |
| 2513 | \\ _ = i; | |
| 2375 | 2514 | \\ return i; |
| 2376 | 2515 | \\} |
| 2377 | 2516 | }); |
| ... | ... | @@ -2387,7 +2526,9 @@ pub fn addCases(cases: *tests.TranslateCContext) void { |
| 2387 | 2526 | , &[_][]const u8{ |
| 2388 | 2527 | \\pub export fn max(arg_a: c_int, arg_b: c_int) c_int { |
| 2389 | 2528 | \\ var a = arg_a; |
| 2529 | \\ _ = a; | |
| 2390 | 2530 | \\ var b = arg_b; |
| 2531 | \\ _ = b; | |
| 2391 | 2532 | \\ if (a == b) return a; |
| 2392 | 2533 | \\ if (a != b) return b; |
| 2393 | 2534 | \\ return a; |
| ... | ... | @@ -2404,6 +2545,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void { |
| 2404 | 2545 | \\pub const yes = [*c]u8; |
| 2405 | 2546 | \\pub export fn foo() void { |
| 2406 | 2547 | \\ var a: yes = undefined; |
| 2548 | \\ _ = a; | |
| 2407 | 2549 | \\ if (a != null) { |
| 2408 | 2550 | \\ _ = @as(c_int, 2); |
| 2409 | 2551 | \\ } |
| ... | ... | @@ -2423,6 +2565,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void { |
| 2423 | 2565 | \\ return blk: { |
| 2424 | 2566 | \\ var a: c_int = 1; |
| 2425 | 2567 | \\ _ = a; |
| 2568 | \\ _ = a; | |
| 2426 | 2569 | \\ break :blk a; |
| 2427 | 2570 | \\ }; |
| 2428 | 2571 | \\} |
| ... | ... | @@ -2448,6 +2591,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void { |
| 2448 | 2591 | \\pub export var b: f32 = 2.0; |
| 2449 | 2592 | \\pub export fn foo() void { |
| 2450 | 2593 | \\ var c: [*c]struct_Foo = undefined; |
| 2594 | \\ _ = c; | |
| 2451 | 2595 | \\ _ = a.b; |
| 2452 | 2596 | \\ _ = c.*.b; |
| 2453 | 2597 | \\} |
| ... | ... | @@ -2467,6 +2611,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void { |
| 2467 | 2611 | \\pub export var array: [100]c_int = [1]c_int{0} ** 100; |
| 2468 | 2612 | \\pub export fn foo(arg_index: c_int) c_int { |
| 2469 | 2613 | \\ var index = arg_index; |
| 2614 | \\ _ = index; | |
| 2470 | 2615 | \\ return array[@intCast(c_uint, index)]; |
| 2471 | 2616 | \\} |
| 2472 | 2617 | , |
| ... | ... | @@ -2481,7 +2626,9 @@ pub fn addCases(cases: *tests.TranslateCContext) void { |
| 2481 | 2626 | , &[_][]const u8{ |
| 2482 | 2627 | \\pub export fn foo() void { |
| 2483 | 2628 | \\ var a: [10]c_int = undefined; |
| 2629 | \\ _ = a; | |
| 2484 | 2630 | \\ var i: c_int = 0; |
| 2631 | \\ _ = i; | |
| 2485 | 2632 | \\ a[@intCast(c_uint, i)] = 0; |
| 2486 | 2633 | \\} |
| 2487 | 2634 | }); |
| ... | ... | @@ -2494,7 +2641,9 @@ pub fn addCases(cases: *tests.TranslateCContext) void { |
| 2494 | 2641 | , &[_][]const u8{ |
| 2495 | 2642 | \\pub export fn foo() void { |
| 2496 | 2643 | \\ var a: [10]c_longlong = undefined; |
| 2644 | \\ _ = a; | |
| 2497 | 2645 | \\ var i: c_longlong = 0; |
| 2646 | \\ _ = i; | |
| 2498 | 2647 | \\ a[@intCast(usize, i)] = 0; |
| 2499 | 2648 | \\} |
| 2500 | 2649 | }); |
| ... | ... | @@ -2507,7 +2656,9 @@ pub fn addCases(cases: *tests.TranslateCContext) void { |
| 2507 | 2656 | , &[_][]const u8{ |
| 2508 | 2657 | \\pub export fn foo() void { |
| 2509 | 2658 | \\ var a: [10]c_uint = undefined; |
| 2659 | \\ _ = a; | |
| 2510 | 2660 | \\ var i: c_uint = 0; |
| 2661 | \\ _ = i; | |
| 2511 | 2662 | \\ a[i] = 0; |
| 2512 | 2663 | \\} |
| 2513 | 2664 | }); |
| ... | ... | @@ -2516,6 +2667,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void { |
| 2516 | 2667 | \\#define CALL(arg) bar(arg) |
| 2517 | 2668 | , &[_][]const u8{ |
| 2518 | 2669 | \\pub inline fn CALL(arg: anytype) @TypeOf(bar(arg)) { |
| 2670 | \\ _ = arg; | |
| 2519 | 2671 | \\ return bar(arg); |
| 2520 | 2672 | \\} |
| 2521 | 2673 | }); |
| ... | ... | @@ -2524,6 +2676,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void { |
| 2524 | 2676 | \\#define CALL(arg) bar() |
| 2525 | 2677 | , &[_][]const u8{ |
| 2526 | 2678 | \\pub inline fn CALL(arg: anytype) @TypeOf(bar()) { |
| 2679 | \\ _ = arg; | |
| 2527 | 2680 | \\ return bar(); |
| 2528 | 2681 | \\} |
| 2529 | 2682 | }); |
| ... | ... | @@ -2539,7 +2692,9 @@ pub fn addCases(cases: *tests.TranslateCContext) void { |
| 2539 | 2692 | , &[_][]const u8{ |
| 2540 | 2693 | \\pub export fn max(arg_a: c_int, arg_b: c_int) c_int { |
| 2541 | 2694 | \\ var a = arg_a; |
| 2695 | \\ _ = a; | |
| 2542 | 2696 | \\ var b = arg_b; |
| 2697 | \\ _ = b; | |
| 2543 | 2698 | \\ if ((a < b) or (a == b)) return b; |
| 2544 | 2699 | \\ if ((a >= b) and (a == b)) return a; |
| 2545 | 2700 | \\ return a; |
| ... | ... | @@ -2561,7 +2716,9 @@ pub fn addCases(cases: *tests.TranslateCContext) void { |
| 2561 | 2716 | , &[_][]const u8{ |
| 2562 | 2717 | \\pub export fn max(arg_a: c_int, arg_b: c_int) c_int { |
| 2563 | 2718 | \\ var a = arg_a; |
| 2719 | \\ _ = a; | |
| 2564 | 2720 | \\ var b = arg_b; |
| 2721 | \\ _ = b; | |
| 2565 | 2722 | \\ if (a < b) return b; |
| 2566 | 2723 | \\ if (a < b) return b else return a; |
| 2567 | 2724 | \\ if (a < b) {} else {} |
| ... | ... | @@ -2582,12 +2739,14 @@ pub fn addCases(cases: *tests.TranslateCContext) void { |
| 2582 | 2739 | \\pub export fn foo() void { |
| 2583 | 2740 | \\ if (true) { |
| 2584 | 2741 | \\ var a: c_int = 2; |
| 2742 | \\ _ = a; | |
| 2585 | 2743 | \\ } |
| 2586 | 2744 | \\ if ((blk: { |
| 2587 | 2745 | \\ _ = @as(c_int, 2); |
| 2588 | 2746 | \\ break :blk @as(c_int, 5); |
| 2589 | 2747 | \\ }) != 0) { |
| 2590 | 2748 | \\ var a: c_int = 2; |
| 2749 | \\ _ = a; | |
| 2591 | 2750 | \\ } |
| 2592 | 2751 | \\} |
| 2593 | 2752 | }); |
| ... | ... | @@ -2610,9 +2769,13 @@ pub fn addCases(cases: *tests.TranslateCContext) void { |
| 2610 | 2769 | \\; |
| 2611 | 2770 | \\pub export fn if_none_bool(arg_a: c_int, arg_b: f32, arg_c: ?*c_void, arg_d: enum_SomeEnum) c_int { |
| 2612 | 2771 | \\ var a = arg_a; |
| 2772 | \\ _ = a; | |
| 2613 | 2773 | \\ var b = arg_b; |
| 2774 | \\ _ = b; | |
| 2614 | 2775 | \\ var c = arg_c; |
| 2776 | \\ _ = c; | |
| 2615 | 2777 | \\ var d = arg_d; |
| 2778 | \\ _ = d; | |
| 2616 | 2779 | \\ if (a != 0) return 0; |
| 2617 | 2780 | \\ if (b != 0) return 1; |
| 2618 | 2781 | \\ if (c != null) return 2; |
| ... | ... | @@ -2640,6 +2803,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void { |
| 2640 | 2803 | , &[_][]const u8{ |
| 2641 | 2804 | \\pub export fn abs(arg_a: c_int) c_int { |
| 2642 | 2805 | \\ var a = arg_a; |
| 2806 | \\ _ = a; | |
| 2643 | 2807 | \\ return if (a < @as(c_int, 0)) -a else a; |
| 2644 | 2808 | \\} |
| 2645 | 2809 | }); |
| ... | ... | @@ -2660,16 +2824,19 @@ pub fn addCases(cases: *tests.TranslateCContext) void { |
| 2660 | 2824 | , &[_][]const u8{ |
| 2661 | 2825 | \\pub export fn foo1(arg_a: c_uint) c_uint { |
| 2662 | 2826 | \\ var a = arg_a; |
| 2827 | \\ _ = a; | |
| 2663 | 2828 | \\ a +%= 1; |
| 2664 | 2829 | \\ return a; |
| 2665 | 2830 | \\} |
| 2666 | 2831 | \\pub export fn foo2(arg_a: c_int) c_int { |
| 2667 | 2832 | \\ var a = arg_a; |
| 2833 | \\ _ = a; | |
| 2668 | 2834 | \\ a += 1; |
| 2669 | 2835 | \\ return a; |
| 2670 | 2836 | \\} |
| 2671 | 2837 | \\pub export fn foo3(arg_a: [*c]c_int) [*c]c_int { |
| 2672 | 2838 | \\ var a = arg_a; |
| 2839 | \\ _ = a; | |
| 2673 | 2840 | \\ a += 1; |
| 2674 | 2841 | \\ return a; |
| 2675 | 2842 | \\} |
| ... | ... | @@ -2695,7 +2862,9 @@ pub fn addCases(cases: *tests.TranslateCContext) void { |
| 2695 | 2862 | \\} |
| 2696 | 2863 | \\pub export fn bar() void { |
| 2697 | 2864 | \\ var f: ?fn () callconv(.C) void = foo; |
| 2865 | \\ _ = f; | |
| 2698 | 2866 | \\ var b: ?fn () callconv(.C) c_int = baz; |
| 2867 | \\ _ = b; | |
| 2699 | 2868 | \\ f.?(); |
| 2700 | 2869 | \\ f.?(); |
| 2701 | 2870 | \\ foo(); |
| ... | ... | @@ -2721,7 +2890,9 @@ pub fn addCases(cases: *tests.TranslateCContext) void { |
| 2721 | 2890 | , &[_][]const u8{ |
| 2722 | 2891 | \\pub export fn foo() void { |
| 2723 | 2892 | \\ var i: c_int = 0; |
| 2893 | \\ _ = i; | |
| 2724 | 2894 | \\ var u: c_uint = 0; |
| 2895 | \\ _ = u; | |
| 2725 | 2896 | \\ i += 1; |
| 2726 | 2897 | \\ i -= 1; |
| 2727 | 2898 | \\ u +%= 1; |
| ... | ... | @@ -2760,7 +2931,9 @@ pub fn addCases(cases: *tests.TranslateCContext) void { |
| 2760 | 2931 | , &[_][]const u8{ |
| 2761 | 2932 | \\pub export fn log2(arg_a: c_uint) c_int { |
| 2762 | 2933 | \\ var a = arg_a; |
| 2934 | \\ _ = a; | |
| 2763 | 2935 | \\ var i: c_int = 0; |
| 2936 | \\ _ = i; | |
| 2764 | 2937 | \\ while (a > @bitCast(c_uint, @as(c_int, 0))) { |
| 2765 | 2938 | \\ a >>= @intCast(@import("std").math.Log2Int(c_int), @as(c_int, 1)); |
| 2766 | 2939 | \\ } |
| ... | ... | @@ -2780,7 +2953,9 @@ pub fn addCases(cases: *tests.TranslateCContext) void { |
| 2780 | 2953 | , &[_][]const u8{ |
| 2781 | 2954 | \\pub export fn log2(arg_a: u32) c_int { |
| 2782 | 2955 | \\ var a = arg_a; |
| 2956 | \\ _ = a; | |
| 2783 | 2957 | \\ var i: c_int = 0; |
| 2958 | \\ _ = i; | |
| 2784 | 2959 | \\ while (a > @bitCast(c_uint, @as(c_int, 0))) { |
| 2785 | 2960 | \\ a >>= @intCast(@import("std").math.Log2Int(c_int), @as(c_int, 1)); |
| 2786 | 2961 | \\ } |
| ... | ... | @@ -2808,7 +2983,9 @@ pub fn addCases(cases: *tests.TranslateCContext) void { |
| 2808 | 2983 | , &[_][]const u8{ |
| 2809 | 2984 | \\pub export fn foo() void { |
| 2810 | 2985 | \\ var a: c_int = 0; |
| 2986 | \\ _ = a; | |
| 2811 | 2987 | \\ var b: c_uint = 0; |
| 2988 | \\ _ = b; | |
| 2812 | 2989 | \\ a += blk: { |
| 2813 | 2990 | \\ const ref = &a; |
| 2814 | 2991 | \\ ref.* += @as(c_int, 1); |
| ... | ... | @@ -2887,6 +3064,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void { |
| 2887 | 3064 | , &[_][]const u8{ |
| 2888 | 3065 | \\pub export fn foo() void { |
| 2889 | 3066 | \\ var a: c_uint = 0; |
| 3067 | \\ _ = a; | |
| 2890 | 3068 | \\ a +%= blk: { |
| 2891 | 3069 | \\ const ref = &a; |
| 2892 | 3070 | \\ ref.* +%= @bitCast(c_uint, @as(c_int, 1)); |
| ... | ... | @@ -2946,7 +3124,9 @@ pub fn addCases(cases: *tests.TranslateCContext) void { |
| 2946 | 3124 | , &[_][]const u8{ |
| 2947 | 3125 | \\pub export fn foo() void { |
| 2948 | 3126 | \\ var i: c_int = 0; |
| 3127 | \\ _ = i; | |
| 2949 | 3128 | \\ var u: c_uint = 0; |
| 3129 | \\ _ = u; | |
| 2950 | 3130 | \\ i += 1; |
| 2951 | 3131 | \\ i -= 1; |
| 2952 | 3132 | \\ u +%= 1; |
| ... | ... | @@ -3041,6 +3221,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void { |
| 3041 | 3221 | \\pub fn bar() callconv(.C) void {} |
| 3042 | 3222 | \\pub export fn foo(arg_baz: ?fn () callconv(.C) [*c]c_int) void { |
| 3043 | 3223 | \\ var baz = arg_baz; |
| 3224 | \\ _ = baz; | |
| 3044 | 3225 | \\ bar(); |
| 3045 | 3226 | \\ _ = baz.?(); |
| 3046 | 3227 | \\} |
| ... | ... | @@ -3082,6 +3263,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void { |
| 3082 | 3263 | \\#define BAZ (uint32_t)(2) |
| 3083 | 3264 | , &[_][]const u8{ |
| 3084 | 3265 | \\pub inline fn FOO(bar: anytype) @TypeOf(baz(@import("std").zig.c_translation.cast(?*c_void, baz))) { |
| 3266 | \\ _ = bar; | |
| 3085 | 3267 | \\ return baz(@import("std").zig.c_translation.cast(?*c_void, baz)); |
| 3086 | 3268 | \\} |
| 3087 | 3269 | , |
| ... | ... | @@ -3122,10 +3304,14 @@ pub fn addCases(cases: *tests.TranslateCContext) void { |
| 3122 | 3304 | \\#define MAX(a, b) ((b) > (a) ? (b) : (a)) |
| 3123 | 3305 | , &[_][]const u8{ |
| 3124 | 3306 | \\pub inline fn MIN(a: anytype, b: anytype) @TypeOf(if (b < a) b else a) { |
| 3307 | \\ _ = a; | |
| 3308 | \\ _ = b; | |
| 3125 | 3309 | \\ return if (b < a) b else a; |
| 3126 | 3310 | \\} |
| 3127 | 3311 | , |
| 3128 | 3312 | \\pub inline fn MAX(a: anytype, b: anytype) @TypeOf(if (b > a) b else a) { |
| 3313 | \\ _ = a; | |
| 3314 | \\ _ = b; | |
| 3129 | 3315 | \\ return if (b > a) b else a; |
| 3130 | 3316 | \\} |
| 3131 | 3317 | }); |
| ... | ... | @@ -3137,7 +3323,9 @@ pub fn addCases(cases: *tests.TranslateCContext) void { |
| 3137 | 3323 | , &[_][]const u8{ |
| 3138 | 3324 | \\pub export fn foo(arg_p: [*c]c_int, arg_x: c_int) c_int { |
| 3139 | 3325 | \\ var p = arg_p; |
| 3326 | \\ _ = p; | |
| 3140 | 3327 | \\ var x = arg_x; |
| 3328 | \\ _ = x; | |
| 3141 | 3329 | \\ return blk: { |
| 3142 | 3330 | \\ const tmp = x; |
| 3143 | 3331 | \\ (blk_1: { |
| ... | ... | @@ -3164,6 +3352,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void { |
| 3164 | 3352 | \\} |
| 3165 | 3353 | \\pub export fn bar(arg_x: c_long) c_ushort { |
| 3166 | 3354 | \\ var x = arg_x; |
| 3355 | \\ _ = x; | |
| 3167 | 3356 | \\ return @bitCast(c_ushort, @truncate(c_short, x)); |
| 3168 | 3357 | \\} |
| 3169 | 3358 | }); |
| ... | ... | @@ -3176,6 +3365,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void { |
| 3176 | 3365 | , &[_][]const u8{ |
| 3177 | 3366 | \\pub export fn foo(arg_bar_1: c_int) void { |
| 3178 | 3367 | \\ var bar_1 = arg_bar_1; |
| 3368 | \\ _ = bar_1; | |
| 3179 | 3369 | \\ bar_1 = 2; |
| 3180 | 3370 | \\} |
| 3181 | 3371 | \\pub export var bar: c_int = 4; |
| ... | ... | @@ -3189,6 +3379,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void { |
| 3189 | 3379 | , &[_][]const u8{ |
| 3190 | 3380 | \\pub export fn foo(arg_bar_1: c_int) void { |
| 3191 | 3381 | \\ var bar_1 = arg_bar_1; |
| 3382 | \\ _ = bar_1; | |
| 3192 | 3383 | \\ bar_1 = 2; |
| 3193 | 3384 | \\} |
| 3194 | 3385 | , |
| ... | ... | @@ -3218,13 +3409,16 @@ pub fn addCases(cases: *tests.TranslateCContext) void { |
| 3218 | 3409 | , &[_][]const u8{ |
| 3219 | 3410 | \\pub export fn foo(arg_a: [*c]c_int) void { |
| 3220 | 3411 | \\ var a = arg_a; |
| 3412 | \\ _ = a; | |
| 3221 | 3413 | \\} |
| 3222 | 3414 | \\pub export fn bar(arg_a: [*c]const c_int) void { |
| 3223 | 3415 | \\ var a = arg_a; |
| 3416 | \\ _ = a; | |
| 3224 | 3417 | \\ foo(@intToPtr([*c]c_int, @ptrToInt(a))); |
| 3225 | 3418 | \\} |
| 3226 | 3419 | \\pub export fn baz(arg_a: [*c]volatile c_int) void { |
| 3227 | 3420 | \\ var a = arg_a; |
| 3421 | \\ _ = a; | |
| 3228 | 3422 | \\ foo(@intToPtr([*c]c_int, @ptrToInt(a))); |
| 3229 | 3423 | \\} |
| 3230 | 3424 | }); |
| ... | ... | @@ -3239,9 +3433,13 @@ pub fn addCases(cases: *tests.TranslateCContext) void { |
| 3239 | 3433 | , &[_][]const u8{ |
| 3240 | 3434 | \\pub export fn foo(arg_x: bool) bool { |
| 3241 | 3435 | \\ var x = arg_x; |
| 3436 | \\ _ = x; | |
| 3242 | 3437 | \\ var a: bool = @as(c_int, @boolToInt(x)) != @as(c_int, 1); |
| 3438 | \\ _ = a; | |
| 3243 | 3439 | \\ var b: bool = @as(c_int, @boolToInt(a)) != @as(c_int, 0); |
| 3440 | \\ _ = b; | |
| 3244 | 3441 | \\ var c: bool = @ptrToInt(foo) != 0; |
| 3442 | \\ _ = c; | |
| 3245 | 3443 | \\ return foo(@as(c_int, @boolToInt(c)) != @as(c_int, @boolToInt(b))); |
| 3246 | 3444 | \\} |
| 3247 | 3445 | }); |
| ... | ... | @@ -3252,7 +3450,9 @@ pub fn addCases(cases: *tests.TranslateCContext) void { |
| 3252 | 3450 | \\} |
| 3253 | 3451 | , &[_][]const u8{ |
| 3254 | 3452 | \\pub export fn max(x: c_int, arg_y: c_int) c_int { |
| 3453 | \\ _ = x; | |
| 3255 | 3454 | \\ var y = arg_y; |
| 3455 | \\ _ = y; | |
| 3256 | 3456 | \\ return if (x > y) x else y; |
| 3257 | 3457 | \\} |
| 3258 | 3458 | }); |
| ... | ... | @@ -3313,6 +3513,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void { |
| 3313 | 3513 | \\ |
| 3314 | 3514 | , &[_][]const u8{ |
| 3315 | 3515 | \\pub inline fn DefaultScreen(dpy: anytype) @TypeOf(@import("std").zig.c_translation.cast(_XPrivDisplay, dpy).*.default_screen) { |
| 3516 | \\ _ = dpy; | |
| 3316 | 3517 | \\ return @import("std").zig.c_translation.cast(_XPrivDisplay, dpy).*.default_screen; |
| 3317 | 3518 | \\} |
| 3318 | 3519 | }); |
| ... | ... | @@ -3532,6 +3733,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void { |
| 3532 | 3733 | \\ const foo = struct { |
| 3533 | 3734 | \\ var static: struct_FOO = @import("std").mem.zeroes(struct_FOO); |
| 3534 | 3735 | \\ }; |
| 3736 | \\ _ = foo; | |
| 3535 | 3737 | \\ return foo.static.x; |
| 3536 | 3738 | \\} |
| 3537 | 3739 | }); |
| ... | ... | @@ -3544,4 +3746,31 @@ pub fn addCases(cases: *tests.TranslateCContext) void { |
| 3544 | 3746 | \\pub const MAP_FAILED = @import("std").zig.c_translation.cast(?*c_void, -@as(c_int, 1)); |
| 3545 | 3747 | \\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))); |
| 3546 | 3748 | }); |
| 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 | }); | |
| 3547 | 3776 | } |