diff options
| author | Andrew Kelley <superjoe30@gmail.com> | 2017-08-29 16:52:31 -0400 |
|---|---|---|
| committer | Andrew Kelley <superjoe30@gmail.com> | 2017-08-29 16:52:31 -0400 |
| commit | 816689a3b1c98ec008438e7f868e1a123889b2a7 (patch) | |
| tree | 84dcf5e1cb51689543fea4c39b4c9f24dde681e9 /test | |
| parent | be94299666e57486be6bdb8fee2b79dbf3623c5d (diff) | |
| download | zig-816689a3b1c98ec008438e7f868e1a123889b2a7.tar.gz zig-816689a3b1c98ec008438e7f868e1a123889b2a7.zip | |
ptrCast gives compile error for increasing alignment
See #37
Diffstat (limited to 'test')
| -rw-r--r-- | test/cases/align.zig | 9 | ||||
| -rw-r--r-- | test/compare_output.zig | 4 | ||||
| -rw-r--r-- | test/compile_errors.zig | 11 |
3 files changed, 22 insertions, 2 deletions
diff --git a/test/cases/align.zig b/test/cases/align.zig index 383d0389b5..ee68408226 100644 --- a/test/cases/align.zig +++ b/test/cases/align.zig @@ -53,3 +53,12 @@ test "implicitly decreasing slice alignment" { assert(addUnalignedSlice((&a)[0..1], (&b)[0..1]) == 7); } fn addUnalignedSlice(a: []align 1 const u32, b: []align 1 const u32) -> u32 { a[0] + b[0] } + +test "specifying alignment allows pointer cast" { + testBytesAlign(0x33); +} +fn testBytesAlign(b: u8) { + var bytes align 4 = []u8{b, b, b, b}; + const ptr = @ptrCast(&u32, &bytes[0]); + assert(*ptr == 0x33333333); +} diff --git a/test/compare_output.zig b/test/compare_output.zig index bf2255881b..39318c31a0 100644 --- a/test/compare_output.zig +++ b/test/compare_output.zig @@ -262,8 +262,8 @@ pub fn addCases(cases: &tests.CompareOutputContext) { \\const c = @cImport(@cInclude("stdlib.h")); \\ \\export fn compare_fn(a: ?&const c_void, b: ?&const c_void) -> c_int { - \\ const a_int = @ptrCast(&i32, a ?? unreachable); - \\ const b_int = @ptrCast(&i32, b ?? unreachable); + \\ const a_int = @ptrCast(&align 1 i32, a ?? unreachable); + \\ const b_int = @ptrCast(&align 1 i32, b ?? unreachable); \\ if (*a_int < *b_int) { \\ -1 \\ } else if (*a_int > *b_int) { diff --git a/test/compile_errors.zig b/test/compile_errors.zig index 03110f98e7..96898d1744 100644 --- a/test/compile_errors.zig +++ b/test/compile_errors.zig @@ -2011,4 +2011,15 @@ pub fn addCases(cases: &tests.CompileErrorContext) { \\} , ".tmp_source.zig:9:17: error: expected type '[]u32', found '[]align 1 u32'"); + + cases.add("increase pointer alignment in @ptrCast", + \\export fn entry() -> u32 { + \\ var bytes: [4]u8 align 4 = []u8{0x01, 0x02, 0x03, 0x04}; + \\ const ptr = @ptrCast(&u32, &bytes[0]); + \\ return *ptr; + \\} + , + ".tmp_source.zig:3:17: error: cast increases pointer alignment", + ".tmp_source.zig:3:38: note: '&u8' has alignment 1", + ".tmp_source.zig:3:27: note: '&u32' has alignment 4"); } |
