Skip to content

Commit 270933b

Browse files
committed
compile error test for casting integer to c pointer
when the int has more bits than pointers See #1059
1 parent 6f05e8d commit 270933b

File tree

2 files changed

+17
-1
lines changed

2 files changed

+17
-1
lines changed

src/ir.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,20 @@
11
const tests = @import("tests.zig");
22

33
pub 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 {};

0 commit comments

Comments
 (0)