File tree 2 files changed +57
-0
lines changed
2 files changed +57
-0
lines changed Original file line number Diff line number Diff line change @@ -1036,3 +1036,32 @@ test "modify nested packed struct aligned field" {
1036
1036
try std .testing .expectEqual (@as (u8 , 1 ), opts .pretty_print .indent );
1037
1037
try std .testing .expect (! opts .baz );
1038
1038
}
1039
+
1040
+ test "assigning packed struct inside another packed struct" {
1041
+ // Originally reported at https://github.com/ziglang/zig/issues/9674
1042
+
1043
+ const S = struct {
1044
+ const Inner = packed struct {
1045
+ bits : u3 ,
1046
+ more_bits : u6 ,
1047
+ };
1048
+
1049
+ const Outer = packed struct {
1050
+ padding : u5 ,
1051
+ inner : Inner ,
1052
+ };
1053
+ fn t (inner : Inner ) void {
1054
+ r .inner = inner ;
1055
+ }
1056
+
1057
+ var mem : Outer = undefined ;
1058
+ var r : * volatile Outer = & mem ;
1059
+ };
1060
+
1061
+ const val = S.Inner { .bits = 1 , .more_bits = 11 };
1062
+ S .mem .padding = 0 ;
1063
+ S .t (val );
1064
+
1065
+ try expectEqual (val , S .mem .inner );
1066
+ try expect (S .mem .padding == 0 );
1067
+ }
Original file line number Diff line number Diff line change @@ -85,3 +85,31 @@ test "flags in packed union at offset" {
85
85
try expectEqual (false , test_bits .adv_flags .adv .flags .enable_1 );
86
86
try expectEqual (false , test_bits .adv_flags .adv .flags .enable_2 );
87
87
}
88
+
89
+ test "packed union in packed struct" {
90
+ // Originally reported at https://github.com/ziglang/zig/issues/16581
91
+
92
+ const ReadRequest = packed struct { key : i32 };
93
+ const RequestType = enum {
94
+ read ,
95
+ insert ,
96
+ };
97
+ const RequestUnion = packed union {
98
+ read : ReadRequest ,
99
+ };
100
+
101
+ const Request = packed struct {
102
+ active_type : RequestType ,
103
+ request : RequestUnion ,
104
+ const Self = @This ();
105
+
106
+ fn init (read : ReadRequest ) Self {
107
+ return .{
108
+ .active_type = .read ,
109
+ .request = RequestUnion { .read = read },
110
+ };
111
+ }
112
+ };
113
+
114
+ try std .testing .expectEqual (RequestType .read , Request .init (.{ .key = 3 }).active_type );
115
+ }
You can’t perform that action at this time.
0 commit comments