ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
| 8 | #include "SkSLCompiler.h" |
| 9 | |
| 10 | #include "Test.h" |
| 11 | |
| 12 | static void test_failure(skiatest::Reporter* r, const char* src, const char* error) { |
| 13 | SkSL::Compiler compiler; |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 14 | SkSL::Program::Settings settings; |
| 15 | sk_sp<GrShaderCaps> caps = SkSL::ShaderCapsFactory::Default(); |
| 16 | settings.fCaps = caps.get(); |
Ethan Nicholas | 0054311 | 2018-07-31 09:44:36 -0400 | [diff] [blame] | 17 | 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 Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 22 | SkSL::String skError(error); |
Ethan Nicholas | 9e1138d | 2016-11-21 10:39:35 -0500 | [diff] [blame] | 23 | if (compiler.errorText() != skError) { |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 24 | SkDebugf("SKSL ERROR:\n source: %s\n expected: %s received: %s", src, error, |
| 25 | compiler.errorText().c_str()); |
| 26 | } |
Ethan Nicholas | 9e1138d | 2016-11-21 10:39:35 -0500 | [diff] [blame] | 27 | REPORTER_ASSERT(r, compiler.errorText() == skError); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 28 | } |
| 29 | |
| 30 | static void test_success(skiatest::Reporter* r, const char* src) { |
| 31 | SkSL::Compiler compiler; |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 32 | 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 Osman | 93ba0a4 | 2017-08-14 14:48:10 -0400 | [diff] [blame] | 36 | SkSL::String(src), settings); |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 37 | REPORTER_ASSERT(r, program); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 38 | } |
| 39 | |
| 40 | DEF_TEST(SkSLUndefinedSymbol, r) { |
| 41 | test_failure(r, |
Ethan Nicholas | 8aa4569 | 2017-09-20 11:24:15 -0400 | [diff] [blame] | 42 | "void main() { x = float2(1); }", |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 43 | "error: 1: unknown identifier 'x'\n1 error\n"); |
| 44 | } |
| 45 | |
| 46 | DEF_TEST(SkSLUndefinedFunction, r) { |
| 47 | test_failure(r, |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 48 | "void main() { int x = foo(1); }", |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 49 | "error: 1: unknown identifier 'foo'\n1 error\n"); |
| 50 | } |
| 51 | |
| 52 | DEF_TEST(SkSLGenericArgumentMismatch, r) { |
| 53 | test_failure(r, |
Ethan Nicholas | 8aa4569 | 2017-09-20 11:24:15 -0400 | [diff] [blame] | 54 | "void main() { float x = sin(1, 2); }", |
ethannicholas | 471e894 | 2016-10-28 09:02:46 -0700 | [diff] [blame] | 55 | "error: 1: call to 'sin' expected 1 argument, but found 2\n1 error\n"); |
| 56 | test_failure(r, |
Ethan Nicholas | 8aa4569 | 2017-09-20 11:24:15 -0400 | [diff] [blame] | 57 | "void main() { float x = sin(true); }", |
ethannicholas | 471e894 | 2016-10-28 09:02:46 -0700 | [diff] [blame] | 58 | "error: 1: no match for sin(bool)\n1 error\n"); |
| 59 | test_success(r, |
Ethan Nicholas | 8aa4569 | 2017-09-20 11:24:15 -0400 | [diff] [blame] | 60 | "void main() { float x = sin(1); }"); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | DEF_TEST(SkSLArgumentCountMismatch, r) { |
| 64 | test_failure(r, |
Ethan Nicholas | 8aa4569 | 2017-09-20 11:24:15 -0400 | [diff] [blame] | 65 | "float foo(float x) { return x * x; }" |
| 66 | "void main() { float x = foo(1, 2); }", |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 67 | "error: 1: call to 'foo' expected 1 argument, but found 2\n1 error\n"); |
| 68 | } |
| 69 | |
| 70 | DEF_TEST(SkSLArgumentMismatch, r) { |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 71 | test_failure(r, |
Ethan Nicholas | 8aa4569 | 2017-09-20 11:24:15 -0400 | [diff] [blame] | 72 | "float foo(float x) { return x * x; }" |
| 73 | "void main() { float x = foo(true); }", |
| 74 | "error: 1: expected 'float', but found 'bool'\n1 error\n"); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | DEF_TEST(SkSLIfTypeMismatch, r) { |
| 78 | test_failure(r, |
| 79 | "void main() { if (3) { } }", |
| 80 | "error: 1: expected 'bool', but found 'int'\n1 error\n"); |
| 81 | } |
| 82 | |
| 83 | DEF_TEST(SkSLDoTypeMismatch, r) { |
| 84 | test_failure(r, |
Ethan Nicholas | 8aa4569 | 2017-09-20 11:24:15 -0400 | [diff] [blame] | 85 | "void main() { do { } while (float2(1)); }", |
| 86 | "error: 1: expected 'bool', but found 'float2'\n1 error\n"); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | DEF_TEST(SkSLWhileTypeMismatch, r) { |
| 90 | test_failure(r, |
Ethan Nicholas | 8aa4569 | 2017-09-20 11:24:15 -0400 | [diff] [blame] | 91 | "void main() { while (float3(1)) { } }", |
| 92 | "error: 1: expected 'bool', but found 'float3'\n1 error\n"); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | DEF_TEST(SkSLForTypeMismatch, r) { |
| 96 | test_failure(r, |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 97 | "void main() { for (int x = 0; x; x++) { } }", |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 98 | "error: 1: expected 'bool', but found 'int'\n1 error\n"); |
| 99 | } |
| 100 | |
| 101 | DEF_TEST(SkSLConstructorTypeMismatch, r) { |
| 102 | test_failure(r, |
Ethan Nicholas | 8aa4569 | 2017-09-20 11:24:15 -0400 | [diff] [blame] | 103 | "void main() { float2 x = float2(1.0, false); }", |
| 104 | "error: 1: expected 'float', but found 'bool'\n1 error\n"); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 105 | test_failure(r, |
Ethan Nicholas | 8aa4569 | 2017-09-20 11:24:15 -0400 | [diff] [blame] | 106 | "void main() { float2 x = float2(bool2(false)); }", |
| 107 | "error: 1: 'bool2' is not a valid parameter to 'float2' constructor\n1 error\n"); |
Ethan Nicholas | 4578a8e | 2016-11-01 11:57:42 -0400 | [diff] [blame] | 108 | test_failure(r, |
Ethan Nicholas | 8aa4569 | 2017-09-20 11:24:15 -0400 | [diff] [blame] | 109 | "void main() { bool2 x = bool2(float2(1)); }", |
| 110 | "error: 1: 'float2' is not a valid parameter to 'bool2' constructor\n1 error\n"); |
Ethan Nicholas | 4578a8e | 2016-11-01 11:57:42 -0400 | [diff] [blame] | 111 | test_failure(r, |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 112 | "void main() { bool x = bool(1.0); }", |
| 113 | "error: 1: cannot construct 'bool'\n1 error\n"); |
| 114 | test_failure(r, |
| 115 | "struct foo { int x; }; void main() { foo x = foo(5); }", |
| 116 | "error: 1: cannot construct 'foo'\n1 error\n"); |
| 117 | test_failure(r, |
Ethan Nicholas | 8aa4569 | 2017-09-20 11:24:15 -0400 | [diff] [blame] | 118 | "struct foo { int x; } foo; void main() { float x = float(foo); }", |
| 119 | "error: 1: invalid argument to 'float' constructor (expected a number or bool, but found 'foo')\n1 error\n"); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 120 | test_failure(r, |
Ethan Nicholas | 8aa4569 | 2017-09-20 11:24:15 -0400 | [diff] [blame] | 121 | "struct foo { int x; } foo; void main() { float2 x = float2(foo); }", |
| 122 | "error: 1: 'foo' is not a valid parameter to 'float2' constructor\n1 error\n"); |
Ethan Nicholas | 3deaeb2 | 2017-04-25 14:42:11 -0400 | [diff] [blame] | 123 | test_failure(r, |
Ethan Nicholas | 8aa4569 | 2017-09-20 11:24:15 -0400 | [diff] [blame] | 124 | "void main() { float2x2 x = float2x2(true); }", |
| 125 | "error: 1: expected 'float', but found 'bool'\n1 error\n"); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 126 | } |
| 127 | |
| 128 | DEF_TEST(SkSLConstructorArgumentCount, r) { |
| 129 | test_failure(r, |
Ethan Nicholas | 8aa4569 | 2017-09-20 11:24:15 -0400 | [diff] [blame] | 130 | "void main() { float3 x = float3(1.0, 2.0); }", |
| 131 | "error: 1: invalid arguments to 'float3' constructor (expected 3 scalars, but " |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 132 | "found 2)\n1 error\n"); |
Ethan Nicholas | 84645e3 | 2017-02-09 13:57:14 -0500 | [diff] [blame] | 133 | test_failure(r, |
Ethan Nicholas | 8aa4569 | 2017-09-20 11:24:15 -0400 | [diff] [blame] | 134 | "void main() { float3 x = float3(1.0, 2.0, 3.0, 4.0); }", |
| 135 | "error: 1: invalid arguments to 'float3' constructor (expected 3 scalars, but found " |
Ethan Nicholas | 84645e3 | 2017-02-09 13:57:14 -0500 | [diff] [blame] | 136 | "4)\n1 error\n"); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 137 | } |
| 138 | |
| 139 | DEF_TEST(SkSLSwizzleScalar, r) { |
| 140 | test_failure(r, |
Ethan Nicholas | 8aa4569 | 2017-09-20 11:24:15 -0400 | [diff] [blame] | 141 | "void main() { float x = 1; float y = x.y; }", |
| 142 | "error: 1: cannot swizzle value of type 'float'\n1 error\n"); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 143 | } |
| 144 | |
| 145 | DEF_TEST(SkSLSwizzleMatrix, r) { |
| 146 | test_failure(r, |
Ethan Nicholas | 8aa4569 | 2017-09-20 11:24:15 -0400 | [diff] [blame] | 147 | "void main() { float2x2 x = float2x2(1); float y = x.y; }", |
| 148 | "error: 1: cannot swizzle value of type 'float2x2'\n1 error\n"); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 149 | } |
| 150 | |
| 151 | DEF_TEST(SkSLSwizzleOutOfBounds, r) { |
| 152 | test_failure(r, |
Ethan Nicholas | 8aa4569 | 2017-09-20 11:24:15 -0400 | [diff] [blame] | 153 | "void main() { float3 test = float2(1).xyz; }", |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 154 | "error: 1: invalid swizzle component 'z'\n1 error\n"); |
| 155 | } |
| 156 | |
| 157 | DEF_TEST(SkSLSwizzleTooManyComponents, r) { |
| 158 | test_failure(r, |
Ethan Nicholas | 8aa4569 | 2017-09-20 11:24:15 -0400 | [diff] [blame] | 159 | "void main() { float4 test = float2(1).xxxxx; }", |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 160 | "error: 1: too many components in swizzle mask 'xxxxx'\n1 error\n"); |
| 161 | } |
| 162 | |
| 163 | DEF_TEST(SkSLSwizzleDuplicateOutput, r) { |
| 164 | test_failure(r, |
Ethan Nicholas | 8aa4569 | 2017-09-20 11:24:15 -0400 | [diff] [blame] | 165 | "void main() { float4 test = float4(1); test.xyyz = float4(1); }", |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 166 | "error: 1: cannot write to the same swizzle field more than once\n1 error\n"); |
| 167 | } |
ethannicholas | ea4567c | 2016-10-17 11:24:37 -0700 | [diff] [blame] | 168 | |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 169 | DEF_TEST(SkSLAssignmentTypeMismatch, r) { |
| 170 | test_failure(r, |
| 171 | "void main() { int x = 1.0; }", |
Ethan Nicholas | 8aa4569 | 2017-09-20 11:24:15 -0400 | [diff] [blame] | 172 | "error: 1: expected 'int', but found 'float'\n1 error\n"); |
ethannicholas | ea4567c | 2016-10-17 11:24:37 -0700 | [diff] [blame] | 173 | test_failure(r, |
| 174 | "void main() { int x; x = 1.0; }", |
Ethan Nicholas | 8aa4569 | 2017-09-20 11:24:15 -0400 | [diff] [blame] | 175 | "error: 1: type mismatch: '=' cannot operate on 'int', 'float'\n1 error\n"); |
ethannicholas | ea4567c | 2016-10-17 11:24:37 -0700 | [diff] [blame] | 176 | test_success(r, |
Ethan Nicholas | 8aa4569 | 2017-09-20 11:24:15 -0400 | [diff] [blame] | 177 | "void main() { float3 x = float3(0); x *= 1.0; }"); |
ethannicholas | ea4567c | 2016-10-17 11:24:37 -0700 | [diff] [blame] | 178 | test_failure(r, |
Ethan Nicholas | 5af9ea3 | 2017-07-28 15:19:46 -0400 | [diff] [blame] | 179 | "void main() { int3 x = int3(0); x *= 1.0; }", |
Ethan Nicholas | 8aa4569 | 2017-09-20 11:24:15 -0400 | [diff] [blame] | 180 | "error: 1: type mismatch: '*=' cannot operate on 'int3', 'float'\n1 error\n"); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 181 | } |
| 182 | |
| 183 | DEF_TEST(SkSLReturnFromVoid, r) { |
| 184 | test_failure(r, |
| 185 | "void main() { return true; }", |
| 186 | "error: 1: may not return a value from a void function\n1 error\n"); |
| 187 | } |
| 188 | |
| 189 | DEF_TEST(SkSLReturnMissingValue, r) { |
| 190 | test_failure(r, |
| 191 | "int foo() { return; } void main() { }", |
| 192 | "error: 1: expected function to return 'int'\n1 error\n"); |
| 193 | } |
| 194 | |
| 195 | DEF_TEST(SkSLReturnTypeMismatch, r) { |
| 196 | test_failure(r, |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 197 | "int foo() { return 1.0; } void main() { }", |
Ethan Nicholas | 8aa4569 | 2017-09-20 11:24:15 -0400 | [diff] [blame] | 198 | "error: 1: expected 'int', but found 'float'\n1 error\n"); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 199 | } |
| 200 | |
| 201 | DEF_TEST(SkSLDuplicateFunction, r) { |
| 202 | test_failure(r, |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 203 | "void main() { } void main() { }", |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 204 | "error: 1: duplicate definition of void main()\n1 error\n"); |
| 205 | test_success(r, |
| 206 | "void main(); void main() { }"); |
| 207 | } |
| 208 | |
| 209 | DEF_TEST(SkSLUsingInvalidValue, r) { |
| 210 | test_failure(r, |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 211 | "void main() { int x = int; }", |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 212 | "error: 1: expected '(' to begin constructor invocation\n1 error\n"); |
| 213 | test_failure(r, |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 214 | "int test() { return 1; } void main() { int x = test; }", |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 215 | "error: 1: expected '(' to begin function call\n1 error\n"); |
| 216 | } |
| 217 | DEF_TEST(SkSLDifferentReturnType, r) { |
| 218 | test_failure(r, |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 219 | "int main() { return 1; } void main() { }", |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 220 | "error: 1: functions 'void main()' and 'int main()' differ only in return type\n1 " |
| 221 | "error\n"); |
| 222 | } |
| 223 | |
| 224 | DEF_TEST(SkSLDifferentModifiers, r) { |
| 225 | test_failure(r, |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 226 | "void test(int x); void test(out int x) { }", |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 227 | "error: 1: modifiers on parameter 1 differ between declaration and definition\n1 " |
| 228 | "error\n"); |
| 229 | } |
| 230 | |
| 231 | DEF_TEST(SkSLDuplicateSymbol, r) { |
| 232 | test_failure(r, |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 233 | "int main; void main() { }", |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 234 | "error: 1: symbol 'main' was already defined\n1 error\n"); |
| 235 | |
| 236 | test_failure(r, |
| 237 | "int x; int x; void main() { }", |
| 238 | "error: 1: symbol 'x' was already defined\n1 error\n"); |
| 239 | |
| 240 | test_success(r, "int x; void main() { int x; }"); |
| 241 | } |
| 242 | |
| 243 | DEF_TEST(SkSLBinaryTypeMismatch, r) { |
| 244 | test_failure(r, |
Ethan Nicholas | 8aa4569 | 2017-09-20 11:24:15 -0400 | [diff] [blame] | 245 | "void main() { float x = 3 * true; }", |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 246 | "error: 1: type mismatch: '*' cannot operate on 'int', 'bool'\n1 error\n"); |
| 247 | test_failure(r, |
| 248 | "void main() { bool x = 1 || 2.0; }", |
Ethan Nicholas | 8aa4569 | 2017-09-20 11:24:15 -0400 | [diff] [blame] | 249 | "error: 1: type mismatch: '||' cannot operate on 'int', 'float'\n1 error\n"); |
Ethan Nicholas | 2346300 | 2018-03-28 15:16:15 -0400 | [diff] [blame] | 250 | test_failure(r, |
| 251 | "void main() { bool x = float2(0) == 0; }", |
| 252 | "error: 1: type mismatch: '==' cannot operate on 'float2', 'int'\n1 error\n"); |
| 253 | test_failure(r, |
| 254 | "void main() { bool x = float2(0) != 0; }", |
| 255 | "error: 1: type mismatch: '!=' cannot operate on 'float2', 'int'\n1 error\n"); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 256 | } |
| 257 | |
| 258 | DEF_TEST(SkSLCallNonFunction, r) { |
| 259 | test_failure(r, |
Ethan Nicholas | 8aa4569 | 2017-09-20 11:24:15 -0400 | [diff] [blame] | 260 | "void main() { float x = 3; x(); }", |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 261 | "error: 1: 'x' is not a function\n1 error\n"); |
| 262 | } |
| 263 | |
| 264 | DEF_TEST(SkSLInvalidUnary, r) { |
| 265 | test_failure(r, |
Ethan Nicholas | 8aa4569 | 2017-09-20 11:24:15 -0400 | [diff] [blame] | 266 | "void main() { float4x4 x = float4x4(1); ++x; }", |
| 267 | "error: 1: '++' cannot operate on 'float4x4'\n1 error\n"); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 268 | test_failure(r, |
Ethan Nicholas | 8aa4569 | 2017-09-20 11:24:15 -0400 | [diff] [blame] | 269 | "void main() { float3 x = float3(1); --x; }", |
| 270 | "error: 1: '--' cannot operate on 'float3'\n1 error\n"); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 271 | test_failure(r, |
Ethan Nicholas | 8aa4569 | 2017-09-20 11:24:15 -0400 | [diff] [blame] | 272 | "void main() { float4x4 x = float4x4(1); x++; }", |
| 273 | "error: 1: '++' cannot operate on 'float4x4'\n1 error\n"); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 274 | test_failure(r, |
Ethan Nicholas | 8aa4569 | 2017-09-20 11:24:15 -0400 | [diff] [blame] | 275 | "void main() { float3 x = float3(1); x--; }", |
| 276 | "error: 1: '--' cannot operate on 'float3'\n1 error\n"); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 277 | test_failure(r, |
| 278 | "void main() { int x = !12; }", |
| 279 | "error: 1: '!' cannot operate on 'int'\n1 error\n"); |
| 280 | test_failure(r, |
| 281 | "struct foo { } bar; void main() { foo x = +bar; }", |
| 282 | "error: 1: '+' cannot operate on 'foo'\n1 error\n"); |
| 283 | test_failure(r, |
| 284 | "struct foo { } bar; void main() { foo x = -bar; }", |
| 285 | "error: 1: '-' cannot operate on 'foo'\n1 error\n"); |
| 286 | test_success(r, |
Ethan Nicholas | 8aa4569 | 2017-09-20 11:24:15 -0400 | [diff] [blame] | 287 | "void main() { float2 x = float2(1, 1); x = +x; x = -x; }"); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 288 | } |
| 289 | |
| 290 | DEF_TEST(SkSLInvalidAssignment, r) { |
| 291 | test_failure(r, |
| 292 | "void main() { 1 = 2; }", |
| 293 | "error: 1: cannot assign to '1'\n1 error\n"); |
| 294 | test_failure(r, |
| 295 | "uniform int x; void main() { x = 0; }", |
| 296 | "error: 1: cannot modify immutable variable 'x'\n1 error\n"); |
| 297 | test_failure(r, |
| 298 | "const int x; void main() { x = 0; }", |
| 299 | "error: 1: cannot modify immutable variable 'x'\n1 error\n"); |
| 300 | } |
| 301 | |
| 302 | DEF_TEST(SkSLBadIndex, r) { |
| 303 | test_failure(r, |
| 304 | "void main() { int x = 2[0]; }", |
| 305 | "error: 1: expected array, but found 'int'\n1 error\n"); |
| 306 | test_failure(r, |
Ethan Nicholas | 8aa4569 | 2017-09-20 11:24:15 -0400 | [diff] [blame] | 307 | "void main() { float2 x = float2(0); int y = x[0][0]; }", |
| 308 | "error: 1: expected array, but found 'float'\n1 error\n"); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 309 | } |
| 310 | |
| 311 | DEF_TEST(SkSLTernaryMismatch, r) { |
| 312 | test_failure(r, |
| 313 | "void main() { int x = 5 > 2 ? true : 1.0; }", |
Ethan Nicholas | 8aa4569 | 2017-09-20 11:24:15 -0400 | [diff] [blame] | 314 | "error: 1: ternary operator result mismatch: 'bool', 'float'\n1 error\n"); |
Ethan Nicholas | 2be687a | 2017-01-03 16:44:39 -0500 | [diff] [blame] | 315 | test_failure(r, |
Ethan Nicholas | 8aa4569 | 2017-09-20 11:24:15 -0400 | [diff] [blame] | 316 | "void main() { int x = 5 > 2 ? float3(1) : 1.0; }", |
| 317 | "error: 1: ternary operator result mismatch: 'float3', 'float'\n1 error\n"); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 318 | } |
| 319 | |
| 320 | DEF_TEST(SkSLInterfaceBlockStorageModifiers, r) { |
| 321 | test_failure(r, |
| 322 | "uniform foo { out int x; };", |
| 323 | "error: 1: interface block fields may not have storage qualifiers\n1 error\n"); |
| 324 | } |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 325 | |
| 326 | DEF_TEST(SkSLUseWithoutInitialize, r) { |
| 327 | test_failure(r, |
| 328 | "void main() { int x; if (5 == 2) x = 3; x++; }", |
| 329 | "error: 1: 'x' has not been assigned\n1 error\n"); |
| 330 | test_failure(r, |
| 331 | "void main() { int x[2][2]; int i; x[i][1] = 4; }", |
| 332 | "error: 1: 'i' has not been assigned\n1 error\n"); |
| 333 | test_failure(r, |
| 334 | "int main() { int r; return r; }", |
| 335 | "error: 1: 'r' has not been assigned\n1 error\n"); |
| 336 | test_failure(r, |
Ethan Nicholas | 82a62d2 | 2017-11-07 14:42:10 +0000 | [diff] [blame] | 337 | "void main() { int x; int y = x; }", |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 338 | "error: 1: 'x' has not been assigned\n1 error\n"); |
| 339 | test_failure(r, |
| 340 | "void main() { bool x; if (true && (false || x)) return; }", |
| 341 | "error: 1: 'x' has not been assigned\n1 error\n"); |
Ethan Nicholas | af19769 | 2017-02-27 13:26:45 -0500 | [diff] [blame] | 342 | test_failure(r, |
| 343 | "void main() { int x; switch (3) { case 0: x = 0; case 1: x = 1; }" |
Ethan Nicholas | 8aa4569 | 2017-09-20 11:24:15 -0400 | [diff] [blame] | 344 | "sk_FragColor = float4(x); }", |
Ethan Nicholas | af19769 | 2017-02-27 13:26:45 -0500 | [diff] [blame] | 345 | "error: 1: 'x' has not been assigned\n1 error\n"); |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 346 | } |
| 347 | |
| 348 | DEF_TEST(SkSLUnreachable, r) { |
| 349 | test_failure(r, |
| 350 | "void main() { return; return; }", |
| 351 | "error: 1: unreachable\n1 error\n"); |
| 352 | test_failure(r, |
| 353 | "void main() { for (;;) { continue; int x = 1; } }", |
| 354 | "error: 1: unreachable\n1 error\n"); |
Ethan Nicholas | d9fe700 | 2017-05-30 10:15:34 -0400 | [diff] [blame] | 355 | /* test_failure(r, |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 356 | "void main() { for (;;) { } return; }", |
Ethan Nicholas | d9fe700 | 2017-05-30 10:15:34 -0400 | [diff] [blame] | 357 | "error: 1: unreachable\n1 error\n");*/ |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 358 | test_failure(r, |
| 359 | "void main() { if (true) return; else discard; return; }", |
| 360 | "error: 1: unreachable\n1 error\n"); |
| 361 | test_failure(r, |
| 362 | "void main() { return; while (true); }", |
| 363 | "error: 1: unreachable\n1 error\n"); |
| 364 | } |
| 365 | |
| 366 | DEF_TEST(SkSLNoReturn, r) { |
| 367 | test_failure(r, |
| 368 | "int foo() { if (2 > 5) return 3; }", |
| 369 | "error: 1: function can exit without returning a value\n1 error\n"); |
| 370 | } |
| 371 | |
| 372 | DEF_TEST(SkSLBreakOutsideLoop, r) { |
| 373 | test_failure(r, |
| 374 | "void foo() { while(true) {} if (true) break; }", |
Ethan Nicholas | af19769 | 2017-02-27 13:26:45 -0500 | [diff] [blame] | 375 | "error: 1: break statement must be inside a loop or switch\n1 error\n"); |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 376 | } |
| 377 | |
| 378 | DEF_TEST(SkSLContinueOutsideLoop, r) { |
| 379 | test_failure(r, |
| 380 | "void foo() { for(;;); continue; }", |
| 381 | "error: 1: continue statement must be inside a loop\n1 error\n"); |
Ethan Nicholas | af19769 | 2017-02-27 13:26:45 -0500 | [diff] [blame] | 382 | test_failure(r, |
| 383 | "void foo() { switch (1) { default: continue; } }", |
| 384 | "error: 1: continue statement must be inside a loop\n1 error\n"); |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 385 | } |
ethannicholas | 08a9211 | 2016-11-09 13:26:45 -0800 | [diff] [blame] | 386 | |
| 387 | DEF_TEST(SkSLStaticIfError, r) { |
| 388 | // ensure eliminated branch of static if / ternary is still checked for errors |
| 389 | test_failure(r, |
| 390 | "void foo() { if (true); else x = 5; }", |
| 391 | "error: 1: unknown identifier 'x'\n1 error\n"); |
| 392 | test_failure(r, |
| 393 | "void foo() { if (false) x = 5; }", |
| 394 | "error: 1: unknown identifier 'x'\n1 error\n"); |
| 395 | test_failure(r, |
| 396 | "void foo() { true ? 5 : x; }", |
| 397 | "error: 1: unknown identifier 'x'\n1 error\n"); |
| 398 | test_failure(r, |
| 399 | "void foo() { false ? x : 5; }", |
| 400 | "error: 1: unknown identifier 'x'\n1 error\n"); |
| 401 | } |
Ethan Nicholas | 7ef4b74 | 2016-11-11 15:16:46 -0500 | [diff] [blame] | 402 | |
Ethan Nicholas | 3605ace | 2016-11-21 15:59:48 -0500 | [diff] [blame] | 403 | DEF_TEST(SkSLBadCap, r) { |
| 404 | test_failure(r, |
| 405 | "bool b = sk_Caps.bugFreeDriver;", |
| 406 | "error: 1: unknown capability flag 'bugFreeDriver'\n1 error\n"); |
| 407 | } |
| 408 | |
Ethan Nicholas | 9a5610e | 2017-01-03 15:16:29 -0500 | [diff] [blame] | 409 | DEF_TEST(SkSLDivByZero, r) { |
| 410 | test_failure(r, |
| 411 | "int x = 1 / 0;", |
| 412 | "error: 1: division by zero\n1 error\n"); |
| 413 | test_failure(r, |
Ethan Nicholas | 8aa4569 | 2017-09-20 11:24:15 -0400 | [diff] [blame] | 414 | "float x = 1 / 0;", |
Ethan Nicholas | 9a5610e | 2017-01-03 15:16:29 -0500 | [diff] [blame] | 415 | "error: 1: division by zero\n1 error\n"); |
| 416 | test_failure(r, |
Ethan Nicholas | 8aa4569 | 2017-09-20 11:24:15 -0400 | [diff] [blame] | 417 | "float x = 1.0 / 0.0;", |
Ethan Nicholas | 9a5610e | 2017-01-03 15:16:29 -0500 | [diff] [blame] | 418 | "error: 1: division by zero\n1 error\n"); |
| 419 | test_failure(r, |
Ethan Nicholas | 8aa4569 | 2017-09-20 11:24:15 -0400 | [diff] [blame] | 420 | "float x = -67.0 / (3.0 - 3);", |
Ethan Nicholas | 9a5610e | 2017-01-03 15:16:29 -0500 | [diff] [blame] | 421 | "error: 1: division by zero\n1 error\n"); |
| 422 | } |
| 423 | |
Ethan Nicholas | 3865711 | 2017-02-09 17:01:22 -0500 | [diff] [blame] | 424 | DEF_TEST(SkSLUnsupportedGLSLIdentifiers, r) { |
| 425 | test_failure(r, |
Ethan Nicholas | 8aa4569 | 2017-09-20 11:24:15 -0400 | [diff] [blame] | 426 | "void main() { float x = gl_FragCoord.x; };", |
Ethan Nicholas | 3865711 | 2017-02-09 17:01:22 -0500 | [diff] [blame] | 427 | "error: 1: unknown identifier 'gl_FragCoord'\n1 error\n"); |
| 428 | test_failure(r, |
Ethan Nicholas | 8aa4569 | 2017-09-20 11:24:15 -0400 | [diff] [blame] | 429 | "void main() { float r = gl_FragColor.r; };", |
Ethan Nicholas | 3865711 | 2017-02-09 17:01:22 -0500 | [diff] [blame] | 430 | "error: 1: unknown identifier 'gl_FragColor'\n1 error\n"); |
| 431 | } |
| 432 | |
Ethan Nicholas | af19769 | 2017-02-27 13:26:45 -0500 | [diff] [blame] | 433 | DEF_TEST(SkSLWrongSwitchTypes, r) { |
| 434 | test_failure(r, |
Ethan Nicholas | 8aa4569 | 2017-09-20 11:24:15 -0400 | [diff] [blame] | 435 | "void main() { switch (float2(1)) { case 1: break; } }", |
| 436 | "error: 1: expected 'int', but found 'float2'\n1 error\n"); |
Ethan Nicholas | af19769 | 2017-02-27 13:26:45 -0500 | [diff] [blame] | 437 | test_failure(r, |
Ethan Nicholas | 8aa4569 | 2017-09-20 11:24:15 -0400 | [diff] [blame] | 438 | "void main() { switch (1) { case float2(1): break; } }", |
| 439 | "error: 1: expected 'int', but found 'float2'\n1 error\n"); |
Ethan Nicholas | af19769 | 2017-02-27 13:26:45 -0500 | [diff] [blame] | 440 | } |
| 441 | |
| 442 | DEF_TEST(SkSLNonConstantCase, r) { |
| 443 | test_failure(r, |
| 444 | "void main() { int x = 1; switch (1) { case x: break; } }", |
| 445 | "error: 1: case value must be a constant\n1 error\n"); |
| 446 | } |
| 447 | |
| 448 | DEF_TEST(SkSLDuplicateCase, r) { |
| 449 | test_failure(r, |
| 450 | "void main() { switch (1) { case 0: case 1: case 0: break; } }", |
| 451 | "error: 1: duplicate case value\n1 error\n"); |
| 452 | } |
| 453 | |
Ethan Nicholas | 0dd30d9 | 2017-05-01 16:57:07 -0400 | [diff] [blame] | 454 | DEF_TEST(SkSLFieldAfterRuntimeArray, r) { |
| 455 | test_failure(r, |
Ethan Nicholas | 8aa4569 | 2017-09-20 11:24:15 -0400 | [diff] [blame] | 456 | "buffer broken { float x[]; float y; };", |
Ethan Nicholas | 0dd30d9 | 2017-05-01 16:57:07 -0400 | [diff] [blame] | 457 | "error: 1: only the last entry in an interface block may be a runtime-sized " |
| 458 | "array\n1 error\n"); |
| 459 | } |
| 460 | |
Ethan Nicholas | 5ac13c2 | 2017-05-10 15:06:17 -0400 | [diff] [blame] | 461 | DEF_TEST(SkSLStaticIf, r) { |
| 462 | test_success(r, |
Ethan Nicholas | 8aa4569 | 2017-09-20 11:24:15 -0400 | [diff] [blame] | 463 | "void main() { float x = 5; float y = 10;" |
| 464 | "@if (x < y) { sk_FragColor = float4(1); } }"); |
Ethan Nicholas | 5ac13c2 | 2017-05-10 15:06:17 -0400 | [diff] [blame] | 465 | test_failure(r, |
Ethan Nicholas | 8aa4569 | 2017-09-20 11:24:15 -0400 | [diff] [blame] | 466 | "void main() { float x = sqrt(25); float y = 10;" |
| 467 | "@if (x < y) { sk_FragColor = float4(1); } }", |
Ethan Nicholas | 5ac13c2 | 2017-05-10 15:06:17 -0400 | [diff] [blame] | 468 | "error: 1: static if has non-static test\n1 error\n"); |
| 469 | } |
| 470 | |
| 471 | DEF_TEST(SkSLStaticSwitch, r) { |
| 472 | test_success(r, |
| 473 | "void main() {" |
| 474 | "int x = 1;" |
| 475 | "@switch (x) {" |
Ethan Nicholas | 8aa4569 | 2017-09-20 11:24:15 -0400 | [diff] [blame] | 476 | "case 1: sk_FragColor = float4(1); break;" |
| 477 | "default: sk_FragColor = float4(0);" |
Ethan Nicholas | 5ac13c2 | 2017-05-10 15:06:17 -0400 | [diff] [blame] | 478 | "}" |
| 479 | "}"); |
| 480 | test_failure(r, |
| 481 | "void main() {" |
| 482 | "int x = int(sqrt(1));" |
| 483 | "@switch (x) {" |
Ethan Nicholas | 8aa4569 | 2017-09-20 11:24:15 -0400 | [diff] [blame] | 484 | "case 1: sk_FragColor = float4(1); break;" |
| 485 | "default: sk_FragColor = float4(0);" |
Ethan Nicholas | 5ac13c2 | 2017-05-10 15:06:17 -0400 | [diff] [blame] | 486 | "}" |
| 487 | "}", |
| 488 | "error: 1: static switch has non-static test\n1 error\n"); |
| 489 | test_failure(r, |
| 490 | "void main() {" |
| 491 | "int x = 1;" |
| 492 | "@switch (x) {" |
Ethan Nicholas | 8aa4569 | 2017-09-20 11:24:15 -0400 | [diff] [blame] | 493 | "case 1: sk_FragColor = float4(1); if (sqrt(0) < sqrt(1)) break;" |
| 494 | "default: sk_FragColor = float4(0);" |
Ethan Nicholas | 5ac13c2 | 2017-05-10 15:06:17 -0400 | [diff] [blame] | 495 | "}" |
| 496 | "}", |
| 497 | "error: 1: static switch contains non-static conditional break\n1 error\n"); |
| 498 | } |
| 499 | |
Ethan Nicholas | 68dd2c1 | 2018-03-01 15:05:17 -0500 | [diff] [blame] | 500 | DEF_TEST(SkSLInterfaceBlockScope, r) { |
| 501 | test_failure(r, |
| 502 | "uniform testBlock {" |
| 503 | "float x;" |
| 504 | "} test[x];", |
| 505 | "error: 1: unknown identifier 'x'\n1 error\n"); |
| 506 | } |
| 507 | |
Ethan Nicholas | 6c94271 | 2018-03-16 09:45:11 -0400 | [diff] [blame] | 508 | DEF_TEST(SkSLDuplicateOutput, r) { |
| 509 | test_failure(r, |
| 510 | "layout (location=0, index=0) out half4 duplicateOutput;", |
| 511 | "error: 1: out location=0, index=0 is reserved for sk_FragColor\n1 error\n"); |
| 512 | } |