| ... | @@ -2184,3 +2184,52 @@ test "pass a pointer to a comptime-only struct field to a function" { | ... | @@ -2184,3 +2184,52 @@ test "pass a pointer to a comptime-only struct field to a function" { |
| 2184 | const s: struct { x: type } = .{ .x = u42 }; | 2184 | const s: struct { x: type } = .{ .x = u42 }; |
| 2185 | try S.checkField(&s.x); | 2185 | try S.checkField(&s.x); |
| 2186 | } | 2186 | } |
| | 2187 | |
| | 2188 | test "overaligned extern struct fields" { |
| | 2189 | const A = struct { |
| | 2190 | a: *anyopaque, |
| | 2191 | b: u64, |
| | 2192 | c: [1][]u8, |
| | 2193 | d: ?anyerror, |
| | 2194 | }; |
| | 2195 | |
| | 2196 | const B = union(enum) { |
| | 2197 | a: struct { |
| | 2198 | a: [2]usize, |
| | 2199 | b: C, |
| | 2200 | }, |
| | 2201 | b: struct { |
| | 2202 | a: *anyopaque, |
| | 2203 | b: []const []u8, |
| | 2204 | c: C, |
| | 2205 | }, |
| | 2206 | const C = union { |
| | 2207 | a: void, |
| | 2208 | b: *anyopaque, |
| | 2209 | c: anyerror!usize, |
| | 2210 | }; |
| | 2211 | }; |
| | 2212 | |
| | 2213 | const D = extern struct { |
| | 2214 | a: u32, |
| | 2215 | }; |
| | 2216 | |
| | 2217 | const E = extern struct { |
| | 2218 | a: u32, |
| | 2219 | b: [2][@sizeOf(A)]u8 align(@alignOf(A)), |
| | 2220 | c: [2]u32, |
| | 2221 | d: [2][@sizeOf(B)]u8 align(@alignOf(B)), |
| | 2222 | |
| | 2223 | fn cast(e: *@This()) *D { |
| | 2224 | e.a = 2; |
| | 2225 | return @ptrCast(e); |
| | 2226 | } |
| | 2227 | }; |
| | 2228 | |
| | 2229 | var e: E = undefined; |
| | 2230 | const d = e.cast(); |
| | 2231 | try expect(d.a == 2); |
| | 2232 | try expect(std.mem.isAligned(@intFromPtr(&e.b), @alignOf(A))); |
| | 2233 | try expect(std.mem.isAligned(@intFromPtr(&e.c), @alignOf(u32))); |
| | 2234 | try expect(std.mem.isAligned(@intFromPtr(&e.d), @alignOf(B))); |
| | 2235 | } |