comptime detection of casting null to pointer

See #1059
This commit is contained in:
Andrew Kelley
2019-02-14 00:40:39 -05:00
parent 5699ab5e77
commit d4d2718bca
2 changed files with 34 additions and 3 deletions

View File

@@ -1,6 +1,26 @@
const tests = @import("tests.zig");
pub fn addCases(cases: *tests.CompileErrorContext) void {
cases.addTest(
"implicit casting null c pointer to zig pointer",
\\comptime {
\\ var c_ptr: [*c]u8 = 0;
\\ var zig_ptr: *u8 = c_ptr;
\\}
,
".tmp_source.zig:3:24: error: null pointer casted to type '*u8'",
);
cases.addTest(
"implicit casting undefined c pointer to zig pointer",
\\comptime {
\\ var c_ptr: [*c]u8 = undefined;
\\ var zig_ptr: *u8 = c_ptr;
\\}
,
".tmp_source.zig:3:24: error: use of undefined value here causes undefined behavior",
);
cases.addTest(
"implicit casting C pointers which would mess up null semantics",
\\export fn entry() void {