source: tests/poly-bare-assn.cfa@ c2f6b79

Last change on this file since c2f6b79 was 119889f, checked in by Michael Brooks <mlbrooks@…>, 5 months ago

Partially fix #185.

This fix applies to functions, but not types.

The added test fails without the cfacpp changes.

  • Property mode set to 100644
File size: 2.7 KB
Line 
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
25MAYBE_TYPE (
26 forall ( | {void fun();} ) {
27
28 struct thing {};
29
30 void ?{}( thing & this ) {
31 fun();
32 }
33 }
34)
35
36void greet() {
37 printf("the world is boring\n");
38}
39
40forall ( | {void fun();} )
41void greet() {
42 printf("this is the world; ");
43 fun();
44}
45
46MAYBE_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
71void greetworld_nofun() {
72 greet();
73}
74
75MAYBE_SYNTAX(
76 void greetgalaxy_nofun() {
77 greet2();
78 }
79)
80
81void fun() {
82 printf("this is fun\n");
83}
84
85void greetworld_withfun() {
86 greet();
87}
88
89MAYBE_SYNTAX(
90 void greetgalaxy_withfun() {
91 greet2();
92 }
93)
94
95MAYBE_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.
111void test_costs() {
112 greetworld_nofun();
113 greetworld_withfun();
114 MAYBE_SYNTAX(
115 greetgalaxy_nofun();
116 greetgalaxy_withfun();
117 )
118}
119
120int main() {
121 MAYBE_TYPE(
122 test_type();
123 )
124 test_costs();
125}
Note: See TracBrowser for help on using the repository browser.