source: tests/exceptions/defaults.cfa @ 25744d2

ADTarm-ehast-experimentalenumforall-pointer-decayjacob/cs343-translationnew-ast-unique-exprpthread-emulationqualifiedEnum
Last change on this file since 25744d2 was b81fd95, checked in by Michael Brooks <mlbrooks@…>, 4 years ago

Fix bug where pointer and reference types allow unsound initialization and return. Fixes #189

There are two instances of the same basic change, which is using conversionCost instead of castCost for resolving...
A: an InitExpr?, always; affects variable initializations
B: a CastExpr?, for type-system-generated casts only; affects function returns

Changing the behaviour of the typechecker on initialization (do A) and cast (do B):
src/ResolvExpr/AlternativeFinder.cc
src/SynTree/Expression.h
testsinit1.*

Making type of string literal consistent with how C defines it (accommodate A):
src/Parser/ExpressionNode.cc

Making type system happy with incumbent use of void* (accommodate A):
libcfa/src/concurrency/kernel.cfa
libcfa/src/containers/list.hfa
tests/bugs/66.cfa
tests/avltree/avl1.cfa
tests/concurrent/signal/block.cfa
tests/searchsort.cfa

Making type system happy with incumbent plan-9 downcast (accommodate B):
libcfa/src/containers/list.hfa

Fixing previously incorrect constness of declarations (accommodate A):
tests/exceptions/defaults.cfa
libcfa/src/iostream.hfa

Fixing previously incorrect isGenerated classification of casts that desugaring introduces (accommodate B):
src/Concurrency/Keywords.cc
src/Concurrency/Waitfor.cc

Working around trac #207 (revealed by A):
tests/io2.cfa

Working around trac #208 (speculatively created by B):
libcfa/src/bits/locks.hfa
libcfa/src/concurrency/preemption.cfa

Misc:
tests/exceptions/conditional.cfa (accommodate A)

a _msg function for an exception was declared with wrong return type, so it was not compatible for assignment into the vtable instance

libcfa/src/stdlib.hfa

the compiler now prohibits a prior attempt to call a nonexistent realloc overload; calling alloc_align in its place

  • Property mode set to 100644
File size: 2.1 KB
Line 
1// Tests for providing new default operations.
2
3#include <string.h>
4#include <exception.hfa>
5
6DATA_EXCEPTION(log_message)(
7        char * msg;
8);
9
10void ?{}(log_message & this, char * msg) {
11        VTABLE_INIT(this, log_message);
12        this.msg = msg;
13}
14
15const char * get_log_message(log_message * this) {
16        return this->msg;
17}
18
19VTABLE_INSTANCE(log_message)(get_log_message);
20
21// Logging messages don't have to be handled.
22void defaultResumptionHandler(log_message &) {}
23
24// And should never be used to terminate computation.
25void defaultTerminationHandler(log_message &) = void;
26
27void log_test(void) {
28        // We can catch log:
29        try {
30                throwResume (log_message){"Should be printed.\n"};
31        } catchResume (log_message * this) {
32                printf("%s", this->virtual_table->msg(this));
33        }
34        // But we don't have to:
35        throwResume (log_message){"Should not be printed.\n"};
36}
37
38// I don't have a good use case for doing the same with termination.
39TRIVIAL_EXCEPTION(jump);
40void defaultTerminationHandler(jump &) {
41        printf("jump default handler.\n");
42}
43
44void jump_test(void) {
45        try {
46                throw (jump){};
47        } catch (jump * this) {
48                printf("jump catch handler.\n");
49        }
50        throw (jump){};
51}
52
53TRIVIAL_EXCEPTION(first);
54TRIVIAL_EXCEPTION(unhandled_exception);
55
56void unhandled_test(void) {
57        forall(dtype T | is_exception(T))
58        void defaultTerminationHandler(T &) {
59                throw (unhandled_exception){};
60        }
61        void defaultTerminationHandler(unhandled_exception &) {
62                abort();
63        }
64        try {
65                throw (first){};
66        } catch (unhandled_exception * t) {
67                printf("Catch unhandled_exception.\n");
68        }
69}
70
71TRIVIAL_EXCEPTION(second);
72
73void cross_test(void) {
74        void defaultTerminationHandler(first &) {
75                printf("cross terminate default\n");
76                throw (second){};
77        }
78        void defaultResumptionHandler(first &) {
79                printf("cross resume default\n");
80                throwResume (second){};
81        }
82        try {
83                printf("cross terminate throw\n");
84                throw (first){};
85        } catch (second *) {
86                printf("cross terminate catch\n");
87        }
88        try {
89                printf("cross resume throw\n");
90                throwResume (first){};
91        } catchResume (second *) {
92                printf("cross resume catch\n");
93        }
94}
95
96int main(int argc, char * argv[]) {
97        log_test();
98        jump_test();
99        unhandled_test();
100        cross_test();
101}
Note: See TracBrowser for help on using the repository browser.