master
  1const std = @import("std");
  2const expect = std.testing.expect;
  3const builtin = @import("builtin");
  4
  5test "inline scalar prongs" {
  6    if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO
  7
  8    var x: usize = 0;
  9    switch (x) {
 10        10 => |*item| try expect(@TypeOf(item) == *usize),
 11        inline 11 => |*item| {
 12            try expect(@TypeOf(item) == *const usize);
 13            try expect(item.* == 11);
 14        },
 15        else => {},
 16    }
 17}
 18
 19test "inline prong ranges" {
 20    if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO
 21
 22    var x: usize = 0;
 23    _ = &x;
 24    switch (x) {
 25        inline 0...20, 24 => |item| {
 26            if (item > 25) @compileError("bad");
 27        },
 28        else => {},
 29    }
 30}
 31
 32const E = enum { a, b, c, d };
 33test "inline switch enums" {
 34    if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO
 35
 36    var x: E = .a;
 37    _ = &x;
 38    switch (x) {
 39        inline .a, .b => |aorb| if (aorb != .a and aorb != .b) @compileError("bad"),
 40        inline .c, .d => |cord| if (cord != .c and cord != .d) @compileError("bad"),
 41    }
 42}
 43
 44const U = union(E) { a: void, b: u2, c: u3, d: u4 };
 45test "inline switch unions" {
 46    if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO
 47    if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO
 48
 49    var x: U = .a;
 50    _ = &x;
 51    switch (x) {
 52        inline .a, .b => |aorb, tag| {
 53            if (tag == .a) {
 54                try expect(@TypeOf(aorb) == void);
 55            } else {
 56                try expect(tag == .b);
 57                try expect(@TypeOf(aorb) == u2);
 58            }
 59        },
 60        inline .c, .d => |cord, tag| {
 61            if (tag == .c) {
 62                try expect(@TypeOf(cord) == u3);
 63            } else {
 64                try expect(tag == .d);
 65                try expect(@TypeOf(cord) == u4);
 66            }
 67        },
 68    }
 69}
 70
 71test "inline else bool" {
 72    if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO
 73
 74    var a = true;
 75    _ = &a;
 76    switch (a) {
 77        true => {},
 78        inline else => |val| if (val != false) @compileError("bad"),
 79    }
 80}
 81
 82test "inline else error" {
 83    if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO
 84
 85    const Err = error{ a, b, c };
 86    var a = Err.a;
 87    _ = &a;
 88    switch (a) {
 89        error.a => {},
 90        inline else => |val| comptime if (val == error.a) @compileError("bad"),
 91    }
 92}
 93
 94test "inline else enum" {
 95    if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO
 96
 97    const E2 = enum(u8) { a = 2, b = 3, c = 4, d = 5 };
 98    var a: E2 = .a;
 99    _ = &a;
100    switch (a) {
101        .a, .b => {},
102        inline else => |val| comptime if (@intFromEnum(val) < 4) @compileError("bad"),
103    }
104}
105
106test "inline else int with gaps" {
107    if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO
108    if (builtin.zig_backend == .stage2_spirv) return error.SkipZigTest; // TODO
109
110    var a: u8 = 0;
111    _ = &a;
112    switch (a) {
113        1...125, 128...254 => {},
114        inline else => |val| {
115            if (val != 0 and
116                val != 126 and
117                val != 127 and
118                val != 255)
119                @compileError("bad");
120        },
121    }
122}
123
124test "inline else int all values" {
125    if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO
126
127    var a: u2 = 0;
128    _ = &a;
129    switch (a) {
130        inline else => |val| {
131            if (val != 0 and
132                val != 1 and
133                val != 2 and
134                val != 3)
135                @compileError("bad");
136        },
137    }
138}
139
140test "inline switch capture is set when switch operand is comptime known" {
141    const U2 = union(enum) {
142        a: u32,
143    };
144    var u: U2 = undefined;
145    switch (u) {
146        inline else => |*f, tag| {
147            try expect(@TypeOf(f) == *u32);
148            try expect(tag == .a);
149        },
150    }
151}