blob: c0af2e6d6e64f18f1578a6ee106ae5578b7bd06c [file] [log] [blame]
ethannicholasb3058bd2016-07-01 08:22:01 -07001/*
2 * Copyright 2016 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "src/sksl/SkSLCompiler.h"
ethannicholasb3058bd2016-07-01 08:22:01 -07009
Mike Kleinc0bd9f92019-04-23 12:05:21 -050010#include "tests/Test.h"
ethannicholasb3058bd2016-07-01 08:22:01 -070011
12static void test_failure(skiatest::Reporter* r, const char* src, const char* error) {
13 SkSL::Compiler compiler;
Ethan Nicholas941e7e22016-12-12 15:33:30 -050014 SkSL::Program::Settings settings;
15 sk_sp<GrShaderCaps> caps = SkSL::ShaderCapsFactory::Default();
16 settings.fCaps = caps.get();
Ethan Nicholas00543112018-07-31 09:44:36 -040017 std::unique_ptr<SkSL::Program> program = compiler.convertProgram(SkSL::Program::kFragment_Kind,
18 SkSL::String(src), settings);
19 if (!compiler.errorCount()) {
20 compiler.optimize(*program);
21 }
Ethan Nicholas0df1b042017-03-31 13:56:23 -040022 SkSL::String skError(error);
Ethan Nicholas9e1138d2016-11-21 10:39:35 -050023 if (compiler.errorText() != skError) {
ethannicholasb3058bd2016-07-01 08:22:01 -070024 SkDebugf("SKSL ERROR:\n source: %s\n expected: %s received: %s", src, error,
25 compiler.errorText().c_str());
26 }
Ethan Nicholas9e1138d2016-11-21 10:39:35 -050027 REPORTER_ASSERT(r, compiler.errorText() == skError);
ethannicholasb3058bd2016-07-01 08:22:01 -070028}
29
30static void test_success(skiatest::Reporter* r, const char* src) {
31 SkSL::Compiler compiler;
Ethan Nicholas941e7e22016-12-12 15:33:30 -050032 SkSL::Program::Settings settings;
33 sk_sp<GrShaderCaps> caps = SkSL::ShaderCapsFactory::Default();
34 settings.fCaps = caps.get();
35 std::unique_ptr<SkSL::Program> program = compiler.convertProgram(SkSL::Program::kFragment_Kind,
Brian Osman93ba0a42017-08-14 14:48:10 -040036 SkSL::String(src), settings);
Ethan Nicholas941e7e22016-12-12 15:33:30 -050037 REPORTER_ASSERT(r, program);
ethannicholasb3058bd2016-07-01 08:22:01 -070038}
39
Brian Osman711e73c2020-06-30 11:19:15 -040040DEF_TEST(SkSLConstVariableComparison, r) {
41 test_success(r,
42 "void main() {"
43 " const float4 a = float4(0);"
44 " const float4 b = float4(1);"
45 " if (a == b) { discard; }"
46 "}");
47}
48
Ethan Nicholas0c8582e2019-07-19 09:26:46 -040049DEF_TEST(SkSLOpenArray, r) {
50 test_failure(r,
51 "void main(inout float4 color) { color.r[ = ( color.g ); }",
52 "error: 1: expected expression, but found '='\n1 error\n");
53}
54
ethannicholasb3058bd2016-07-01 08:22:01 -070055DEF_TEST(SkSLUndefinedSymbol, r) {
56 test_failure(r,
Ethan Nicholas8aa45692017-09-20 11:24:15 -040057 "void main() { x = float2(1); }",
ethannicholasb3058bd2016-07-01 08:22:01 -070058 "error: 1: unknown identifier 'x'\n1 error\n");
59}
60
61DEF_TEST(SkSLUndefinedFunction, r) {
62 test_failure(r,
Ethan Nicholasf7b88202017-09-18 14:10:39 -040063 "void main() { int x = foo(1); }",
ethannicholasb3058bd2016-07-01 08:22:01 -070064 "error: 1: unknown identifier 'foo'\n1 error\n");
65}
66
67DEF_TEST(SkSLGenericArgumentMismatch, r) {
68 test_failure(r,
Ethan Nicholas8aa45692017-09-20 11:24:15 -040069 "void main() { float x = sin(1, 2); }",
Ethan Nicholase1f55022019-02-05 17:17:40 -050070 "error: 1: no match for sin(int, int)\n1 error\n");
ethannicholas471e8942016-10-28 09:02:46 -070071 test_failure(r,
Ethan Nicholas8aa45692017-09-20 11:24:15 -040072 "void main() { float x = sin(true); }",
ethannicholas471e8942016-10-28 09:02:46 -070073 "error: 1: no match for sin(bool)\n1 error\n");
74 test_success(r,
Ethan Nicholas8aa45692017-09-20 11:24:15 -040075 "void main() { float x = sin(1); }");
ethannicholasb3058bd2016-07-01 08:22:01 -070076}
77
78DEF_TEST(SkSLArgumentCountMismatch, r) {
79 test_failure(r,
Ethan Nicholas8aa45692017-09-20 11:24:15 -040080 "float foo(float x) { return x * x; }"
81 "void main() { float x = foo(1, 2); }",
ethannicholasb3058bd2016-07-01 08:22:01 -070082 "error: 1: call to 'foo' expected 1 argument, but found 2\n1 error\n");
83}
84
85DEF_TEST(SkSLArgumentMismatch, r) {
Ethan Nicholasf7b88202017-09-18 14:10:39 -040086 test_failure(r,
Ethan Nicholas8aa45692017-09-20 11:24:15 -040087 "float foo(float x) { return x * x; }"
88 "void main() { float x = foo(true); }",
89 "error: 1: expected 'float', but found 'bool'\n1 error\n");
ethannicholasb3058bd2016-07-01 08:22:01 -070090}
91
92DEF_TEST(SkSLIfTypeMismatch, r) {
93 test_failure(r,
94 "void main() { if (3) { } }",
95 "error: 1: expected 'bool', but found 'int'\n1 error\n");
96}
97
98DEF_TEST(SkSLDoTypeMismatch, r) {
99 test_failure(r,
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400100 "void main() { do { } while (float2(1)); }",
101 "error: 1: expected 'bool', but found 'float2'\n1 error\n");
ethannicholasb3058bd2016-07-01 08:22:01 -0700102}
103
104DEF_TEST(SkSLWhileTypeMismatch, r) {
105 test_failure(r,
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400106 "void main() { while (float3(1)) { } }",
107 "error: 1: expected 'bool', but found 'float3'\n1 error\n");
ethannicholasb3058bd2016-07-01 08:22:01 -0700108}
109
110DEF_TEST(SkSLForTypeMismatch, r) {
111 test_failure(r,
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400112 "void main() { for (int x = 0; x; x++) { } }",
ethannicholasb3058bd2016-07-01 08:22:01 -0700113 "error: 1: expected 'bool', but found 'int'\n1 error\n");
114}
115
116DEF_TEST(SkSLConstructorTypeMismatch, r) {
117 test_failure(r,
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400118 "void main() { float2 x = float2(1.0, false); }",
119 "error: 1: expected 'float', but found 'bool'\n1 error\n");
ethannicholasb3058bd2016-07-01 08:22:01 -0700120 test_failure(r,
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400121 "void main() { float2 x = float2(bool2(false)); }",
122 "error: 1: 'bool2' is not a valid parameter to 'float2' constructor\n1 error\n");
Ethan Nicholas4578a8e2016-11-01 11:57:42 -0400123 test_failure(r,
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400124 "void main() { bool2 x = bool2(float2(1)); }",
125 "error: 1: 'float2' is not a valid parameter to 'bool2' constructor\n1 error\n");
Ethan Nicholas4578a8e2016-11-01 11:57:42 -0400126 test_failure(r,
ethannicholasb3058bd2016-07-01 08:22:01 -0700127 "void main() { bool x = bool(1.0); }",
128 "error: 1: cannot construct 'bool'\n1 error\n");
129 test_failure(r,
130 "struct foo { int x; }; void main() { foo x = foo(5); }",
131 "error: 1: cannot construct 'foo'\n1 error\n");
132 test_failure(r,
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400133 "struct foo { int x; } foo; void main() { float x = float(foo); }",
134 "error: 1: invalid argument to 'float' constructor (expected a number or bool, but found 'foo')\n1 error\n");
ethannicholasb3058bd2016-07-01 08:22:01 -0700135 test_failure(r,
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400136 "struct foo { int x; } foo; void main() { float2 x = float2(foo); }",
137 "error: 1: 'foo' is not a valid parameter to 'float2' constructor\n1 error\n");
Ethan Nicholas3deaeb22017-04-25 14:42:11 -0400138 test_failure(r,
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400139 "void main() { float2x2 x = float2x2(true); }",
140 "error: 1: expected 'float', but found 'bool'\n1 error\n");
ethannicholasb3058bd2016-07-01 08:22:01 -0700141}
142
143DEF_TEST(SkSLConstructorArgumentCount, r) {
144 test_failure(r,
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400145 "void main() { float3 x = float3(1.0, 2.0); }",
146 "error: 1: invalid arguments to 'float3' constructor (expected 3 scalars, but "
ethannicholasb3058bd2016-07-01 08:22:01 -0700147 "found 2)\n1 error\n");
Ethan Nicholas84645e32017-02-09 13:57:14 -0500148 test_failure(r,
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400149 "void main() { float3 x = float3(1.0, 2.0, 3.0, 4.0); }",
150 "error: 1: invalid arguments to 'float3' constructor (expected 3 scalars, but found "
Ethan Nicholas84645e32017-02-09 13:57:14 -0500151 "4)\n1 error\n");
ethannicholasb3058bd2016-07-01 08:22:01 -0700152}
153
154DEF_TEST(SkSLSwizzleScalar, r) {
155 test_failure(r,
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400156 "void main() { float x = 1; float y = x.y; }",
157 "error: 1: cannot swizzle value of type 'float'\n1 error\n");
ethannicholasb3058bd2016-07-01 08:22:01 -0700158}
159
160DEF_TEST(SkSLSwizzleMatrix, r) {
161 test_failure(r,
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400162 "void main() { float2x2 x = float2x2(1); float y = x.y; }",
163 "error: 1: cannot swizzle value of type 'float2x2'\n1 error\n");
ethannicholasb3058bd2016-07-01 08:22:01 -0700164}
165
166DEF_TEST(SkSLSwizzleOutOfBounds, r) {
167 test_failure(r,
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400168 "void main() { float3 test = float2(1).xyz; }",
ethannicholasb3058bd2016-07-01 08:22:01 -0700169 "error: 1: invalid swizzle component 'z'\n1 error\n");
170}
171
172DEF_TEST(SkSLSwizzleTooManyComponents, r) {
173 test_failure(r,
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400174 "void main() { float4 test = float2(1).xxxxx; }",
ethannicholasb3058bd2016-07-01 08:22:01 -0700175 "error: 1: too many components in swizzle mask 'xxxxx'\n1 error\n");
176}
177
178DEF_TEST(SkSLSwizzleDuplicateOutput, r) {
179 test_failure(r,
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400180 "void main() { float4 test = float4(1); test.xyyz = float4(1); }",
ethannicholasb3058bd2016-07-01 08:22:01 -0700181 "error: 1: cannot write to the same swizzle field more than once\n1 error\n");
182}
ethannicholasea4567c2016-10-17 11:24:37 -0700183
Ethan Nicholascb0f4092019-04-19 11:26:50 -0400184DEF_TEST(SkSLSwizzleConstantOutput, r) {
185 test_failure(r,
186 "void main() { float4 test = float4(1); test.xyz0 = float4(1); }",
187 "error: 1: cannot write to a swizzle mask containing a constant\n1 error\n");
188}
189
ethannicholasb3058bd2016-07-01 08:22:01 -0700190DEF_TEST(SkSLAssignmentTypeMismatch, r) {
191 test_failure(r,
192 "void main() { int x = 1.0; }",
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400193 "error: 1: expected 'int', but found 'float'\n1 error\n");
ethannicholasea4567c2016-10-17 11:24:37 -0700194 test_failure(r,
195 "void main() { int x; x = 1.0; }",
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400196 "error: 1: type mismatch: '=' cannot operate on 'int', 'float'\n1 error\n");
ethannicholasea4567c2016-10-17 11:24:37 -0700197 test_success(r,
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400198 "void main() { float3 x = float3(0); x *= 1.0; }");
ethannicholasea4567c2016-10-17 11:24:37 -0700199 test_failure(r,
Ethan Nicholas5af9ea32017-07-28 15:19:46 -0400200 "void main() { int3 x = int3(0); x *= 1.0; }",
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400201 "error: 1: type mismatch: '*=' cannot operate on 'int3', 'float'\n1 error\n");
ethannicholasb3058bd2016-07-01 08:22:01 -0700202}
203
204DEF_TEST(SkSLReturnFromVoid, r) {
205 test_failure(r,
206 "void main() { return true; }",
207 "error: 1: may not return a value from a void function\n1 error\n");
208}
209
210DEF_TEST(SkSLReturnMissingValue, r) {
211 test_failure(r,
212 "int foo() { return; } void main() { }",
213 "error: 1: expected function to return 'int'\n1 error\n");
214}
215
216DEF_TEST(SkSLReturnTypeMismatch, r) {
217 test_failure(r,
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400218 "int foo() { return 1.0; } void main() { }",
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400219 "error: 1: expected 'int', but found 'float'\n1 error\n");
ethannicholasb3058bd2016-07-01 08:22:01 -0700220}
221
222DEF_TEST(SkSLDuplicateFunction, r) {
223 test_failure(r,
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400224 "void main() { } void main() { }",
ethannicholasb3058bd2016-07-01 08:22:01 -0700225 "error: 1: duplicate definition of void main()\n1 error\n");
226 test_success(r,
227 "void main(); void main() { }");
228}
229
230DEF_TEST(SkSLUsingInvalidValue, r) {
231 test_failure(r,
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400232 "void main() { int x = int; }",
ethannicholasb3058bd2016-07-01 08:22:01 -0700233 "error: 1: expected '(' to begin constructor invocation\n1 error\n");
234 test_failure(r,
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400235 "int test() { return 1; } void main() { int x = test; }",
ethannicholasb3058bd2016-07-01 08:22:01 -0700236 "error: 1: expected '(' to begin function call\n1 error\n");
237}
238DEF_TEST(SkSLDifferentReturnType, r) {
239 test_failure(r,
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400240 "int main() { return 1; } void main() { }",
ethannicholasb3058bd2016-07-01 08:22:01 -0700241 "error: 1: functions 'void main()' and 'int main()' differ only in return type\n1 "
242 "error\n");
243}
244
245DEF_TEST(SkSLDifferentModifiers, r) {
246 test_failure(r,
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400247 "void test(int x); void test(out int x) { }",
ethannicholasb3058bd2016-07-01 08:22:01 -0700248 "error: 1: modifiers on parameter 1 differ between declaration and definition\n1 "
249 "error\n");
250}
251
252DEF_TEST(SkSLDuplicateSymbol, r) {
253 test_failure(r,
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400254 "int main; void main() { }",
ethannicholasb3058bd2016-07-01 08:22:01 -0700255 "error: 1: symbol 'main' was already defined\n1 error\n");
256
257 test_failure(r,
258 "int x; int x; void main() { }",
259 "error: 1: symbol 'x' was already defined\n1 error\n");
260
261 test_success(r, "int x; void main() { int x; }");
262}
263
264DEF_TEST(SkSLBinaryTypeMismatch, r) {
265 test_failure(r,
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400266 "void main() { float x = 3 * true; }",
ethannicholasb3058bd2016-07-01 08:22:01 -0700267 "error: 1: type mismatch: '*' cannot operate on 'int', 'bool'\n1 error\n");
268 test_failure(r,
269 "void main() { bool x = 1 || 2.0; }",
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400270 "error: 1: type mismatch: '||' cannot operate on 'int', 'float'\n1 error\n");
Ethan Nicholas23463002018-03-28 15:16:15 -0400271 test_failure(r,
272 "void main() { bool x = float2(0) == 0; }",
273 "error: 1: type mismatch: '==' cannot operate on 'float2', 'int'\n1 error\n");
274 test_failure(r,
275 "void main() { bool x = float2(0) != 0; }",
276 "error: 1: type mismatch: '!=' cannot operate on 'float2', 'int'\n1 error\n");
ethannicholasb3058bd2016-07-01 08:22:01 -0700277}
278
279DEF_TEST(SkSLCallNonFunction, r) {
280 test_failure(r,
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400281 "void main() { float x = 3; x(); }",
Ethan Nicholas2a099da2020-01-02 14:40:54 -0500282 "error: 1: not a function\n1 error\n");
ethannicholasb3058bd2016-07-01 08:22:01 -0700283}
284
285DEF_TEST(SkSLInvalidUnary, r) {
286 test_failure(r,
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400287 "void main() { float4x4 x = float4x4(1); ++x; }",
288 "error: 1: '++' cannot operate on 'float4x4'\n1 error\n");
ethannicholasb3058bd2016-07-01 08:22:01 -0700289 test_failure(r,
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400290 "void main() { float3 x = float3(1); --x; }",
291 "error: 1: '--' cannot operate on 'float3'\n1 error\n");
ethannicholasb3058bd2016-07-01 08:22:01 -0700292 test_failure(r,
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400293 "void main() { float4x4 x = float4x4(1); x++; }",
294 "error: 1: '++' cannot operate on 'float4x4'\n1 error\n");
ethannicholasb3058bd2016-07-01 08:22:01 -0700295 test_failure(r,
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400296 "void main() { float3 x = float3(1); x--; }",
297 "error: 1: '--' cannot operate on 'float3'\n1 error\n");
ethannicholasb3058bd2016-07-01 08:22:01 -0700298 test_failure(r,
299 "void main() { int x = !12; }",
300 "error: 1: '!' cannot operate on 'int'\n1 error\n");
301 test_failure(r,
302 "struct foo { } bar; void main() { foo x = +bar; }",
303 "error: 1: '+' cannot operate on 'foo'\n1 error\n");
304 test_failure(r,
305 "struct foo { } bar; void main() { foo x = -bar; }",
306 "error: 1: '-' cannot operate on 'foo'\n1 error\n");
307 test_success(r,
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400308 "void main() { float2 x = float2(1, 1); x = +x; x = -x; }");
ethannicholasb3058bd2016-07-01 08:22:01 -0700309}
310
311DEF_TEST(SkSLInvalidAssignment, r) {
312 test_failure(r,
313 "void main() { 1 = 2; }",
Ethan Nicholas2a099da2020-01-02 14:40:54 -0500314 "error: 1: cannot assign to this expression\n1 error\n");
ethannicholasb3058bd2016-07-01 08:22:01 -0700315 test_failure(r,
316 "uniform int x; void main() { x = 0; }",
317 "error: 1: cannot modify immutable variable 'x'\n1 error\n");
318 test_failure(r,
319 "const int x; void main() { x = 0; }",
320 "error: 1: cannot modify immutable variable 'x'\n1 error\n");
321}
322
323DEF_TEST(SkSLBadIndex, r) {
324 test_failure(r,
325 "void main() { int x = 2[0]; }",
326 "error: 1: expected array, but found 'int'\n1 error\n");
327 test_failure(r,
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400328 "void main() { float2 x = float2(0); int y = x[0][0]; }",
329 "error: 1: expected array, but found 'float'\n1 error\n");
ethannicholasb3058bd2016-07-01 08:22:01 -0700330}
331
332DEF_TEST(SkSLTernaryMismatch, r) {
333 test_failure(r,
334 "void main() { int x = 5 > 2 ? true : 1.0; }",
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400335 "error: 1: ternary operator result mismatch: 'bool', 'float'\n1 error\n");
Ethan Nicholas2be687a2017-01-03 16:44:39 -0500336 test_failure(r,
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400337 "void main() { int x = 5 > 2 ? float3(1) : 1.0; }",
338 "error: 1: ternary operator result mismatch: 'float3', 'float'\n1 error\n");
ethannicholasb3058bd2016-07-01 08:22:01 -0700339}
340
341DEF_TEST(SkSLInterfaceBlockStorageModifiers, r) {
342 test_failure(r,
343 "uniform foo { out int x; };",
344 "error: 1: interface block fields may not have storage qualifiers\n1 error\n");
345}
ethannicholas22f939e2016-10-13 13:25:34 -0700346
347DEF_TEST(SkSLUseWithoutInitialize, r) {
348 test_failure(r,
349 "void main() { int x; if (5 == 2) x = 3; x++; }",
350 "error: 1: 'x' has not been assigned\n1 error\n");
351 test_failure(r,
352 "void main() { int x[2][2]; int i; x[i][1] = 4; }",
353 "error: 1: 'i' has not been assigned\n1 error\n");
354 test_failure(r,
355 "int main() { int r; return r; }",
356 "error: 1: 'r' has not been assigned\n1 error\n");
357 test_failure(r,
Ethan Nicholas82a62d22017-11-07 14:42:10 +0000358 "void main() { int x; int y = x; }",
ethannicholas22f939e2016-10-13 13:25:34 -0700359 "error: 1: 'x' has not been assigned\n1 error\n");
360 test_failure(r,
361 "void main() { bool x; if (true && (false || x)) return; }",
362 "error: 1: 'x' has not been assigned\n1 error\n");
Ethan Nicholasaf197692017-02-27 13:26:45 -0500363 test_failure(r,
364 "void main() { int x; switch (3) { case 0: x = 0; case 1: x = 1; }"
Ethan Nicholase1f55022019-02-05 17:17:40 -0500365 "sk_FragColor = half4(x); }",
Ethan Nicholasaf197692017-02-27 13:26:45 -0500366 "error: 1: 'x' has not been assigned\n1 error\n");
ethannicholas22f939e2016-10-13 13:25:34 -0700367}
368
369DEF_TEST(SkSLUnreachable, r) {
370 test_failure(r,
371 "void main() { return; return; }",
372 "error: 1: unreachable\n1 error\n");
373 test_failure(r,
374 "void main() { for (;;) { continue; int x = 1; } }",
375 "error: 1: unreachable\n1 error\n");
Ethan Nicholasd9fe7002017-05-30 10:15:34 -0400376/* test_failure(r,
ethannicholas22f939e2016-10-13 13:25:34 -0700377 "void main() { for (;;) { } return; }",
Ethan Nicholasd9fe7002017-05-30 10:15:34 -0400378 "error: 1: unreachable\n1 error\n");*/
ethannicholas22f939e2016-10-13 13:25:34 -0700379 test_failure(r,
380 "void main() { if (true) return; else discard; return; }",
381 "error: 1: unreachable\n1 error\n");
382 test_failure(r,
Ethan Nicholas70728ef2020-05-28 07:09:00 -0400383 "void main() { return; main(); }",
ethannicholas22f939e2016-10-13 13:25:34 -0700384 "error: 1: unreachable\n1 error\n");
385}
386
387DEF_TEST(SkSLNoReturn, r) {
388 test_failure(r,
389 "int foo() { if (2 > 5) return 3; }",
Ethan Nicholasdb80f692019-11-22 14:06:12 -0500390 "error: 1: function 'foo' can exit without returning a value\n1 error\n");
ethannicholas22f939e2016-10-13 13:25:34 -0700391}
392
393DEF_TEST(SkSLBreakOutsideLoop, r) {
394 test_failure(r,
395 "void foo() { while(true) {} if (true) break; }",
Ethan Nicholasaf197692017-02-27 13:26:45 -0500396 "error: 1: break statement must be inside a loop or switch\n1 error\n");
ethannicholas22f939e2016-10-13 13:25:34 -0700397}
398
399DEF_TEST(SkSLContinueOutsideLoop, r) {
400 test_failure(r,
401 "void foo() { for(;;); continue; }",
402 "error: 1: continue statement must be inside a loop\n1 error\n");
Ethan Nicholasaf197692017-02-27 13:26:45 -0500403 test_failure(r,
404 "void foo() { switch (1) { default: continue; } }",
405 "error: 1: continue statement must be inside a loop\n1 error\n");
ethannicholas22f939e2016-10-13 13:25:34 -0700406}
ethannicholas08a92112016-11-09 13:26:45 -0800407
408DEF_TEST(SkSLStaticIfError, r) {
409 // ensure eliminated branch of static if / ternary is still checked for errors
410 test_failure(r,
411 "void foo() { if (true); else x = 5; }",
412 "error: 1: unknown identifier 'x'\n1 error\n");
413 test_failure(r,
414 "void foo() { if (false) x = 5; }",
415 "error: 1: unknown identifier 'x'\n1 error\n");
416 test_failure(r,
417 "void foo() { true ? 5 : x; }",
418 "error: 1: unknown identifier 'x'\n1 error\n");
419 test_failure(r,
420 "void foo() { false ? x : 5; }",
421 "error: 1: unknown identifier 'x'\n1 error\n");
422}
Ethan Nicholas7ef4b742016-11-11 15:16:46 -0500423
Ethan Nicholas3605ace2016-11-21 15:59:48 -0500424DEF_TEST(SkSLBadCap, r) {
425 test_failure(r,
426 "bool b = sk_Caps.bugFreeDriver;",
427 "error: 1: unknown capability flag 'bugFreeDriver'\n1 error\n");
428}
429
Ethan Nicholas9a5610e2017-01-03 15:16:29 -0500430DEF_TEST(SkSLDivByZero, r) {
431 test_failure(r,
432 "int x = 1 / 0;",
433 "error: 1: division by zero\n1 error\n");
434 test_failure(r,
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400435 "float x = 1 / 0;",
Ethan Nicholas9a5610e2017-01-03 15:16:29 -0500436 "error: 1: division by zero\n1 error\n");
437 test_failure(r,
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400438 "float x = 1.0 / 0.0;",
Ethan Nicholas9a5610e2017-01-03 15:16:29 -0500439 "error: 1: division by zero\n1 error\n");
440 test_failure(r,
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400441 "float x = -67.0 / (3.0 - 3);",
Ethan Nicholas9a5610e2017-01-03 15:16:29 -0500442 "error: 1: division by zero\n1 error\n");
443}
444
Ethan Nicholas38657112017-02-09 17:01:22 -0500445DEF_TEST(SkSLUnsupportedGLSLIdentifiers, r) {
446 test_failure(r,
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400447 "void main() { float x = gl_FragCoord.x; };",
Ethan Nicholas38657112017-02-09 17:01:22 -0500448 "error: 1: unknown identifier 'gl_FragCoord'\n1 error\n");
449 test_failure(r,
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400450 "void main() { float r = gl_FragColor.r; };",
Ethan Nicholas38657112017-02-09 17:01:22 -0500451 "error: 1: unknown identifier 'gl_FragColor'\n1 error\n");
452}
453
Ethan Nicholasaf197692017-02-27 13:26:45 -0500454DEF_TEST(SkSLWrongSwitchTypes, r) {
455 test_failure(r,
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400456 "void main() { switch (float2(1)) { case 1: break; } }",
457 "error: 1: expected 'int', but found 'float2'\n1 error\n");
Ethan Nicholasaf197692017-02-27 13:26:45 -0500458 test_failure(r,
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400459 "void main() { switch (1) { case float2(1): break; } }",
460 "error: 1: expected 'int', but found 'float2'\n1 error\n");
Ethan Nicholasaf197692017-02-27 13:26:45 -0500461}
462
463DEF_TEST(SkSLNonConstantCase, r) {
464 test_failure(r,
465 "void main() { int x = 1; switch (1) { case x: break; } }",
466 "error: 1: case value must be a constant\n1 error\n");
467}
468
469DEF_TEST(SkSLDuplicateCase, r) {
470 test_failure(r,
471 "void main() { switch (1) { case 0: case 1: case 0: break; } }",
472 "error: 1: duplicate case value\n1 error\n");
473}
474
Ethan Nicholas0dd30d92017-05-01 16:57:07 -0400475DEF_TEST(SkSLFieldAfterRuntimeArray, r) {
476 test_failure(r,
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400477 "buffer broken { float x[]; float y; };",
Ethan Nicholas0dd30d92017-05-01 16:57:07 -0400478 "error: 1: only the last entry in an interface block may be a runtime-sized "
479 "array\n1 error\n");
480}
481
Ethan Nicholas5ac13c22017-05-10 15:06:17 -0400482DEF_TEST(SkSLStaticIf, r) {
483 test_success(r,
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400484 "void main() { float x = 5; float y = 10;"
Ethan Nicholase1f55022019-02-05 17:17:40 -0500485 "@if (x < y) { sk_FragColor = half4(1); } }");
Ethan Nicholas5ac13c22017-05-10 15:06:17 -0400486 test_failure(r,
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400487 "void main() { float x = sqrt(25); float y = 10;"
Ethan Nicholase1f55022019-02-05 17:17:40 -0500488 "@if (x < y) { sk_FragColor = half4(1); } }",
Ethan Nicholas5ac13c22017-05-10 15:06:17 -0400489 "error: 1: static if has non-static test\n1 error\n");
490}
491
492DEF_TEST(SkSLStaticSwitch, r) {
493 test_success(r,
494 "void main() {"
495 "int x = 1;"
496 "@switch (x) {"
Ethan Nicholase1f55022019-02-05 17:17:40 -0500497 "case 1: sk_FragColor = half4(1); break;"
498 "default: sk_FragColor = half4(0);"
Ethan Nicholas5ac13c22017-05-10 15:06:17 -0400499 "}"
500 "}");
501 test_failure(r,
502 "void main() {"
503 "int x = int(sqrt(1));"
504 "@switch (x) {"
Ethan Nicholase1f55022019-02-05 17:17:40 -0500505 "case 1: sk_FragColor = half4(1); break;"
506 "default: sk_FragColor = half4(0);"
Ethan Nicholas5ac13c22017-05-10 15:06:17 -0400507 "}"
508 "}",
509 "error: 1: static switch has non-static test\n1 error\n");
510 test_failure(r,
511 "void main() {"
512 "int x = 1;"
513 "@switch (x) {"
Ethan Nicholase1f55022019-02-05 17:17:40 -0500514 "case 1: sk_FragColor = half4(1); if (sqrt(0) < sqrt(1)) break;"
515 "default: sk_FragColor = half4(0);"
Ethan Nicholas5ac13c22017-05-10 15:06:17 -0400516 "}"
517 "}",
518 "error: 1: static switch contains non-static conditional break\n1 error\n");
519}
520
Ethan Nicholas68dd2c12018-03-01 15:05:17 -0500521DEF_TEST(SkSLInterfaceBlockScope, r) {
522 test_failure(r,
523 "uniform testBlock {"
524 "float x;"
525 "} test[x];",
526 "error: 1: unknown identifier 'x'\n1 error\n");
527}
528
Ethan Nicholas6c942712018-03-16 09:45:11 -0400529DEF_TEST(SkSLDuplicateOutput, r) {
530 test_failure(r,
531 "layout (location=0, index=0) out half4 duplicateOutput;",
532 "error: 1: out location=0, index=0 is reserved for sk_FragColor\n1 error\n");
533}
Ethan Nicholas5a9a0b32019-09-17 16:18:22 -0400534
535DEF_TEST(SkSLSpuriousFloat, r) {
536 test_failure(r,
537 "void main() { float x; x = 1.5 2.5; }",
538 "error: 1: expected ';', but found '2.5'\n1 error\n");
539}