aboutsummaryrefslogtreecommitdiff
path: root/test/behavior/error.zig
blob: aa39b685ca7ae20dda357085565a963a24e2b237 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
const builtin = @import("builtin");
const std = @import("std");
const assert = std.debug.assert;
const expect = std.testing.expect;
const expectEqual = std.testing.expectEqual;
const mem = std.mem;

/// A more basic implementation of std.testing.expectError which
/// does not require formatter/printing support
fn expectError(expected_err: anyerror, observed_err_union: anytype) !void {
    if (observed_err_union) |_| {
        return error.TestExpectedError;
    } else |err| if (err == expected_err) {
        return; // Success
    }
    return error.TestExpectedError;
}

test "error values" {
    const a = @intFromError(error.err1);
    const b = @intFromError(error.err2);
    try expect(a != b);
}

test "redefinition of error values allowed" {
    shouldBeNotEqual(error.AnError, error.SecondError);
}
fn shouldBeNotEqual(a: anyerror, b: anyerror) void {
    if (a == b) unreachable;
}

test "error binary operator" {
    if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO

    const a = errBinaryOperatorG(true) catch 3;
    const b = errBinaryOperatorG(false) catch 3;
    try expect(a == 3);
    try expect(b == 10);
}
fn errBinaryOperatorG(x: bool) anyerror!isize {
    return if (x) error.ItBroke else @as(isize, 10);
}

test "empty error union" {
    const x = error{} || error{};
    _ = x;
}

pub fn foo() anyerror!i32 {
    const x = try bar();
    return x + 1;
}

pub fn bar() anyerror!i32 {
    return 13;
}

pub fn baz() anyerror!i32 {
    const y = foo() catch 1234;
    return y + 1;
}

test "error wrapping" {
    if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO

    try expect((baz() catch unreachable) == 15);
}

test "unwrap simple value from error" {
    if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO

    const i = unwrapSimpleValueFromErrorDo() catch unreachable;
    try expect(i == 13);
}
fn unwrapSimpleValueFromErrorDo() anyerror!isize {
    return 13;
}

test "error return in assignment" {
    if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO

    doErrReturnInAssignment() catch unreachable;
}

fn doErrReturnInAssignment() anyerror!void {
    var x: i32 = undefined;
    x = try makeANonErr();
}

fn makeANonErr() anyerror!i32 {
    return 1;
}

test "syntax: optional operator in front of error union operator" {
    comptime {
        try expect(?(anyerror!i32) == ?(anyerror!i32));
    }
}

test "widen cast integer payload of error union function call" {
    if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest;
    if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO

    const S = struct {
        fn errorable() !u64 {
            return @as(u64, try number());
        }

        fn number() anyerror!u32 {
            return 1234;
        }
    };
    try expect((try S.errorable()) == 1234);
}

test "debug info for optional error set" {
    if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest;
    if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO

    const SomeError = error{ Hello, Hello2 };
    var a_local_variable: ?SomeError = null;
    _ = &a_local_variable;
}

test "implicit cast to optional to error union to return result loc" {
    if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO
    if (builtin.zig_backend == .stage2_spirv) return error.SkipZigTest;

    const S = struct {
        fn entry() !void {
            var x: Foo = undefined;
            if (func(&x)) |opt| {
                try expect(opt != null);
            } else |_| @panic("expected non error");
        }
        fn func(f: *Foo) anyerror!?*Foo {
            return f;
        }
        const Foo = struct {
            field: i32,
        };
    };
    try S.entry();
    //comptime S.entry(); TODO
}

test "fn returning empty error set can be passed as fn returning any error" {
    entry();
    comptime entry();
}

test "fn returning empty error set can be passed as fn returning any error - pointer" {
    if (builtin.zig_backend == .stage2_spirv) return error.SkipZigTest;

    entryPtr();
    comptime entryPtr();
}

fn entry() void {
    foo2(bar2);
}

