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
signaturelock-open 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
1088210882 ira->codegen->builtin_types.entry_usize->data.integral.bit_count)
1088310883 {
1088410884 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)));
1088610888 return ira->codegen->invalid_instruction;
1088710889 }
1088810890
test/compile_errors.zig+14
......@@ -1,6 +1,20 @@
11const tests = @import("tests.zig");
22
33pub 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
418 cases.addTest(
519 "C pointer pointing to non C ABI compatible type",
620 \\const Foo = struct {};