[119889f] | 1 | // A bare assertion is one that occurs without any here-bound type variables.
|
---|
| 2 | // The test shows that bare assertions can occur on a function or on a type,
|
---|
| 3 | // without introducing any "fake" type variables.
|
---|
| 4 |
|
---|
| 5 | // Related to Trac #185.
|
---|
| 6 | // Present state, the test (in a test.py run) shows a fix of the #185
|
---|
| 7 | // sub-case where there two overloads, already differentiated by other factors,
|
---|
| 8 | // and one of them gets a bare assertion, without being charged a polymorphism
|
---|
| 9 | // cost. The two "still required" points following do not run under test.py.
|
---|
| 10 |
|
---|
| 11 | // Still required to fix 185: parse bare assertions on traits
|
---|
| 12 | #ifdef TRY_MISSING_SYNTAX
|
---|
| 13 | #define MAYBE_SYNTAX(...) __VA_ARGS__
|
---|
| 14 | #else
|
---|
| 15 | #define MAYBE_SYNTAX(...)
|
---|
| 16 | #endif
|
---|
| 17 |
|
---|
| 18 | // Still required to fix 185: support bare assertions on types (orig 185 repro)
|
---|
| 19 | #ifdef TRY_BROKEN_TYPE
|
---|
| 20 | #define MAYBE_TYPE(...) __VA_ARGS__
|
---|
| 21 | #else
|
---|
| 22 | #define MAYBE_TYPE(...)
|
---|
| 23 | #endif
|
---|
| 24 |
|
---|
| 25 | MAYBE_TYPE (
|
---|
| 26 | forall ( | {void fun();} ) {
|
---|
| 27 |
|
---|
| 28 | struct thing {};
|
---|
| 29 |
|
---|
| 30 | void ?{}( thing & this ) {
|
---|
| 31 | fun();
|
---|
| 32 | }
|
---|
| 33 | }
|
---|
| 34 | )
|
---|
| 35 |
|
---|
| 36 | void greet() {
|
---|
| 37 | printf("the world is boring\n");
|
---|
| 38 | }
|
---|
| 39 |
|
---|
| 40 | forall ( | {void fun();} )
|
---|
| 41 | void greet() {
|
---|
| 42 | printf("this is the world; ");
|
---|
| 43 | fun();
|
---|
| 44 | }
|
---|
| 45 |
|
---|
| 46 | MAYBE_SYNTAX(
|
---|
| 47 | trait world_is_fun() { void fun(); }
|
---|
| 48 |
|
---|
| 49 | MAYBE_TYPE(
|
---|
| 50 | forall ( | world_is_fun() ) {
|
---|
| 51 |
|
---|
| 52 | struct thing2 {};
|
---|
| 53 |
|
---|
| 54 | void ?{}( thing2 & this ) {
|
---|
| 55 | fun();
|
---|
| 56 | }
|
---|
| 57 | }
|
---|
| 58 | )
|
---|
| 59 |
|
---|
| 60 | void greet2() {
|
---|
| 61 | printf("the galaxy is boring\n");
|
---|
| 62 | }
|
---|
| 63 |
|
---|
| 64 | forall ( | {void fun();} )
|
---|
| 65 | void greet2() {
|
---|
| 66 | printf("this is the galaxy; ");
|
---|
| 67 | fun();
|
---|
| 68 | }
|
---|
| 69 | )
|
---|
| 70 |
|
---|
| 71 | void greetworld_nofun() {
|
---|
| 72 | greet();
|
---|
| 73 | }
|
---|
| 74 |
|
---|
| 75 | MAYBE_SYNTAX(
|
---|
| 76 | void greetgalaxy_nofun() {
|
---|
| 77 | greet2();
|
---|
| 78 | }
|
---|
| 79 | )
|
---|
| 80 |
|
---|
| 81 | void fun() {
|
---|
| 82 | printf("this is fun\n");
|
---|
| 83 | }
|
---|
| 84 |
|
---|
| 85 | void greetworld_withfun() {
|
---|
| 86 | greet();
|
---|
| 87 | }
|
---|
| 88 |
|
---|
| 89 | MAYBE_SYNTAX(
|
---|
| 90 | void greetgalaxy_withfun() {
|
---|
| 91 | greet2();
|
---|
| 92 | }
|
---|
| 93 | )
|
---|
| 94 |
|
---|
| 95 | MAYBE_TYPE(
|
---|
| 96 | // A type declared with a bare assertion can be used ("instantiated") without
|
---|
| 97 | // adding type arguments. You shouldn't have to provide any type arguments
|
---|
| 98 | // when you use it because it was decalred with no type parameters.
|
---|
| 99 | void test_type() {
|
---|
| 100 | thing x; (void) x;
|
---|
| 101 | #ifdef TRY_MISSING_SYNTAX
|
---|
| 102 | thing2 y; (void) y;
|
---|
| 103 | #endif
|
---|
| 104 | }
|
---|
| 105 | )
|
---|
| 106 |
|
---|
| 107 | // A function overload declared with a bare assertion is called when the
|
---|
| 108 | // assertion is satisfied. Your cost calculation should not have the asserting
|
---|
| 109 | // overload incur the penalty of a type variable because it was decalred with
|
---|
| 110 | // no type parameters.
|
---|
| 111 | void test_costs() {
|
---|
| 112 | greetworld_nofun();
|
---|
| 113 | greetworld_withfun();
|
---|
| 114 | MAYBE_SYNTAX(
|
---|
| 115 | greetgalaxy_nofun();
|
---|
| 116 | greetgalaxy_withfun();
|
---|
| 117 | )
|
---|
| 118 | }
|
---|
| 119 |
|
---|
| 120 | int main() {
|
---|
| 121 | MAYBE_TYPE(
|
---|
| 122 | test_type();
|
---|
| 123 | )
|
---|
| 124 | test_costs();
|
---|
| 125 | }
|
---|