authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-02-12 00:51:06-05:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-02-12 00:51:06-05:00
log285e2f62ba0648d6d8e7ff64d1ee7d2900481e2f
treeca99c7ae5e4e29bb12a750b92a8d292b3e09a556
parent0abe6d668eb52aefa59c75a8d8f782f2372f8600
signaturelock-open Commit is signed but in an unrecognized format.

disallow C pointers to non-C-ABI-compatible element types

See #1059

4 files changed, 16 insertions(+), 2 deletions(-)

src/analyze.cpp+1-1
...@@ -1469,7 +1469,7 @@ static bool type_allowed_in_packed_struct(ZigType *type_entry) {...@@ -1469,7 +1469,7 @@ static bool type_allowed_in_packed_struct(ZigType *type_entry) {
1469 zig_unreachable();1469 zig_unreachable();
1470}1470}
14711471
1472static bool type_allowed_in_extern(CodeGen *g, ZigType *type_entry) {1472bool type_allowed_in_extern(CodeGen *g, ZigType *type_entry) {
1473 switch (type_entry->id) {1473 switch (type_entry->id) {
1474 case ZigTypeIdInvalid:1474 case ZigTypeIdInvalid:
1475 zig_unreachable();1475 zig_unreachable();
src/analyze.hpp+1-1
...@@ -44,7 +44,7 @@ void find_libc_include_path(CodeGen *g);...@@ -44,7 +44,7 @@ void find_libc_include_path(CodeGen *g);
44void find_libc_lib_path(CodeGen *g);44void find_libc_lib_path(CodeGen *g);
4545
46bool type_has_bits(ZigType *type_entry);46bool type_has_bits(ZigType *type_entry);
4747bool type_allowed_in_extern(CodeGen *g, ZigType *type_entry);
4848
49ImportTableEntry *add_source_file(CodeGen *g, PackageTableEntry *package, Buf *abs_full_path, Buf *source_code);49ImportTableEntry *add_source_file(CodeGen *g, PackageTableEntry *package, Buf *abs_full_path, Buf *source_code);
5050
src/ir.cpp+4
...@@ -21145,6 +21145,10 @@ static IrInstruction *ir_analyze_instruction_ptr_type(IrAnalyze *ira, IrInstruct...@@ -21145,6 +21145,10 @@ static IrInstruction *ir_analyze_instruction_ptr_type(IrAnalyze *ira, IrInstruct
21145 } else if (child_type->id == ZigTypeIdOpaque && instruction->ptr_len == PtrLenUnknown) {21145 } else if (child_type->id == ZigTypeIdOpaque && instruction->ptr_len == PtrLenUnknown) {
21146 ir_add_error(ira, &instruction->base, buf_sprintf("unknown-length pointer to opaque"));21146 ir_add_error(ira, &instruction->base, buf_sprintf("unknown-length pointer to opaque"));
21147 return ira->codegen->invalid_instruction;21147 return ira->codegen->invalid_instruction;
21148 } else if (instruction->ptr_len == PtrLenC && !type_allowed_in_extern(ira->codegen, child_type)) {
21149 ir_add_error(ira, &instruction->base,
21150 buf_sprintf("C pointers cannot point to non-C-ABI-compatible type '%s'", buf_ptr(&child_type->name)));
21151 return ira->codegen->invalid_instruction;
21148 }21152 }
2114921153
21150 uint32_t align_bytes;21154 uint32_t align_bytes;
test/compile_errors.zig+10
...@@ -1,6 +1,16 @@...@@ -1,6 +1,16 @@
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 "C pointer pointing to non C ABI compatible type",
6 \\const Foo = struct {};
7 \\export fn entry() [*c]Foo {
8 \\ return undefined;
9 \\}
10 ,
11 ".tmp_source.zig:2:19: error: C pointers cannot point to non-C-ABI-compatible type 'Foo'",
12 );
13
4 cases.addTest(14 cases.addTest(
5 "@truncate undefined value",15 "@truncate undefined value",
6 \\export fn entry() void {16 \\export fn entry() void {