Debian Codegraph @

gcc/testsuite/gdc.test/fail_compilation/test_switch_error.d

raw
1 /++
2 https://issues.dlang.org/show_bug.cgi?id=22514
3 TEST_OUTPUT:
4 ---
5 fail_compilation/test_switch_error.d(13): Error: undefined identifier `doesNotExist`
6 fail_compilation/test_switch_error.d(16): Error: undefined identifier `alsoDoesNotExits`
7 fail_compilation/test_switch_error.d(19): Error: duplicate `case 2` in `switch` statement
8 ---
9 ++/
10
11 void test1()
12 {
13 switch (doesNotExist)
14 {
15 case 1:
16 alsoDoesNotExits();
17 break;
18 case 2: break;
19 case 2: break;
20 }
21 }
22
23 /++
24 TEST_OUTPUT:
25 ---
26 fail_compilation/test_switch_error.d(105): Error: undefined identifier `doesNotExist`
27 ---
28 ++/
29 #line 100
30
31 enum foo = 1;
32
33 void test2()
34 {
35 switch (doesNotExist)
36 {
37 case foo: break;
38 }
39 }
40
41 /++
42 TEST_OUTPUT:
43 ---
44 fail_compilation/test_switch_error.d(206): Error: undefined identifier `a`
45 fail_compilation/test_switch_error.d(207): Error: undefined identifier `b`
46 ---
47 ++/
48 #line 200
49
50 void test3()
51 {
52
53 switch (1)
54 {
55 case a: break;
56 case b: break;
57 }
58 }
59
60 /++
61 TEST_OUTPUT:
62 ---
63 fail_compilation/test_switch_error.d(303): Error: undefined identifier `doesNotExits`
64 ---
65 ++/
66 #line 300
67
68 void test4()
69 {
70 auto foo = doesNotExits();
71 switch (1)
72 {
73 case foo: break;
74 case foo: break;
75 }
76 }
77
78 /++
79 TEST_OUTPUT:
80 ---
81 fail_compilation/test_switch_error.d(405): Error: `case` variables have to be `const` or `immutable`
82 fail_compilation/test_switch_error.d(412): Error: `case` variables not allowed in `final switch` statements
83 ---
84 ++/
85 #line 400
86
87 void test5(int i)
88 {
89 switch (i)
90 {
91 case i: break;
92 default: break;
93 }
94
95 const int j = i;
96 final switch (i)
97 {
98 case j: break;
99
100 }
101 }
102
103 /++
104 TEST_OUTPUT:
105 ---
106 fail_compilation/test_switch_error.d(513): Error: undefined identifier `undefinedFunc`
107 fail_compilation/test_switch_error.d(517): Error: `case` expression must be a compile-time `string` or an integral constant, not `Strukt(1)`
108 fail_compilation/test_switch_error.d(518): Error: `case` variables have to be `const` or `immutable`
109 fail_compilation/test_switch_error.d(518): Error: `case` variables not allowed in `final switch` statements
110 fail_compilation/test_switch_error.d(519): Error: `case` variables not allowed in `final switch` statements
111 fail_compilation/test_switch_error.d(522): Error: undefined identifier `undefinedFunc2`
112 ---
113 ++/
114 #line 500
115
116 enum Foo
117 {
118 one, two
119 }
120
121 struct Strukt
122 {
123 int i;
124 }
125
126 void errorsWithErrors(int param, immutable int constant)
127 {
128 final switch(undefinedFunc())
129 {
130 case Foo.one: break;
131 case Foo.two: break;
132 case Strukt(1): break;
133 case param: break;
134 case constant: break;
135 }
136
137 switch (undefinedFunc2())
138 {
139 case constant: break;
140 }
141 }
142
143 /++
144 TEST_OUTPUT:
145 ---
146 fail_compilation/test_switch_error.d(622): Error: undefined identifier `undefinedFunc`
147 fail_compilation/test_switch_error.d(624): Error: `case` expression must be a compile-time `string` or an integral constant, not `SubtypeOfInt(2)`
148 fail_compilation/test_switch_error.d(625): Error: `case` expression must be a compile-time `string` or an integral constant, not `SubtypeOfIntMethod()`
149 ---
150 ++/
151 #line 600
152
153 struct SubtypeOfInt
154 {
155 int i;
156 alias i this;
157 }
158
159 struct SubtypeOfIntMethod
160 {
161 int getI() { return 0; }
162 alias getI this;
163 }
164
165 void errorsWithErrors2(int param)
166 {
167 final switch(param)
168 {
169 case SubtypeOfInt(1): break;
170 case SubtypeOfIntMethod(): break;
171 }
172
173 // This snippet causes somewhat misleading error messages
174 final switch(undefinedFunc())
175 {
176 case SubtypeOfInt(2): break;
177 case SubtypeOfIntMethod(): break;
178 }
179 }