Skip to content

Commit 5e4a619

Browse files
committed
Add compile error test for struct layout depending on being pointer aligned.
Move circular dependency tests into struct / union tests.
1 parent b7b152f commit 5e4a619

File tree

4 files changed

+49
-41
lines changed

4 files changed

+49
-41
lines changed

test/behavior/container_circular_dependency.zig

Lines changed: 0 additions & 41 deletions
This file was deleted.

test/behavior/struct.zig

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1803,3 +1803,22 @@ test "initializer takes a pointer to a variable inside its struct" {
18031803
try namespace.doTheTest();
18041804
comptime try namespace.doTheTest();
18051805
}
1806+
1807+
const StructInner = extern struct {
1808+
outer: StructOuter = std.mem.zeroes(StructOuter),
1809+
};
1810+
1811+
const StructMiddle = extern struct {
1812+
outer: ?*StructInner,
1813+
inner: ?*StructOuter,
1814+
};
1815+
1816+
const StructOuter = extern struct {
1817+
middle: StructMiddle = std.mem.zeroes(StructMiddle),
1818+
};
1819+
1820+
test "circular dependency through pointer field of a struct" {
1821+
var outer: StructOuter = .{};
1822+
try expect(outer.middle.outer == null);
1823+
try expect(outer.middle.inner == null);
1824+
}

test/behavior/union.zig

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2001,3 +2001,22 @@ test "union field is a pointer to an aligned version of itself" {
20012001

20022002
try expect(&e == e.next);
20032003
}
2004+
2005+
const UnionInner = extern struct {
2006+
outer: UnionOuter = std.mem.zeroes(UnionOuter),
2007+
};
2008+
2009+
const UnionMiddle = extern union {
2010+
outer: ?*UnionOuter,
2011+
inner: ?*UnionInner,
2012+
};
2013+
2014+
const UnionOuter = extern struct {
2015+
u: UnionMiddle = std.mem.zeroes(UnionMiddle),
2016+
};
2017+
2018+
test "circular dependency through pointer field of a union" {
2019+
var outer: UnionOuter = .{};
2020+
try expect(outer.u.outer == null);
2021+
try expect(outer.u.inner == null);
2022+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
const S = struct {
2+
next: ?*align(1) S align(128),
3+
};
4+
5+
export fn entry() usize {
6+
return @alignOf(S);
7+
}
8+
9+
// error
10+
//
11+
// :1:11: error: struct layout depends on being pointer aligned

0 commit comments

Comments
 (0)