1#exe {
2-Mroot=main.zig
3#}
4
5#write main.zig {
6const Foo = packed struct(u8) { a: u4, b: i4 };
7const Bar = packed union(u10) { a: u10, b: i10 };
8pub fn main() void {}
9comptime {
10 @compileLog(@typeInfo(Foo).@"struct".backing_integer.?);
11}
12comptime {
13 @compileLog(@bitSizeOf(Bar));
14}
15const std = @import("std");
16const io = std.Io.Threaded.global_single_threaded.io();
17#}
18#update
19#check errors {
20main.zig:5:5: error: found compile log statement
21main.zig:8:5: note: also here
22
23Compile Log Output:
24@as(type, u8)
25@as(comptime_int, 10)
26#}
27
28#write main.zig {
29const Foo = packed struct(i8) { a: u4, b: i4 };
30const Bar = packed union(i10) { a: u10, b: i10 };
31pub fn main() void {}
32comptime {
33 @compileLog(@typeInfo(Foo).@"struct".backing_integer.?);
34}
35comptime {
36 @compileLog(@bitSizeOf(Bar));
37}
38const std = @import("std");
39const io = std.Io.Threaded.global_single_threaded.io();
40#}
41#update "make backing types signed"
42#check errors {
43main.zig:5:5: error: found compile log statement
44main.zig:8:5: note: also here
45
46Compile Log Output:
47@as(type, i8)
48@as(comptime_int, 10)
49#}
50
51#write main.zig {
52const Foo = packed struct(i5) { a: u4, b: i4 };
53const Bar = packed union(i8) { a: u10, b: i10 };
54pub fn main() void {}
55comptime {
56 @compileLog(@typeInfo(Foo).@"struct".backing_integer.?);
57}
58comptime {
59 @compileLog(@bitSizeOf(Bar));
60}
61const std = @import("std");
62const io = std.Io.Threaded.global_single_threaded.io();
63#}
64#update "make backing types too small"
65#check errors {
66main.zig:1:20: error: backing integer bit width does not match total bit width of fields
67main.zig:1:27: note: backing integer 'i5' has bit width '5'
68main.zig:1:20: note: struct fields have total bit width '8'
69main.zig:2:35: error: field bit width does not match backing integer
70main.zig:2:35: note: field type 'u10' has bit width '10'
71main.zig:2:26: note: backing integer 'i8' has bit width '8'
72main.zig:2:35: note: all fields in a packed union must have the same bit width
73#}
74
75#write main.zig {
76const Foo = packed struct(u10) { a: u4, b: i4 };
77const Bar = packed union(u32) { a: u10, b: i10 };
78pub fn main() void {}
79comptime {
80 @compileLog(@typeInfo(Foo).@"struct".backing_integer.?);
81}
82comptime {
83 @compileLog(@bitSizeOf(Bar));
84}
85const std = @import("std");
86const io = std.Io.Threaded.global_single_threaded.io();
87#}
88#update "make backing types too big"
89#check errors {
90main.zig:1:20: error: backing integer bit width does not match total bit width of fields
91main.zig:1:27: note: backing integer 'u10' has bit width '10'
92main.zig:1:20: note: struct fields have total bit width '8'
93main.zig:2:36: error: field bit width does not match backing integer
94main.zig:2:36: note: field type 'u10' has bit width '10'
95main.zig:2:26: note: backing integer 'u32' has bit width '32'
96main.zig:2:36: note: all fields in a packed union must have the same bit width
97#}