authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-02-12 10:25:21-05:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-02-12 10:25:21-05:00
log270933b1e997c91a9c2d28b6896d625c0ae1b163
treee093841812f97af44544f318a70f74bfbb6b3737
parent6f05e8d1be083a429673e717e8b3c736c7ddb8d2
signature Commit is signed but in an unrecognized format.

compile error test for casting integer to c pointer

when the int has more bits than pointers See #1059

2 files changed, 17 insertions(+), 1 deletions(-)

src/ir.cpp+3-1
...@@ -10882,7 +10882,9 @@ static IrInstruction *ir_analyze_int_to_c_ptr(IrAnalyze *ira, IrInstruction *sou...@@ -10882,7 +10882,9 @@ static IrInstruction *ir_analyze_int_to_c_ptr(IrAnalyze *ira, IrInstruction *sou
10882 ira->codegen->builtin_types.entry_usize->data.integral.bit_count)10882 ira->codegen->builtin_types.entry_usize->data.integral.bit_count)
10883 {10883 {
10884 ir_add_error(ira, source_instr,10884 ir_add_error(ira, source_instr,
10885 buf_sprintf("integer type too big for implicit @intToPtr to type '%s'", buf_ptr(&dest_type->name)));10885 buf_sprintf("integer type '%s' too big for implicit @intToPtr to type '%s'",
10886 buf_ptr(&integer->value.type->name),
10887 buf_ptr(&dest_type->name)));
10886 return ira->codegen->invalid_instruction;10888 return ira->codegen->invalid_instruction;
10887 }10889 }
1088810890
test/compile_errors.zig+14
...@@ -1,6 +1,20 @@...@@ -1,6 +1,20 @@
1const tests = @import("tests.zig");1const tests = @import("tests.zig");
22
3pub fn addCases(cases: *tests.CompileErrorContext) void {3pub fn addCases(cases: *tests.CompileErrorContext) void {
4 cases.addTest(
5 "implicit casting too big integers to C pointers",
6 \\export fn a() void {
7 \\ var ptr: [*c]u8 = (1 << 64) + 1;
8 \\}
9 \\export fn b() void {
10 \\ var x: @IntType(false, 65) = 0x1234;
11 \\ var ptr: [*c]u8 = x;
12 \\}
13 ,
14 ".tmp_source.zig:2:33: error: integer value 71615590737044764481 cannot be implicitly casted to type 'usize'",
15 ".tmp_source.zig:6:23: error: integer type 'u65' too big for implicit @intToPtr to type '[*c]u8'",
16 );
17
4 cases.addTest(18 cases.addTest(
5 "C pointer pointing to non C ABI compatible type",19 "C pointer pointing to non C ABI compatible type",
6 \\const Foo = struct {};20 \\const Foo = struct {};