diff options
| author | Veikka Tuominen <git@vexu.eu> | 2022-09-16 23:49:00 +0300 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2022-09-16 23:49:00 +0300 |
| commit | b2aedb07096fa4ed8766d3aa87e70704cee68265 (patch) | |
| tree | 415ec2d04881991f541477ec0d0c1d96a21d056d /test/behavior | |
| parent | 8edd7219c0d5cc5799ae26ee8299b4d4114f7aed (diff) | |
| parent | 31daea74d23be813737892a166cc16ade1272a1a (diff) | |
| download | zig-b2aedb07096fa4ed8766d3aa87e70704cee68265.tar.gz zig-b2aedb07096fa4ed8766d3aa87e70704cee68265.zip | |
Merge pull request #12796 from Vexu/referenced-by-v2
stage2: add referenced by trace to compile errors attempt #2 (+ some fixes)
Diffstat (limited to 'test/behavior')
| -rw-r--r-- | test/behavior/bugs/12786.zig | 28 | ||||
| -rw-r--r-- | test/behavior/bugs/12794.zig | 38 | ||||
| -rw-r--r-- | test/behavior/bugs/12801-1.zig | 13 | ||||
| -rw-r--r-- | test/behavior/bugs/12801-2.zig | 24 | ||||
| -rw-r--r-- | test/behavior/eval.zig | 61 | ||||
| -rw-r--r-- | test/behavior/generics.zig | 11 |
6 files changed, 175 insertions, 0 deletions
diff --git a/test/behavior/bugs/12786.zig b/test/behavior/bugs/12786.zig new file mode 100644 index 0000000000..e8c1a2333f --- /dev/null +++ b/test/behavior/bugs/12786.zig @@ -0,0 +1,28 @@ +const std = @import("std"); + +fn NamespacedGlobals(comptime modules: anytype) type { + return @Type(.{ + .Struct = .{ + .layout = .Auto, + .is_tuple = false, + .fields = &.{ + .{ + .name = "globals", + .field_type = modules.mach.globals, + .default_value = null, + .is_comptime = false, + .alignment = @alignOf(modules.mach.globals), + }, + }, + .decls = &[_]std.builtin.Type.Declaration{}, + }, + }); +} + +test { + _ = NamespacedGlobals(.{ + .mach = .{ + .globals = struct {}, + }, + }); +} diff --git a/test/behavior/bugs/12794.zig b/test/behavior/bugs/12794.zig new file mode 100644 index 0000000000..530f81cff2 --- /dev/null +++ b/test/behavior/bugs/12794.zig @@ -0,0 +1,38 @@ +const std = @import("std"); + +fn NamespacedComponents(comptime modules: anytype) type { + return @Type(.{ + .Struct = .{ + .layout = .Auto, + .is_tuple = false, + .fields = &.{.{ + .name = "components", + .field_type = @TypeOf(modules.components), + .default_value = null, + .is_comptime = false, + .alignment = @alignOf(@TypeOf(modules.components)), + }}, + .decls = &[_]std.builtin.Type.Declaration{}, + }, + }); +} + +fn namespacedComponents(comptime modules: anytype) NamespacedComponents(modules) { + var x: NamespacedComponents(modules) = undefined; + x.components = modules.components; + return x; +} + +pub fn World(comptime modules: anytype) type { + const all_components = namespacedComponents(modules); + _ = all_components; + return struct {}; +} + +test { + _ = World(.{ + .components = .{ + .location = struct {}, + }, + }); +} diff --git a/test/behavior/bugs/12801-1.zig b/test/behavior/bugs/12801-1.zig new file mode 100644 index 0000000000..ff94382d1f --- /dev/null +++ b/test/behavior/bugs/12801-1.zig @@ -0,0 +1,13 @@ +const std = @import("std"); +const builtin = @import("builtin"); + +comptime capacity: fn () u64 = capacity_, +fn capacity_() u64 { + return 64; +} + +test { + if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO + + try std.testing.expect((@This(){}).capacity() == 64); +} diff --git a/test/behavior/bugs/12801-2.zig b/test/behavior/bugs/12801-2.zig new file mode 100644 index 0000000000..f98fcfbcff --- /dev/null +++ b/test/behavior/bugs/12801-2.zig @@ -0,0 +1,24 @@ +const std = @import("std"); +const builtin = @import("builtin"); + +const Auto = struct { + auto: [max_len]u8 = undefined, + offset: u64 = 0, + + comptime capacity: *const fn () u64 = capacity, + + const max_len: u64 = 32; + + fn capacity() u64 { + return max_len; + } +}; +test { + if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO + if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO + if (builtin.zig_backend == .stage2_x86_64) return error.SkipZigTest; // TODO + + const a: Auto = .{ .offset = 16, .capacity = Auto.capacity }; + try std.testing.expect(a.capacity() == 32); + try std.testing.expect((a.capacity)() == 32); +} diff --git a/test/behavior/eval.zig b/test/behavior/eval.zig index 47d2e4374e..849e0ca6cc 100644 --- a/test/behavior/eval.zig +++ b/test/behavior/eval.zig @@ -1337,3 +1337,64 @@ test "lazy value is resolved as slice operand" { try expect(@ptrToInt(ptr1) == @ptrToInt(ptr2)); try expect(ptr1.len == ptr2.len); } + +test "break from inline loop depends on runtime condition" { + const S = struct { + fn foo(a: u8) bool { + return a == 4; + } + }; + const arr = [_]u8{ 1, 2, 3, 4 }; + { + const blk = blk: { + inline for (arr) |val| { + if (S.foo(val)) { + break :blk val; + } + } + return error.TestFailed; + }; + try expect(blk == 4); + } + + { + comptime var i = 0; + const blk = blk: { + inline while (i < arr.len) : (i += 1) { + const val = arr[i]; + if (S.foo(val)) { + break :blk val; + } + } + return error.TestFailed; + }; + try expect(blk == 4); + } +} + +test "inline for inside a runtime condition" { + var a = false; + if (a) { + const arr = .{ 1, 2, 3 }; + inline for (arr) |val| { + if (val < 3) continue; + try expect(val == 3); + } + } +} + +test "continue in inline for inside a comptime switch" { + const arr = .{ 1, 2, 3 }; + var count: u8 = 0; + switch (arr[1]) { + 2 => { + inline for (arr) |val| { + if (val == 2) continue; + + count += val; + } + }, + else => {}, + } + try expect(count == 4); +} diff --git a/test/behavior/generics.zig b/test/behavior/generics.zig index a82456aa70..b3c399cea8 100644 --- a/test/behavior/generics.zig +++ b/test/behavior/generics.zig @@ -358,3 +358,14 @@ test "nested generic function" { try expect(@typeInfo(@TypeOf(S.g)).Fn.is_generic); try S.foo(u32, S.bar, 123); } + +test "extern function used as generic parameter" { + const S = struct { + extern fn foo() void; + extern fn bar() void; + inline fn baz(comptime _: anytype) type { + return struct {}; + } + }; + try expect(S.baz(S.foo) != S.baz(S.bar)); +} |
