aboutsummaryrefslogtreecommitdiff
path: root/test
diff options
context:
space:
mode:
authorEvan Haas <evan@lagerdata.com>2021-02-11 09:43:30 -0800
committerVeikka Tuominen <git@vexu.eu>2021-02-12 01:40:43 +0200
commitd98f09e4f67fb2848be6052466db035450326605 (patch)
tree736c6f57967ddaaa9e386026d0114c66c1dba5fd /test
parent61bcac108cecba0ab8e1b442bd71c51306c0e9fc (diff)
downloadzig-d98f09e4f67fb2848be6052466db035450326605.tar.gz
zig-d98f09e4f67fb2848be6052466db035450326605.zip
translate-c: comma operator should introduce a new scope
This prevents inadvertent side-effects when an expression is not evaluated due to boolean short-circuiting Fixes #7989
Diffstat (limited to 'test')
-rw-r--r--test/run_translated_c.zig13
-rw-r--r--test/translate_c.zig32
2 files changed, 35 insertions, 10 deletions
diff --git a/test/run_translated_c.zig b/test/run_translated_c.zig
index e28bdc96f0..b8af201e36 100644
--- a/test/run_translated_c.zig
+++ b/test/run_translated_c.zig
@@ -909,4 +909,17 @@ pub fn addCases(cases: *tests.RunTranslatedCContext) void {
\\ return 1 != 1;
\\}
, "");
+
+ cases.add("Comma operator should create new scope; issue #7989",
+ \\#include <stdlib.h>
+ \\#include <stdio.h>
+ \\int main(void) {
+ \\ if (1 || (abort(), 1)) {}
+ \\ if (0 && (1, printf("do not print\n"))) {}
+ \\ int x = 0;
+ \\ x = (x = 3, 4, x + 1);
+ \\ if (x != 4) abort();
+ \\ return 0;
+ \\}
+ , "");
}
diff --git a/test/translate_c.zig b/test/translate_c.zig
index 03ca87d5f6..2097e17323 100644
--- a/test/translate_c.zig
+++ b/test/translate_c.zig
@@ -1723,11 +1723,17 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
\\}
, &[_][]const u8{
\\pub export fn foo() c_int {
- \\ _ = @as(c_int, 2);
- \\ _ = @as(c_int, 4);
- \\ _ = @as(c_int, 2);
- \\ _ = @as(c_int, 4);
- \\ return @as(c_int, 6);
+ \\ _ = (blk: {
+ \\ _ = @as(c_int, 2);
+ \\ break :blk @as(c_int, 4);
+ \\ });
+ \\ return (blk: {
+ \\ _ = (blk_1: {
+ \\ _ = @as(c_int, 2);
+ \\ break :blk_1 @as(c_int, 4);
+ \\ });
+ \\ break :blk @as(c_int, 6);
+ \\ });
\\}
});
@@ -1774,8 +1780,10 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
\\ while (true) {
\\ var a_1: c_int = 4;
\\ a_1 = 9;
- \\ _ = @as(c_int, 6);
- \\ return a_1;
+ \\ return (blk: {
+ \\ _ = @as(c_int, 6);
+ \\ break :blk a_1;
+ \\ });
\\ }
\\ while (true) {
\\ var a_1: c_int = 2;
@@ -1805,9 +1813,13 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
\\ var b: c_int = 4;
\\ while ((i + @as(c_int, 2)) != 0) : (i = 2) {
\\ var a: c_int = 2;
- \\ a = 6;
- \\ _ = @as(c_int, 5);
- \\ _ = @as(c_int, 7);
+ \\ _ = (blk: {
+ \\ _ = (blk_1: {
+ \\ a = 6;
+ \\ break :blk_1 @as(c_int, 5);
+ \\ });
+ \\ break :blk @as(c_int, 7);
+ \\ });
\\ }
\\ }
\\ var i: u8 = @bitCast(u8, @truncate(i8, @as(c_int, 2)));