fn entryPtr() void {
    var ptr = &bar2;
    _ = &ptr;
    fooPtr(ptr);
}

fn foo2(comptime f: fn () anyerror!void) void {
    const x = f();
    x catch {
        @panic("fail");
    };
}

fn fooPtr(f: *const fn () anyerror!void) void {
    const x = f();
    x catch {
        @panic("fail");
    };
}

fn bar2() (error{}!void) {}

test "error union type " {
    try testErrorUnionType();
    try comptime testErrorUnionType();
}

fn testErrorUnionType() !void {
    const x: anyerror!i32 = 1234;
    if (x) |value| try expect(value == 1234) else |_| unreachable;
    try expect(@typeInfo(@TypeOf(x)) == .error_union);
    try expect(@typeInfo(@typeInfo(@TypeOf(x)).error_union.error_set) == .error_set);
    try expect(@typeInfo(@TypeOf(x)).error_union.error_set == anyerror);
}

test "error set type" {
    try testErrorSetType();
    try comptime testErrorSetType();
}

const MyErrSet = error{
    OutOfMemory,
    FileNotFound,
};

fn testErrorSetType() !void {
    try expect(@typeInfo(MyErrSet).error_set.?.len == 2);

    const a: MyErrSet!i32 = 5678;
    const b: MyErrSet!i32 = MyErrSet.OutOfMemory;
    try expect(b catch error.OutOfMemory == error.OutOfMemory);

    if (a) |value| try expect(value == 5678) else |err| switch (err) {
        error.OutOfMemory => unreachable,
        error.FileNotFound => unreachable,
    }
}

test "explicit error set cast" {
    if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO

    try testExplicitErrorSetCast(Set1.A);
    try comptime testExplicitErrorSetCast(Set1.A);
}

const Set1 = error{ A, B };
const Set2 = error{ A, C };

fn testExplicitErrorSetCast(set1: Set1) !void {
    const x: Set2 = @errorCast(set1);
    try expect(@TypeOf(x) == Set2);
    const y: Set1 = @errorCast(x);
    try expect(@TypeOf(y) == Set1);
    try expect(y == error.A);
}

test "@errorCast on error unions" {
    const S = struct {
        fn doTheTest() !void {
            {
                const casted: error{Bad}!i32 = @errorCast(retErrUnion());
                try expect((try casted) == 1234);
            }
            {
                const casted: error{Bad}!i32 = @errorCast(retInferredErrUnion());
                try expect((try casted) == 5678);
            }
        }

        fn retErrUnion() anyerror!i32 {
            return 1234;
        }

        fn retInferredErrUnion() !i32 {
            return 5678;
        }
    };

    try S.doTheTest();
    try comptime S.doTheTest();
}

test "comptime test error for empty error set" {
    if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO

    try testComptimeTestErrorEmptySet(1234);
    try comptime testComptimeTestErrorEmptySet(1234);
}

const EmptyErrorSet = error{};

fn testComptimeTestErrorEmptySet(x: EmptyErrorSet!i32) !void {
    if (x) |v| try expect(v == 1234) else |err| {
        _ = err;
        @compileError("bad");
    }
}

test "comptime err to int of error set with only 1 possible value" {
    testErrToIntWithOnePossibleValue(error.A, @intFromError(error.A));
    comptime testErrToIntWithOnePossibleValue(error.A, @intFromError(error.A));
}
fn testErrToIntWithOnePossibleValue(
    x: error{A},
    comptime value: u32,
) void {
    if (@intFromError(x) != value) {
        @compileError("bad");
    }
}

test "inferred empty error set comptime catch" {
    const S = struct {
        fn foo() !void {}
    };
    S.foo() catch @compileError("fail");
}

