Skip to content

Commit b35dad8

Browse files
committed
add tests for function alignment handling
See #37
1 parent 2d57622 commit b35dad8

File tree

2 files changed

+43
-0
lines changed

2 files changed

+43
-0
lines changed

test/cases/align.zig

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,15 @@ test "global variable alignment" {
99
}
1010

1111
fn derp() align (@sizeOf(usize) * 2) -> i32 { 1234 }
12+
fn noop1() align 1 {}
13+
fn noop4() align 4 {}
1214

1315
test "function alignment" {
1416
assert(derp() == 1234);
17+
assert(@typeOf(noop1) == fn() align 1);
18+
assert(@typeOf(noop4) == fn() align 4);
19+
noop1();
20+
noop4();
1521
}
1622

1723

@@ -96,3 +102,28 @@ fn sliceExpectsOnly1(slice: []align 1 u32) {
96102
fn sliceExpects4(slice: []align 4 u32) {
97103
slice[0] += 1;
98104
}
105+
106+
107+
test "implicitly decreasing fn alignment" {
108+
testImplicitlyDecreaseFnAlign(alignedSmall, 1234);
109+
testImplicitlyDecreaseFnAlign(alignedBig, 5678);
110+
}
111+
112+
fn testImplicitlyDecreaseFnAlign(ptr: fn () align 1 -> i32, answer: i32) {
113+
assert(ptr() == answer);
114+
}
115+
116+
fn alignedSmall() align 8 -> i32 { 1234 }
117+
fn alignedBig() align 16 -> i32 { 5678 }
118+
119+
120+
test "@alignCast functions" {
121+
assert(fnExpectsOnly1(simple4) == 0x19);
122+
}
123+
fn fnExpectsOnly1(ptr: fn()align 1 -> i32) -> i32 {
124+
fnExpects4(@alignCast(4, ptr))
125+
}
126+
fn fnExpects4(ptr: fn()align 4 -> i32) -> i32 {
127+
ptr()
128+
}
129+
fn simple4() align 4 -> i32 { 0x19 }

test/compile_errors.zig

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2039,4 +2039,16 @@ pub fn addCases(cases: &tests.CompileErrorContext) {
20392039
\\}
20402040
,
20412041
".tmp_source.zig:2:22: error: expected pointer or slice, found 'u32'");
2042+
2043+
cases.add("passing an under-aligned function pointer",
2044+
\\export fn entry() {
2045+
\\ testImplicitlyDecreaseFnAlign(alignedSmall, 1234);
2046+
\\}
2047+
\\fn testImplicitlyDecreaseFnAlign(ptr: fn () align 8 -> i32, answer: i32) {
2048+
\\ if (ptr() != answer) unreachable;
2049+
\\}
2050+
\\fn alignedSmall() align 4 -> i32 { 1234 }
2051+
,
2052+
".tmp_source.zig:2:35: error: expected type 'fn() align 8 -> i32', found 'fn() align 4 -> i32'");
2053+
20422054
}

0 commit comments

Comments
 (0)