authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2022-09-12 19:37:03-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2022-09-13 02:04:20-07:00
logf8a9bc57ce1a2a5a1b90e9a33501b64f2dd79019
treeefe1f2ac3ba62ea641687bb82c927826ef72cee1
parent188902a710a64b08762d7731aab81cb695322184

translate-c: lower discards differently

This makes translate-c lower discards as `_ = @TypeOf(foo);` to avoid tripping the "pointless discard" error. Ideally, translate-c would avoid emitting pointless discards, in which case this commit can be reverted, however, that is a separate enhancement.

2 files changed, 78 insertions(+), 65 deletions(-)

src/translate_c/ast.zig+21-8
......@@ -1550,14 +1550,27 @@ fn renderNode(c: *Context, node: Node) Allocator.Error!NodeIndex {
15501550 .main_token = try c.addToken(.identifier, "_"),
15511551 .data = undefined,
15521552 });
1553 return c.addNode(.{
1554 .tag = .assign,
1555 .main_token = try c.addToken(.equal, "="),
1556 .data = .{
1557 .lhs = lhs,
1558 .rhs = try renderNode(c, payload.value),
1559 },
1560 });
1553 const main_token = try c.addToken(.equal, "=");
1554 if (payload.value.tag() == .identifier) {
1555 // Render as `_ = @TypeOf(foo);` to avoid tripping "pointless discard" error.
1556 return c.addNode(.{
1557 .tag = .assign,
1558 .main_token = main_token,
1559 .data = .{
1560 .lhs = lhs,
1561 .rhs = try renderBuiltinCall(c, "@TypeOf", &.{payload.value}),
1562 },
1563 });
1564 } else {
1565 return c.addNode(.{
1566 .tag = .assign,
1567 .main_token = main_token,
1568 .data = .{
1569 .lhs = lhs,
1570 .rhs = try renderNode(c, payload.value),
1571 },
1572 });
1573 }
15611574 },
15621575 .@"while" => {
15631576 const payload = node.castTag(.@"while").?.data;
test/translate_c.zig+57-57
......@@ -116,10 +116,10 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
116116 \\pub export fn foo() void {
117117 \\ while (true) if (true) {
118118 \\ var a: c_int = 1;
119 \\ _ = a;
119 \\ _ = @TypeOf(a);
120120 \\ } else {
121121 \\ var b: c_int = 2;
122 \\ _ = b;
122 \\ _ = @TypeOf(b);
123123 \\ };
124124 \\ if (true) if (true) {};
125125 \\}
......@@ -192,7 +192,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
192192 \\ .B = 0,
193193 \\ .C = 0,
194194 \\ };
195 \\ _ = a;
195 \\ _ = @TypeOf(a);
196196 \\ {
197197 \\ const struct_Foo_1 = extern struct {
198198 \\ A: c_int,
......@@ -204,7 +204,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
204204 \\ .B = 0,
205205 \\ .C = 0,
206206 \\ };
207 \\ _ = a_2;
207 \\ _ = @TypeOf(a_2);
208208 \\ }
209209 \\}
210210 });
......@@ -233,24 +233,24 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
233233 \\ B: c_int,
234234 \\ C: c_int,
235235 \\ };
236 \\ _ = union_unnamed_1;
236 \\ _ = @TypeOf(union_unnamed_1);
237237 \\ const Foo = union_unnamed_1;
238238 \\ var a: Foo = Foo{
239239 \\ .A = @as(c_int, 0),
240240 \\ };
241 \\ _ = a;
241 \\ _ = @TypeOf(a);
242242 \\ {
243243 \\ const union_unnamed_2 = extern union {
244244 \\ A: c_int,
245245 \\ B: c_int,
246246 \\ C: c_int,
247247 \\ };
248 \\ _ = union_unnamed_2;
248 \\ _ = @TypeOf(union_unnamed_2);
249249 \\ const Foo_1 = union_unnamed_2;
250250 \\ var a_2: Foo_1 = Foo_1{
251251 \\ .A = @as(c_int, 0),
252252 \\ };
253 \\ _ = a_2;
253 \\ _ = @TypeOf(a_2);
254254 \\ }
255255 \\}
256256 });
......@@ -318,7 +318,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
318318 \\ const bar_1 = struct {
319319 \\ threadlocal var static: c_int = 2;
320320 \\ };
321 \\ _ = bar_1;
321 \\ _ = @TypeOf(bar_1);
322322 \\ return 0;
323323 \\}
324324 });
......@@ -337,7 +337,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
337337 \\}
338338 \\pub export fn bar() c_int {
339339 \\ var a: c_int = 2;
340 \\ _ = a;
340 \\ _ = @TypeOf(a);
341341 \\ return 0;
342342 \\}
343343 \\pub export fn baz() c_int {
......@@ -352,7 +352,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
352352 , &[_][]const u8{
353353 \\pub export fn main() void {
354354 \\ var a: c_int = @bitCast(c_int, @truncate(c_uint, @alignOf(c_int)));
355 \\ _ = a;
355 \\ _ = @TypeOf(a);
356356 \\}
357357 });
358358
......@@ -500,7 +500,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
500500 \\#define bar(x) (&x, +3, 4 == 4, 5 * 6, baz(1, 2), 2 % 2, baz(1,2))
501501 , &[_][]const u8{
502502 \\pub const foo = blk: {
503 \\ _ = foo;
503 \\ _ = @TypeOf(foo);
504504 \\ break :blk bar;
505505 \\};
506506 ,
......@@ -724,7 +724,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
724724 \\pub export fn function(arg_opaque_1: ?*struct_opaque) void {
725725 \\ var opaque_1 = arg_opaque_1;
726726 \\ var cast: ?*struct_opaque_2 = @ptrCast(?*struct_opaque_2, opaque_1);
727 \\ _ = cast;
727 \\ _ = @TypeOf(cast);
728728 \\}
729729 });
730730
......@@ -761,7 +761,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
761761 \\pub export fn my_fn() align(128) void {}
762762 \\pub export fn other_fn() void {
763763 \\ var ARR: [16]u8 align(16) = undefined;
764 \\ _ = ARR;
764 \\ _ = @TypeOf(ARR);
765765 \\}
766766 });
767767 }
......@@ -798,17 +798,17 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
798798 , &[_][]const u8{
799799 \\pub export fn foo() void {
800800 \\ var a: c_int = undefined;
801 \\ _ = a;
801 \\ _ = @TypeOf(a);
802802 \\ var b: u8 = 123;
803 \\ _ = b;
803 \\ _ = @TypeOf(b);
804804 \\ const c: c_int = undefined;
805 \\ _ = c;
805 \\ _ = @TypeOf(c);
806806 \\ const d: c_uint = @bitCast(c_uint, @as(c_int, 440));
807 \\ _ = d;
807 \\ _ = @TypeOf(d);
808808 \\ var e: c_int = 10;
809 \\ _ = e;
809 \\ _ = @TypeOf(e);
810810 \\ var f: c_uint = 10;
811 \\ _ = f;
811 \\ _ = @TypeOf(f);
812812 \\}
813813 });
814814
......@@ -867,7 +867,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
867867 \\ const v2 = struct {
868868 \\ const static: [5:0]u8 = "2.2.2".*;
869869 \\ };
870 \\ _ = v2;
870 \\ _ = @TypeOf(v2);
871871 \\}
872872 });
873873
......@@ -911,7 +911,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
911911 \\pub export fn bar() void {
912912 \\ var func_ptr: ?*anyopaque = @ptrCast(?*anyopaque, foo);
913913 \\ var typed_func_ptr: ?*const fn () callconv(.C) void = @intToPtr(?*const fn () callconv(.C) void, @intCast(c_ulong, @ptrToInt(func_ptr)));
914 \\ _ = typed_func_ptr;
914 \\ _ = @TypeOf(typed_func_ptr);
915915 \\}
916916 });
917917 }
......@@ -1353,7 +1353,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
13531353 , &[_][]const u8{
13541354 \\pub export fn foo() void {
13551355 \\ var a: c_int = undefined;
1356 \\ _ = a;
1356 \\ _ = @TypeOf(a);
13571357 \\}
13581358 });
13591359
......@@ -1524,23 +1524,23 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
15241524 \\ var p: ?*anyopaque = undefined;
15251525 \\ {
15261526 \\ var to_char: [*c]u8 = @ptrCast([*c]u8, @alignCast(@import("std").meta.alignment([*c]u8), p));
1527 \\ _ = to_char;
1527 \\ _ = @TypeOf(to_char);
15281528 \\ var to_short: [*c]c_short = @ptrCast([*c]c_short, @alignCast(@import("std").meta.alignment([*c]c_short), p));
1529 \\ _ = to_short;
1529 \\ _ = @TypeOf(to_short);
15301530 \\ var to_int: [*c]c_int = @ptrCast([*c]c_int, @alignCast(@import("std").meta.alignment([*c]c_int), p));
1531 \\ _ = to_int;
1531 \\ _ = @TypeOf(to_int);
15321532 \\ var to_longlong: [*c]c_longlong = @ptrCast([*c]c_longlong, @alignCast(@import("std").meta.alignment([*c]c_longlong), p));
1533 \\ _ = to_longlong;
1533 \\ _ = @TypeOf(to_longlong);
15341534 \\ }
15351535 \\ {
15361536 \\ var to_char: [*c]u8 = @ptrCast([*c]u8, @alignCast(@import("std").meta.alignment([*c]u8), p));
1537 \\ _ = to_char;
1537 \\ _ = @TypeOf(to_char);
15381538 \\ var to_short: [*c]c_short = @ptrCast([*c]c_short, @alignCast(@import("std").meta.alignment([*c]c_short), p));
1539 \\ _ = to_short;
1539 \\ _ = @TypeOf(to_short);
15401540 \\ var to_int: [*c]c_int = @ptrCast([*c]c_int, @alignCast(@import("std").meta.alignment([*c]c_int), p));
1541 \\ _ = to_int;
1541 \\ _ = @TypeOf(to_int);
15421542 \\ var to_longlong: [*c]c_longlong = @ptrCast([*c]c_longlong, @alignCast(@import("std").meta.alignment([*c]c_longlong), p));
1543 \\ _ = to_longlong;
1543 \\ _ = @TypeOf(to_longlong);
15441544 \\ }
15451545 \\}
15461546 });
......@@ -1786,11 +1786,11 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
17861786 \\ var arr: [10]u8 = [1]u8{
17871787 \\ 1,
17881788 \\ } ++ [1]u8{0} ** 9;
1789 \\ _ = arr;
1789 \\ _ = @TypeOf(arr);
17901790 \\ var arr1: [10][*c]u8 = [1][*c]u8{
17911791 \\ null,
17921792 \\ } ++ [1][*c]u8{null} ** 9;
1793 \\ _ = arr1;
1793 \\ _ = @TypeOf(arr1);
17941794 \\}
17951795 });
17961796
......@@ -2038,16 +2038,16 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
20382038 \\pub var c: c_int = 4;
20392039 \\pub export fn foo(arg_c_1: u8) void {
20402040 \\ var c_1 = arg_c_1;
2041 \\ _ = c_1;
2041 \\ _ = @TypeOf(c_1);
20422042 \\ var a_2: c_int = undefined;
20432043 \\ var b_3: u8 = 123;
20442044 \\ b_3 = @bitCast(u8, @truncate(i8, a_2));
20452045 \\ {
20462046 \\ var d: c_int = 5;
2047 \\ _ = d;
2047 \\ _ = @TypeOf(d);
20482048 \\ }
20492049 \\ var d: c_uint = @bitCast(c_uint, @as(c_int, 440));
2050 \\ _ = d;
2050 \\ _ = @TypeOf(d);
20512051 \\}
20522052 });
20532053
......@@ -2146,7 +2146,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
21462146 \\ {
21472147 \\ var i: c_int = 2;
21482148 \\ var b: c_int = 4;
2149 \\ _ = b;
2149 \\ _ = @TypeOf(b);
21502150 \\ while ((i + @as(c_int, 2)) != 0) : (i = 2) {
21512151 \\ var a: c_int = 2;
21522152 \\ _ = blk: {
......@@ -2159,7 +2159,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
21592159 \\ }
21602160 \\ }
21612161 \\ var i: u8 = 2;
2162 \\ _ = i;
2162 \\ _ = @TypeOf(i);
21632163 \\}
21642164 });
21652165
......@@ -2396,27 +2396,27 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
23962396 , &[_][]const u8{
23972397 \\pub export fn escapes() [*c]const u8 {
23982398 \\ var a: u8 = '\'';
2399 \\ _ = a;
2399 \\ _ = @TypeOf(a);
24002400 \\ var b: u8 = '\\';
2401 \\ _ = b;
2401 \\ _ = @TypeOf(b);
24022402 \\ var c: u8 = '\x07';
2403 \\ _ = c;
2403 \\ _ = @TypeOf(c);
24042404 \\ var d: u8 = '\x08';
2405 \\ _ = d;
2405 \\ _ = @TypeOf(d);
24062406 \\ var e: u8 = '\x0c';
2407 \\ _ = e;
2407 \\ _ = @TypeOf(e);
24082408 \\ var f: u8 = '\n';
2409 \\ _ = f;
2409 \\ _ = @TypeOf(f);
24102410 \\ var g: u8 = '\r';
2411 \\ _ = g;
2411 \\ _ = @TypeOf(g);
24122412 \\ var h: u8 = '\t';
2413 \\ _ = h;
2413 \\ _ = @TypeOf(h);
24142414 \\ var i: u8 = '\x0b';
2415 \\ _ = i;
2415 \\ _ = @TypeOf(i);
24162416 \\ var j: u8 = '\x00';
2417 \\ _ = j;
2417 \\ _ = @TypeOf(j);
24182418 \\ var k: u8 = '"';
2419 \\ _ = k;
2419 \\ _ = @TypeOf(k);
24202420 \\ return "'\\\x07\x08\x0c\n\r\t\x0b\x00\"";
24212421 \\}
24222422 });
......@@ -2612,7 +2612,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
26122612 \\pub export fn foo() c_int {
26132613 \\ return blk: {
26142614 \\ var a: c_int = 1;
2615 \\ _ = a;
2615 \\ _ = @TypeOf(a);
26162616 \\ break :blk a;
26172617 \\ };
26182618 \\}
......@@ -2716,7 +2716,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
27162716 \\int bar(void) { return 0; }
27172717 , &[_][]const u8{
27182718 \\pub inline fn CALL(arg: anytype) @TypeOf(bar()) {
2719 \\ _ = arg;
2719 \\ _ = @TypeOf(arg);
27202720 \\ return bar();
27212721 \\}
27222722 });
......@@ -2775,14 +2775,14 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
27752775 \\pub export fn foo() void {
27762776 \\ if (true) {
27772777 \\ var a: c_int = 2;
2778 \\ _ = a;
2778 \\ _ = @TypeOf(a);
27792779 \\ }
27802780 \\ if ((blk: {
27812781 \\ _ = @as(c_int, 2);
27822782 \\ break :blk @as(c_int, 5);
27832783 \\ }) != 0) {
27842784 \\ var a: c_int = 2;
2785 \\ _ = a;
2785 \\ _ = @TypeOf(a);
27862786 \\ }
27872787 \\}
27882788 });
......@@ -3285,7 +3285,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
32853285 \\#define a 2
32863286 , &[_][]const u8{
32873287 \\pub inline fn FOO(bar: anytype) @TypeOf(baz(@import("std").zig.c_translation.cast(?*anyopaque, baz))) {
3288 \\ _ = bar;
3288 \\ _ = @TypeOf(bar);
32893289 \\ return baz(@import("std").zig.c_translation.cast(?*anyopaque, baz));
32903290 \\}
32913291 ,
......@@ -3425,7 +3425,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
34253425 , &[_][]const u8{
34263426 \\pub export fn foo(arg_a: [*c]c_int) void {
34273427 \\ var a = arg_a;
3428 \\ _ = a;
3428 \\ _ = @TypeOf(a);
34293429 \\}
34303430 \\pub export fn bar(arg_a: [*c]const c_int) void {
34313431 \\ var a = arg_a;
......@@ -3785,12 +3785,12 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
37853785 \\pub export fn bar(arg_x: c_int, arg_y: c_int) c_int {
37863786 \\ var x = arg_x;
37873787 \\ var y = arg_y;
3788 \\ _ = y;
3788 \\ _ = @TypeOf(y);
37893789 \\ return x;
37903790 \\}
37913791 ,
37923792 \\pub inline fn FOO(A: anytype, B: anytype) @TypeOf(A) {
3793 \\ _ = B;
3793 \\ _ = @TypeOf(B);
37943794 \\ return A;
37953795 \\}
37963796 });