test "error inference with an empty set" {
    const S = struct {
        const Struct = struct {
            pub fn func() (error{})!usize {
                return 0;
            }
        };

        fn AnotherStruct(comptime SubStruct: type) type {
            return struct {
                fn anotherFunc() !void {
                    try expect(0 == (try SubStruct.func()));
                }
            };
        }
    };

    const GeneratedStruct = S.AnotherStruct(S.Struct);
    try GeneratedStruct.anotherFunc();
}

test "error union peer type resolution" {
    if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO

    try testErrorUnionPeerTypeResolution(1);
}

fn testErrorUnionPeerTypeResolution(x: i32) !void {
    const y = switch (x) {
        1 => bar_1(),
        2 => baz_1(),
        else => quux_1(),
    };
    if (y) |_| {
        @panic("expected error");
    } else |e| {
        try expect(e == error.A);
    }
}

fn bar_1() anyerror {
    return error.A;
}

fn baz_1() !i32 {
    return error.B;
}

fn quux_1() !i32 {
    return error.C;
}

test "error: Zero sized error set returned with value payload crash" {
    if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO

    _ = try foo3(0);
    _ = try comptime foo3(0);
}

const Error = error{};
fn foo3(b: usize) Error!usize {
    return b;
}

test "error: Infer error set from literals" {
    if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO
    if (builtin.zig_backend == .stage2_spirv) return error.SkipZigTest;

    _ = nullLiteral("n") catch |err| handleErrors(err);
    _ = floatLiteral("n") catch |err| handleErrors(err);
    _ = intLiteral("n") catch |err| handleErrors(err);
    _ = comptime nullLiteral("n") catch |err| handleErrors(err);
    _ = comptime floatLiteral("n") catch |err| handleErrors(err);
    _ = comptime intLiteral("n") catch |err| handleErrors(err);
}

fn handleErrors(err: anytype) noreturn {
    switch (err) {
        error.T => {},
    }

    unreachable;
}

fn nullLiteral(str: []const u8) !?i64 {
    if (str[0] == 'n') return null;

    return error.T;
}

fn floatLiteral(str: []const u8) !?f64 {
    if (str[0] == 'n') return 1.0;

    return error.T;
}

fn intLiteral(str: []const u8) !?i64 {
    if (str[0] == 'n') return 1;

    return error.T;
}

test "nested error union function call in optional unwrap" {
    if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO
    if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO

    const S = struct {
        const Foo = struct {
            a: i32,
        };

        fn errorable() !i32 {
            const x: Foo = (try getFoo()) orelse return error.Other;
            return x.a;
        }

        fn errorable2() !i32 {
            const x: Foo = (try getFoo2()) orelse return error.Other;
            return x.a;
        }

        fn errorable3() !i32 {
            const x: Foo = (try getFoo3()) orelse return error.Other;
            return x.a;
        }

        fn getFoo() anyerror!?Foo {
            return Foo{ .a = 1234 };
        }

        fn getFoo2() anyerror!?Foo {
            return error.Failure;
        }

        fn getFoo3() anyerror!?Foo {
            return null;
        }
    };
    try expect((try S.errorable()) == 1234);
    try expectError(error.Failure, S.errorable2());
    try expectError(error.Other, S.errorable3());
    comptime {
        try expect((try S.errorable()) == 1234);
        try expectError(error.Failure, S.errorable2());
        try expectError(error.Other, S.errorable3());
    }
}

test "return function call to error set from error union function" {
    if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO

    const S = struct {
        fn errorable() anyerror!i32 {
            return fail();
        }

        fn fail() anyerror {
            return error.Failure;
        }
    };
    try expectError(error.Failure, S.errorable());
    comptime assert(error.Failure == S.errorable());
}

test "optional error set is the same size as error set" {
    if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO
    if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO

    comptime assert(@sizeOf(?anyerror) == @sizeOf(anyerror));
    comptime assert(@alignOf(?anyerror) == @alignOf(anyerror));
    const S = struct {
        fn returnsOptErrSet() ?anyerror {
            return null;
        }
    };
    try expect(S.returnsOptErrSet() == null);
    comptime assert(S.returnsOptErrSet() == null);
}

