authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2021-11-30 00:19:37-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2021-11-30 00:19:37-07:00
log692c254336da71cbe21aaf9fbc21240fd1269b95
treefe9c5fdfd23281c593345ae40c90ccecab48ce9b
parent902df103c6151c257c90de9ba5f29f7f4b9dbea2

Revert "I found some more passing behavior tests"

This reverts commit 0a9b4d092f58595888f9e4be8ef683b2ed8a0da1. Hm, these are all passing for me locally. I'll have to do some troubleshooting to figure out which one(s) are failing on the CI.

6 files changed, 262 insertions(+), 248 deletions(-)

test/behavior.zig+18-16
...@@ -10,41 +10,32 @@ test {...@@ -10,41 +10,32 @@ test {
10 _ = @import("behavior/bugs/655.zig");10 _ = @import("behavior/bugs/655.zig");
11 _ = @import("behavior/bugs/679.zig");11 _ = @import("behavior/bugs/679.zig");
12 _ = @import("behavior/bugs/704.zig");12 _ = @import("behavior/bugs/704.zig");
13 _ = @import("behavior/bugs/1111.zig");
14 _ = @import("behavior/bugs/1486.zig");13 _ = @import("behavior/bugs/1486.zig");
15 _ = @import("behavior/bugs/2346.zig");14 _ = @import("behavior/bugs/2346.zig");
16 _ = @import("behavior/bugs/2692.zig");15 _ = @import("behavior/bugs/2692.zig");
17 _ = @import("behavior/bugs/2889.zig");16 _ = @import("behavior/bugs/2889.zig");
18 _ = @import("behavior/bugs/3046.zig");
19 _ = @import("behavior/bugs/3586.zig");17 _ = @import("behavior/bugs/3586.zig");
20 _ = @import("behavior/bugs/4560.zig");18 _ = @import("behavior/bugs/4560.zig");
21 _ = @import("behavior/bugs/4769_a.zig");19 _ = @import("behavior/bugs/4769_a.zig");
22 _ = @import("behavior/bugs/4769_b.zig");20 _ = @import("behavior/bugs/4769_b.zig");
23 _ = @import("behavior/bugs/4954.zig");
24 _ = @import("behavior/bugs/6850.zig");21 _ = @import("behavior/bugs/6850.zig");
25 _ = @import("behavior/byval_arg_var.zig");
26 _ = @import("behavior/call.zig");22 _ = @import("behavior/call.zig");
27 _ = @import("behavior/cast.zig");23 _ = @import("behavior/cast.zig");
28 _ = @import("behavior/defer.zig");24 _ = @import("behavior/defer.zig");
29 _ = @import("behavior/enum.zig");25 _ = @import("behavior/enum.zig");
30 _ = @import("behavior/error.zig");26 _ = @import("behavior/error.zig");
31 _ = @import("behavior/fn_in_struct_in_comptime.zig");
32 _ = @import("behavior/generics.zig");27 _ = @import("behavior/generics.zig");
33 _ = @import("behavior/hasdecl.zig");28 _ = @import("behavior/hasdecl.zig");
34 _ = @import("behavior/hasfield.zig");29 _ = @import("behavior/hasfield.zig");
35 _ = @import("behavior/if.zig");30 _ = @import("behavior/if.zig");
36 _ = @import("behavior/import.zig");31 _ = @import("behavior/import.zig");
37 _ = @import("behavior/incomplete_struct_param_tld.zig");
38 _ = @import("behavior/int128.zig");32 _ = @import("behavior/int128.zig");
39 _ = @import("behavior/inttoptr.zig");
40 _ = @import("behavior/member_func.zig");33 _ = @import("behavior/member_func.zig");
41 _ = @import("behavior/null.zig");34 _ = @import("behavior/null.zig");
42 _ = @import("behavior/optional.zig");35 _ = @import("behavior/optional.zig");
43 _ = @import("behavior/pointers.zig");36 _ = @import("behavior/pointers.zig");
44 _ = @import("behavior/ptrcast.zig");37 _ = @import("behavior/ptrcast.zig");
45 _ = @import("behavior/pub_enum.zig");38 _ = @import("behavior/pub_enum.zig");
46 _ = @import("behavior/ref_var_in_if_after_if_2nd_switch_prong.zig");
47 _ = @import("behavior/slice_sentinel_comptime.zig");
48 _ = @import("behavior/struct.zig");39 _ = @import("behavior/struct.zig");
49 _ = @import("behavior/this.zig");40 _ = @import("behavior/this.zig");
50 _ = @import("behavior/translate_c_macros.zig");41 _ = @import("behavior/translate_c_macros.zig");
...@@ -56,22 +47,17 @@ test {...@@ -56,22 +47,17 @@ test {
56 if (builtin.object_format != .c) {47 if (builtin.object_format != .c) {
57 // Tests that pass for stage1 and stage2 but not the C backend.48 // Tests that pass for stage1 and stage2 but not the C backend.
58 _ = @import("behavior/align_llvm.zig");49 _ = @import("behavior/align_llvm.zig");
59 _ = @import("behavior/alignof.zig");
60 _ = @import("behavior/array.zig");50 _ = @import("behavior/array.zig");
61 _ = @import("behavior/atomics.zig");51 _ = @import("behavior/atomics.zig");
62 _ = @import("behavior/basic_llvm.zig");52 _ = @import("behavior/basic_llvm.zig");
63 _ = @import("behavior/bugs/394.zig");53 _ = @import("behavior/bugs/394.zig");
64 _ = @import("behavior/bugs/656.zig");
65 _ = @import("behavior/bugs/1277.zig");54 _ = @import("behavior/bugs/1277.zig");
66 _ = @import("behavior/bugs/1381.zig");
67 _ = @import("behavior/bugs/1500.zig");55 _ = @import("behavior/bugs/1500.zig");
68 _ = @import("behavior/bugs/1741.zig");56 _ = @import("behavior/bugs/1741.zig");
69 _ = @import("behavior/bugs/2006.zig");57 _ = @import("behavior/bugs/2006.zig");
70 _ = @import("behavior/bugs/2578.zig");
71 _ = @import("behavior/bugs/3007.zig");
72 _ = @import("behavior/bugs/3112.zig");58 _ = @import("behavior/bugs/3112.zig");
73 _ = @import("behavior/bugs/7250.zig");
74 _ = @import("behavior/cast_llvm.zig");59 _ = @import("behavior/cast_llvm.zig");
60 _ = @import("behavior/error.zig");
75 _ = @import("behavior/eval.zig");61 _ = @import("behavior/eval.zig");
76 _ = @import("behavior/floatop.zig");62 _ = @import("behavior/floatop.zig");
77 _ = @import("behavior/fn.zig");63 _ = @import("behavior/fn.zig");
...@@ -79,7 +65,6 @@ test {...@@ -79,7 +65,6 @@ test {
79 _ = @import("behavior/generics_llvm.zig");65 _ = @import("behavior/generics_llvm.zig");
80 _ = @import("behavior/math.zig");66 _ = @import("behavior/math.zig");
81 _ = @import("behavior/maximum_minimum.zig");67 _ = @import("behavior/maximum_minimum.zig");
82 _ = @import("behavior/namespace_depends_on_compile_var.zig");
83 _ = @import("behavior/null_llvm.zig");68 _ = @import("behavior/null_llvm.zig");
84 _ = @import("behavior/optional_llvm.zig");69 _ = @import("behavior/optional_llvm.zig");
85 _ = @import("behavior/popcount.zig");70 _ = @import("behavior/popcount.zig");
...@@ -99,6 +84,7 @@ test {...@@ -99,6 +84,7 @@ test {
99 _ = @import("behavior/slice_stage2.zig");84 _ = @import("behavior/slice_stage2.zig");
100 } else {85 } else {
101 _ = @import("behavior/align_stage1.zig");86 _ = @import("behavior/align_stage1.zig");
87 _ = @import("behavior/alignof.zig");
102 _ = @import("behavior/array_stage1.zig");88 _ = @import("behavior/array_stage1.zig");
103 if (builtin.os.tag != .wasi) {89 if (builtin.os.tag != .wasi) {
104 _ = @import("behavior/asm.zig");90 _ = @import("behavior/asm.zig");
...@@ -110,15 +96,18 @@ test {...@@ -110,15 +96,18 @@ test {
110 _ = @import("behavior/bitreverse.zig");96 _ = @import("behavior/bitreverse.zig");
111 _ = @import("behavior/bugs/421.zig");97 _ = @import("behavior/bugs/421.zig");
112 _ = @import("behavior/bugs/529.zig");98 _ = @import("behavior/bugs/529.zig");
99 _ = @import("behavior/bugs/656.zig");
113 _ = @import("behavior/bugs/718.zig");100 _ = @import("behavior/bugs/718.zig");
114 _ = @import("behavior/bugs/726.zig");101 _ = @import("behavior/bugs/726.zig");
115 _ = @import("behavior/bugs/828.zig");102 _ = @import("behavior/bugs/828.zig");
116 _ = @import("behavior/bugs/920.zig");103 _ = @import("behavior/bugs/920.zig");
117 _ = @import("behavior/bugs/1025.zig");104 _ = @import("behavior/bugs/1025.zig");
118 _ = @import("behavior/bugs/1076.zig");105 _ = @import("behavior/bugs/1076.zig");
106 _ = @import("behavior/bugs/1111.zig");
119 _ = @import("behavior/bugs/1120.zig");107 _ = @import("behavior/bugs/1120.zig");
120 _ = @import("behavior/bugs/1310.zig");108 _ = @import("behavior/bugs/1310.zig");
121 _ = @import("behavior/bugs/1322.zig");109 _ = @import("behavior/bugs/1322.zig");
110 _ = @import("behavior/bugs/1381.zig");
122 _ = @import("behavior/bugs/1421.zig");111 _ = @import("behavior/bugs/1421.zig");
123 _ = @import("behavior/bugs/1442.zig");112 _ = @import("behavior/bugs/1442.zig");
124 _ = @import("behavior/bugs/1607.zig");113 _ = @import("behavior/bugs/1607.zig");
...@@ -126,11 +115,15 @@ test {...@@ -126,11 +115,15 @@ test {
126 _ = @import("behavior/bugs/1851.zig");115 _ = @import("behavior/bugs/1851.zig");
127 _ = @import("behavior/bugs/1914.zig");116 _ = @import("behavior/bugs/1914.zig");
128 _ = @import("behavior/bugs/2114.zig");117 _ = @import("behavior/bugs/2114.zig");
118 _ = @import("behavior/bugs/2578.zig");
119 _ = @import("behavior/bugs/3007.zig");
120 _ = @import("behavior/bugs/3046.zig");
129 _ = @import("behavior/bugs/3367.zig");121 _ = @import("behavior/bugs/3367.zig");
130 _ = @import("behavior/bugs/3384.zig");122 _ = @import("behavior/bugs/3384.zig");
131 _ = @import("behavior/bugs/3742.zig");123 _ = @import("behavior/bugs/3742.zig");
132 _ = @import("behavior/bugs/3779.zig");124 _ = @import("behavior/bugs/3779.zig");
133 _ = @import("behavior/bugs/4328.zig");125 _ = @import("behavior/bugs/4328.zig");
126 _ = @import("behavior/bugs/4954.zig");
134 _ = @import("behavior/bugs/5398.zig");127 _ = @import("behavior/bugs/5398.zig");
135 _ = @import("behavior/bugs/5413.zig");128 _ = @import("behavior/bugs/5413.zig");
136 _ = @import("behavior/bugs/5474.zig");129 _ = @import("behavior/bugs/5474.zig");
...@@ -140,10 +133,12 @@ test {...@@ -140,10 +133,12 @@ test {
140 _ = @import("behavior/bugs/7003.zig");133 _ = @import("behavior/bugs/7003.zig");
141 _ = @import("behavior/bugs/7027.zig");134 _ = @import("behavior/bugs/7027.zig");
142 _ = @import("behavior/bugs/7047.zig");135 _ = @import("behavior/bugs/7047.zig");
136 _ = @import("behavior/bugs/7250.zig");
143 _ = @import("behavior/bugs/9584.zig");137 _ = @import("behavior/bugs/9584.zig");
144 _ = @import("behavior/bugs/9967.zig");138 _ = @import("behavior/bugs/9967.zig");
145 _ = @import("behavior/bugs/10147.zig");139 _ = @import("behavior/bugs/10147.zig");
146 _ = @import("behavior/byteswap.zig");140 _ = @import("behavior/byteswap.zig");
141 _ = @import("behavior/byval_arg_var.zig");
147 _ = @import("behavior/call_stage1.zig");142 _ = @import("behavior/call_stage1.zig");
148 _ = @import("behavior/cast_stage1.zig");143 _ = @import("behavior/cast_stage1.zig");
149 _ = @import("behavior/const_slice_child.zig");144 _ = @import("behavior/const_slice_child.zig");
...@@ -155,22 +150,28 @@ test {...@@ -155,22 +150,28 @@ test {
155 _ = @import("behavior/floatop_stage1.zig");150 _ = @import("behavior/floatop_stage1.zig");
156 _ = @import("behavior/fn_stage1.zig");151 _ = @import("behavior/fn_stage1.zig");
157 _ = @import("behavior/fn_delegation.zig");152 _ = @import("behavior/fn_delegation.zig");
153 _ = @import("behavior/fn_in_struct_in_comptime.zig");
158 _ = @import("behavior/for_stage1.zig");154 _ = @import("behavior/for_stage1.zig");
159 _ = @import("behavior/if_stage1.zig");155 _ = @import("behavior/if_stage1.zig");
156 _ = @import("behavior/incomplete_struct_param_tld.zig");
157 _ = @import("behavior/inttoptr.zig");
160 _ = @import("behavior/ir_block_deps.zig");158 _ = @import("behavior/ir_block_deps.zig");
161 _ = @import("behavior/math_stage1.zig");159 _ = @import("behavior/math_stage1.zig");
162 _ = @import("behavior/merge_error_sets.zig");160 _ = @import("behavior/merge_error_sets.zig");
163 _ = @import("behavior/misc.zig");161 _ = @import("behavior/misc.zig");
164 _ = @import("behavior/muladd.zig");162 _ = @import("behavior/muladd.zig");
163 _ = @import("behavior/namespace_depends_on_compile_var.zig");
165 _ = @import("behavior/null_stage1.zig");164 _ = @import("behavior/null_stage1.zig");
166 _ = @import("behavior/optional_stage1.zig");165 _ = @import("behavior/optional_stage1.zig");
167 _ = @import("behavior/pointers_stage1.zig");166 _ = @import("behavior/pointers_stage1.zig");
168 _ = @import("behavior/popcount_stage1.zig");167 _ = @import("behavior/popcount_stage1.zig");
169 _ = @import("behavior/ptrcast_stage1.zig");168 _ = @import("behavior/ptrcast_stage1.zig");
169 _ = @import("behavior/ref_var_in_if_after_if_2nd_switch_prong.zig");
170 _ = @import("behavior/reflection.zig");170 _ = @import("behavior/reflection.zig");
171 _ = @import("behavior/select.zig");171 _ = @import("behavior/select.zig");
172 _ = @import("behavior/shuffle.zig");172 _ = @import("behavior/shuffle.zig");
173 _ = @import("behavior/sizeof_and_typeof_stage1.zig");173 _ = @import("behavior/sizeof_and_typeof_stage1.zig");
174 _ = @import("behavior/slice_sentinel_comptime.zig");
174 _ = @import("behavior/slice_stage1.zig");175 _ = @import("behavior/slice_stage1.zig");
175 _ = @import("behavior/struct_contains_null_ptr_itself.zig");176 _ = @import("behavior/struct_contains_null_ptr_itself.zig");
176 _ = @import("behavior/struct_contains_slice_of_itself.zig");177 _ = @import("behavior/struct_contains_slice_of_itself.zig");
...@@ -185,6 +186,7 @@ test {...@@ -185,6 +186,7 @@ test {
185 _ = @import("behavior/typename.zig");186 _ = @import("behavior/typename.zig");
186 _ = @import("behavior/union_stage1.zig");187 _ = @import("behavior/union_stage1.zig");
187 _ = @import("behavior/union_with_members.zig");188 _ = @import("behavior/union_with_members.zig");
189 _ = @import("behavior/usingnamespace_stage1.zig");
188 _ = @import("behavior/var_args.zig");190 _ = @import("behavior/var_args.zig");
189 _ = @import("behavior/vector.zig");191 _ = @import("behavior/vector.zig");
190 if (builtin.target.cpu.arch == .wasm32) {192 if (builtin.target.cpu.arch == .wasm32) {
test/behavior/struct.zig-141
...@@ -6,55 +6,12 @@ const expectEqual = std.testing.expectEqual;...@@ -6,55 +6,12 @@ const expectEqual = std.testing.expectEqual;
6const expectEqualSlices = std.testing.expectEqualSlices;6const expectEqualSlices = std.testing.expectEqualSlices;
7const maxInt = std.math.maxInt;7const maxInt = std.math.maxInt;
88
9top_level_field: i32,
10
11test "top level fields" {
12 var instance = @This(){
13 .top_level_field = 1234,
14 };
15 instance.top_level_field += 1;
16 try expect(@as(i32, 1235) == instance.top_level_field);
17}
18
19const StructWithNoFields = struct {9const StructWithNoFields = struct {
20 fn add(a: i32, b: i32) i32 {10 fn add(a: i32, b: i32) i32 {
21 return a + b;11 return a + b;
22 }12 }
23};13};
2414
25const StructFoo = struct {
26 a: i32,
27 b: bool,
28 c: f32,
29};
30
31test "structs" {
32 var foo: StructFoo = undefined;
33 @memset(@ptrCast([*]u8, &foo), 0, @sizeOf(StructFoo));
34 foo.a += 1;
35 foo.b = foo.a == 1;
36 try testFoo(foo);
37 testMutation(&foo);
38 try expect(foo.c == 100);
39}
40fn testFoo(foo: StructFoo) !void {
41 try expect(foo.b);
42}
43fn testMutation(foo: *StructFoo) void {
44 foo.c = 100;
45}
46
47test "struct byval assign" {
48 var foo1: StructFoo = undefined;
49 var foo2: StructFoo = undefined;
50
51 foo1.a = 1234;
52 foo2.a = 0;
53 try expect(foo2.a == 0);
54 foo2 = foo1;
55 try expect(foo2.a == 1234);
56}
57
58test "call struct static method" {15test "call struct static method" {
59 const result = StructWithNoFields.add(3, 4);16 const result = StructWithNoFields.add(3, 4);
60 try expect(result == 7);17 try expect(result == 7);
...@@ -84,23 +41,6 @@ const Val = struct {...@@ -84,23 +41,6 @@ const Val = struct {
84 x: i32,41 x: i32,
85};42};
8643
87test "fn call of struct field" {
88 const Foo = struct {
89 ptr: fn () i32,
90 };
91 const S = struct {
92 fn aFunc() i32 {
93 return 13;
94 }
95
96 fn callStructField(foo: Foo) i32 {
97 return foo.ptr();
98 }
99 };
100
101 try expect(S.callStructField(Foo{ .ptr = S.aFunc }) == 13);
102}
103
104test "struct initializer" {44test "struct initializer" {
105 const val = Val{ .x = 42 };45 const val = Val{ .x = 42 };
106 try expect(val.x == 42);46 try expect(val.x == 42);
...@@ -118,84 +58,3 @@ test "call member function directly" {...@@ -118,84 +58,3 @@ test "call member function directly" {
118 const result = MemberFnTestFoo.member(instance);58 const result = MemberFnTestFoo.member(instance);
119 try expect(result == 1234);59 try expect(result == 1234);
120}60}
121
122test "store member function in variable" {
123 const instance = MemberFnTestFoo{ .x = 1234 };
124 const memberFn = MemberFnTestFoo.member;
125 const result = memberFn(instance);
126 try expect(result == 1234);
127}
128
129test "member functions" {
130 const r = MemberFnRand{ .seed = 1234 };
131 try expect(r.getSeed() == 1234);
132}
133const MemberFnRand = struct {
134 seed: u32,
135 pub fn getSeed(r: *const MemberFnRand) u32 {
136 return r.seed;
137 }
138};
139
140test "return struct byval from function" {
141 const bar = makeBar2(1234, 5678);
142 try expect(bar.y == 5678);
143}
144const Bar = struct {
145 x: i32,
146 y: i32,
147};
148fn makeBar2(x: i32, y: i32) Bar {
149 return Bar{
150 .x = x,
151 .y = y,
152 };
153}
154
155test "call method with mutable reference to struct with no fields" {
156 const S = struct {
157 fn doC(s: *const @This()) bool {
158 _ = s;
159 return true;
160 }
161 fn do(s: *@This()) bool {
162 _ = s;
163 return true;
164 }
165 };
166
167 var s = S{};
168 try expect(S.doC(&s));
169 try expect(s.doC());
170 try expect(S.do(&s));
171 try expect(s.do());
172}
173
174test "usingnamespace within struct scope" {
175 const S = struct {
176 usingnamespace struct {
177 pub fn inner() i32 {
178 return 42;
179 }
180 };
181 };
182 try expect(@as(i32, 42) == S.inner());
183}
184
185test "struct field init with catch" {
186 const S = struct {
187 fn doTheTest() !void {
188 var x: anyerror!isize = 1;
189 var req = Foo{
190 .field = x catch undefined,
191 };
192 try expect(req.field == 1);
193 }
194
195 pub const Foo = extern struct {
196 field: isize,
197 };
198 };
199 try S.doTheTest();
200 comptime try S.doTheTest();
201}
test/behavior/struct_llvm.zig+64-51
...@@ -6,6 +6,44 @@ const expectEqual = std.testing.expectEqual;...@@ -6,6 +6,44 @@ const expectEqual = std.testing.expectEqual;
6const expectEqualSlices = std.testing.expectEqualSlices;6const expectEqualSlices = std.testing.expectEqualSlices;
7const maxInt = std.math.maxInt;7const maxInt = std.math.maxInt;
88
9const StructWithNoFields = struct {
10 fn add(a: i32, b: i32) i32 {
11 return a + b;
12 }
13};
14
15const StructFoo = struct {
16 a: i32,
17 b: bool,
18 c: f32,
19};
20test "structs" {
21 var foo: StructFoo = undefined;
22 @memset(@ptrCast([*]u8, &foo), 0, @sizeOf(StructFoo));
23 foo.a += 1;
24 foo.b = foo.a == 1;
25 try testFoo(foo);
26 testMutation(&foo);
27 try expect(foo.c == 100);
28}
29fn testFoo(foo: StructFoo) !void {
30 try expect(foo.b);
31}
32fn testMutation(foo: *StructFoo) void {
33 foo.c = 100;
34}
35
36test "struct byval assign" {
37 var foo1: StructFoo = undefined;
38 var foo2: StructFoo = undefined;
39
40 foo1.a = 1234;
41 foo2.a = 0;
42 try expect(foo2.a == 0);
43 foo2 = foo1;
44 try expect(foo2.a == 1234);
45}
46
9const Node = struct {47const Node = struct {
10 val: Val,48 val: Val,
11 next: *Node,49 next: *Node,
...@@ -43,61 +81,36 @@ const VoidStructFieldsFoo = struct {...@@ -43,61 +81,36 @@ const VoidStructFieldsFoo = struct {
43 c: void,81 c: void,
44};82};
4583
46test "return empty struct from fn" {84test "member functions" {
47 _ = testReturnEmptyStructFromFn();85 const r = MemberFnRand{ .seed = 1234 };
48}86 try expect(r.getSeed() == 1234);
49const EmptyStruct2 = struct {};
50fn testReturnEmptyStructFromFn() EmptyStruct2 {
51 return EmptyStruct2{};
52}87}
88const MemberFnRand = struct {
89 seed: u32,
90 pub fn getSeed(r: *const MemberFnRand) u32 {
91 return r.seed;
92 }
93};
5394
54test "pass slice of empty struct to fn" {95test "return struct byval from function" {
55 try expect(testPassSliceOfEmptyStructToFn(&[_]EmptyStruct2{EmptyStruct2{}}) == 1);96 const bar = makeBar2(1234, 5678);
97 try expect(bar.y == 5678);
56}98}
57fn testPassSliceOfEmptyStructToFn(slice: []const EmptyStruct2) usize {99const Bar = struct {
58 return slice.len;100 x: i32,
59}101 y: i32,
60102};
61test "for loop over pointers to struct, getting field from struct pointer" {103fn makeBar2(x: i32, y: i32) Bar {
62 const S = struct {104 return Bar{
63 const Foo = struct {105 .x = x,
64 name: []const u8,106 .y = y,
65 };
66
67 var ok = true;
68
69 fn eql(a: []const u8) bool {
70 _ = a;
71 return true;
72 }
73
74 const ArrayList = struct {
75 fn toSlice(self: *ArrayList) []*Foo {
76 _ = self;
77 return @as([*]*Foo, undefined)[0..0];
78 }
79 };
80
81 fn doTheTest() !void {
82 var objects: ArrayList = undefined;
83
84 for (objects.toSlice()) |obj| {
85 if (eql(obj.name)) {
86 ok = false;
87 }
88 }
89
90 try expect(ok);
91 }
92 };107 };
93 try S.doTheTest();
94}108}
95109
96test "self-referencing struct via array member" {110test "return empty struct from fn" {
97 const T = struct {111 _ = testReturnEmptyStructFromFn();
98 children: [1]*@This(),112}
99 };113const EmptyStruct2 = struct {};
100 var x: T = undefined;114fn testReturnEmptyStructFromFn() EmptyStruct2 {
101 x = T{ .children = .{&x} };115 return EmptyStruct2{};
102 try expect(x.children[0] == &x);
103}116}
test/behavior/struct_stage1.zig+139
...@@ -6,6 +6,16 @@ const expectEqual = std.testing.expectEqual;...@@ -6,6 +6,16 @@ const expectEqual = std.testing.expectEqual;
6const expectEqualSlices = std.testing.expectEqualSlices;6const expectEqualSlices = std.testing.expectEqualSlices;
7const maxInt = std.math.maxInt;7const maxInt = std.math.maxInt;
88
9top_level_field: i32,
10
11test "top level fields" {
12 var instance = @This(){
13 .top_level_field = 1234,
14 };
15 instance.top_level_field += 1;
16 try expectEqual(@as(i32, 1235), instance.top_level_field);
17}
18
9const StructFoo = struct {19const StructFoo = struct {
10 a: i32,20 a: i32,
11 b: bool,21 b: bool,
...@@ -21,6 +31,36 @@ const Val = struct {...@@ -21,6 +31,36 @@ const Val = struct {
21 x: i32,31 x: i32,
22};32};
2333
34test "fn call of struct field" {
35 const Foo = struct {
36 ptr: fn () i32,
37 };
38 const S = struct {
39 fn aFunc() i32 {
40 return 13;
41 }
42
43 fn callStructField(foo: Foo) i32 {
44 return foo.ptr();
45 }
46 };
47
48 try expect(S.callStructField(Foo{ .ptr = S.aFunc }) == 13);
49}
50
51const MemberFnTestFoo = struct {
52 x: i32,
53 fn member(foo: MemberFnTestFoo) i32 {
54 return foo.x;
55 }
56};
57test "store member function in variable" {
58 const instance = MemberFnTestFoo{ .x = 1234 };
59 const memberFn = MemberFnTestFoo.member;
60 const result = memberFn(instance);
61 try expect(result == 1234);
62}
63
24test "empty struct method call" {64test "empty struct method call" {
25 const es = EmptyStruct{};65 const es = EmptyStruct{};
26 try expect(es.method() == 1234);66 try expect(es.method() == 1234);
...@@ -37,6 +77,13 @@ fn testReturnEmptyStructFromFn() EmptyStruct2 {...@@ -37,6 +77,13 @@ fn testReturnEmptyStructFromFn() EmptyStruct2 {
37 return EmptyStruct2{};77 return EmptyStruct2{};
38}78}
3979
80test "pass slice of empty struct to fn" {
81 try expect(testPassSliceOfEmptyStructToFn(&[_]EmptyStruct2{EmptyStruct2{}}) == 1);
82}
83fn testPassSliceOfEmptyStructToFn(slice: []const EmptyStruct2) usize {
84 return slice.len;
85}
86
40const APackedStruct = packed struct {87const APackedStruct = packed struct {
41 x: u8,88 x: u8,
42 y: u8,89 y: u8,
...@@ -285,6 +332,25 @@ fn alloc(comptime T: type) []T {...@@ -285,6 +332,25 @@ fn alloc(comptime T: type) []T {
285 return &[_]T{};332 return &[_]T{};
286}333}
287334
335test "call method with mutable reference to struct with no fields" {
336 const S = struct {
337 fn doC(s: *const @This()) bool {
338 _ = s;
339 return true;
340 }
341 fn do(s: *@This()) bool {
342 _ = s;
343 return true;
344 }
345 };
346
347 var s = S{};
348 try expect(S.doC(&s));
349 try expect(s.doC());
350 try expect(S.do(&s));
351 try expect(s.do());
352}
353
288test "implicit cast packed struct field to const ptr" {354test "implicit cast packed struct field to const ptr" {
289 const LevelUpMove = packed struct {355 const LevelUpMove = packed struct {
290 move_id: u9,356 move_id: u9,
...@@ -384,6 +450,17 @@ test "packed struct with fp fields" {...@@ -384,6 +450,17 @@ test "packed struct with fp fields" {
384 try expectEqual(@as(f32, 20.0), s.data[2]);450 try expectEqual(@as(f32, 20.0), s.data[2]);
385}451}
386452
453test "use within struct scope" {
454 const S = struct {
455 usingnamespace struct {
456 pub fn inner() i32 {
457 return 42;
458 }
459 };
460 };
461 try expectEqual(@as(i32, 42), S.inner());
462}
463
387test "default struct initialization fields" {464test "default struct initialization fields" {
388 const S = struct {465 const S = struct {
389 a: i32 = 1234,466 a: i32 = 1234,
...@@ -424,6 +501,41 @@ test "fn with C calling convention returns struct by value" {...@@ -424,6 +501,41 @@ test "fn with C calling convention returns struct by value" {
424 comptime try S.entry();501 comptime try S.entry();
425}502}
426503
504test "for loop over pointers to struct, getting field from struct pointer" {
505 const S = struct {
506 const Foo = struct {
507 name: []const u8,
508 };
509
510 var ok = true;
511
512 fn eql(a: []const u8) bool {
513 _ = a;
514 return true;
515 }
516
517 const ArrayList = struct {
518 fn toSlice(self: *ArrayList) []*Foo {
519 _ = self;
520 return @as([*]*Foo, undefined)[0..0];
521 }
522 };
523
524 fn doTheTest() !void {
525 var objects: ArrayList = undefined;
526
527 for (objects.toSlice()) |obj| {
528 if (eql(obj.name)) {
529 ok = false;
530 }
531 }
532
533 try expect(ok);
534 }
535 };
536 try S.doTheTest();
537}
538
427test "zero-bit field in packed struct" {539test "zero-bit field in packed struct" {
428 const S = packed struct {540 const S = packed struct {
429 x: u10,541 x: u10,
...@@ -433,6 +545,24 @@ test "zero-bit field in packed struct" {...@@ -433,6 +545,24 @@ test "zero-bit field in packed struct" {
433 _ = x;545 _ = x;
434}546}
435547
548test "struct field init with catch" {
549 const S = struct {
550 fn doTheTest() !void {
551 var x: anyerror!isize = 1;
552 var req = Foo{
553 .field = x catch undefined,
554 };
555 try expect(req.field == 1);
556 }
557
558 pub const Foo = extern struct {
559 field: isize,
560 };
561 };
562 try S.doTheTest();
563 comptime try S.doTheTest();
564}
565
436test "packed struct with non-ABI-aligned field" {566test "packed struct with non-ABI-aligned field" {
437 const S = packed struct {567 const S = packed struct {
438 x: u9,568 x: u9,
...@@ -587,6 +717,15 @@ test "anon struct literal field value initialized with fn call" {...@@ -587,6 +717,15 @@ test "anon struct literal field value initialized with fn call" {
587 comptime try S.doTheTest();717 comptime try S.doTheTest();
588}718}
589719
720test "self-referencing struct via array member" {
721 const T = struct {
722 children: [1]*@This(),
723 };
724 var x: T = undefined;
725 x = T{ .children = .{&x} };
726 try expect(x.children[0] == &x);
727}
728
590test "struct with union field" {729test "struct with union field" {
591 const Value = struct {730 const Value = struct {
592 ref: u32 = 2,731 ref: u32 = 2,
test/behavior/usingnamespace.zig-40
...@@ -1,5 +1,4 @@...@@ -1,5 +1,4 @@
1const std = @import("std");1const std = @import("std");
2const expect = std.testing.expect;
32
4const A = struct {3const A = struct {
5 pub const B = bool;4 pub const B = bool;
...@@ -12,42 +11,3 @@ const C = struct {...@@ -12,42 +11,3 @@ const C = struct {
12test "basic usingnamespace" {11test "basic usingnamespace" {
13 try std.testing.expect(C.B == bool);12 try std.testing.expect(C.B == bool);
14}13}
15
16fn Foo(comptime T: type) type {
17 return struct {
18 usingnamespace T;
19 };
20}
21
22test "usingnamespace inside a generic struct" {
23 const std2 = Foo(std);
24 const testing2 = Foo(std.testing);
25 try std2.testing.expect(true);
26 try testing2.expect(true);
27}
28
29usingnamespace struct {
30 pub const foo = 42;
31};
32
33test "usingnamespace does not redeclare an imported variable" {
34 comptime try std.testing.expect(@This().foo == 42);
35}
36
37usingnamespace @import("usingnamespace/foo.zig");
38test "usingnamespace omits mixing in private functions" {
39 try expect(@This().privateFunction());
40 try expect(!@This().printText());
41}
42fn privateFunction() bool {
43 return true;
44}
45
46test {
47 _ = @import("usingnamespace/import_segregation.zig");
48}
49
50usingnamespace @import("usingnamespace/a.zig");
51test "two files usingnamespace import each other" {
52 try expect(@This().ok());
53}
test/behavior/usingnamespace_stage1.zig created+41
...@@ -0,0 +1,41 @@
1const std = @import("std");
2const expect = std.testing.expect;
3
4fn Foo(comptime T: type) type {
5 return struct {
6 usingnamespace T;
7 };
8}
9
10test "usingnamespace inside a generic struct" {
11 const std2 = Foo(std);
12 const testing2 = Foo(std.testing);
13 try std2.testing.expect(true);
14 try testing2.expect(true);
15}
16
17usingnamespace struct {
18 pub const foo = 42;
19};
20
21test "usingnamespace does not redeclare an imported variable" {
22 comptime try std.testing.expect(@This().foo == 42);
23}
24
25usingnamespace @import("usingnamespace/foo.zig");
26test "usingnamespace omits mixing in private functions" {
27 try expect(@This().privateFunction());
28 try expect(!@This().printText());
29}
30fn privateFunction() bool {
31 return true;
32}
33
34test {
35 _ = @import("usingnamespace/import_segregation.zig");
36}
37
38usingnamespace @import("usingnamespace/a.zig");
39test "two files usingnamespace import each other" {
40 try expect(@This().ok());
41}