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
signaturelock-open 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...@@ -5057,6 +5057,11 @@ static IrInstruction *ir_gen_pointer_type(IrBuilder *irb, Scope *scope, AstNode
50575057
5058 IrInstruction *align_value;5058 IrInstruction *align_value;
5059 if (align_expr != nullptr) {5059 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 }
5060 align_value = ir_gen_node(irb, align_expr, scope);5065 align_value = ir_gen_node(irb, align_expr, scope);
5061 if (align_value == irb->codegen->invalid_instruction)5066 if (align_value == irb->codegen->invalid_instruction)
5062 return align_value;5067 return align_value;
test/compile_errors.zig+8-4
...@@ -118,13 +118,17 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {...@@ -118,13 +118,17 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
118 );118 );
119119
120 cases.addTest(120 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",
122 \\const Foo = struct {};122 \\const Foo = struct {};
123 \\export fn entry() [*c]Foo {123 \\export fn a() void {
124 \\ return undefined;124 \\ const T = [*c]Foo;
125 \\}
126 \\export fn b() void {
127 \\ const T = [*c]align(4) u8;
125 \\}128 \\}
126 ,129 ,
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",
128 );132 );
129133
130 cases.addTest(134 cases.addTest(
test/stage1/behavior/type_info.zig+2-2
...@@ -67,12 +67,12 @@ test "type info: C pointer type info" {...@@ -67,12 +67,12 @@ test "type info: C pointer type info" {
67}67}
6868
69fn testCPtr() void {69fn testCPtr() void {
70 const ptr_info = @typeInfo([*c]align(4) const i8);70 const ptr_info = @typeInfo([*c]const i8);
71 expect(TypeId(ptr_info) == TypeId.Pointer);71 expect(TypeId(ptr_info) == TypeId.Pointer);
72 expect(ptr_info.Pointer.size == TypeInfo.Pointer.Size.C);72 expect(ptr_info.Pointer.size == TypeInfo.Pointer.Size.C);
73 expect(ptr_info.Pointer.is_const);73 expect(ptr_info.Pointer.is_const);
74 expect(!ptr_info.Pointer.is_volatile);74 expect(!ptr_info.Pointer.is_volatile);
75 expect(ptr_info.Pointer.alignment == 4);75 expect(ptr_info.Pointer.alignment == 1);
76 expect(ptr_info.Pointer.child == i8);76 expect(ptr_info.Pointer.child == i8);
77}77}
7878