test "nested catch" {
    if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO

    const S = struct {
        fn entry() !void {
            try expectError(error.Bad, func());
        }
        fn fail() anyerror!Foo {
            return error.Wrong;
        }
        fn func() anyerror!Foo {
            _ = fail() catch
                fail() catch
                return error.Bad;
            unreachable;
        }
        const Foo = struct {
            field: i32,
        };
    };
    try S.entry();
    try comptime S.entry();
}

test "function pointer with return type that is error union with payload which is pointer of parent struct" {
    if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO
    if (builtin.zig_backend == .stage2_spirv) return error.SkipZigTest;

    const S = struct {
        const Foo = struct {
            fun: *const fn (a: i32) (anyerror!*Foo),
        };

        const Err = error{UnspecifiedErr};

        fn bar(a: i32) anyerror!*Foo {
            _ = a;
            return Err.UnspecifiedErr;
        }

        fn doTheTest() !void {
            var x = Foo{ .fun = @This().bar };
            try expectError(error.UnspecifiedErr, x.fun(1));
        }
    };
    try S.doTheTest();
}

test "return result loc as peer result loc in inferred error set function" {
    if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest;
    if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO
    if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO

    const S = struct {
        fn doTheTest() !void {
            if (quux(2)) |x| {
                try expect(x.Two);
            } else |e| switch (e) {
                error.Whatever => @panic("fail"),
            }
            try expectError(error.Whatever, quux(99));
        }
        const FormValue = union(enum) {
            One: void,
            Two: bool,
        };

        fn quux(id: u64) !FormValue {
            return switch (id) {
                2 => FormValue{ .Two = true },
                1 => FormValue{ .One = {} },
                else => return error.Whatever,
            };
        }
    };
    try S.doTheTest();
    try comptime S.doTheTest();
}

test "error payload type is correctly resolved" {
    if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO
    if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO

    const MyIntWrapper = struct {
        const Self = @This();

        x: i32,

        pub fn create() anyerror!Self {
            return Self{ .x = 42 };
        }
    };

    try expect(std.meta.eql(MyIntWrapper{ .x = 42 }, try MyIntWrapper.create()));
}

test "error union comptime caching" {
    const S = struct {
        fn quux(comptime arg: anytype) void {
            arg catch {};
        }
    };

    S.quux(@as(anyerror!void, {}));
    S.quux(@as(anyerror!void, {}));
}

test "@errorName" {
    if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest;
    if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO
    if (builtin.zig_backend == .stage2_spirv) return error.SkipZigTest;
    if (builtin.zig_backend == .stage2_riscv64) return error.SkipZigTest;

    try expect(mem.eql(u8, @errorName(error.AnError), "AnError"));
    try expect(mem.eql(u8, @errorName(error.ALongerErrorName), "ALongerErrorName"));
    try expect(mem.eql(u8, @errorName(gimmeItBroke()), "ItBroke"));
}
fn gimmeItBroke() anyerror {
    return error.ItBroke;
}

test "@errorName sentinel length matches slice length" {
    if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest;
    if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO
    if (builtin.zig_backend == .stage2_spirv) return error.SkipZigTest;
    if (builtin.zig_backend == .stage2_riscv64) return error.SkipZigTest;

    const name = testBuiltinErrorName(error.FooBar);
    const length: usize = 6;
    try expect(length == std.mem.indexOfSentinel(u8, 0, name.ptr));
    try expect(length == name.len);
}

pub fn testBuiltinErrorName(err: anyerror) [:0]const u8 {
    return @errorName(err);
}

test "error set equality" {
    const a = error{One};
    const b = error{One};

    try expect(a == a);
    try expect(a == b);
    try expect(a == error{One});

    // should treat as a set
    const c = error{ One, Two };
    const d = error{ Two, One };

    try expect(c == d);
}

