authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2020-01-05 15:01:21-05:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2020-01-05 15:01:21-05:00
logd7bff05098445ec56082a6455b8a00d87a982e23
tree5cd56e04fb41b049de89b2b0b18146bd88cd715b
parenta0ca34979ea5ff9be0d353f539e7c86dedbf8693
parentac867cc45f8e99297647bd29ddfa746462500b72
signaturebadge-question-mark Signed by PGP key 4AEE18F83AFDEB23

Merge pull request #4064 from ziglang/fix-4054

use @intCast instead of @as for shift rhs

2 files changed, 26 insertions(+), 26 deletions(-)

src-self-hosted/translate_c.zig+15-15
......@@ -2781,13 +2781,13 @@ fn transCreateCompoundAssign(
27812781 try transExpr(rp, scope, rhs, .used, .r_value);
27822782
27832783 if (is_shift) {
2784 const as_node = try transCreateNodeBuiltinFnCall(rp.c, "@as");
2784 const cast_node = try transCreateNodeBuiltinFnCall(rp.c, "@intCast");
27852785 const rhs_type = try qualTypeToLog2IntRef(rp, getExprQualType(rp.c, rhs), loc);
2786 try as_node.params.push(rhs_type);
2786 try cast_node.params.push(rhs_type);
27872787 _ = try appendToken(rp.c, .Comma, ",");
2788 try as_node.params.push(rhs_node);
2789 as_node.rparen_token = try appendToken(rp.c, .RParen, ")");
2790 rhs_node = &as_node.base;
2788 try cast_node.params.push(rhs_node);
2789 cast_node.rparen_token = try appendToken(rp.c, .RParen, ")");
2790 rhs_node = &cast_node.base;
27912791 }
27922792 if (scope.id != .Condition)
27932793 _ = try appendToken(rp.c, .Semicolon, ";");
......@@ -2818,13 +2818,13 @@ fn transCreateCompoundAssign(
28182818 const bin_token = try appendToken(rp.c, bin_tok_id, bin_bytes);
28192819 var rhs_node = try transExpr(rp, scope, rhs, .used, .r_value);
28202820 if (is_shift) {
2821 const as_node = try transCreateNodeBuiltinFnCall(rp.c, "@as");
2821 const cast_node = try transCreateNodeBuiltinFnCall(rp.c, "@intCast");
28222822 const rhs_type = try qualTypeToLog2IntRef(rp, getExprQualType(rp.c, rhs), loc);
2823 try as_node.params.push(rhs_type);
2823 try cast_node.params.push(rhs_type);
28242824 _ = try appendToken(rp.c, .Comma, ",");
2825 try as_node.params.push(rhs_node);
2826 as_node.rparen_token = try appendToken(rp.c, .RParen, ")");
2827 rhs_node = &as_node.base;
2825 try cast_node.params.push(rhs_node);
2826 cast_node.rparen_token = try appendToken(rp.c, .RParen, ")");
2827 rhs_node = &cast_node.base;
28282828 }
28292829 const rhs_bin = try transCreateNodeInfixOp(rp, scope, ref_node, bin_op, bin_token, rhs_node, .used, false);
28302830
......@@ -3886,20 +3886,20 @@ fn transCreateNodeShiftOp(
38863886 const lhs = try transExpr(rp, scope, lhs_expr, .used, .l_value);
38873887 const op_token = try appendToken(rp.c, op_tok_id, bytes);
38883888
3889 const as_node = try transCreateNodeBuiltinFnCall(rp.c, "@as");
3889 const cast_node = try transCreateNodeBuiltinFnCall(rp.c, "@intCast");
38903890 const rhs_type = try qualTypeToLog2IntRef(rp, ZigClangBinaryOperator_getType(stmt), rhs_location);
3891 try as_node.params.push(rhs_type);
3891 try cast_node.params.push(rhs_type);
38923892 _ = try appendToken(rp.c, .Comma, ",");
38933893 const rhs = try transExprCoercing(rp, scope, rhs_expr, .used, .r_value);
3894 try as_node.params.push(rhs);
3895 as_node.rparen_token = try appendToken(rp.c, .RParen, ")");
3894 try cast_node.params.push(rhs);
3895 cast_node.rparen_token = try appendToken(rp.c, .RParen, ")");
38963896
38973897 const node = try rp.c.a().create(ast.Node.InfixOp);
38983898 node.* = ast.Node.InfixOp{
38993899 .op_token = op_token,
39003900 .lhs = lhs,
39013901 .op = op,
3902 .rhs = &as_node.base,
3902 .rhs = &cast_node.base,
39033903 };
39043904
39053905 return &node.base;
test/translate_c.zig+11-11
......@@ -835,7 +835,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
835835 \\}
836836 , &[_][]const u8{
837837 \\pub export fn foo() c_int {
838 \\ return (@as(c_int, 1) << @as(@import("std").math.Log2Int(c_int), 2)) >> @as(@import("std").math.Log2Int(c_int), 1);
838 \\ return (@as(c_int, 1) << @intCast(@import("std").math.Log2Int(c_int), 2)) >> @intCast(@import("std").math.Log2Int(c_int), 1);
839839 \\}
840840 });
841841
......@@ -2001,7 +2001,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
20012001 \\ var a = arg_a;
20022002 \\ var i: c_int = 0;
20032003 \\ while (a > @bitCast(c_uint, @as(c_int, 0))) {
2004 \\ a >>= @as(@import("std").math.Log2Int(c_int), 1);
2004 \\ a >>= @intCast(@import("std").math.Log2Int(c_int), 1);
20052005 \\ }
20062006 \\ return i;
20072007 \\}
......@@ -2021,7 +2021,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
20212021 \\ var a = arg_a;
20222022 \\ var i: c_int = 0;
20232023 \\ while (a > @bitCast(c_uint, @as(c_int, 0))) {
2024 \\ a >>= @as(@import("std").math.Log2Int(c_int), 1);
2024 \\ a >>= @intCast(@import("std").math.Log2Int(c_int), 1);
20252025 \\ }
20262026 \\ return i;
20272027 \\}
......@@ -2072,14 +2072,14 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
20722072 \\ ref.* = ref.* ^ @as(c_int, 1);
20732073 \\ break :blk ref.*;
20742074 \\ });
2075 \\ a >>= @as(@import("std").math.Log2Int(c_int), (blk: {
2075 \\ a >>= @intCast(@import("std").math.Log2Int(c_int), (blk: {
20762076 \\ const ref = &a;
2077 \\ ref.* = ref.* >> @as(@import("std").math.Log2Int(c_int), @as(c_int, 1));
2077 \\ ref.* = ref.* >> @intCast(@import("std").math.Log2Int(c_int), @as(c_int, 1));
20782078 \\ break :blk ref.*;
20792079 \\ }));
2080 \\ a <<= @as(@import("std").math.Log2Int(c_int), (blk: {
2080 \\ a <<= @intCast(@import("std").math.Log2Int(c_int), (blk: {
20812081 \\ const ref = &a;
2082 \\ ref.* = ref.* << @as(@import("std").math.Log2Int(c_int), @as(c_int, 1));
2082 \\ ref.* = ref.* << @intCast(@import("std").math.Log2Int(c_int), @as(c_int, 1));
20832083 \\ break :blk ref.*;
20842084 \\ }));
20852085 \\}
......@@ -2130,14 +2130,14 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
21302130 \\ ref.* = ref.* ^ @bitCast(c_uint, @as(c_int, 1));
21312131 \\ break :blk ref.*;
21322132 \\ });
2133 \\ a >>= @as(@import("std").math.Log2Int(c_uint), (blk: {
2133 \\ a >>= @intCast(@import("std").math.Log2Int(c_uint), (blk: {
21342134 \\ const ref = &a;
2135 \\ ref.* = ref.* >> @as(@import("std").math.Log2Int(c_int), @as(c_int, 1));
2135 \\ ref.* = ref.* >> @intCast(@import("std").math.Log2Int(c_int), @as(c_int, 1));
21362136 \\ break :blk ref.*;
21372137 \\ }));
2138 \\ a <<= @as(@import("std").math.Log2Int(c_uint), (blk: {
2138 \\ a <<= @intCast(@import("std").math.Log2Int(c_uint), (blk: {
21392139 \\ const ref = &a;
2140 \\ ref.* = ref.* << @as(@import("std").math.Log2Int(c_int), @as(c_int, 1));
2140 \\ ref.* = ref.* << @intCast(@import("std").math.Log2Int(c_int), @as(c_int, 1));
21412141 \\ break :blk ref.*;
21422142 \\ }));
21432143 \\}