source: tests/exceptions/conditional.cfa @ 29185fc

ADTarm-ehast-experimentalenumforall-pointer-decayjacob/cs343-translationnew-ast-unique-exprpthread-emulationqualifiedEnum
Last change on this file since 29185fc 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: 1.7 KB
Line 
1// Conditional Catch Test
2
3// I may fold this back into terminate.cfa and resume.cfa once setting
4// up the non-trivial exception is reasonable to do.
5
6#include <exception.hfa>
7#include <stdio.h>
8
9VTABLE_DECLARATION(num_error)(
10        int (*code)(num_error *this);
11);
12
13struct num_error {
14        VTABLE_FIELD(num_error);
15    char * msg;
16    int num;
17};
18
19const char * num_error_msg(num_error * this) {
20    if ( ! this->msg ) {
21        static const char * base = "Num Error with code: X";
22        this->msg = (char *)malloc(22);
23        for (int i = 0 ; (this->msg[i] = base[i]) ; ++i);
24    }
25    this->msg[21] = '0' + this->num;
26    return this->msg;
27}
28void ?{}(num_error & this, int num) {
29        VTABLE_INIT(this, num_error);
30    this.msg = 0;
31    this.num = num;
32}
33void ?{}(num_error & this, num_error & other) {
34    this.virtual_table = other.virtual_table;
35    this.msg = 0;
36    this.num = other.num;
37}
38void ^?{}(num_error & this) {
39    if( this.msg ) free( this.msg );
40}
41int num_error_code( num_error * this ) {
42    return this->num;
43}
44
45VTABLE_INSTANCE(num_error)(
46        num_error_msg,
47        num_error_code,
48);
49
50void caught_num_error(int expect, num_error * actual) {
51        printf("Caught num_error: expected=%d actual=%d.\n", expect, actual->num);
52}
53
54int main(int argc, char * argv[]) {
55        num_error exc = 2;
56
57        try {
58                throw exc;
59        } catch (num_error * error ; 3 == error->virtual_table->code( error )) {
60                caught_num_error(3, error);
61        } catch (num_error * error ; 2 == error->virtual_table->code( error )) {
62                caught_num_error(2, error);
63        }
64
65        try {
66                throwResume exc;
67        } catchResume (num_error * error ; 3 == error->virtual_table->code( error )) {
68                caught_num_error(3, error);
69        } catchResume (num_error * error ; 2 == error->virtual_table->code( error )) {
70                caught_num_error(2, error);
71        }
72}
Note: See TracBrowser for help on using the repository browser.