test "inferred error set equality" {
    if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO

    const S = struct {
        fn foo() !void {
            return @This().bar();
        }

        fn bar() !void {
            return error.Bad;
        }

        fn baz() !void {
            return quux();
        }

        fn quux() anyerror!void {}
    };

    const FooError = @typeInfo(@typeInfo(@TypeOf(S.foo)).@"fn".return_type.?).error_union.error_set;
    const BarError = @typeInfo(@typeInfo(@TypeOf(S.bar)).@"fn".return_type.?).error_union.error_set;
    const BazError = @typeInfo(@typeInfo(@TypeOf(S.baz)).@"fn".return_type.?).error_union.error_set;

    try expect(BarError != error{Bad});

    try expect(FooError != anyerror);
    try expect(BarError != anyerror);
    try expect(BazError != anyerror);

    try expect(FooError != BarError);
    try expect(FooError != BazError);
    try expect(BarError != BazError);

    try expect(FooError == FooError);
    try expect(BarError == BarError);
    try expect(BazError == BazError);
}

test "peer type resolution of two different error unions" {
    const a: error{B}!void = {};
    const b: error{A}!void = {};
    var cond = true;
    _ = &cond;
    const err = if (cond) a else b;
    try err;
}

test "coerce error set to the current inferred error set" {
    const S = struct {
        fn foo() !void {
            var a = false;
            _ = &a;
            if (a) {
                const b: error{A}!void = error.A;
                return b;
            }
            const b = error.A;
            return b;
        }
    };
    S.foo() catch {};
}

test "error union payload is properly aligned" {
    if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO
    if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO
    if (builtin.zig_backend == .stage2_spirv) return error.SkipZigTest;
    if (builtin.zig_backend == .stage2_riscv64) return error.SkipZigTest;

    const S = struct {
        a: u128,
        b: u128,
        c: u128,
        fn foo() error{}!@This() {
            return @This(){ .a = 1, .b = 2, .c = 3 };
        }
    };
    const blk = S.foo() catch unreachable;
    if (blk.a != 1) unreachable;
}

test "ret_ptr doesn't cause own inferred error set to be resolved" {
    if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO

    const S = struct {
        fn foo() !void {}

        fn doTheTest() !void {
            errdefer @compileError("bad");

            return try @This().foo();
        }
    };
    try S.doTheTest();
}

test "simple else prong allowed even when all errors handled" {
    if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO

    const S = struct {
        fn foo() !u8 {
            return error.Foo;
        }
    };
    var value = S.foo() catch |err| switch (err) {
        error.Foo => @as(u8, 255),
        else => |e| return e,
    };
    try expect(value == 255);
    value = S.foo() catch |err| switch (err) {
        error.Foo => 255,
        else => unreachable,
    };
    try expect(value == 255);
    value = S.foo() catch |err| switch (err) {
        error.Foo => 255,
        else => return,
    };
    try expect(value == 255);
}

test "pointer to error union payload" {
    if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO
    if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO
    if (builtin.zig_backend == .stage2_spirv) return error.SkipZigTest;
    if (builtin.zig_backend == .stage2_riscv64) return error.SkipZigTest;

    var err_union: anyerror!u8 = 15;

    const payload_ptr = &(err_union catch unreachable);
    try expect(payload_ptr.* == 15);
}

const NoReturn = struct {
    var a: u32 = undefined;
    fn someData() bool {
        a -= 1;
        return a == 0;
    }
    fn loop() !noreturn {
        while (true) {
            if (someData())
                return error.GenericFailure;
        }
    }
    fn testTry() anyerror {
        try loop();
    }
    fn testCatch() anyerror {
        loop() catch return error.OtherFailure;
        @compileError("bad");
    }
};

