diff options
| author | travisstaloch <twostepted@gmail.com> | 2020-01-09 21:08:24 -0800 |
|---|---|---|
| committer | Andrew Kelley <andrew@ziglang.org> | 2020-01-10 00:08:24 -0500 |
| commit | 3f98756f8542e5ca6b45322f17eb59b74706fb62 (patch) | |
| tree | 609251a690623f262f5657b7cd649bd7cccb0e9d /test | |
| parent | ae324985a67b1258c226a799f2680906686670f2 (diff) | |
| download | zig-3f98756f8542e5ca6b45322f17eb59b74706fb62.tar.gz zig-3f98756f8542e5ca6b45322f17eb59b74706fb62.zip | |
Fix translation of signed array indices (#4113)
* cast only if the index is long long or signed
* cast long long to usize rather than c_uint
closes #4075
Diffstat (limited to 'test')
| -rw-r--r-- | test/run_translated_c.zig | 20 | ||||
| -rw-r--r-- | test/translate_c.zig | 41 |
2 files changed, 60 insertions, 1 deletions
diff --git a/test/run_translated_c.zig b/test/run_translated_c.zig index 5d8233ec96..dd34713ee3 100644 --- a/test/run_translated_c.zig +++ b/test/run_translated_c.zig @@ -149,4 +149,24 @@ pub fn addCases(cases: *tests.RunTranslatedCContext) void { \\ return 0; \\} , ""); + + cases.add("cast signed array index to unsigned", + \\#include <stdlib.h> + \\int main(int argc, char **argv) { + \\ int a[10], i = 0; + \\ a[i] = 0; + \\ if (a[i] != 0) abort(); + \\ return 0; + \\} + , ""); + + cases.add("cast long long array index to unsigned", + \\#include <stdlib.h> + \\int main(int argc, char **argv) { + \\ long long a[10], i = 0; + \\ a[i] = 0; + \\ if (a[i] != 0) abort(); + \\ return 0; + \\} + , ""); } diff --git a/test/translate_c.zig b/test/translate_c.zig index 2ad4a1564c..f8b6c33dcd 100644 --- a/test/translate_c.zig +++ b/test/translate_c.zig @@ -1843,12 +1843,51 @@ pub fn addCases(cases: *tests.TranslateCContext) void { \\pub export var array: [100]c_int = .{0} ** 100; \\pub export fn foo(arg_index: c_int) c_int { \\ var index = arg_index; - \\ return array[index]; + \\ return array[@intCast(c_uint, index)]; \\} , \\pub const ACCESS = array[2]; }); + cases.add("cast signed array index to unsigned", + \\void foo() { + \\ int a[10], i = 0; + \\ a[i] = 0; + \\} + , &[_][]const u8{ + \\pub export fn foo() void { + \\ var a: [10]c_int = undefined; + \\ var i: c_int = 0; + \\ a[@intCast(c_uint, i)] = 0; + \\} + }); + + cases.add("long long array index cast to usize", + \\void foo() { + \\ long long a[10], i = 0; + \\ a[i] = 0; + \\} + , &[_][]const u8{ + \\pub export fn foo() void { + \\ var a: [10]c_longlong = undefined; + \\ var i: c_longlong = @bitCast(c_longlong, @as(c_longlong, @as(c_int, 0))); + \\ a[@intCast(usize, i)] = @bitCast(c_longlong, @as(c_longlong, @as(c_int, 0))); + \\} + }); + + cases.add("unsigned array index skips cast", + \\void foo() { + \\ unsigned int a[10], i = 0; + \\ a[i] = 0; + \\} + , &[_][]const u8{ + \\pub export fn foo() void { + \\ var a: [10]c_uint = undefined; + \\ var i: c_uint = @bitCast(c_uint, @as(c_int, 0)); + \\ a[i] = @bitCast(c_uint, @as(c_int, 0)); + \\} + }); + cases.add("macro call", \\#define CALL(arg) bar(arg) , &[_][]const u8{ |
