1 // Copyright 2021 The Tint Authors.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 #include "src/transform/unshadow.h"
16
17 #include "src/transform/test_helper.h"
18
19 namespace tint {
20 namespace transform {
21 namespace {
22
23 using UnshadowTest = TransformTest;
24
TEST_F(UnshadowTest,EmptyModule)25 TEST_F(UnshadowTest, EmptyModule) {
26 auto* src = "";
27 auto* expect = "";
28
29 auto got = Run<Unshadow>(src);
30
31 EXPECT_EQ(expect, str(got));
32 }
33
TEST_F(UnshadowTest,Noop)34 TEST_F(UnshadowTest, Noop) {
35 auto* src = R"(
36 var<private> a : i32;
37
38 let b : i32 = 1;
39
40 fn F(c : i32) {
41 var d : i32;
42 let e : i32 = 1;
43 {
44 var f : i32;
45 let g : i32 = 1;
46 }
47 }
48 )";
49
50 auto* expect = src;
51
52 auto got = Run<Unshadow>(src);
53
54 EXPECT_EQ(expect, str(got));
55 }
56
TEST_F(UnshadowTest,LocalShadowsAlias)57 TEST_F(UnshadowTest, LocalShadowsAlias) {
58 auto* src = R"(
59 type a = i32;
60
61 fn X() {
62 var a = false;
63 }
64
65 fn Y() {
66 let a = true;
67 }
68 )";
69
70 auto* expect = R"(
71 type a = i32;
72
73 fn X() {
74 var a_1 = false;
75 }
76
77 fn Y() {
78 let a_2 = true;
79 }
80 )";
81
82 auto got = Run<Unshadow>(src);
83
84 EXPECT_EQ(expect, str(got));
85 }
86
TEST_F(UnshadowTest,LocalShadowsStruct)87 TEST_F(UnshadowTest, LocalShadowsStruct) {
88 auto* src = R"(
89 struct a {
90 m : i32;
91 };
92
93 fn X() {
94 var a = true;
95 }
96
97 fn Y() {
98 let a = false;
99 }
100 )";
101
102 auto* expect = R"(
103 struct a {
104 m : i32;
105 };
106
107 fn X() {
108 var a_1 = true;
109 }
110
111 fn Y() {
112 let a_2 = false;
113 }
114 )";
115
116 auto got = Run<Unshadow>(src);
117
118 EXPECT_EQ(expect, str(got));
119 }
120
TEST_F(UnshadowTest,LocalShadowsFunction)121 TEST_F(UnshadowTest, LocalShadowsFunction) {
122 auto* src = R"(
123 fn a() {
124 var a = true;
125 }
126
127 fn b() {
128 let b = false;
129 }
130 )";
131
132 auto* expect = R"(
133 fn a() {
134 var a_1 = true;
135 }
136
137 fn b() {
138 let b_1 = false;
139 }
140 )";
141
142 auto got = Run<Unshadow>(src);
143
144 EXPECT_EQ(expect, str(got));
145 }
146
TEST_F(UnshadowTest,LocalShadowsGlobalVar)147 TEST_F(UnshadowTest, LocalShadowsGlobalVar) {
148 auto* src = R"(
149 var<private> a : i32;
150
151 fn X() {
152 var a = (a == 123);
153 }
154
155 fn Y() {
156 let a = (a == 321);
157 }
158 )";
159
160 auto* expect = R"(
161 var<private> a : i32;
162
163 fn X() {
164 var a_1 = (a == 123);
165 }
166
167 fn Y() {
168 let a_2 = (a == 321);
169 }
170 )";
171
172 auto got = Run<Unshadow>(src);
173
174 EXPECT_EQ(expect, str(got));
175 }
176
TEST_F(UnshadowTest,LocalShadowsGlobalLet)177 TEST_F(UnshadowTest, LocalShadowsGlobalLet) {
178 auto* src = R"(
179 let a : i32 = 1;
180
181 fn X() {
182 var a = (a == 123);
183 }
184
185 fn Y() {
186 let a = (a == 321);
187 }
188 )";
189
190 auto* expect = R"(
191 let a : i32 = 1;
192
193 fn X() {
194 var a_1 = (a == 123);
195 }
196
197 fn Y() {
198 let a_2 = (a == 321);
199 }
200 )";
201
202 auto got = Run<Unshadow>(src);
203
204 EXPECT_EQ(expect, str(got));
205 }
206
TEST_F(UnshadowTest,LocalShadowsLocalVar)207 TEST_F(UnshadowTest, LocalShadowsLocalVar) {
208 auto* src = R"(
209 fn X() {
210 var a : i32;
211 {
212 var a = (a == 123);
213 }
214 {
215 let a = (a == 321);
216 }
217 }
218 )";
219
220 auto* expect = R"(
221 fn X() {
222 var a : i32;
223 {
224 var a_1 = (a == 123);
225 }
226 {
227 let a_2 = (a == 321);
228 }
229 }
230 )";
231
232 auto got = Run<Unshadow>(src);
233
234 EXPECT_EQ(expect, str(got));
235 }
236
TEST_F(UnshadowTest,LocalShadowsLocalLet)237 TEST_F(UnshadowTest, LocalShadowsLocalLet) {
238 auto* src = R"(
239 fn X() {
240 let a = 1;
241 {
242 var a = (a == 123);
243 }
244 {
245 let a = (a == 321);
246 }
247 }
248 )";
249
250 auto* expect = R"(
251 fn X() {
252 let a = 1;
253 {
254 var a_1 = (a == 123);
255 }
256 {
257 let a_2 = (a == 321);
258 }
259 }
260 )";
261
262 auto got = Run<Unshadow>(src);
263
264 EXPECT_EQ(expect, str(got));
265 }
266
TEST_F(UnshadowTest,LocalShadowsParam)267 TEST_F(UnshadowTest, LocalShadowsParam) {
268 auto* src = R"(
269 fn F(a : i32) {
270 {
271 var a = (a == 123);
272 }
273 {
274 let a = (a == 321);
275 }
276 }
277 )";
278
279 auto* expect = R"(
280 fn F(a : i32) {
281 {
282 var a_1 = (a == 123);
283 }
284 {
285 let a_2 = (a == 321);
286 }
287 }
288 )";
289
290 auto got = Run<Unshadow>(src);
291
292 EXPECT_EQ(expect, str(got));
293 }
294
TEST_F(UnshadowTest,ParamShadowsFunction)295 TEST_F(UnshadowTest, ParamShadowsFunction) {
296 auto* src = R"(
297 fn a(a : i32) {
298 {
299 var a = (a == 123);
300 }
301 {
302 let a = (a == 321);
303 }
304 }
305 )";
306
307 auto* expect = R"(
308 fn a(a_1 : i32) {
309 {
310 var a_2 = (a_1 == 123);
311 }
312 {
313 let a_3 = (a_1 == 321);
314 }
315 }
316 )";
317
318 auto got = Run<Unshadow>(src);
319
320 EXPECT_EQ(expect, str(got));
321 }
322
TEST_F(UnshadowTest,ParamShadowsGlobalVar)323 TEST_F(UnshadowTest, ParamShadowsGlobalVar) {
324 auto* src = R"(
325 var<private> a : i32;
326
327 fn F(a : bool) {
328 }
329 )";
330
331 auto* expect = R"(
332 var<private> a : i32;
333
334 fn F(a_1 : bool) {
335 }
336 )";
337
338 auto got = Run<Unshadow>(src);
339
340 EXPECT_EQ(expect, str(got));
341 }
342
TEST_F(UnshadowTest,ParamShadowsGlobalLet)343 TEST_F(UnshadowTest, ParamShadowsGlobalLet) {
344 auto* src = R"(
345 let a : i32 = 1;
346
347 fn F(a : bool) {
348 }
349 )";
350
351 auto* expect = R"(
352 let a : i32 = 1;
353
354 fn F(a_1 : bool) {
355 }
356 )";
357
358 auto got = Run<Unshadow>(src);
359
360 EXPECT_EQ(expect, str(got));
361 }
362
TEST_F(UnshadowTest,ParamShadowsAlias)363 TEST_F(UnshadowTest, ParamShadowsAlias) {
364 auto* src = R"(
365 type a = i32;
366
367 fn F(a : a) {
368 {
369 var a = (a == 123);
370 }
371 {
372 let a = (a == 321);
373 }
374 }
375 )";
376
377 auto* expect = R"(
378 type a = i32;
379
380 fn F(a_1 : a) {
381 {
382 var a_2 = (a_1 == 123);
383 }
384 {
385 let a_3 = (a_1 == 321);
386 }
387 }
388 )";
389
390 auto got = Run<Unshadow>(src);
391
392 EXPECT_EQ(expect, str(got));
393 }
394
395 } // namespace
396 } // namespace transform
397 } // namespace tint
398