test "error union of noreturn used with if" {
    if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO
    if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO
    if (builtin.zig_backend == .stage2_spirv) return error.SkipZigTest;
    if (builtin.zig_backend == .stage2_riscv64) return error.SkipZigTest;

    NoReturn.a = 64;
    if (NoReturn.loop()) {
        @compileError("bad");
    } else |err| {
        try expect(err == error.GenericFailure);
    }
}

test "error union of noreturn used with try" {
    if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO
    if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO
    if (builtin.zig_backend == .stage2_spirv) return error.SkipZigTest;
    if (builtin.zig_backend == .stage2_riscv64) return error.SkipZigTest;

    NoReturn.a = 64;
    const err = NoReturn.testTry();
    try expect(err == error.GenericFailure);
}

test "error union of noreturn used with catch" {
    if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO
    if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO
    if (builtin.zig_backend == .stage2_spirv) return error.SkipZigTest;
    if (builtin.zig_backend == .stage2_riscv64) return error.SkipZigTest;

    NoReturn.a = 64;
    const err = NoReturn.testCatch();
    try expect(err == error.OtherFailure);
}

test "alignment of wrapping an error union payload" {
    if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest;
    if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO
    if (builtin.zig_backend == .stage2_spirv) return error.SkipZigTest;
    if (builtin.zig_backend == .stage2_riscv64) return error.SkipZigTest;

    const S = struct {
        const I = extern struct { x: i128 };

        fn foo() anyerror!I {
            var i: I = .{ .x = 1234 };
            _ = &i;
            return i;
        }
    };
    try expect((S.foo() catch unreachable).x == 1234);
}

test "compare error union and error set" {
    if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO

    var a: anyerror = error.Foo;
    var b: anyerror!u32 = error.Bar;
    _ = &a;

    try expect(a != b);
    try expect(b != a);

    b = error.Foo;

    try expect(a == b);
    try expect(b == a);

    b = 2;

    try expect(a != b);
    try expect(b != a);
}

fn non_errorable() void {
    // Make sure catch works even in a function that does not call any errorable functions.
    //
    // This test is needed because stage 2's fix for #1923 means that catch blocks interact
    // with the error return trace index.
    var x: error{Foo}!void = {};
    _ = &x;
    return x catch {};
}

test "catch within a function that calls no errorable functions" {
    non_errorable();
}

test "error from comptime string" {
    if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO
    if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO
    if (builtin.zig_backend == .stage2_spirv) return error.SkipZigTest;
    if (builtin.zig_backend == .stage2_riscv64) return error.SkipZigTest;

    const name = "Weird error name!";
    const S = struct {
        fn foo() !void {
            return @field(anyerror, name);
        }
    };
    if (S.foo()) unreachable else |err| {
        try expect(mem.eql(u8, name, @errorName(err)));
    }
}

test "field access of anyerror results in smaller error set" {
    const E1 = @TypeOf(error.Foo);
    try expect(@TypeOf(E1.Foo) == E1);
    const E2 = error{ A, B, C };
    try expect(@TypeOf(E2.A) == E2);
    try expect(@TypeOf(@field(anyerror, "NotFound")) == error{NotFound});
}

test "optional error union return type" {
    if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO

    const S = struct {
        fn foo() ?anyerror!u32 {
            var x: u32 = 1234;
            _ = &x;
            return @as(anyerror!u32, x);
        }
    };
    try expect(1234 == try S.foo().?);
}

test "optional error set return type" {
    if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO

    const E = error{ A, B };
    const S = struct {
        fn foo(return_null: bool) ?E {
            return if (return_null) null else E.A;
        }
    };

    try expect(null == S.foo(true));
    try expect(E.A == S.foo(false).?);
}

test "optional error set function parameter" {
    const S = struct {
        fn doTheTest(a: ?anyerror) !void {
            try std.testing.expect(a.? == error.OutOfMemory);
        }
    };
    try S.doTheTest(error.OutOfMemory);
    try comptime S.doTheTest(error.OutOfMemory);
}

