blob: 53214d2584b1be1e0242e7de2a1fc2af0e79ae73 [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
Ethan Nicholas0c8582e2019-07-19 09:26:46 -040040DEF_TEST(SkSLOpenArray, r) {
41 test_failure(r,
42 "void main(inout float4 color) { color.r[ = ( color.g ); }",
43 "error: 1: expected expression, but found '='\n1 error\n");
44}
45
ethannicholasb3058bd2016-07-01 08:22:01 -070046DEF_TEST(SkSLUndefinedSymbol, r) {
47 test_failure(r,
Ethan Nicholas8aa45692017-09-20 11:24:15 -040048 "void main() { x = float2(1); }",
ethannicholasb3058bd2016-07-01 08:22:01 -070049 "error: 1: unknown identifier 'x'\n1 error\n");
50}
51
52DEF_TEST(SkSLUndefinedFunction, r) {
53 test_failure(r,
Ethan Nicholasf7b88202017-09-18 14:10:39 -040054 "void main() { int x = foo(1); }",
ethannicholasb3058bd2016-07-01 08:22:01 -070055 "error: 1: unknown identifier 'foo'\n1 error\n");
56}
57
58DEF_TEST(SkSLGenericArgumentMismatch, r) {
59 test_failure(r,
Ethan Nicholas8aa45692017-09-20 11:24:15 -040060 "void main() { float x = sin(1, 2); }",
Ethan Nicholase1f55022019-02-05 17:17:40 -050061 "error: 1: no match for sin(int, int)\n1 error\n");
ethannicholas471e8942016-10-28 09:02:46 -070062 test_failure(r,
Ethan Nicholas8aa45692017-09-20 11:24:15 -040063 "void main() { float x = sin(true); }",
ethannicholas471e8942016-10-28 09:02:46 -070064 "error: 1: no match for sin(bool)\n1 error\n");
65 test_success(r,
Ethan Nicholas8aa45692017-09-20 11:24:15 -040066 "void main() { float x = sin(1); }");
ethannicholasb3058bd2016-07-01 08:22:01 -070067}
68
69DEF_TEST(SkSLArgumentCountMismatch, r) {
70 test_failure(r,
Ethan Nicholas8aa45692017-09-20 11:24:15 -040071 "float foo(float x) { return x * x; }"
72 "void main() { float x = foo(1, 2); }",
ethannicholasb3058bd2016-07-01 08:22:01 -070073 "error: 1: call to 'foo' expected 1 argument, but found 2\n1 error\n");
74}
75
76DEF_TEST(SkSLArgumentMismatch, r) {
Ethan Nicholasf7b88202017-09-18 14:10:39 -040077 test_failure(r,
Ethan Nicholas8aa45692017-09-20 11:24:15 -040078 "float foo(float x) { return x * x; }"
79 "void main() { float x = foo(true); }",
80 "error: 1: expected 'float', but found 'bool'\n1 error\n");
ethannicholasb3058bd2016-07-01 08:22:01 -070081}
82
83DEF_TEST(SkSLIfTypeMismatch, r) {
84 test_failure(r,
85 "void main() { if (3) { } }",
86 "error: 1: expected 'bool', but found 'int'\n1 error\n");
87}
88
89DEF_TEST(SkSLDoTypeMismatch, r) {
90 test_failure(r,
Ethan Nicholas8aa45692017-09-20 11:24:15 -040091 "void main() { do { } while (float2(1)); }",
92 "error: 1: expected 'bool', but found 'float2'\n1 error\n");
ethannicholasb3058bd2016-07-01 08:22:01 -070093}
94
95DEF_TEST(SkSLWhileTypeMismatch, r) {
96 test_failure(r,
Ethan Nicholas8aa45692017-09-20 11:24:15 -040097 "void main() { while (float3(1)) { } }",
98 "error: 1: expected 'bool', but found 'float3'\n1 error\n");
ethannicholasb3058bd2016-07-01 08:22:01 -070099}
100
101DEF_TEST(SkSLForTypeMismatch, r) {
102 test_failure(r,
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400103 "void main() { for (int x = 0; x; x++) { } }",
ethannicholasb3058bd2016-07-01 08:22:01 -0700104 "error: 1: expected 'bool', but found 'int'\n1 error\n");
105}
106
107DEF_TEST(SkSLConstructorTypeMismatch, r) {
108 test_failure(r,
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400109 "void main() { float2 x = float2(1.0, false); }",
110 "error: 1: expected 'float', but found 'bool'\n1 error\n");
ethannicholasb3058bd2016-07-01 08:22:01 -0700111 test_failure(r,
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400112 "void main() { float2 x = float2(bool2(false)); }",
113 "error: 1: 'bool2' is not a valid parameter to 'float2' constructor\n1 error\n");
Ethan Nicholas4578a8e2016-11-01 11:57:42 -0400114 test_failure(r,
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400115 "void main() { bool2 x = bool2(float2(1)); }",
116 "error: 1: 'float2' is not a valid parameter to 'bool2' constructor\n1 error\n");
Ethan Nicholas4578a8e2016-11-01 11:57:42 -0400117 test_failure(r,
ethannicholasb3058bd2016-07-01 08:22:01 -0700118 "void main() { bool x = bool(1.0); }",
119 "error: 1: cannot construct 'bool'\n1 error\n");
120 test_failure(r,
121 "struct foo { int x; }; void main() { foo x = foo(5); }",
122 "error: 1: cannot construct 'foo'\n1 error\n");
123 test_failure(r,
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400124 "struct foo { int x; } foo; void main() { float x = float(foo); }",
125 "error: 1: invalid argument to 'float' constructor (expected a number or bool, but found 'foo')\n1 error\n");
ethannicholasb3058bd2016-07-01 08:22:01 -0700126 test_failure(r,
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400127 "struct foo { int x; } foo; void main() { float2 x = float2(foo); }",
128 "error: 1: 'foo' is not a valid parameter to 'float2' constructor\n1 error\n");
Ethan Nicholas3deaeb22017-04-25 14:42:11 -0400129 test_failure(r,
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400130 "void main() { float2x2 x = float2x2(true); }",
131 "error: 1: expected 'float', but found 'bool'\n1 error\n");
ethannicholasb3058bd2016-07-01 08:22:01 -0700132}
133
134DEF_TEST(SkSLConstructorArgumentCount, r) {
135 test_failure(r,
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400136 "void main() { float3 x = float3(1.0, 2.0); }",
137 "error: 1: invalid arguments to 'float3' constructor (expected 3 scalars, but "
ethannicholasb3058bd2016-07-01 08:22:01 -0700138 "found 2)\n1 error\n");
Ethan Nicholas84645e32017-02-09 13:57:14 -0500139 test_failure(r,
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400140 "void main() { float3 x = float3(1.0, 2.0, 3.0, 4.0); }",
141 "error: 1: invalid arguments to 'float3' constructor (expected 3 scalars, but found "
Ethan Nicholas84645e32017-02-09 13:57:14 -0500142 "4)\n1 error\n");
ethannicholasb3058bd2016-07-01 08:22:01 -0700143}
144
145DEF_TEST(SkSLSwizzleScalar, r) {
146 test_failure(r,
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400147 "void main() { float x = 1; float y = x.y; }",
148 "error: 1: cannot swizzle value of type 'float'\n1 error\n");
ethannicholasb3058bd2016-07-01 08:22:01 -0700149}
150
151DEF_TEST(SkSLSwizzleMatrix, r) {
152 test_failure(r,
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400153 "void main() { float2x2 x = float2x2(1); float y = x.y; }",
154 "error: 1: cannot swizzle value of type 'float2x2'\n1 error\n");
ethannicholasb3058bd2016-07-01 08:22:01 -0700155}
156
157DEF_TEST(SkSLSwizzleOutOfBounds, r) {
158 test_failure(r,
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400159 "void main() { float3 test = float2(1).xyz; }",
ethannicholasb3058bd2016-07-01 08:22:01 -0700160 "error: 1: invalid swizzle component 'z'\n1 error\n");
161}
162
163DEF_TEST(SkSLSwizzleTooManyComponents, r) {
164 test_failure(r,
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400165 "void main() { float4 test = float2(1).xxxxx; }",
ethannicholasb3058bd2016-07-01 08:22:01 -0700166 "error: 1: too many components in swizzle mask 'xxxxx'\n1 error\n");
167}
168
169DEF_TEST(SkSLSwizzleDuplicateOutput, r) {
170 test_failure(r,
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400171 "void main() { float4 test = float4(1); test.xyyz = float4(1); }",
ethannicholasb3058bd2016-07-01 08:22:01 -0700172 "error: 1: cannot write to the same swizzle field more than once\n1 error\n");
173}
ethannicholasea4567c2016-10-17 11:24:37 -0700174
Ethan Nicholascb0f4092019-04-19 11:26:50 -0400175DEF_TEST(SkSLSwizzleConstantOutput, r) {
176 test_failure(r,
177 "void main() { float4 test = float4(1); test.xyz0 = float4(1); }",
178 "error: 1: cannot write to a swizzle mask containing a constant\n1 error\n");
179}
180
ethannicholasb3058bd2016-07-01 08:22:01 -0700181DEF_TEST(SkSLAssignmentTypeMismatch, r) {
182 test_failure(r,
183 "void main() { int x = 1.0; }",
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400184 "error: 1: expected 'int', but found 'float'\n1 error\n");
ethannicholasea4567c2016-10-17 11:24:37 -0700185 test_failure(r,
186 "void main() { int x; x = 1.0; }",
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400187 "error: 1: type mismatch: '=' cannot operate on 'int', 'float'\n1 error\n");
ethannicholasea4567c2016-10-17 11:24:37 -0700188 test_success(r,
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400189 "void main() { float3 x = float3(0); x *= 1.0; }");
ethannicholasea4567c2016-10-17 11:24:37 -0700190 test_failure(r,
Ethan Nicholas5af9ea32017-07-28 15:19:46 -0400191 "void main() { int3 x = int3(0); x *= 1.0; }",
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400192 "error: 1: type mismatch: '*=' cannot operate on 'int3', 'float'\n1 error\n");
ethannicholasb3058bd2016-07-01 08:22:01 -0700193}
194
195DEF_TEST(SkSLReturnFromVoid, r) {
196 test_failure(r,
197 "void main() { return true; }",
198 "error: 1: may not return a value from a void function\n1 error\n");
199}
200
201DEF_TEST(SkSLReturnMissingValue, r) {
202 test_failure(r,
203 "int foo() { return; } void main() { }",
204 "error: 1: expected function to return 'int'\n1 error\n");
205}
206
207DEF_TEST(SkSLReturnTypeMismatch, r) {
208 test_failure(r,
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400209 "int foo() { return 1.0; } void main() { }",
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400210 "error: 1: expected 'int', but found 'float'\n1 error\n");
ethannicholasb3058bd2016-07-01 08:22:01 -0700211}
212
213DEF_TEST(SkSLDuplicateFunction, r) {
214 test_failure(r,
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400215 "void main() { } void main() { }",
ethannicholasb3058bd2016-07-01 08:22:01 -0700216 "error: 1: duplicate definition of void main()\n1 error\n");
217 test_success(r,
218 "void main(); void main() { }");
219}
220
221DEF_TEST(SkSLUsingInvalidValue, r) {
222 test_failure(r,
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400223 "void main() { int x = int; }",
ethannicholasb3058bd2016-07-01 08:22:01 -0700224 "error: 1: expected '(' to begin constructor invocation\n1 error\n");
225 test_failure(r,
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400226 "int test() { return 1; } void main() { int x = test; }",
ethannicholasb3058bd2016-07-01 08:22:01 -0700227 "error: 1: expected '(' to begin function call\n1 error\n");
228}
229DEF_TEST(SkSLDifferentReturnType, r) {
230 test_failure(r,
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400231 "int main() { return 1; } void main() { }",
ethannicholasb3058bd2016-07-01 08:22:01 -0700232 "error: 1: functions 'void main()' and 'int main()' differ only in return type\n1 "
233 "error\n");
234}
235
236DEF_TEST(SkSLDifferentModifiers, r) {
237 test_failure(r,
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400238 "void test(int x); void test(out int x) { }",
ethannicholasb3058bd2016-07-01 08:22:01 -0700239 "error: 1: modifiers on parameter 1 differ between declaration and definition\n1 "
240 "error\n");
241}
242
243DEF_TEST(SkSLDuplicateSymbol, r) {
244 test_failure(r,
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400245 "int main; void main() { }",
ethannicholasb3058bd2016-07-01 08:22:01 -0700246 "error: 1: symbol 'main' was already defined\n1 error\n");
247
248 test_failure(r,
249 "int x; int x; void main() { }",
250 "error: 1: symbol 'x' was already defined\n1 error\n");
251
252 test_success(r, "int x; void main() { int x; }");
253}
254
255DEF_TEST(SkSLBinaryTypeMismatch, r) {
256 test_failure(r,
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400257 "void main() { float x = 3 * true; }",
ethannicholasb3058bd2016-07-01 08:22:01 -0700258 "error: 1: type mismatch: '*' cannot operate on 'int', 'bool'\n1 error\n");
259 test_failure(r,
260 "void main() { bool x = 1 || 2.0; }",
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400261 "error: 1: type mismatch: '||' cannot operate on 'int', 'float'\n1 error\n");
Ethan Nicholas23463002018-03-28 15:16:15 -0400262 test_failure(r,
263 "void main() { bool x = float2(0) == 0; }",
264 "error: 1: type mismatch: '==' cannot operate on 'float2', 'int'\n1 error\n");
265 test_failure(r,
266 "void main() { bool x = float2(0) != 0; }",
267 "error: 1: type mismatch: '!=' cannot operate on 'float2', 'int'\n1 error\n");
ethannicholasb3058bd2016-07-01 08:22:01 -0700268}
269
270DEF_TEST(SkSLCallNonFunction, r) {
271 test_failure(r,
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400272 "void main() { float x = 3; x(); }",
Ethan Nicholas2a099da2020-01-02 14:40:54 -0500273 "error: 1: not a function\n1 error\n");
ethannicholasb3058bd2016-07-01 08:22:01 -0700274}
275
276DEF_TEST(SkSLInvalidUnary, r) {
277 test_failure(r,
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400278 "void main() { float4x4 x = float4x4(1); ++x; }",
279 "error: 1: '++' cannot operate on 'float4x4'\n1 error\n");
ethannicholasb3058bd2016-07-01 08:22:01 -0700280 test_failure(r,
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400281 "void main() { float3 x = float3(1); --x; }",
282 "error: 1: '--' cannot operate on 'float3'\n1 error\n");
ethannicholasb3058bd2016-07-01 08:22:01 -0700283 test_failure(r,
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400284 "void main() { float4x4 x = float4x4(1); x++; }",
285 "error: 1: '++' cannot operate on 'float4x4'\n1 error\n");
ethannicholasb3058bd2016-07-01 08:22:01 -0700286 test_failure(r,
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400287 "void main() { float3 x = float3(1); x--; }",
288 "error: 1: '--' cannot operate on 'float3'\n1 error\n");
ethannicholasb3058bd2016-07-01 08:22:01 -0700289 test_failure(r,
290 "void main() { int x = !12; }",
291 "error: 1: '!' cannot operate on 'int'\n1 error\n");
292 test_failure(r,
293 "struct foo { } bar; void main() { foo x = +bar; }",
294 "error: 1: '+' cannot operate on 'foo'\n1 error\n");
295 test_failure(r,
296 "struct foo { } bar; void main() { foo x = -bar; }",
297 "error: 1: '-' cannot operate on 'foo'\n1 error\n");
298 test_success(r,
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400299 "void main() { float2 x = float2(1, 1); x = +x; x = -x; }");
ethannicholasb3058bd2016-07-01 08:22:01 -0700300}
301
302DEF_TEST(SkSLInvalidAssignment, r) {
303 test_failure(r,
304 "void main() { 1 = 2; }",
Ethan Nicholas2a099da2020-01-02 14:40:54 -0500305 "error: 1: cannot assign to this expression\n1 error\n");
ethannicholasb3058bd2016-07-01 08:22:01 -0700306 test_failure(r,
307 "uniform int x; void main() { x = 0; }",
308 "error: 1: cannot modify immutable variable 'x'\n1 error\n");
309 test_failure(r,
310 "const int x; void main() { x = 0; }",
311 "error: 1: cannot modify immutable variable 'x'\n1 error\n");
312}
313
314DEF_TEST(SkSLBadIndex, r) {
315 test_failure(r,
316 "void main() { int x = 2[0]; }",
317 "error: 1: expected array, but found 'int'\n1 error\n");
318 test_failure(r,
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400319 "void main() { float2 x = float2(0); int y = x[0][0]; }",
320 "error: 1: expected array, but found 'float'\n1 error\n");
ethannicholasb3058bd2016-07-01 08:22:01 -0700321}
322
323DEF_TEST(SkSLTernaryMismatch, r) {
324 test_failure(r,
325 "void main() { int x = 5 > 2 ? true : 1.0; }",
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400326 "error: 1: ternary operator result mismatch: 'bool', 'float'\n1 error\n");
Ethan Nicholas2be687a2017-01-03 16:44:39 -0500327 test_failure(r,
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400328 "void main() { int x = 5 > 2 ? float3(1) : 1.0; }",
329 "error: 1: ternary operator result mismatch: 'float3', 'float'\n1 error\n");
ethannicholasb3058bd2016-07-01 08:22:01 -0700330}
331
332DEF_TEST(SkSLInterfaceBlockStorageModifiers, r) {
333 test_failure(r,
334 "uniform foo { out int x; };",
335 "error: 1: interface block fields may not have storage qualifiers\n1 error\n");
336}
ethannicholas22f939e2016-10-13 13:25:34 -0700337
338DEF_TEST(SkSLUseWithoutInitialize, r) {
339 test_failure(r,
340 "void main() { int x; if (5 == 2) x = 3; x++; }",
341 "error: 1: 'x' has not been assigned\n1 error\n");
342 test_failure(r,
343 "void main() { int x[2][2]; int i; x[i][1] = 4; }",
344 "error: 1: 'i' has not been assigned\n1 error\n");
345 test_failure(r,
346 "int main() { int r; return r; }",
347 "error: 1: 'r' has not been assigned\n1 error\n");
348 test_failure(r,
Ethan Nicholas82a62d22017-11-07 14:42:10 +0000349 "void main() { int x; int y = x; }",
ethannicholas22f939e2016-10-13 13:25:34 -0700350 "error: 1: 'x' has not been assigned\n1 error\n");
351 test_failure(r,
352 "void main() { bool x; if (true && (false || x)) return; }",
353 "error: 1: 'x' has not been assigned\n1 error\n");
Ethan Nicholasaf197692017-02-27 13:26:45 -0500354 test_failure(r,
355 "void main() { int x; switch (3) { case 0: x = 0; case 1: x = 1; }"
Ethan Nicholase1f55022019-02-05 17:17:40 -0500356 "sk_FragColor = half4(x); }",
Ethan Nicholasaf197692017-02-27 13:26:45 -0500357 "error: 1: 'x' has not been assigned\n1 error\n");
ethannicholas22f939e2016-10-13 13:25:34 -0700358}
359
360DEF_TEST(SkSLUnreachable, r) {
361 test_failure(r,
362 "void main() { return; return; }",
363 "error: 1: unreachable\n1 error\n");
364 test_failure(r,
365 "void main() { for (;;) { continue; int x = 1; } }",
366 "error: 1: unreachable\n1 error\n");
Ethan Nicholasd9fe7002017-05-30 10:15:34 -0400367/* test_failure(r,
ethannicholas22f939e2016-10-13 13:25:34 -0700368 "void main() { for (;;) { } return; }",
Ethan Nicholasd9fe7002017-05-30 10:15:34 -0400369 "error: 1: unreachable\n1 error\n");*/
ethannicholas22f939e2016-10-13 13:25:34 -0700370 test_failure(r,
371 "void main() { if (true) return; else discard; return; }",
372 "error: 1: unreachable\n1 error\n");
373 test_failure(r,
374 "void main() { return; while (true); }",
375 "error: 1: unreachable\n1 error\n");
376}
377
378DEF_TEST(SkSLNoReturn, r) {
379 test_failure(r,
380 "int foo() { if (2 > 5) return 3; }",
Ethan Nicholasdb80f692019-11-22 14:06:12 -0500381 "error: 1: function 'foo' can exit without returning a value\n1 error\n");
ethannicholas22f939e2016-10-13 13:25:34 -0700382}
383
384DEF_TEST(SkSLBreakOutsideLoop, r) {
385 test_failure(r,
386 "void foo() { while(true) {} if (true) break; }",
Ethan Nicholasaf197692017-02-27 13:26:45 -0500387 "error: 1: break statement must be inside a loop or switch\n1 error\n");
ethannicholas22f939e2016-10-13 13:25:34 -0700388}
389
390DEF_TEST(SkSLContinueOutsideLoop, r) {
391 test_failure(r,
392 "void foo() { for(;;); continue; }",
393 "error: 1: continue statement must be inside a loop\n1 error\n");
Ethan Nicholasaf197692017-02-27 13:26:45 -0500394 test_failure(r,
395 "void foo() { switch (1) { default: continue; } }",
396 "error: 1: continue statement must be inside a loop\n1 error\n");
ethannicholas22f939e2016-10-13 13:25:34 -0700397}
ethannicholas08a92112016-11-09 13:26:45 -0800398
399DEF_TEST(SkSLStaticIfError, r) {
400 // ensure eliminated branch of static if / ternary is still checked for errors
401 test_failure(r,
402 "void foo() { if (true); else x = 5; }",
403 "error: 1: unknown identifier 'x'\n1 error\n");
404 test_failure(r,
405 "void foo() { if (false) x = 5; }",
406 "error: 1: unknown identifier 'x'\n1 error\n");
407 test_failure(r,
408 "void foo() { true ? 5 : x; }",
409 "error: 1: unknown identifier 'x'\n1 error\n");
410 test_failure(r,
411 "void foo() { false ? x : 5; }",
412 "error: 1: unknown identifier 'x'\n1 error\n");
413}
Ethan Nicholas7ef4b742016-11-11 15:16:46 -0500414
Ethan Nicholas3605ace2016-11-21 15:59:48 -0500415DEF_TEST(SkSLBadCap, r) {
416 test_failure(r,
417 "bool b = sk_Caps.bugFreeDriver;",
418 "error: 1: unknown capability flag 'bugFreeDriver'\n1 error\n");
419}
420
Ethan Nicholas9a5610e2017-01-03 15:16:29 -0500421DEF_TEST(SkSLDivByZero, r) {
422 test_failure(r,
423 "int x = 1 / 0;",
424 "error: 1: division by zero\n1 error\n");
425 test_failure(r,
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400426 "float x = 1 / 0;",
Ethan Nicholas9a5610e2017-01-03 15:16:29 -0500427 "error: 1: division by zero\n1 error\n");
428 test_failure(r,
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400429 "float x = 1.0 / 0.0;",
Ethan Nicholas9a5610e2017-01-03 15:16:29 -0500430 "error: 1: division by zero\n1 error\n");
431 test_failure(r,
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400432 "float x = -67.0 / (3.0 - 3);",
Ethan Nicholas9a5610e2017-01-03 15:16:29 -0500433 "error: 1: division by zero\n1 error\n");
434}
435
Ethan Nicholas38657112017-02-09 17:01:22 -0500436DEF_TEST(SkSLUnsupportedGLSLIdentifiers, r) {
437 test_failure(r,
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400438 "void main() { float x = gl_FragCoord.x; };",
Ethan Nicholas38657112017-02-09 17:01:22 -0500439 "error: 1: unknown identifier 'gl_FragCoord'\n1 error\n");
440 test_failure(r,
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400441 "void main() { float r = gl_FragColor.r; };",
Ethan Nicholas38657112017-02-09 17:01:22 -0500442 "error: 1: unknown identifier 'gl_FragColor'\n1 error\n");
443}
444
Ethan Nicholasaf197692017-02-27 13:26:45 -0500445DEF_TEST(SkSLWrongSwitchTypes, r) {
446 test_failure(r,
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400447 "void main() { switch (float2(1)) { case 1: break; } }",
448 "error: 1: expected 'int', but found 'float2'\n1 error\n");
Ethan Nicholasaf197692017-02-27 13:26:45 -0500449 test_failure(r,
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400450 "void main() { switch (1) { case float2(1): break; } }",
451 "error: 1: expected 'int', but found 'float2'\n1 error\n");
Ethan Nicholasaf197692017-02-27 13:26:45 -0500452}
453
454DEF_TEST(SkSLNonConstantCase, r) {
455 test_failure(r,
456 "void main() { int x = 1; switch (1) { case x: break; } }",
457 "error: 1: case value must be a constant\n1 error\n");
458}
459
460DEF_TEST(SkSLDuplicateCase, r) {
461 test_failure(r,
462 "void main() { switch (1) { case 0: case 1: case 0: break; } }",
463 "error: 1: duplicate case value\n1 error\n");
464}
465
Ethan Nicholas0dd30d92017-05-01 16:57:07 -0400466DEF_TEST(SkSLFieldAfterRuntimeArray, r) {
467 test_failure(r,
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400468 "buffer broken { float x[]; float y; };",
Ethan Nicholas0dd30d92017-05-01 16:57:07 -0400469 "error: 1: only the last entry in an interface block may be a runtime-sized "
470 "array\n1 error\n");
471}
472
Ethan Nicholas5ac13c22017-05-10 15:06:17 -0400473DEF_TEST(SkSLStaticIf, r) {
474 test_success(r,
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400475 "void main() { float x = 5; float y = 10;"
Ethan Nicholase1f55022019-02-05 17:17:40 -0500476 "@if (x < y) { sk_FragColor = half4(1); } }");
Ethan Nicholas5ac13c22017-05-10 15:06:17 -0400477 test_failure(r,
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400478 "void main() { float x = sqrt(25); float y = 10;"
Ethan Nicholase1f55022019-02-05 17:17:40 -0500479 "@if (x < y) { sk_FragColor = half4(1); } }",
Ethan Nicholas5ac13c22017-05-10 15:06:17 -0400480 "error: 1: static if has non-static test\n1 error\n");
481}
482
483DEF_TEST(SkSLStaticSwitch, r) {
484 test_success(r,
485 "void main() {"
486 "int x = 1;"
487 "@switch (x) {"
Ethan Nicholase1f55022019-02-05 17:17:40 -0500488 "case 1: sk_FragColor = half4(1); break;"
489 "default: sk_FragColor = half4(0);"
Ethan Nicholas5ac13c22017-05-10 15:06:17 -0400490 "}"
491 "}");
492 test_failure(r,
493 "void main() {"
494 "int x = int(sqrt(1));"
495 "@switch (x) {"
Ethan Nicholase1f55022019-02-05 17:17:40 -0500496 "case 1: sk_FragColor = half4(1); break;"
497 "default: sk_FragColor = half4(0);"
Ethan Nicholas5ac13c22017-05-10 15:06:17 -0400498 "}"
499 "}",
500 "error: 1: static switch has non-static test\n1 error\n");
501 test_failure(r,
502 "void main() {"
503 "int x = 1;"
504 "@switch (x) {"
Ethan Nicholase1f55022019-02-05 17:17:40 -0500505 "case 1: sk_FragColor = half4(1); if (sqrt(0) < sqrt(1)) break;"
506 "default: sk_FragColor = half4(0);"
Ethan Nicholas5ac13c22017-05-10 15:06:17 -0400507 "}"
508 "}",
509 "error: 1: static switch contains non-static conditional break\n1 error\n");
510}
511
Ethan Nicholas68dd2c12018-03-01 15:05:17 -0500512DEF_TEST(SkSLInterfaceBlockScope, r) {
513 test_failure(r,
514 "uniform testBlock {"
515 "float x;"
516 "} test[x];",
517 "error: 1: unknown identifier 'x'\n1 error\n");
518}
519
Ethan Nicholas6c942712018-03-16 09:45:11 -0400520DEF_TEST(SkSLDuplicateOutput, r) {
521 test_failure(r,
522 "layout (location=0, index=0) out half4 duplicateOutput;",
523 "error: 1: out location=0, index=0 is reserved for sk_FragColor\n1 error\n");
524}
Ethan Nicholas5a9a0b32019-09-17 16:18:22 -0400525
526DEF_TEST(SkSLSpuriousFloat, r) {
527 test_failure(r,
528 "void main() { float x; x = 1.5 2.5; }",
529 "error: 1: expected ';', but found '2.5'\n1 error\n");
530}