authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-02-14 19:53:46-05:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-02-14 20:02:29-05:00
logcc7060d0d934135d797bd2bc24288ecab095051a
treef7a94569505fe31bd71bb0b61ff4c20bbcdbf3f8
parent973a93d43b8b44d2ee8bfde09e78f8295470f337
signature Commit is signed but in an unrecognized format.

compile error for C pointer with align attribute

See #1059

3 files changed, 15 insertions(+), 6 deletions(-)

src/ir.cpp+5
......@@ -5057,6 +5057,11 @@ static IrInstruction *ir_gen_pointer_type(IrBuilder *irb, Scope *scope, AstNode
50575057
50585058 IrInstruction *align_value;
50595059 if (align_expr != nullptr) {
5060 if (ptr_len == PtrLenC) {
5061 exec_add_error_node(irb->codegen, irb->exec, node,
5062 buf_sprintf("[*c] pointers may not have align attribute"));
5063 return irb->codegen->invalid_instruction;
5064 }
50605065 align_value = ir_gen_node(irb, align_expr, scope);
50615066 if (align_value == irb->codegen->invalid_instruction)
50625067 return align_value;
test/compile_errors.zig+8-4
......@@ -118,13 +118,17 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
118118 );
119119
120120 cases.addTest(
121 "C pointer pointing to non C ABI compatible type",
121 "C pointer pointing to non C ABI compatible type or has align attr",
122122 \\const Foo = struct {};
123 \\export fn entry() [*c]Foo {
124 \\ return undefined;
123 \\export fn a() void {
124 \\ const T = [*c]Foo;
125 \\}
126 \\export fn b() void {
127 \\ const T = [*c]align(4) u8;
125128 \\}
126129 ,
127 ".tmp_source.zig:2:19: error: C pointers cannot point to non-C-ABI-compatible type 'Foo'",
130 ".tmp_source.zig:3:15: error: C pointers cannot point to non-C-ABI-compatible type 'Foo'",
131 ".tmp_source.zig:6:15: error: [*c] pointers may not have align attribute",
128132 );
129133
130134 cases.addTest(
test/stage1/behavior/type_info.zig+2-2
......@@ -67,12 +67,12 @@ test "type info: C pointer type info" {
6767}
6868
6969fn testCPtr() void {
70 const ptr_info = @typeInfo([*c]align(4) const i8);
70 const ptr_info = @typeInfo([*c]const i8);
7171 expect(TypeId(ptr_info) == TypeId.Pointer);
7272 expect(ptr_info.Pointer.size == TypeInfo.Pointer.Size.C);
7373 expect(ptr_info.Pointer.is_const);
7474 expect(!ptr_info.Pointer.is_volatile);
75 expect(ptr_info.Pointer.alignment == 4);
75 expect(ptr_info.Pointer.alignment == 1);
7676 expect(ptr_info.Pointer.child == i8);
7777}
7878