test "returning an error union containing a type with no runtime bits" {
    if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO
    if (builtin.zig_backend == .stage2_spirv) return error.SkipZigTest;

    const ZeroByteType = struct {
        foo: void,

        pub fn init() !@This() {
            return .{ .foo = {} };
        }
    };

    var zero_byte: ZeroByteType = undefined;
    (&zero_byte).* = try ZeroByteType.init();
}

test "try used in recursive function with inferred error set" {
    if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO
    if (builtin.zig_backend == .stage2_spirv) return error.SkipZigTest;

    const Value = union(enum) {
        values: []const @This(),
        b,

        fn x(value: @This()) !void {
            switch (value.values[0]) {
                .values => return try x(value.values[0]),
                .b => return error.a,
            }
        }
    };
    const a = Value{
        .values = &[1]Value{
            .{
                .values = &[1]Value{.{ .b = {} }},
            },
        },
    };
    try expectError(error.a, Value.x(a));
}

test "generic inline function returns inferred error set" {
    const S = struct {
        inline fn retErr(comptime T: type) !T {
            return error.AnError;
        }

        fn main0() !void {
            _ = try retErr(u8);
        }
    };
    S.main0() catch |e| {
        try std.testing.expect(e == error.AnError);
    };
}

test "function called at runtime is properly analyzed for inferred error set" {
    if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO
    if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO

    const S = struct {
        fn foo() !void {
            var a = true;
            _ = &a;
            if (a) return error.Foo;
            return error.Bar;
        }
        fn bar() !void {
            try @This().foo();
        }
    };

    S.bar() catch |err| switch (err) {
        error.Foo => {},
        error.Bar => {},
    };
}

test "errorCast to adhoc inferred error set" {
    const S = struct {
        inline fn baz() !i32 {
            return @errorCast(err());
        }
        fn err() anyerror!i32 {
            return 1234;
        }
    };
    try std.testing.expect((try S.baz()) == 1234);
}

test "@errorCast from error set to error union" {
    const S = struct {
        fn doTheTest(set: error{ A, B }) error{A}!i32 {
            return @errorCast(set);
        }
    };
    try expectError(error.A, S.doTheTest(error.A));
    try expectError(error.A, comptime S.doTheTest(error.A));
}

test "@errorCast from error union to error union" {
    const S = struct {
        fn doTheTest(set: error{ A, B }!i32) error{A}!i32 {
            return @errorCast(set);
        }
    };
    try expectError(error.A, S.doTheTest(error.A));
    try expectError(error.A, comptime S.doTheTest(error.A));
}

test "result location initialization of error union with OPV payload" {
    if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest;
    if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO
    if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO
    if (builtin.zig_backend == .stage2_spirv) return error.SkipZigTest;
    if (builtin.zig_backend == .stage2_riscv64) return error.SkipZigTest; // TODO

    const S = struct {
        x: u0,
    };

    const a: anyerror!S = .{ .x = 0 };
    comptime assert((a catch unreachable).x == 0);

    comptime {
        var b: anyerror!S = .{ .x = 0 };
        _ = &b;
        assert((b catch unreachable).x == 0);
    }

    var c: anyerror!S = .{ .x = 0 };
    _ = &c;
    try expectEqual(0, (c catch return error.TestFailed).x);
}

test "return error union with i65" {
    if (builtin.zig_backend == .stage2_riscv64) return error.SkipZigTest;
    if (builtin.zig_backend == .stage2_spirv) return error.SkipZigTest;

    try expect(try add(1000, 234) == 1234);
}

fn add(x: i65, y: i65) anyerror!i65 {
    return x + y;
}

test "compare error union to error set" {
    const S = struct {
        fn doTheTest(val: error{Foo}!i32) !void {
            if (error.Foo == val) return error.Unexpected;
            if (val == error.Foo) return error.Unexpected;
        }
    };
    try S.doTheTest(0);
    try comptime S.doTheTest(0);
}