Debian Codegraph @

gcc/testsuite/gdc.test/runnable/eh.d

raw
1 // PERMUTE_ARGS: -O -fPIC
2
3 extern(C) int printf(const char*, ...);
4
5 /****************************************************/
6
7 class Abc : Exception
8 {
9 this()
10 {
11 super("");
12 }
13 int i;
14 }
15
16 int y;
17
18 alias int boo;
19
20 void foo(int x)
21 {
22 y = cast(boo)1;
23 L6:
24 try
25 {
26 printf("try 1\n");
27 y += 4;
28 if (y == 5)
29 goto L6;
30 y += 3;
31 }
32 finally
33 {
34 y += 5;
35 printf("finally 1\n");
36 }
37 try
38 {
39 printf("try 2\n");
40 y = 1;
41 if (y == 4)
42 goto L6;
43 y++;
44 }
45 catch (Abc c)
46 {
47 printf("catch 2\n");
48 y = 2 + c.i;
49 }
50 y++;
51 printf("done\n");
52 }
53
54 /****************************************************/
55
56
57 class IntException : Exception
58 {
59 this(int i)
60 {
61 m_i = i;
62 super("");
63 }
64
65 int getValue()
66 {
67 return m_i;
68 }
69
70 int m_i;
71 }
72
73
74 void test2()
75 {
76 int cIterations = 10;
77
78 int i;
79 long total_x = 0;
80 long total_nox = 0;
81
82 for(int WARMUPS = 2; WARMUPS-- > 0; )
83 {
84 for(total_x = 0, i = 0; i < cIterations; ++i)
85 {
86 total_nox += fn2_nox();
87 }
88 printf("foo\n");
89
90 for(total_nox = 0, i = 0; i < cIterations; ++i)
91 {
92 printf("i = %d\n", i);
93 try
94 {
95 int z = 1;
96
97 throw new IntException(z);
98 }
99 catch(IntException x)
100 {
101 printf("catch, i = %d\n", i);
102 total_x += x.getValue();
103 }
104 }
105 }
106
107 printf("iterations %d totals: %lld, %lld\n", cIterations, total_x, total_nox);
108 }
109
110 int fn2_nox()
111 {
112 return 47;
113 }
114
115
116 /****************************************************/
117
118 void test3()
119 {
120 static int x;
121 try
122 {
123 }
124 finally
125 {
126 printf("a\n");
127 assert(x == 0);
128 x++;
129 }
130 printf("--\n");
131 assert(x == 1);
132 try
133 {
134 printf("tb\n");
135 assert(x == 1);
136 }
137 finally
138 {
139 printf("b\n");
140 assert(x == 1);
141 x++;
142 }
143 assert(x == 2);
144 }
145
146 /****************************************************/
147
148 class Tester
149 {
150 this(void delegate() dg_) { dg = dg_; }
151 void delegate() dg;
152 void stuff() { dg(); }
153 }
154
155 void test4()
156 {
157 printf("Starting test\n");
158
159 int a = 0;
160 int b = 0;
161 int c = 0;
162 int d = 0;
163
164 try
165 {
166 a++;
167 throw new Exception("test1");
168 a++;
169 }
170 catch(Exception e)
171 {
172 auto es = e.toString();
173 printf("%.*s\n", cast(int)es.length, es.ptr);
174 b++;
175 }
176 finally
177 {
178 c++;
179 }
180
181 printf("initial test.\n");
182
183 assert(a == 1);
184 assert(b == 1);
185 assert(c == 1);
186
187 printf("pass\n");
188
189 Tester t = new Tester(
190 delegate void()
191 {
192 try
193 {
194 a++;
195 throw new Exception("test2");
196 a++;
197 }
198 catch(Exception e)
199 {
200 b++;
201 throw e;
202 b++;
203 }
204 });
205
206 try
207 {
208 c++;
209 t.stuff();
210 c++;
211 }
212 catch(Exception e)
213 {
214 d++;
215 string es = e.toString;
216 printf("%.*s\n", cast(int)es.length, es.ptr);
217 }
218
219 assert(a == 2);
220 assert(b == 2);
221 assert(c == 2);
222 assert(d == 1);
223
224
225 int q0 = 0;
226 int q1 = 0;
227 int q2 = 0;
228 int q3 = 0;
229
230 Tester t2 = new Tester(
231 delegate void()
232 {
233 try
234 {
235 q0++;
236 throw new Exception("test3");
237 q0++;
238 }
239 catch(Exception e)
240 {
241 printf("Never called.\n");
242 q1++;
243 throw e;
244 q1++;
245 }
246 });
247
248 try
249 {
250 q2++;
251 t2.stuff();
252 q2++;
253 }
254 catch(Exception e)
255 {
256 q3++;
257 string es = e.toString;
258 printf("%.*s\n", cast(int)es.length, es.ptr);
259 }
260
261 assert(q0 == 1);
262 assert(q1 == 1);
263 assert(q2 == 1);
264 assert(q3 == 1);
265
266 printf("Passed!\n");
267 }
268
269 /****************************************************/
270
271 void test5()
272 {
273 char[] result;
274 int i = 3;
275 while(i--)
276 {
277 try
278 {
279 printf("i: %d\n", i);
280 result ~= 't';
281 if (i == 1)
282 continue;
283 }
284 finally
285 {
286 printf("finally\n");
287 result ~= cast(char)('a' + i);
288 }
289 }
290 printf("--- %.*s", cast(int)result.length, result.ptr);
291 if (result != "tctbta")
292 assert(0);
293 }
294
295 /****************************************************/
296
297 void test6()
298 {
299 char[] result;
300
301 while (true)
302 {
303 try
304 {
305 printf("one\n");
306 result ~= 'a';
307 break;
308 }
309 finally
310 {
311 printf("two\n");
312 result ~= 'b';
313 }
314 }
315 printf("three\n");
316 result ~= 'c';
317 if (result != "abc")
318 assert(0);
319 }
320
321 /****************************************************/
322
323 string a7;
324
325 void doScan(int i)
326 {
327 a7 ~= "a";
328 try
329 {
330 try
331 {
332 a7 ~= "b";
333 return;
334 }
335 finally
336 {
337 a7 ~= "c";
338 }
339 }
340 finally
341 {
342 a7 ~= "d";
343 }
344 }
345
346 void test7()
347 {
348 doScan(0);
349 assert(a7 == "abcd");
350 }
351
352
353 /****************************************************
354 * Exception chaining tests. See also test4.d
355 * Don writes about the complexity:
356
357 I can explain this, since I did the original implementation.
358 When I originally implemented this, I discovered that the idea of
359 "chained exceptions" was hopeless naive. The idea was that while
360 processing one exception, if you encounter a second one, and you
361 chain them together. Then you get a third, fourth, etc.
362
363 The problem is that it's much more complicated than that. Each of
364 the exceptions can be a chain of exceptions themselves. This means
365 that you don't end up with a chain of exceptions, but rather a tree
366 of exceptions. That's why there are those really nasty test cases
367 in the test suite.
368
369 The examples in the test suite are very difficult to understand if
370 you expect it to be a simple chain!
371
372 On the one hand, I was very proud that I was able to work out the
373 barely-documented behaviour of Windows SEH, and it was really
374 thorough. In the initial implementation, all the complexity
375 was covered. It wasn't the bugfix-driven-development which dmd
376 usually operates under <g>.
377
378 But on the other hand, once you can see all of the complexity,
379 exception chaining becomes much less convincing as a concept. Sure,
380 the full exception tree is available in the final exception which
381 you catch. But, is it of any use? I doubt it very much.
382 It's pretty clearly a nett loss to the language, it increases
383 complexity with negligible benefit. Fortunately in this case, the
384 cost isn't really high.
385
386 https://digitalmars.com/d/archives/digitalmars/D/Dicebot_on_leaving_D_It_is_anarchy_driven_development_in_all_its_317950.html#N318305
387 ****************************************************/
388 int result1513;
389
390 void bug1513a()
391 {
392 throw new Exception("d");
393 }
394
395 void bug1513b()
396 {
397 try
398 {
399 try
400 {
401 bug1513a();
402 }
403 finally
404 {
405 result1513 |=4;
406 throw new Exception("f");
407 }
408 }
409 catch(Exception e)
410 {
411 assert(e.msg == "d");
412 assert(e.next.msg == "f");
413 assert(!e.next.next);
414 int i;
415 foreach (u; e)
416 {
417 if (i)
418 assert(u.msg == "f");
419 else
420 assert(u.msg == "d");
421 ++i;
422 }
423 }
424 }
425
426 void bug1513c()
427 {
428 try
429 {
430 try
431 {
432 throw new Exception("a");
433 }
434 finally
435 {
436 result1513 |= 1;
437 throw new Exception("b");
438 }
439 }
440 finally
441 {
442 bug1513b();
443 result1513 |= 2;
444 throw new Exception("c");
445 }
446 }
447
448 void bug1513()
449 {
450 result1513 = 0;
451 try
452 {
453 bug1513c();
454 }
455 catch(Exception e)
456 {
457 assert(result1513 == 7);
458 assert(e.msg == "a");
459 assert(e.next.msg == "b");
460 assert(e.next.next.msg == "c");
461 }
462 }
463
464 void collideone()
465 {
466 try
467 {
468 throw new Exception("x");
469 }
470 finally
471 {
472 throw new Exception("y");
473 }
474 }
475
476 void doublecollide()
477 {
478 try
479 {
480 try
481 {
482 try
483 {
484 throw new Exception("p");
485 }
486 finally
487 {
488 throw new Exception("q");
489 }
490 }
491 finally
492 {
493 collideone();
494 }
495 }
496 catch(Exception e)
497 {
498 assert(e.msg == "p");
499 assert(e.next.msg == "q");
500 assert(e.next.next.msg == "x");
501 assert(e.next.next.next.msg == "y");
502 assert(!e.next.next.next.next);
503 }
504 }
505
506 void collidetwo()
507 {
508 try
509 {
510 try
511 {
512 throw new Exception("p2");
513 }
514 finally
515 {
516 throw new Exception("q2");
517 }
518 }
519 finally
520 {
521 collideone();
522 }
523 }
524
525 void collideMixed()
526 {
527 int works = 6;
528 try
529 {
530 try
531 {
532 try
533 {
534 throw new Exception("e");
535 }
536 finally
537 {
538 throw new Error("t");
539 }
540 }
541 catch(Exception f)
542 { // Doesn't catch, because Error is chained to it.
543 works += 2;
544 }
545 }
546 catch(Error z)
547 {
548 works += 4;
549 assert(z.msg=="t"); // Error comes first
550 assert(z.next is null);
551 assert(z.bypassedException.msg == "e");
552 }
553 assert(works == 10);
554 }
555
556 class AnotherException : Exception
557 {
558 this(string s)
559 {
560 super(s);
561 }
562 }
563
564 void multicollide()
565 {
566 try
567 {
568 try
569 {
570 try
571 {
572 try
573 {
574 throw new Exception("m2");
575 }
576 finally
577 {
578 throw new AnotherException("n2");
579 }
580 }
581 catch(AnotherException s)
582 { // Not caught -- we needed to catch the root cause "m2", not
583 // just the collateral "n2" (which would leave m2 uncaught).
584 assert(0);
585 }
586 }
587 finally
588 {
589 collidetwo();
590 }
591 }
592 catch(Exception f)
593 {
594 assert(f.msg == "m2");
595 assert(f.next.msg == "n2");
596 Throwable e = f.next.next;
597 assert(e.msg == "p2");
598 assert(e.next.msg == "q2");
599 assert(e.next.next.msg == "x");
600 assert(e.next.next.next.msg == "y");
601 assert(!e.next.next.next.next);
602 }
603 }
604
605 /****************************************************/
606
607 void use9568(char [] x, char [] y) {}
608
609 int bug9568()
610 {
611 try
612 return 7;
613 finally
614 use9568(null,null);
615 }
616
617 void test9568()
618 {
619 assert( bug9568() == 7 );
620 }
621
622 /****************************************************/
623
624 version (DigitalMars)
625 {
626 void test8a()
627 {
628 int a;
629 goto L2; // L2 is not addressable.
630
631 try {
632 a += 2;
633 }
634 catch (Exception) {
635 a += 3;
636 L2: ;
637 a += 100;
638 }
639 assert(a == 100);
640 }
641
642 void test8b()
643 {
644 int a;
645 goto L2; // L2 is not addressable.
646
647 try {
648 }
649 catch (Exception) {
650 a += 3;
651 L2: ;
652 a += 100;
653 }
654 assert(a == 100);
655 }
656
657 void test8c()
658 {
659 int a;
660 goto L2; // L2 is not addressable.
661
662 try
663 static assert(true);
664 catch (Exception) {
665 a += 3;
666 L2: ;
667 a += 100;
668 }
669 assert(a == 100);
670 }
671
672 void test8()
673 {
674 test8a();
675 test8b();
676 test8c();
677 }
678 }
679
680 /****************************************************/
681
682 uint foo9(uint i)
683 {
684 try
685 {
686 ++i;
687 return 3;
688 }
689 catch (Exception e)
690 {
691 debug printf("Exception happened\n");
692 }
693 return 4;
694 }
695
696 void test9()
697 {
698 assert(foo9(7) == 3);
699 }
700
701 /****************************************************/
702 // https://issues.dlang.org/show_bug.cgi?id=10964
703
704 void test10964()
705 {
706 static struct S
707 {
708 this(this)
709 {
710 throw new Exception("BOOM!");
711 }
712 }
713
714 S ss;
715 S[1] sa;
716 int result;
717
718 result = 0;
719 try
720 {
721 ss = ss;
722 }
723 catch (Exception e) result = 1;
724 catch (Error e) result = 2;
725 catch (Throwable e) result = 3;
726 assert(result == 1);
727
728 try
729 {
730 sa = ss;
731 }
732 catch (Exception e) result = 1;
733 catch (Error e) result = 2;
734 catch (Throwable e) result = 3;
735 assert(result == 1);
736
737 try
738 {
739 sa = sa;
740 }
741 catch (Exception e) result = 1;
742 catch (Error e) result = 2;
743 catch (Throwable e) result = 3;
744 assert(result == 1);
745 }
746
747 /****************************************************/
748
749 alias Action = void delegate();
750
751 class A10
752 {
753 invariant()
754 {
755 }
756
757 public Action foo(Action a)
758 {
759 synchronized
760 {
761 B10 elements = new B10;
762 Action[] actions = [a];
763
764 elements.bar(actions);
765
766 if (actions.length > 1)
767 elements.bar(actions);
768 return actions[0];
769 }
770 return null;
771 }
772 }
773
774 class B10
775 {
776 public bool bar(ref Action[])
777 {
778 return false;
779 }
780 }
781
782 class D10
783 {
784 void baz()
785 {
786 }
787 }
788
789 void test12989()
790 {
791 auto a = new A10;
792 auto d = new D10;
793
794 assert(a.foo(&d.baz) == &d.baz);
795 }
796
797 /****************************************************/
798
799 int bar10(int c)
800 {
801 if (c <= 0xFFFF)
802 {
803 L3:
804 return 3;
805 }
806 throw new Exception("msg");
807 goto L3;
808 }
809
810 void test10()
811 {
812 int x;
813 try
814 {
815 bar10(0x110000);
816 }
817 catch (Exception e)
818 {
819 printf("caught\n");
820 x = 1;
821 }
822 assert(x == 1);
823 printf("test10 success\n");
824 }
825
826 /****************************************************/
827
828 class ParseException : Exception
829 {
830 @safe pure nothrow this( string msg )
831 {
832 super( msg );
833 }
834 }
835
836 class OverflowException : Exception
837 {
838 @safe pure nothrow this( string msg )
839 {
840 super( msg );
841 }
842 }
843
844 void test11()
845 {
846 int x;
847 try
848 {
849 printf("test11()\n");
850 throw new ParseException("msg");
851 }
852 catch( OverflowException e )
853 {
854 printf( "catch OverflowException\n" );
855 }
856 catch( ParseException e )
857 {
858 printf( "catch ParseException: %.*s\n", cast(int) e.msg.length, e.msg.ptr );
859 x = 1;
860 }
861 assert(x == 1);
862 }
863
864 /****************************************************/
865 // https://issues.dlang.org/show_bug.cgi?id=17481
866
867 class C17481
868 {
869 synchronized void trigger(){ new ubyte[1]; }
870 }
871
872 void test17481()
873 {
874 auto k = new shared C17481;
875 k.trigger;
876 }
877
878 /****************************************************/
879
880 // a nothrow function, even though it is not marked as nothrow
881 void test12()
882 {
883 int i = 3;
884 try
885 {
886 try
887 {
888 ++i;
889 goto L10;
890 }
891 finally
892 {
893 i *= 2;
894 printf("f1\n");
895 }
896 }
897 finally
898 {
899 i += 5;
900 printf("f2\n");
901 }
902
903 L10:
904 printf("3\n");
905 assert(i == (3 + 1) * 2 + 5);
906 }
907
908 /****************************************************/
909
910 void foo13() { }
911
912 void test13()
913 {
914 int i = 3;
915 try
916 {
917 try
918 {
919 foo13(); // compiler assumes it throws
920 ++i;
921 goto L10;
922 }
923 finally
924 {
925 i *= 2;
926 printf("f1\n");
927 }
928 }
929 finally
930 {
931 i += 5;
932 printf("f2\n");
933 }
934
935 L10:
936 printf("3\n");
937 assert(i == (3 + 1) * 2 + 5);
938 }
939
940 /****************************************************/
941
942 // https://issues.dlang.org/show_bug.cgi?id=10966
943
944 void bug10966a(void* p)
945 {
946 void* pstart = p;
947
948 try
949 {
950 p = null;
951 throw new Exception("dummy");
952 }
953 catch (Throwable o)
954 {
955 assert(p != pstart);
956 }
957 }
958
959 void bug10966b()
960 {
961 int x = 0;
962 int i = 0;
963 try
964 {
965 i = 1;
966 throw new Exception("dummy");
967 }
968 catch (Throwable o)
969 {
970 x = i;
971 }
972 assert(x == 1);
973 }
974
975 void test10966()
976 {
977 int s;
978 bug10966a(&s);
979 bug10966b();
980 }
981
982 /****************************************************/
983
984 // https://issues.dlang.org/show_bug.cgi?id=11049
985
986 void test11049()
987 {
988 int[] arr = [1,2,3];
989
990 #line 4100 "foo"
991 try { auto n = arr[3]; }
992 catch (Error e)
993 {
994 //printf("e.file = %s\n", e.file.ptr);
995 assert(e.file == "foo"); // fails
996 assert(e.line == 4100);
997 }
998
999 #line 4200 "bar"
1000 try { auto a = arr[3..9]; }
1001 catch (Error e)
1002 {
1003 //printf("e.file = %s\n", e.file.ptr);
1004 assert(e.file == "bar"); // fails
1005 assert(e.line == 4200);
1006 }
1007 }
1008
1009 /****************************************************/
1010
1011 int main()
1012 {
1013 printf("start\n");
1014 foo(3);
1015 test2();
1016 test3();
1017 test4();
1018 test5();
1019 test6();
1020 test7();
1021
1022 bug1513();
1023 doublecollide();
1024 collideMixed();
1025 multicollide();
1026 test9568();
1027
1028 version(DigitalMars) test8();
1029 test9();
1030 test10964();
1031 test12989();
1032 test10();
1033 test11();
1034 test17481();
1035 test12();
1036 test13();
1037 test10966();
1038 test11049();
1039
1040 printf("finish\n");
1041 return 0;
1042 }