Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 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/tint/resolver/resolver.h" |
| 16 | |
| 17 | #include "gmock/gmock.h" |
| 18 | #include "gtest/gtest-spi.h" |
| 19 | #include "src/tint/ast/assignment_statement.h" |
| 20 | #include "src/tint/ast/bitcast_expression.h" |
| 21 | #include "src/tint/ast/break_statement.h" |
| 22 | #include "src/tint/ast/builtin_texture_helper_test.h" |
| 23 | #include "src/tint/ast/call_statement.h" |
| 24 | #include "src/tint/ast/continue_statement.h" |
| 25 | #include "src/tint/ast/discard_statement.h" |
| 26 | #include "src/tint/ast/if_statement.h" |
| 27 | #include "src/tint/ast/loop_statement.h" |
| 28 | #include "src/tint/ast/return_statement.h" |
| 29 | #include "src/tint/ast/stage_attribute.h" |
| 30 | #include "src/tint/ast/switch_statement.h" |
| 31 | #include "src/tint/ast/unary_op_expression.h" |
| 32 | #include "src/tint/ast/variable_decl_statement.h" |
| 33 | #include "src/tint/resolver/resolver_test_helper.h" |
| 34 | #include "src/tint/sem/call.h" |
| 35 | #include "src/tint/sem/function.h" |
| 36 | #include "src/tint/sem/member_accessor_expression.h" |
Tint team | f462606 | 2022-05-06 08:50:33 +0000 | [diff] [blame] | 37 | #include "src/tint/sem/sampled_texture.h" |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 38 | #include "src/tint/sem/statement.h" |
| 39 | #include "src/tint/sem/variable.h" |
| 40 | |
| 41 | using ::testing::ElementsAre; |
| 42 | using ::testing::HasSubstr; |
| 43 | |
Tint team | f462606 | 2022-05-06 08:50:33 +0000 | [diff] [blame] | 44 | using namespace tint::number_suffixes; // NOLINT |
| 45 | |
Tint team | d2323c5 | 2022-04-11 20:49:10 +0000 | [diff] [blame] | 46 | namespace tint::resolver { |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 47 | namespace { |
| 48 | |
| 49 | using ResolverValidationTest = ResolverTest; |
| 50 | |
Ben Clayton | 41f8d2a | 2022-03-07 18:37:46 +0000 | [diff] [blame] | 51 | class FakeStmt final : public Castable<FakeStmt, ast::Statement> { |
Tint team | f462606 | 2022-05-06 08:50:33 +0000 | [diff] [blame] | 52 | public: |
| 53 | FakeStmt(ProgramID pid, Source src) : Base(pid, src) {} |
| 54 | FakeStmt* Clone(CloneContext*) const override { return nullptr; } |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 55 | }; |
| 56 | |
Ben Clayton | 41f8d2a | 2022-03-07 18:37:46 +0000 | [diff] [blame] | 57 | class FakeExpr final : public Castable<FakeExpr, ast::Expression> { |
Tint team | f462606 | 2022-05-06 08:50:33 +0000 | [diff] [blame] | 58 | public: |
| 59 | FakeExpr(ProgramID pid, Source src) : Base(pid, src) {} |
| 60 | FakeExpr* Clone(CloneContext*) const override { return nullptr; } |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 61 | }; |
| 62 | |
| 63 | TEST_F(ResolverValidationTest, WorkgroupMemoryUsedInVertexStage) { |
Tint team | f462606 | 2022-05-06 08:50:33 +0000 | [diff] [blame] | 64 | Global(Source{{1, 2}}, "wg", ty.vec4<f32>(), ast::StorageClass::kWorkgroup); |
| 65 | Global("dst", ty.vec4<f32>(), ast::StorageClass::kPrivate); |
| 66 | auto* stmt = Assign(Expr("dst"), Expr(Source{{3, 4}}, "wg")); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 67 | |
Tint team | f462606 | 2022-05-06 08:50:33 +0000 | [diff] [blame] | 68 | Func(Source{{9, 10}}, "f0", ast::VariableList{}, ty.vec4<f32>(), {stmt, Return(Expr("dst"))}, |
| 69 | ast::AttributeList{Stage(ast::PipelineStage::kVertex)}, |
| 70 | ast::AttributeList{Builtin(ast::Builtin::kPosition)}); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 71 | |
Tint team | f462606 | 2022-05-06 08:50:33 +0000 | [diff] [blame] | 72 | EXPECT_FALSE(r()->Resolve()); |
| 73 | EXPECT_EQ(r()->error(), |
| 74 | "3:4 error: workgroup memory cannot be used by vertex pipeline " |
| 75 | "stage\n1:2 note: variable is declared here"); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | TEST_F(ResolverValidationTest, WorkgroupMemoryUsedInFragmentStage) { |
Tint team | f462606 | 2022-05-06 08:50:33 +0000 | [diff] [blame] | 79 | // var<workgroup> wg : vec4<f32>; |
| 80 | // var<workgroup> dst : vec4<f32>; |
| 81 | // fn f2(){ dst = wg; } |
| 82 | // fn f1() { f2(); } |
| 83 | // @stage(fragment) |
| 84 | // fn f0() { |
| 85 | // f1(); |
| 86 | //} |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 87 | |
Tint team | f462606 | 2022-05-06 08:50:33 +0000 | [diff] [blame] | 88 | Global(Source{{1, 2}}, "wg", ty.vec4<f32>(), ast::StorageClass::kWorkgroup); |
| 89 | Global("dst", ty.vec4<f32>(), ast::StorageClass::kPrivate); |
| 90 | auto* stmt = Assign(Expr("dst"), Expr(Source{{3, 4}}, "wg")); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 91 | |
Tint team | f462606 | 2022-05-06 08:50:33 +0000 | [diff] [blame] | 92 | Func(Source{{5, 6}}, "f2", {}, ty.void_(), {stmt}); |
| 93 | Func(Source{{7, 8}}, "f1", {}, ty.void_(), {CallStmt(Call("f2"))}); |
| 94 | Func(Source{{9, 10}}, "f0", {}, ty.void_(), {CallStmt(Call("f1"))}, |
| 95 | ast::AttributeList{Stage(ast::PipelineStage::kFragment)}); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 96 | |
Tint team | f462606 | 2022-05-06 08:50:33 +0000 | [diff] [blame] | 97 | EXPECT_FALSE(r()->Resolve()); |
| 98 | EXPECT_EQ(r()->error(), |
| 99 | R"(3:4 error: workgroup memory cannot be used by fragment pipeline stage |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 100 | 1:2 note: variable is declared here |
| 101 | 5:6 note: called by function 'f2' |
| 102 | 7:8 note: called by function 'f1' |
| 103 | 9:10 note: called by entry point 'f0')"); |
| 104 | } |
| 105 | |
| 106 | TEST_F(ResolverValidationTest, UnhandledStmt) { |
Tint team | f462606 | 2022-05-06 08:50:33 +0000 | [diff] [blame] | 107 | EXPECT_FATAL_FAILURE( |
| 108 | { |
| 109 | ProgramBuilder b; |
| 110 | b.WrapInFunction(b.create<FakeStmt>()); |
| 111 | Program(std::move(b)); |
| 112 | }, |
| 113 | "internal compiler error: unhandled node type: tint::resolver::FakeStmt"); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | TEST_F(ResolverValidationTest, Stmt_If_NonBool) { |
Tint team | f462606 | 2022-05-06 08:50:33 +0000 | [diff] [blame] | 117 | // if (1.23f) {} |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 118 | |
Tint team | 2e186f2 | 2022-05-11 08:42:48 +0000 | [diff] [blame] | 119 | WrapInFunction(If(Expr(Source{{12, 34}}, 1.23_f), Block())); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 120 | |
Tint team | f462606 | 2022-05-06 08:50:33 +0000 | [diff] [blame] | 121 | EXPECT_FALSE(r()->Resolve()); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 122 | |
Tint team | f462606 | 2022-05-06 08:50:33 +0000 | [diff] [blame] | 123 | EXPECT_EQ(r()->error(), "12:34 error: if statement condition must be bool, got f32"); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 124 | } |
| 125 | |
Tint team | f462606 | 2022-05-06 08:50:33 +0000 | [diff] [blame] | 126 | TEST_F(ResolverValidationTest, Stmt_ElseIf_NonBool) { |
| 127 | // else if (1.23f) {} |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 128 | |
Tint team | 2e186f2 | 2022-05-11 08:42:48 +0000 | [diff] [blame] | 129 | WrapInFunction(If(Expr(true), Block(), Else(If(Expr(Source{{12, 34}}, 1.23_f), Block())))); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 130 | |
Tint team | f462606 | 2022-05-06 08:50:33 +0000 | [diff] [blame] | 131 | EXPECT_FALSE(r()->Resolve()); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 132 | |
Tint team | f462606 | 2022-05-06 08:50:33 +0000 | [diff] [blame] | 133 | EXPECT_EQ(r()->error(), "12:34 error: if statement condition must be bool, got f32"); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 134 | } |
| 135 | |
| 136 | TEST_F(ResolverValidationTest, Expr_ErrUnknownExprType) { |
Tint team | f462606 | 2022-05-06 08:50:33 +0000 | [diff] [blame] | 137 | EXPECT_FATAL_FAILURE( |
| 138 | { |
| 139 | ProgramBuilder b; |
| 140 | b.WrapInFunction(b.create<FakeExpr>()); |
| 141 | Resolver(&b).Resolve(); |
| 142 | }, |
| 143 | "internal compiler error: unhandled expression type: " |
| 144 | "tint::resolver::FakeExpr"); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 145 | } |
| 146 | |
| 147 | TEST_F(ResolverValidationTest, Expr_DontCall_Function) { |
Tint team | f462606 | 2022-05-06 08:50:33 +0000 | [diff] [blame] | 148 | Func("func", {}, ty.void_(), {}, {}); |
| 149 | WrapInFunction(Expr(Source{{{3, 3}, {3, 8}}}, "func")); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 150 | |
Tint team | f462606 | 2022-05-06 08:50:33 +0000 | [diff] [blame] | 151 | EXPECT_FALSE(r()->Resolve()); |
| 152 | EXPECT_EQ(r()->error(), "3:8 error: missing '(' for function call"); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 153 | } |
| 154 | |
| 155 | TEST_F(ResolverValidationTest, Expr_DontCall_Builtin) { |
Tint team | f462606 | 2022-05-06 08:50:33 +0000 | [diff] [blame] | 156 | WrapInFunction(Expr(Source{{{3, 3}, {3, 8}}}, "round")); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 157 | |
Tint team | f462606 | 2022-05-06 08:50:33 +0000 | [diff] [blame] | 158 | EXPECT_FALSE(r()->Resolve()); |
| 159 | EXPECT_EQ(r()->error(), "3:8 error: missing '(' for builtin call"); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 160 | } |
| 161 | |
| 162 | TEST_F(ResolverValidationTest, Expr_DontCall_Type) { |
Tint team | f462606 | 2022-05-06 08:50:33 +0000 | [diff] [blame] | 163 | Alias("T", ty.u32()); |
| 164 | WrapInFunction(Expr(Source{{{3, 3}, {3, 8}}}, "T")); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 165 | |
Tint team | f462606 | 2022-05-06 08:50:33 +0000 | [diff] [blame] | 166 | EXPECT_FALSE(r()->Resolve()); |
| 167 | EXPECT_EQ(r()->error(), "3:8 error: missing '(' for type constructor or cast"); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 168 | } |
| 169 | |
| 170 | TEST_F(ResolverValidationTest, AssignmentStmt_InvalidLHS_BuiltinFunctionName) { |
Tint team | f462606 | 2022-05-06 08:50:33 +0000 | [diff] [blame] | 171 | // normalize = 2; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 172 | |
Tint team | f462606 | 2022-05-06 08:50:33 +0000 | [diff] [blame] | 173 | auto* lhs = Expr(Source{{12, 34}}, "normalize"); |
| 174 | auto* rhs = Expr(2_i); |
| 175 | auto* assign = Assign(lhs, rhs); |
| 176 | WrapInFunction(assign); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 177 | |
Tint team | f462606 | 2022-05-06 08:50:33 +0000 | [diff] [blame] | 178 | EXPECT_FALSE(r()->Resolve()); |
| 179 | EXPECT_EQ(r()->error(), "12:34 error: missing '(' for builtin call"); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 180 | } |
| 181 | |
| 182 | TEST_F(ResolverValidationTest, UsingUndefinedVariable_Fail) { |
Tint team | f462606 | 2022-05-06 08:50:33 +0000 | [diff] [blame] | 183 | // b = 2; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 184 | |
Tint team | f462606 | 2022-05-06 08:50:33 +0000 | [diff] [blame] | 185 | auto* lhs = Expr(Source{{12, 34}}, "b"); |
| 186 | auto* rhs = Expr(2_i); |
| 187 | auto* assign = Assign(lhs, rhs); |
| 188 | WrapInFunction(assign); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 189 | |
Tint team | f462606 | 2022-05-06 08:50:33 +0000 | [diff] [blame] | 190 | EXPECT_FALSE(r()->Resolve()); |
| 191 | EXPECT_EQ(r()->error(), "12:34 error: unknown identifier: 'b'"); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 192 | } |
| 193 | |
| 194 | TEST_F(ResolverValidationTest, UsingUndefinedVariableInBlockStatement_Fail) { |
Tint team | f462606 | 2022-05-06 08:50:33 +0000 | [diff] [blame] | 195 | // { |
| 196 | // b = 2; |
| 197 | // } |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 198 | |
Tint team | f462606 | 2022-05-06 08:50:33 +0000 | [diff] [blame] | 199 | auto* lhs = Expr(Source{{12, 34}}, "b"); |
| 200 | auto* rhs = Expr(2_i); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 201 | |
Tint team | f462606 | 2022-05-06 08:50:33 +0000 | [diff] [blame] | 202 | auto* body = Block(Assign(lhs, rhs)); |
| 203 | WrapInFunction(body); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 204 | |
Tint team | f462606 | 2022-05-06 08:50:33 +0000 | [diff] [blame] | 205 | EXPECT_FALSE(r()->Resolve()); |
| 206 | EXPECT_EQ(r()->error(), "12:34 error: unknown identifier: 'b'"); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 207 | } |
| 208 | |
| 209 | TEST_F(ResolverValidationTest, UsingUndefinedVariableGlobalVariable_Pass) { |
Tint team | f462606 | 2022-05-06 08:50:33 +0000 | [diff] [blame] | 210 | // var global_var: f32 = 2.1; |
| 211 | // fn my_func() { |
| 212 | // global_var = 3.14; |
| 213 | // return; |
| 214 | // } |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 215 | |
Tint team | 2e186f2 | 2022-05-11 08:42:48 +0000 | [diff] [blame] | 216 | Global("global_var", ty.f32(), ast::StorageClass::kPrivate, Expr(2.1_f)); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 217 | |
Tint team | f462606 | 2022-05-06 08:50:33 +0000 | [diff] [blame] | 218 | Func("my_func", ast::VariableList{}, ty.void_(), |
| 219 | { |
Tint team | 2e186f2 | 2022-05-11 08:42:48 +0000 | [diff] [blame] | 220 | Assign(Expr(Source{{12, 34}}, "global_var"), 3.14_f), |
Tint team | f462606 | 2022-05-06 08:50:33 +0000 | [diff] [blame] | 221 | Return(), |
| 222 | }); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 223 | |
Tint team | f462606 | 2022-05-06 08:50:33 +0000 | [diff] [blame] | 224 | EXPECT_TRUE(r()->Resolve()) << r()->error(); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 225 | } |
| 226 | |
| 227 | TEST_F(ResolverValidationTest, UsingUndefinedVariableInnerScope_Fail) { |
Tint team | f462606 | 2022-05-06 08:50:33 +0000 | [diff] [blame] | 228 | // { |
| 229 | // if (true) { var a : f32 = 2.0; } |
| 230 | // a = 3.14; |
| 231 | // } |
Tint team | 2e186f2 | 2022-05-11 08:42:48 +0000 | [diff] [blame] | 232 | auto* var = Var("a", ty.f32(), ast::StorageClass::kNone, Expr(2_f)); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 233 | |
Tint team | f462606 | 2022-05-06 08:50:33 +0000 | [diff] [blame] | 234 | auto* cond = Expr(true); |
| 235 | auto* body = Block(Decl(var)); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 236 | |
Tint team | f462606 | 2022-05-06 08:50:33 +0000 | [diff] [blame] | 237 | SetSource(Source{{12, 34}}); |
| 238 | auto* lhs = Expr(Source{{12, 34}}, "a"); |
Tint team | 2e186f2 | 2022-05-11 08:42:48 +0000 | [diff] [blame] | 239 | auto* rhs = Expr(3.14_f); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 240 | |
Tint team | f462606 | 2022-05-06 08:50:33 +0000 | [diff] [blame] | 241 | auto* outer_body = Block(If(cond, body), Assign(lhs, rhs)); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 242 | |
Tint team | f462606 | 2022-05-06 08:50:33 +0000 | [diff] [blame] | 243 | WrapInFunction(outer_body); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 244 | |
Tint team | f462606 | 2022-05-06 08:50:33 +0000 | [diff] [blame] | 245 | EXPECT_FALSE(r()->Resolve()); |
| 246 | EXPECT_EQ(r()->error(), "12:34 error: unknown identifier: 'a'"); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 247 | } |
| 248 | |
| 249 | TEST_F(ResolverValidationTest, UsingUndefinedVariableOuterScope_Pass) { |
Tint team | f462606 | 2022-05-06 08:50:33 +0000 | [diff] [blame] | 250 | // { |
| 251 | // var a : f32 = 2.0; |
| 252 | // if (true) { a = 3.14; } |
| 253 | // } |
Tint team | 2e186f2 | 2022-05-11 08:42:48 +0000 | [diff] [blame] | 254 | auto* var = Var("a", ty.f32(), ast::StorageClass::kNone, Expr(2_f)); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 255 | |
Tint team | f462606 | 2022-05-06 08:50:33 +0000 | [diff] [blame] | 256 | auto* lhs = Expr(Source{{12, 34}}, "a"); |
Tint team | 2e186f2 | 2022-05-11 08:42:48 +0000 | [diff] [blame] | 257 | auto* rhs = Expr(3.14_f); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 258 | |
Tint team | f462606 | 2022-05-06 08:50:33 +0000 | [diff] [blame] | 259 | auto* cond = Expr(true); |
| 260 | auto* body = Block(Assign(lhs, rhs)); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 261 | |
Tint team | f462606 | 2022-05-06 08:50:33 +0000 | [diff] [blame] | 262 | auto* outer_body = Block(Decl(var), If(cond, body)); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 263 | |
Tint team | f462606 | 2022-05-06 08:50:33 +0000 | [diff] [blame] | 264 | WrapInFunction(outer_body); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 265 | |
Tint team | f462606 | 2022-05-06 08:50:33 +0000 | [diff] [blame] | 266 | EXPECT_TRUE(r()->Resolve()) << r()->error(); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 267 | } |
| 268 | |
| 269 | TEST_F(ResolverValidationTest, UsingUndefinedVariableDifferentScope_Fail) { |
Tint team | f462606 | 2022-05-06 08:50:33 +0000 | [diff] [blame] | 270 | // { |
| 271 | // { var a : f32 = 2.0; } |
| 272 | // { a = 3.14; } |
| 273 | // } |
Tint team | 2e186f2 | 2022-05-11 08:42:48 +0000 | [diff] [blame] | 274 | auto* var = Var("a", ty.f32(), ast::StorageClass::kNone, Expr(2_f)); |
Tint team | f462606 | 2022-05-06 08:50:33 +0000 | [diff] [blame] | 275 | auto* first_body = Block(Decl(var)); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 276 | |
Tint team | f462606 | 2022-05-06 08:50:33 +0000 | [diff] [blame] | 277 | auto* lhs = Expr(Source{{12, 34}}, "a"); |
Tint team | 2e186f2 | 2022-05-11 08:42:48 +0000 | [diff] [blame] | 278 | auto* rhs = Expr(3.14_f); |
Tint team | f462606 | 2022-05-06 08:50:33 +0000 | [diff] [blame] | 279 | auto* second_body = Block(Assign(lhs, rhs)); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 280 | |
Tint team | f462606 | 2022-05-06 08:50:33 +0000 | [diff] [blame] | 281 | auto* outer_body = Block(first_body, second_body); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 282 | |
Tint team | f462606 | 2022-05-06 08:50:33 +0000 | [diff] [blame] | 283 | WrapInFunction(outer_body); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 284 | |
Tint team | f462606 | 2022-05-06 08:50:33 +0000 | [diff] [blame] | 285 | EXPECT_FALSE(r()->Resolve()); |
| 286 | EXPECT_EQ(r()->error(), "12:34 error: unknown identifier: 'a'"); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 287 | } |
| 288 | |
| 289 | TEST_F(ResolverValidationTest, StorageClass_FunctionVariableWorkgroupClass) { |
Tint team | f462606 | 2022-05-06 08:50:33 +0000 | [diff] [blame] | 290 | auto* var = Var("var", ty.i32(), ast::StorageClass::kWorkgroup); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 291 | |
Tint team | f462606 | 2022-05-06 08:50:33 +0000 | [diff] [blame] | 292 | auto* stmt = Decl(var); |
| 293 | Func("func", ast::VariableList{}, ty.void_(), {stmt}, ast::AttributeList{}); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 294 | |
Tint team | f462606 | 2022-05-06 08:50:33 +0000 | [diff] [blame] | 295 | EXPECT_FALSE(r()->Resolve()); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 296 | |
Tint team | f462606 | 2022-05-06 08:50:33 +0000 | [diff] [blame] | 297 | EXPECT_EQ(r()->error(), "error: function variable has a non-function storage class"); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 298 | } |
| 299 | |
| 300 | TEST_F(ResolverValidationTest, StorageClass_FunctionVariableI32) { |
Tint team | f462606 | 2022-05-06 08:50:33 +0000 | [diff] [blame] | 301 | auto* var = Var("s", ty.i32(), ast::StorageClass::kPrivate); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 302 | |
Tint team | f462606 | 2022-05-06 08:50:33 +0000 | [diff] [blame] | 303 | auto* stmt = Decl(var); |
| 304 | Func("func", ast::VariableList{}, ty.void_(), {stmt}, ast::AttributeList{}); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 305 | |
Tint team | f462606 | 2022-05-06 08:50:33 +0000 | [diff] [blame] | 306 | EXPECT_FALSE(r()->Resolve()); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 307 | |
Tint team | f462606 | 2022-05-06 08:50:33 +0000 | [diff] [blame] | 308 | EXPECT_EQ(r()->error(), "error: function variable has a non-function storage class"); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 309 | } |
| 310 | |
| 311 | TEST_F(ResolverValidationTest, StorageClass_SamplerExplicitStorageClass) { |
Tint team | f462606 | 2022-05-06 08:50:33 +0000 | [diff] [blame] | 312 | auto* t = ty.sampler(ast::SamplerKind::kSampler); |
| 313 | Global(Source{{12, 34}}, "var", t, ast::StorageClass::kHandle, |
| 314 | ast::AttributeList{ |
| 315 | create<ast::BindingAttribute>(0), |
| 316 | create<ast::GroupAttribute>(0), |
| 317 | }); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 318 | |
Tint team | f462606 | 2022-05-06 08:50:33 +0000 | [diff] [blame] | 319 | EXPECT_FALSE(r()->Resolve()); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 320 | |
Tint team | f462606 | 2022-05-06 08:50:33 +0000 | [diff] [blame] | 321 | EXPECT_EQ(r()->error(), |
| 322 | R"(12:34 error: variables of type 'sampler' must not have a storage class)"); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 323 | } |
| 324 | |
| 325 | TEST_F(ResolverValidationTest, StorageClass_TextureExplicitStorageClass) { |
Tint team | f462606 | 2022-05-06 08:50:33 +0000 | [diff] [blame] | 326 | auto* t = ty.sampled_texture(ast::TextureDimension::k1d, ty.f32()); |
| 327 | Global(Source{{12, 34}}, "var", t, ast::StorageClass::kHandle, |
| 328 | ast::AttributeList{ |
| 329 | create<ast::BindingAttribute>(0), |
| 330 | create<ast::GroupAttribute>(0), |
| 331 | }); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 332 | |
Tint team | f462606 | 2022-05-06 08:50:33 +0000 | [diff] [blame] | 333 | EXPECT_FALSE(r()->Resolve()) << r()->error(); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 334 | |
Tint team | f462606 | 2022-05-06 08:50:33 +0000 | [diff] [blame] | 335 | EXPECT_EQ(r()->error(), |
| 336 | R"(12:34 error: variables of type 'texture_1d<f32>' must not have a storage class)"); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 337 | } |
| 338 | |
| 339 | TEST_F(ResolverValidationTest, Expr_MemberAccessor_VectorSwizzle_BadChar) { |
Tint team | f462606 | 2022-05-06 08:50:33 +0000 | [diff] [blame] | 340 | Global("my_vec", ty.vec3<f32>(), ast::StorageClass::kPrivate); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 341 | |
Tint team | f462606 | 2022-05-06 08:50:33 +0000 | [diff] [blame] | 342 | auto* ident = Expr(Source{{{3, 3}, {3, 7}}}, "xyqz"); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 343 | |
Tint team | f462606 | 2022-05-06 08:50:33 +0000 | [diff] [blame] | 344 | auto* mem = MemberAccessor("my_vec", ident); |
| 345 | WrapInFunction(mem); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 346 | |
Tint team | f462606 | 2022-05-06 08:50:33 +0000 | [diff] [blame] | 347 | EXPECT_FALSE(r()->Resolve()); |
| 348 | EXPECT_EQ(r()->error(), "3:5 error: invalid vector swizzle character"); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 349 | } |
| 350 | |
| 351 | TEST_F(ResolverValidationTest, Expr_MemberAccessor_VectorSwizzle_MixedChars) { |
Tint team | f462606 | 2022-05-06 08:50:33 +0000 | [diff] [blame] | 352 | Global("my_vec", ty.vec4<f32>(), ast::StorageClass::kPrivate); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 353 | |
Tint team | f462606 | 2022-05-06 08:50:33 +0000 | [diff] [blame] | 354 | auto* ident = Expr(Source{{{3, 3}, {3, 7}}}, "rgyw"); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 355 | |
Tint team | f462606 | 2022-05-06 08:50:33 +0000 | [diff] [blame] | 356 | auto* mem = MemberAccessor("my_vec", ident); |
| 357 | WrapInFunction(mem); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 358 | |
Tint team | f462606 | 2022-05-06 08:50:33 +0000 | [diff] [blame] | 359 | EXPECT_FALSE(r()->Resolve()); |
| 360 | EXPECT_EQ(r()->error(), |
| 361 | "3:3 error: invalid mixing of vector swizzle characters rgba with xyzw"); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 362 | } |
| 363 | |
| 364 | TEST_F(ResolverValidationTest, Expr_MemberAccessor_VectorSwizzle_BadLength) { |
Tint team | f462606 | 2022-05-06 08:50:33 +0000 | [diff] [blame] | 365 | Global("my_vec", ty.vec3<f32>(), ast::StorageClass::kPrivate); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 366 | |
Tint team | f462606 | 2022-05-06 08:50:33 +0000 | [diff] [blame] | 367 | auto* ident = Expr(Source{{{3, 3}, {3, 8}}}, "zzzzz"); |
| 368 | auto* mem = MemberAccessor("my_vec", ident); |
| 369 | WrapInFunction(mem); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 370 | |
Tint team | f462606 | 2022-05-06 08:50:33 +0000 | [diff] [blame] | 371 | EXPECT_FALSE(r()->Resolve()); |
| 372 | EXPECT_EQ(r()->error(), "3:3 error: invalid vector swizzle size"); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 373 | } |
| 374 | |
| 375 | TEST_F(ResolverValidationTest, Expr_MemberAccessor_VectorSwizzle_BadIndex) { |
Tint team | f462606 | 2022-05-06 08:50:33 +0000 | [diff] [blame] | 376 | Global("my_vec", ty.vec2<f32>(), ast::StorageClass::kPrivate); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 377 | |
Tint team | f462606 | 2022-05-06 08:50:33 +0000 | [diff] [blame] | 378 | auto* ident = Expr(Source{{3, 3}}, "z"); |
| 379 | auto* mem = MemberAccessor("my_vec", ident); |
| 380 | WrapInFunction(mem); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 381 | |
Tint team | f462606 | 2022-05-06 08:50:33 +0000 | [diff] [blame] | 382 | EXPECT_FALSE(r()->Resolve()); |
| 383 | EXPECT_EQ(r()->error(), "3:3 error: invalid vector swizzle member"); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 384 | } |
| 385 | |
| 386 | TEST_F(ResolverValidationTest, Expr_MemberAccessor_BadParent) { |
Tint team | f462606 | 2022-05-06 08:50:33 +0000 | [diff] [blame] | 387 | // var param: vec4<f32> |
| 388 | // let ret: f32 = *(¶m).x; |
| 389 | auto* param = Var("param", ty.vec4<f32>()); |
| 390 | auto* x = Expr(Source{{{3, 3}, {3, 8}}}, "x"); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 391 | |
Tint team | f462606 | 2022-05-06 08:50:33 +0000 | [diff] [blame] | 392 | auto* addressOf_expr = AddressOf(Source{{12, 34}}, param); |
| 393 | auto* accessor_expr = MemberAccessor(addressOf_expr, x); |
| 394 | auto* star_p = Deref(accessor_expr); |
| 395 | auto* ret = Var("r", ty.f32(), star_p); |
| 396 | WrapInFunction(Decl(param), Decl(ret)); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 397 | |
Tint team | f462606 | 2022-05-06 08:50:33 +0000 | [diff] [blame] | 398 | EXPECT_FALSE(r()->Resolve()); |
| 399 | EXPECT_EQ(r()->error(), |
| 400 | "12:34 error: invalid member accessor expression. Expected vector " |
| 401 | "or struct, got 'ptr<function, vec4<f32>, read_write>'"); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 402 | } |
| 403 | |
| 404 | TEST_F(ResolverValidationTest, EXpr_MemberAccessor_FuncGoodParent) { |
Tint team | f462606 | 2022-05-06 08:50:33 +0000 | [diff] [blame] | 405 | // fn func(p: ptr<function, vec4<f32>>) -> f32 { |
| 406 | // let x: f32 = (*p).z; |
| 407 | // return x; |
| 408 | // } |
| 409 | auto* p = Param("p", ty.pointer(ty.vec4<f32>(), ast::StorageClass::kFunction)); |
| 410 | auto* star_p = Deref(p); |
| 411 | auto* z = Expr(Source{{{3, 3}, {3, 8}}}, "z"); |
| 412 | auto* accessor_expr = MemberAccessor(star_p, z); |
| 413 | auto* x = Var("x", ty.f32(), accessor_expr); |
| 414 | Func("func", {p}, ty.f32(), {Decl(x), Return(x)}); |
| 415 | EXPECT_TRUE(r()->Resolve()) << r()->error(); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 416 | } |
| 417 | |
| 418 | TEST_F(ResolverValidationTest, EXpr_MemberAccessor_FuncBadParent) { |
Tint team | f462606 | 2022-05-06 08:50:33 +0000 | [diff] [blame] | 419 | // fn func(p: ptr<function, vec4<f32>>) -> f32 { |
| 420 | // let x: f32 = *p.z; |
| 421 | // return x; |
| 422 | // } |
| 423 | auto* p = Param("p", ty.pointer(ty.vec4<f32>(), ast::StorageClass::kFunction)); |
| 424 | auto* z = Expr(Source{{{3, 3}, {3, 8}}}, "z"); |
| 425 | auto* accessor_expr = MemberAccessor(p, z); |
| 426 | auto* star_p = Deref(accessor_expr); |
| 427 | auto* x = Var("x", ty.f32(), star_p); |
| 428 | Func("func", {p}, ty.f32(), {Decl(x), Return(x)}); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 429 | |
Tint team | f462606 | 2022-05-06 08:50:33 +0000 | [diff] [blame] | 430 | EXPECT_FALSE(r()->Resolve()); |
| 431 | EXPECT_EQ(r()->error(), |
| 432 | "error: invalid member accessor expression. " |
| 433 | "Expected vector or struct, got 'ptr<function, vec4<f32>, read_write>'"); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 434 | } |
| 435 | |
| 436 | TEST_F(ResolverValidationTest, |
| 437 | Stmt_Loop_ContinueInLoopBodyBeforeDeclAndAfterDecl_UsageInContinuing) { |
Tint team | f462606 | 2022-05-06 08:50:33 +0000 | [diff] [blame] | 438 | // loop { |
| 439 | // continue; // Bypasses z decl |
| 440 | // var z : i32; // unreachable |
| 441 | // |
| 442 | // continuing { |
| 443 | // z = 2; |
| 444 | // } |
| 445 | // } |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 446 | |
Tint team | f462606 | 2022-05-06 08:50:33 +0000 | [diff] [blame] | 447 | auto error_loc = Source{{12, 34}}; |
| 448 | auto* body = Block(Continue(), Decl(error_loc, Var("z", ty.i32(), ast::StorageClass::kNone))); |
| 449 | auto* continuing = Block(Assign(Expr("z"), 2_i)); |
| 450 | auto* loop_stmt = Loop(body, continuing); |
| 451 | WrapInFunction(loop_stmt); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 452 | |
Tint team | f462606 | 2022-05-06 08:50:33 +0000 | [diff] [blame] | 453 | EXPECT_FALSE(r()->Resolve()); |
| 454 | EXPECT_EQ(r()->error(), |
| 455 | R"(12:34 warning: code is unreachable |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 456 | error: continue statement bypasses declaration of 'z' |
| 457 | note: identifier 'z' declared here |
| 458 | note: identifier 'z' referenced in continuing block here)"); |
| 459 | } |
| 460 | |
Tint team | f462606 | 2022-05-06 08:50:33 +0000 | [diff] [blame] | 461 | TEST_F(ResolverValidationTest, Stmt_Loop_ContinueInLoopBodyAfterDecl_UsageInContinuing_InBlocks) { |
| 462 | // loop { |
| 463 | // if (false) { break; } |
| 464 | // var z : i32; |
| 465 | // {{{continue;}}} |
| 466 | // continue; // Ok |
| 467 | // |
| 468 | // continuing { |
| 469 | // z = 2i; |
| 470 | // } |
| 471 | // } |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 472 | |
Tint team | f462606 | 2022-05-06 08:50:33 +0000 | [diff] [blame] | 473 | auto* body = |
| 474 | Block(If(false, Block(Break())), // |
| 475 | Decl(Var("z", ty.i32(), ast::StorageClass::kNone)), Block(Block(Block(Continue())))); |
| 476 | auto* continuing = Block(Assign(Expr("z"), 2_i)); |
| 477 | auto* loop_stmt = Loop(body, continuing); |
| 478 | WrapInFunction(loop_stmt); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 479 | |
Tint team | f462606 | 2022-05-06 08:50:33 +0000 | [diff] [blame] | 480 | ASSERT_TRUE(r()->Resolve()) << r()->error(); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 481 | } |
| 482 | |
Tint team | f462606 | 2022-05-06 08:50:33 +0000 | [diff] [blame] | 483 | TEST_F(ResolverValidationTest, Stmt_Loop_ContinueInLoopBodySubscopeBeforeDecl_UsageInContinuing) { |
| 484 | // loop { |
| 485 | // if (true) { |
| 486 | // continue; // Still bypasses z decl (if we reach here) |
| 487 | // } |
| 488 | // var z : i32; |
| 489 | // continuing { |
| 490 | // z = 2i; |
| 491 | // } |
| 492 | // } |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 493 | |
Tint team | f462606 | 2022-05-06 08:50:33 +0000 | [diff] [blame] | 494 | auto cont_loc = Source{{12, 34}}; |
| 495 | auto decl_loc = Source{{56, 78}}; |
| 496 | auto ref_loc = Source{{90, 12}}; |
| 497 | auto* body = Block(If(Expr(true), Block(Continue(cont_loc))), |
| 498 | Decl(Var(decl_loc, "z", ty.i32(), ast::StorageClass::kNone))); |
| 499 | auto* continuing = Block(Assign(Expr(ref_loc, "z"), 2_i)); |
| 500 | auto* loop_stmt = Loop(body, continuing); |
| 501 | WrapInFunction(loop_stmt); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 502 | |
Tint team | f462606 | 2022-05-06 08:50:33 +0000 | [diff] [blame] | 503 | EXPECT_FALSE(r()->Resolve()) << r()->error(); |
| 504 | EXPECT_EQ(r()->error(), |
| 505 | R"(12:34 error: continue statement bypasses declaration of 'z' |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 506 | 56:78 note: identifier 'z' declared here |
| 507 | 90:12 note: identifier 'z' referenced in continuing block here)"); |
| 508 | } |
| 509 | |
| 510 | TEST_F(ResolverValidationTest, |
Tint team | f462606 | 2022-05-06 08:50:33 +0000 | [diff] [blame] | 511 | Stmt_Loop_ContinueInLoopBodySubscopeBeforeDecl_UsageInContinuingSubscope) { |
| 512 | // loop { |
| 513 | // if (true) { |
| 514 | // continue; // Still bypasses z decl (if we reach here) |
| 515 | // } |
| 516 | // var z : i32; |
| 517 | // continuing { |
| 518 | // if (true) { |
| 519 | // z = 2i; // Must fail even if z is in a sub-scope |
| 520 | // } |
| 521 | // } |
| 522 | // } |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 523 | |
Tint team | f462606 | 2022-05-06 08:50:33 +0000 | [diff] [blame] | 524 | auto cont_loc = Source{{12, 34}}; |
| 525 | auto decl_loc = Source{{56, 78}}; |
| 526 | auto ref_loc = Source{{90, 12}}; |
| 527 | auto* body = Block(If(Expr(true), Block(Continue(cont_loc))), |
| 528 | Decl(Var(decl_loc, "z", ty.i32(), ast::StorageClass::kNone))); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 529 | |
Tint team | f462606 | 2022-05-06 08:50:33 +0000 | [diff] [blame] | 530 | auto* continuing = Block(If(Expr(true), Block(Assign(Expr(ref_loc, "z"), 2_i)))); |
| 531 | auto* loop_stmt = Loop(body, continuing); |
| 532 | WrapInFunction(loop_stmt); |
| 533 | |
| 534 | EXPECT_FALSE(r()->Resolve()) << r()->error(); |
| 535 | EXPECT_EQ(r()->error(), |
| 536 | R"(12:34 error: continue statement bypasses declaration of 'z' |
| 537 | 56:78 note: identifier 'z' declared here |
| 538 | 90:12 note: identifier 'z' referenced in continuing block here)"); |
| 539 | } |
| 540 | |
| 541 | TEST_F(ResolverValidationTest, Stmt_Loop_ContinueInLoopBodySubscopeBeforeDecl_UsageOutsideBlock) { |
| 542 | // loop { |
| 543 | // if (true) { |
| 544 | // continue; // bypasses z decl (if we reach here) |
| 545 | // } |
| 546 | // var z : i32; |
| 547 | // continuing { |
| 548 | // // Must fail even if z is used in an expression that isn't |
| 549 | // // directly contained inside a block. |
| 550 | // if (z < 2i) { |
| 551 | // } |
| 552 | // } |
| 553 | // } |
| 554 | |
| 555 | auto cont_loc = Source{{12, 34}}; |
| 556 | auto decl_loc = Source{{56, 78}}; |
| 557 | auto ref_loc = Source{{90, 12}}; |
| 558 | auto* body = Block(If(Expr(true), Block(Continue(cont_loc))), |
| 559 | Decl(Var(decl_loc, "z", ty.i32(), ast::StorageClass::kNone))); |
| 560 | auto* compare = |
| 561 | create<ast::BinaryExpression>(ast::BinaryOp::kLessThan, Expr(ref_loc, "z"), Expr(2_i)); |
| 562 | auto* continuing = Block(If(compare, Block())); |
| 563 | auto* loop_stmt = Loop(body, continuing); |
| 564 | WrapInFunction(loop_stmt); |
| 565 | |
| 566 | EXPECT_FALSE(r()->Resolve()) << r()->error(); |
| 567 | EXPECT_EQ(r()->error(), |
| 568 | R"(12:34 error: continue statement bypasses declaration of 'z' |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 569 | 56:78 note: identifier 'z' declared here |
| 570 | 90:12 note: identifier 'z' referenced in continuing block here)"); |
| 571 | } |
| 572 | |
| 573 | TEST_F(ResolverValidationTest, |
| 574 | Stmt_Loop_ContinueInLoopBodySubscopeBeforeDecl_UsageInContinuingLoop) { |
Tint team | f462606 | 2022-05-06 08:50:33 +0000 | [diff] [blame] | 575 | // loop { |
| 576 | // if (true) { |
| 577 | // continue; // Still bypasses z decl (if we reach here) |
| 578 | // } |
| 579 | // var z : i32; |
| 580 | // continuing { |
| 581 | // loop { |
| 582 | // z = 2i; // Must fail even if z is in a sub-scope |
| 583 | // } |
| 584 | // } |
| 585 | // } |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 586 | |
Tint team | f462606 | 2022-05-06 08:50:33 +0000 | [diff] [blame] | 587 | auto cont_loc = Source{{12, 34}}; |
| 588 | auto decl_loc = Source{{56, 78}}; |
| 589 | auto ref_loc = Source{{90, 12}}; |
| 590 | auto* body = Block(If(Expr(true), Block(Continue(cont_loc))), |
| 591 | Decl(Var(decl_loc, "z", ty.i32(), ast::StorageClass::kNone))); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 592 | |
Tint team | f462606 | 2022-05-06 08:50:33 +0000 | [diff] [blame] | 593 | auto* continuing = Block(Loop(Block(Assign(Expr(ref_loc, "z"), 2_i)))); |
| 594 | auto* loop_stmt = Loop(body, continuing); |
| 595 | WrapInFunction(loop_stmt); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 596 | |
Tint team | f462606 | 2022-05-06 08:50:33 +0000 | [diff] [blame] | 597 | EXPECT_FALSE(r()->Resolve()) << r()->error(); |
| 598 | EXPECT_EQ(r()->error(), |
| 599 | R"(12:34 error: continue statement bypasses declaration of 'z' |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 600 | 56:78 note: identifier 'z' declared here |
| 601 | 90:12 note: identifier 'z' referenced in continuing block here)"); |
| 602 | } |
| 603 | |
Tint team | f462606 | 2022-05-06 08:50:33 +0000 | [diff] [blame] | 604 | TEST_F(ResolverValidationTest, Stmt_Loop_ContinueInNestedLoopBodyBeforeDecl_UsageInContinuing) { |
| 605 | // loop { |
| 606 | // loop { |
| 607 | // if (true) { continue; } // OK: not part of the outer loop |
| 608 | // break; |
| 609 | // } |
| 610 | // var z : i32; |
| 611 | // break; |
| 612 | // continuing { |
| 613 | // z = 2i; |
| 614 | // } |
| 615 | // } |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 616 | |
Tint team | f462606 | 2022-05-06 08:50:33 +0000 | [diff] [blame] | 617 | auto* inner_loop = Loop(Block( // |
| 618 | If(true, Block(Continue())), // |
| 619 | Break())); |
| 620 | auto* body = Block(inner_loop, // |
| 621 | Decl(Var("z", ty.i32(), ast::StorageClass::kNone)), // |
| 622 | Break()); |
| 623 | auto* continuing = Block(Assign("z", 2_i)); |
| 624 | auto* loop_stmt = Loop(body, continuing); |
| 625 | WrapInFunction(loop_stmt); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 626 | |
Tint team | f462606 | 2022-05-06 08:50:33 +0000 | [diff] [blame] | 627 | EXPECT_TRUE(r()->Resolve()) << r()->error(); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 628 | } |
| 629 | |
| 630 | TEST_F(ResolverValidationTest, |
| 631 | Stmt_Loop_ContinueInNestedLoopBodyBeforeDecl_UsageInContinuingSubscope) { |
Tint team | f462606 | 2022-05-06 08:50:33 +0000 | [diff] [blame] | 632 | // loop { |
| 633 | // loop { |
| 634 | // if (true) { continue; } // OK: not part of the outer loop |
| 635 | // break; |
| 636 | // } |
| 637 | // var z : i32; |
| 638 | // break; |
| 639 | // continuing { |
| 640 | // if (true) { |
| 641 | // z = 2i; |
| 642 | // } |
| 643 | // } |
| 644 | // } |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 645 | |
Tint team | f462606 | 2022-05-06 08:50:33 +0000 | [diff] [blame] | 646 | auto* inner_loop = Loop(Block(If(true, Block(Continue())), // |
| 647 | Break())); |
| 648 | auto* body = Block(inner_loop, // |
| 649 | Decl(Var("z", ty.i32(), ast::StorageClass::kNone)), // |
| 650 | Break()); |
| 651 | auto* continuing = Block(If(Expr(true), Block(Assign("z", 2_i)))); |
| 652 | auto* loop_stmt = Loop(body, continuing); |
| 653 | WrapInFunction(loop_stmt); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 654 | |
Tint team | f462606 | 2022-05-06 08:50:33 +0000 | [diff] [blame] | 655 | EXPECT_TRUE(r()->Resolve()) << r()->error(); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 656 | } |
| 657 | |
Tint team | f462606 | 2022-05-06 08:50:33 +0000 | [diff] [blame] | 658 | TEST_F(ResolverValidationTest, Stmt_Loop_ContinueInNestedLoopBodyBeforeDecl_UsageInContinuingLoop) { |
| 659 | // loop { |
| 660 | // loop { |
| 661 | // if (true) { continue; } // OK: not part of the outer loop |
| 662 | // break; |
| 663 | // } |
| 664 | // var z : i32; |
| 665 | // break; |
| 666 | // continuing { |
| 667 | // loop { |
| 668 | // z = 2i; |
| 669 | // break; |
| 670 | // } |
| 671 | // } |
| 672 | // } |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 673 | |
Tint team | f462606 | 2022-05-06 08:50:33 +0000 | [diff] [blame] | 674 | auto* inner_loop = Loop(Block(If(true, Block(Continue())), // |
| 675 | Break())); |
| 676 | auto* body = Block(inner_loop, // |
| 677 | Decl(Var("z", ty.i32(), ast::StorageClass::kNone)), // |
| 678 | Break()); |
| 679 | auto* continuing = Block(Loop(Block(Assign("z", 2_i), // |
| 680 | Break()))); |
| 681 | auto* loop_stmt = Loop(body, continuing); |
| 682 | WrapInFunction(loop_stmt); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 683 | |
Tint team | f462606 | 2022-05-06 08:50:33 +0000 | [diff] [blame] | 684 | EXPECT_TRUE(r()->Resolve()) << r()->error(); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 685 | } |
| 686 | |
| 687 | TEST_F(ResolverTest, Stmt_Loop_ContinueInLoopBodyAfterDecl_UsageInContinuing) { |
Tint team | f462606 | 2022-05-06 08:50:33 +0000 | [diff] [blame] | 688 | // loop { |
| 689 | // var z : i32; |
| 690 | // if (true) { continue; } |
| 691 | // break; |
| 692 | // continuing { |
| 693 | // z = 2i; |
| 694 | // } |
| 695 | // } |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 696 | |
Tint team | f462606 | 2022-05-06 08:50:33 +0000 | [diff] [blame] | 697 | auto error_loc = Source{{12, 34}}; |
| 698 | auto* body = |
| 699 | Block(Decl(Var("z", ty.i32(), ast::StorageClass::kNone)), If(true, Block(Continue())), // |
| 700 | Break()); |
| 701 | auto* continuing = Block(Assign(Expr(error_loc, "z"), 2_i)); |
| 702 | auto* loop_stmt = Loop(body, continuing); |
| 703 | WrapInFunction(loop_stmt); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 704 | |
Tint team | f462606 | 2022-05-06 08:50:33 +0000 | [diff] [blame] | 705 | EXPECT_TRUE(r()->Resolve()); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 706 | } |
| 707 | |
| 708 | TEST_F(ResolverTest, Stmt_Loop_ReturnInContinuing_Direct) { |
Tint team | f462606 | 2022-05-06 08:50:33 +0000 | [diff] [blame] | 709 | // loop { |
| 710 | // continuing { |
| 711 | // return; |
| 712 | // } |
| 713 | // } |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 714 | |
Tint team | f462606 | 2022-05-06 08:50:33 +0000 | [diff] [blame] | 715 | WrapInFunction(Loop( // loop |
| 716 | Block(), // loop block |
| 717 | Block( // loop continuing block |
| 718 | Return(Source{{12, 34}})))); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 719 | |
Tint team | f462606 | 2022-05-06 08:50:33 +0000 | [diff] [blame] | 720 | EXPECT_FALSE(r()->Resolve()); |
| 721 | EXPECT_EQ(r()->error(), |
| 722 | R"(12:34 error: continuing blocks must not contain a return statement)"); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 723 | } |
| 724 | |
| 725 | TEST_F(ResolverTest, Stmt_Loop_ReturnInContinuing_Indirect) { |
Tint team | f462606 | 2022-05-06 08:50:33 +0000 | [diff] [blame] | 726 | // loop { |
| 727 | // if (false) { break; } |
| 728 | // continuing { |
| 729 | // loop { |
| 730 | // return; |
| 731 | // } |
| 732 | // } |
| 733 | // } |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 734 | |
Tint team | f462606 | 2022-05-06 08:50:33 +0000 | [diff] [blame] | 735 | WrapInFunction(Loop( // outer loop |
| 736 | Block(If(false, Block(Break()))), // outer loop block |
| 737 | Block(Source{{56, 78}}, // outer loop continuing block |
| 738 | Loop( // inner loop |
| 739 | Block( // inner loop block |
| 740 | Return(Source{{12, 34}})))))); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 741 | |
Tint team | f462606 | 2022-05-06 08:50:33 +0000 | [diff] [blame] | 742 | EXPECT_FALSE(r()->Resolve()); |
| 743 | EXPECT_EQ(r()->error(), |
| 744 | R"(12:34 error: continuing blocks must not contain a return statement |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 745 | 56:78 note: see continuing block here)"); |
| 746 | } |
| 747 | |
| 748 | TEST_F(ResolverTest, Stmt_Loop_DiscardInContinuing_Direct) { |
Tint team | f462606 | 2022-05-06 08:50:33 +0000 | [diff] [blame] | 749 | // loop { |
| 750 | // continuing { |
| 751 | // discard; |
| 752 | // } |
| 753 | // } |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 754 | |
Tint team | f462606 | 2022-05-06 08:50:33 +0000 | [diff] [blame] | 755 | WrapInFunction(Loop( // loop |
| 756 | Block(), // loop block |
| 757 | Block( // loop continuing block |
| 758 | Discard(Source{{12, 34}})))); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 759 | |
Tint team | f462606 | 2022-05-06 08:50:33 +0000 | [diff] [blame] | 760 | EXPECT_FALSE(r()->Resolve()); |
| 761 | EXPECT_EQ(r()->error(), |
| 762 | R"(12:34 error: continuing blocks must not contain a discard statement)"); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 763 | } |
| 764 | |
| 765 | TEST_F(ResolverTest, Stmt_Loop_DiscardInContinuing_Indirect) { |
Tint team | f462606 | 2022-05-06 08:50:33 +0000 | [diff] [blame] | 766 | // loop { |
| 767 | // if (false) { break; } |
| 768 | // continuing { |
| 769 | // loop { discard; } |
| 770 | // } |
| 771 | // } |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 772 | |
Tint team | f462606 | 2022-05-06 08:50:33 +0000 | [diff] [blame] | 773 | WrapInFunction(Loop( // outer loop |
| 774 | Block(If(false, Block(Break()))), // outer loop block |
| 775 | Block(Source{{56, 78}}, // outer loop continuing block |
| 776 | Loop( // inner loop |
| 777 | Block( // inner loop block |
| 778 | Discard(Source{{12, 34}})))))); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 779 | |
Tint team | f462606 | 2022-05-06 08:50:33 +0000 | [diff] [blame] | 780 | EXPECT_FALSE(r()->Resolve()); |
| 781 | EXPECT_EQ(r()->error(), |
| 782 | R"(12:34 error: continuing blocks must not contain a discard statement |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 783 | 56:78 note: see continuing block here)"); |
| 784 | } |
| 785 | |
| 786 | TEST_F(ResolverTest, Stmt_Loop_DiscardInContinuing_Indirect_ViaCall) { |
Tint team | f462606 | 2022-05-06 08:50:33 +0000 | [diff] [blame] | 787 | // fn MayDiscard() { if (true) { discard; } } |
| 788 | // fn F() { MayDiscard(); } |
| 789 | // loop { |
| 790 | // continuing { |
| 791 | // loop { F(); } |
| 792 | // } |
| 793 | // } |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 794 | |
Tint team | f462606 | 2022-05-06 08:50:33 +0000 | [diff] [blame] | 795 | Func("MayDiscard", {}, ty.void_(), {If(true, Block(Discard()))}); |
| 796 | Func("SomeFunc", {}, ty.void_(), {CallStmt(Call("MayDiscard"))}); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 797 | |
Tint team | f462606 | 2022-05-06 08:50:33 +0000 | [diff] [blame] | 798 | WrapInFunction(Loop( // outer loop |
| 799 | Block(), // outer loop block |
| 800 | Block(Source{{56, 78}}, // outer loop continuing block |
| 801 | Loop( // inner loop |
| 802 | Block( // inner loop block |
| 803 | CallStmt(Call(Source{{12, 34}}, "SomeFunc"))))))); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 804 | |
Tint team | f462606 | 2022-05-06 08:50:33 +0000 | [diff] [blame] | 805 | EXPECT_FALSE(r()->Resolve()); |
| 806 | EXPECT_EQ(r()->error(), |
| 807 | R"(12:34 error: cannot call a function that may discard inside a continuing block |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 808 | 56:78 note: see continuing block here)"); |
| 809 | } |
| 810 | |
| 811 | TEST_F(ResolverTest, Stmt_Loop_ContinueInContinuing_Direct) { |
Tint team | f462606 | 2022-05-06 08:50:33 +0000 | [diff] [blame] | 812 | // loop { |
| 813 | // continuing { |
| 814 | // continue; |
| 815 | // } |
| 816 | // } |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 817 | |
Tint team | f462606 | 2022-05-06 08:50:33 +0000 | [diff] [blame] | 818 | WrapInFunction(Loop( // loop |
| 819 | Block(), // loop block |
| 820 | Block(Source{{56, 78}}, // loop continuing block |
| 821 | Continue(Source{{12, 34}})))); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 822 | |
Tint team | f462606 | 2022-05-06 08:50:33 +0000 | [diff] [blame] | 823 | EXPECT_FALSE(r()->Resolve()); |
| 824 | EXPECT_EQ(r()->error(), "12:34 error: continuing blocks must not contain a continue statement"); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 825 | } |
| 826 | |
| 827 | TEST_F(ResolverTest, Stmt_Loop_ContinueInContinuing_Indirect) { |
Tint team | f462606 | 2022-05-06 08:50:33 +0000 | [diff] [blame] | 828 | // loop { |
| 829 | // if (false) { break; } |
| 830 | // continuing { |
| 831 | // loop { |
| 832 | // if (false) { break; } |
| 833 | // continue; |
| 834 | // } |
| 835 | // } |
| 836 | // } |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 837 | |
Tint team | f462606 | 2022-05-06 08:50:33 +0000 | [diff] [blame] | 838 | WrapInFunction(Loop( // outer loop |
| 839 | Block( // outer loop block |
| 840 | If(false, Block(Break()))), // if (false) { break; } |
| 841 | Block( // outer loop continuing block |
| 842 | Loop( // inner loop |
| 843 | Block( // inner loop block |
| 844 | If(false, Block(Break())), // if (false) { break; } |
| 845 | Continue(Source{{12, 34}})))))); // continue |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 846 | |
Tint team | f462606 | 2022-05-06 08:50:33 +0000 | [diff] [blame] | 847 | EXPECT_TRUE(r()->Resolve()) << r()->error(); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 848 | } |
| 849 | |
| 850 | TEST_F(ResolverTest, Stmt_ForLoop_ReturnInContinuing_Direct) { |
Tint team | f462606 | 2022-05-06 08:50:33 +0000 | [diff] [blame] | 851 | // for(;; return) { |
| 852 | // break; |
| 853 | // } |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 854 | |
Tint team | f462606 | 2022-05-06 08:50:33 +0000 | [diff] [blame] | 855 | WrapInFunction(For(nullptr, nullptr, Return(Source{{12, 34}}), // |
| 856 | Block(Break()))); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 857 | |
Tint team | f462606 | 2022-05-06 08:50:33 +0000 | [diff] [blame] | 858 | EXPECT_FALSE(r()->Resolve()); |
| 859 | EXPECT_EQ(r()->error(), |
| 860 | R"(12:34 error: continuing blocks must not contain a return statement)"); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 861 | } |
| 862 | |
| 863 | TEST_F(ResolverTest, Stmt_ForLoop_ReturnInContinuing_Indirect) { |
Tint team | f462606 | 2022-05-06 08:50:33 +0000 | [diff] [blame] | 864 | // for(;; loop { return }) { |
| 865 | // break; |
| 866 | // } |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 867 | |
Tint team | f462606 | 2022-05-06 08:50:33 +0000 | [diff] [blame] | 868 | WrapInFunction(For(nullptr, nullptr, |
| 869 | Loop(Source{{56, 78}}, // |
| 870 | Block(Return(Source{{12, 34}}))), // |
| 871 | Block(Break()))); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 872 | |
Tint team | f462606 | 2022-05-06 08:50:33 +0000 | [diff] [blame] | 873 | EXPECT_FALSE(r()->Resolve()); |
| 874 | EXPECT_EQ(r()->error(), |
| 875 | R"(12:34 error: continuing blocks must not contain a return statement |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 876 | 56:78 note: see continuing block here)"); |
| 877 | } |
| 878 | |
| 879 | TEST_F(ResolverTest, Stmt_ForLoop_DiscardInContinuing_Direct) { |
Tint team | f462606 | 2022-05-06 08:50:33 +0000 | [diff] [blame] | 880 | // for(;; discard) { |
| 881 | // break; |
| 882 | // } |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 883 | |
Tint team | f462606 | 2022-05-06 08:50:33 +0000 | [diff] [blame] | 884 | WrapInFunction(For(nullptr, nullptr, Discard(Source{{12, 34}}), // |
| 885 | Block(Break()))); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 886 | |
Tint team | f462606 | 2022-05-06 08:50:33 +0000 | [diff] [blame] | 887 | EXPECT_FALSE(r()->Resolve()); |
| 888 | EXPECT_EQ(r()->error(), |
| 889 | R"(12:34 error: continuing blocks must not contain a discard statement)"); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 890 | } |
| 891 | |
| 892 | TEST_F(ResolverTest, Stmt_ForLoop_DiscardInContinuing_Indirect) { |
Tint team | f462606 | 2022-05-06 08:50:33 +0000 | [diff] [blame] | 893 | // for(;; loop { discard }) { |
| 894 | // break; |
| 895 | // } |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 896 | |
Tint team | f462606 | 2022-05-06 08:50:33 +0000 | [diff] [blame] | 897 | WrapInFunction(For(nullptr, nullptr, |
| 898 | Loop(Source{{56, 78}}, // |
| 899 | Block(Discard(Source{{12, 34}}))), // |
| 900 | Block(Break()))); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 901 | |
Tint team | f462606 | 2022-05-06 08:50:33 +0000 | [diff] [blame] | 902 | EXPECT_FALSE(r()->Resolve()); |
| 903 | EXPECT_EQ(r()->error(), |
| 904 | R"(12:34 error: continuing blocks must not contain a discard statement |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 905 | 56:78 note: see continuing block here)"); |
| 906 | } |
| 907 | |
| 908 | TEST_F(ResolverTest, Stmt_ForLoop_DiscardInContinuing_Indirect_ViaCall) { |
Tint team | f462606 | 2022-05-06 08:50:33 +0000 | [diff] [blame] | 909 | // fn MayDiscard() { if (true) { discard; } } |
| 910 | // fn F() { MayDiscard(); } |
| 911 | // for(;; loop { F() }) { |
| 912 | // break; |
| 913 | // } |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 914 | |
Tint team | f462606 | 2022-05-06 08:50:33 +0000 | [diff] [blame] | 915 | Func("MayDiscard", {}, ty.void_(), {If(true, Block(Discard()))}); |
| 916 | Func("F", {}, ty.void_(), {CallStmt(Call("MayDiscard"))}); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 917 | |
Tint team | f462606 | 2022-05-06 08:50:33 +0000 | [diff] [blame] | 918 | WrapInFunction(For(nullptr, nullptr, |
| 919 | Loop(Source{{56, 78}}, // |
| 920 | Block(CallStmt(Call(Source{{12, 34}}, "F")))), // |
| 921 | Block(Break()))); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 922 | |
Tint team | f462606 | 2022-05-06 08:50:33 +0000 | [diff] [blame] | 923 | EXPECT_FALSE(r()->Resolve()); |
| 924 | EXPECT_EQ(r()->error(), |
| 925 | R"(12:34 error: cannot call a function that may discard inside a continuing block |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 926 | 56:78 note: see continuing block here)"); |
| 927 | } |
| 928 | |
| 929 | TEST_F(ResolverTest, Stmt_ForLoop_ContinueInContinuing_Direct) { |
Tint team | f462606 | 2022-05-06 08:50:33 +0000 | [diff] [blame] | 930 | // for(;; continue) { |
| 931 | // break; |
| 932 | // } |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 933 | |
Tint team | f462606 | 2022-05-06 08:50:33 +0000 | [diff] [blame] | 934 | WrapInFunction(For(nullptr, nullptr, Continue(Source{{12, 34}}), // |
| 935 | Block(Break()))); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 936 | |
Tint team | f462606 | 2022-05-06 08:50:33 +0000 | [diff] [blame] | 937 | EXPECT_FALSE(r()->Resolve()); |
| 938 | EXPECT_EQ(r()->error(), "12:34 error: continuing blocks must not contain a continue statement"); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 939 | } |
| 940 | |
| 941 | TEST_F(ResolverTest, Stmt_ForLoop_ContinueInContinuing_Indirect) { |
Tint team | f462606 | 2022-05-06 08:50:33 +0000 | [diff] [blame] | 942 | // for(;; loop { if (false) { break; } continue }) { |
| 943 | // break; |
| 944 | // } |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 945 | |
Tint team | f462606 | 2022-05-06 08:50:33 +0000 | [diff] [blame] | 946 | WrapInFunction(For(nullptr, nullptr, |
| 947 | Loop( // |
| 948 | Block(If(false, Block(Break())), // |
| 949 | Continue(Source{{12, 34}}))), // |
| 950 | Block(Break()))); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 951 | |
Tint team | f462606 | 2022-05-06 08:50:33 +0000 | [diff] [blame] | 952 | EXPECT_TRUE(r()->Resolve()) << r()->error(); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 953 | } |
| 954 | |
| 955 | TEST_F(ResolverTest, Stmt_ForLoop_CondIsBoolRef) { |
Tint team | f462606 | 2022-05-06 08:50:33 +0000 | [diff] [blame] | 956 | // var cond : bool = true; |
| 957 | // for (; cond; ) { |
| 958 | // } |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 959 | |
Tint team | f462606 | 2022-05-06 08:50:33 +0000 | [diff] [blame] | 960 | auto* cond = Var("cond", ty.bool_(), Expr(true)); |
| 961 | WrapInFunction(Decl(cond), For(nullptr, "cond", nullptr, Block())); |
| 962 | EXPECT_TRUE(r()->Resolve()) << r()->error(); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 963 | } |
| 964 | |
| 965 | TEST_F(ResolverTest, Stmt_ForLoop_CondIsNotBool) { |
Tint team | f462606 | 2022-05-06 08:50:33 +0000 | [diff] [blame] | 966 | // for (; 1.0f; ) { |
| 967 | // } |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 968 | |
Tint team | 2e186f2 | 2022-05-11 08:42:48 +0000 | [diff] [blame] | 969 | WrapInFunction(For(nullptr, Expr(Source{{12, 34}}, 1_f), nullptr, Block())); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 970 | |
Tint team | f462606 | 2022-05-06 08:50:33 +0000 | [diff] [blame] | 971 | EXPECT_FALSE(r()->Resolve()); |
| 972 | EXPECT_EQ(r()->error(), "12:34 error: for-loop condition must be bool, got f32"); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 973 | } |
| 974 | |
| 975 | TEST_F(ResolverValidationTest, Stmt_ContinueInLoop) { |
Tint team | f462606 | 2022-05-06 08:50:33 +0000 | [diff] [blame] | 976 | WrapInFunction(Loop(Block(If(false, Block(Break())), // |
| 977 | Continue(Source{{12, 34}})))); |
| 978 | EXPECT_TRUE(r()->Resolve()) << r()->error(); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 979 | } |
| 980 | |
| 981 | TEST_F(ResolverValidationTest, Stmt_ContinueNotInLoop) { |
Tint team | f462606 | 2022-05-06 08:50:33 +0000 | [diff] [blame] | 982 | WrapInFunction(Continue(Source{{12, 34}})); |
| 983 | EXPECT_FALSE(r()->Resolve()); |
| 984 | EXPECT_EQ(r()->error(), "12:34 error: continue statement must be in a loop"); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 985 | } |
| 986 | |
| 987 | TEST_F(ResolverValidationTest, Stmt_BreakInLoop) { |
Tint team | f462606 | 2022-05-06 08:50:33 +0000 | [diff] [blame] | 988 | WrapInFunction(Loop(Block(Break(Source{{12, 34}})))); |
| 989 | EXPECT_TRUE(r()->Resolve()) << r()->error(); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 990 | } |
| 991 | |
| 992 | TEST_F(ResolverValidationTest, Stmt_BreakInSwitch) { |
Tint team | f462606 | 2022-05-06 08:50:33 +0000 | [diff] [blame] | 993 | WrapInFunction(Loop(Block(Switch(Expr(1_i), // |
| 994 | Case(Expr(1_i), // |
| 995 | Block(Break())), // |
| 996 | DefaultCase()), // |
| 997 | Break()))); // |
| 998 | EXPECT_TRUE(r()->Resolve()) << r()->error(); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 999 | } |
| 1000 | |
| 1001 | TEST_F(ResolverValidationTest, Stmt_BreakInIfTrueInContinuing) { |
Tint team | f462606 | 2022-05-06 08:50:33 +0000 | [diff] [blame] | 1002 | auto* cont = Block( // continuing { |
| 1003 | If(true, Block( // if(true) { |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 1004 | Break(Source{{12, 34}})))); // break; |
| 1005 | // } |
| 1006 | // } |
Tint team | f462606 | 2022-05-06 08:50:33 +0000 | [diff] [blame] | 1007 | WrapInFunction(Loop(Block(), cont)); |
| 1008 | EXPECT_TRUE(r()->Resolve()) << r()->error(); |
| 1009 | } |
| 1010 | |
| 1011 | TEST_F(ResolverValidationTest, Stmt_BreakInIfElseInContinuing) { |
| 1012 | auto* cont = Block( // continuing { |
| 1013 | If(true, Block(), // if(true) { |
| 1014 | Else(Block( // } else { |
| 1015 | Break(Source{{12, 34}}))))); // break; |
| 1016 | // } |
| 1017 | // } |
| 1018 | WrapInFunction(Loop(Block(), cont)); |
| 1019 | EXPECT_TRUE(r()->Resolve()) << r()->error(); |
| 1020 | } |
| 1021 | |
| 1022 | TEST_F(ResolverValidationTest, Stmt_BreakInContinuing) { |
| 1023 | auto* cont = Block( // continuing { |
| 1024 | Block(Break(Source{{12, 34}}))); // break; |
| 1025 | // } |
| 1026 | WrapInFunction(Loop(Block(), cont)); |
| 1027 | EXPECT_FALSE(r()->Resolve()); |
| 1028 | EXPECT_EQ(r()->error(), |
| 1029 | "12:34 error: break statement in a continuing block must be the single " |
| 1030 | "statement of an if statement's true or false block, and that if " |
| 1031 | "statement must be the last statement of the continuing block\n" |
| 1032 | "12:34 note: break statement is not directly in if statement block"); |
| 1033 | } |
| 1034 | |
| 1035 | TEST_F(ResolverValidationTest, Stmt_BreakInIfInIfInContinuing) { |
| 1036 | auto* cont = Block( // continuing { |
| 1037 | If(true, Block( // if(true) { |
| 1038 | If(Source{{56, 78}}, true, // if(true) { |
| 1039 | Block(Break(Source{{12, 34}})))))); // break; |
| 1040 | // } |
| 1041 | // } |
| 1042 | // } |
| 1043 | WrapInFunction(Loop(Block(), cont)); |
| 1044 | EXPECT_FALSE(r()->Resolve()); |
| 1045 | EXPECT_EQ(r()->error(), |
| 1046 | "12:34 error: break statement in a continuing block must be the single " |
| 1047 | "statement of an if statement's true or false block, and that if " |
| 1048 | "statement must be the last statement of the continuing block\n" |
| 1049 | "56:78 note: if statement containing break statement is not directly in " |
| 1050 | "continuing block"); |
| 1051 | } |
| 1052 | |
| 1053 | TEST_F(ResolverValidationTest, Stmt_BreakInIfTrueMultipleStmtsInContinuing) { |
| 1054 | auto* cont = Block( // continuing { |
| 1055 | If(true, Block(Source{{56, 78}}, // if(true) { |
| 1056 | Assign(Phony(), 1_i), // _ = 1i; |
| 1057 | Break(Source{{12, 34}})))); // break; |
| 1058 | // } |
| 1059 | // } |
| 1060 | WrapInFunction(Loop(Block(), cont)); |
| 1061 | EXPECT_FALSE(r()->Resolve()); |
| 1062 | EXPECT_EQ(r()->error(), |
| 1063 | "12:34 error: break statement in a continuing block must be the single " |
| 1064 | "statement of an if statement's true or false block, and that if " |
| 1065 | "statement must be the last statement of the continuing block\n" |
| 1066 | "56:78 note: if statement block contains multiple statements"); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 1067 | } |
| 1068 | |
| 1069 | TEST_F(ResolverValidationTest, Stmt_BreakInIfElseMultipleStmtsInContinuing) { |
Tint team | f462606 | 2022-05-06 08:50:33 +0000 | [diff] [blame] | 1070 | auto* cont = Block( // continuing { |
| 1071 | If(true, Block(), // if(true) { |
| 1072 | Else(Block(Source{{56, 78}}, // } else { |
| 1073 | Assign(Phony(), 1_i), // _ = 1i; |
| 1074 | Break(Source{{12, 34}}))))); // break; |
| 1075 | // } |
| 1076 | // } |
| 1077 | WrapInFunction(Loop(Block(), cont)); |
| 1078 | EXPECT_FALSE(r()->Resolve()); |
| 1079 | EXPECT_EQ(r()->error(), |
| 1080 | "12:34 error: break statement in a continuing block must be the single " |
| 1081 | "statement of an if statement's true or false block, and that if " |
| 1082 | "statement must be the last statement of the continuing block\n" |
| 1083 | "56:78 note: if statement block contains multiple statements"); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 1084 | } |
| 1085 | |
| 1086 | TEST_F(ResolverValidationTest, Stmt_BreakInIfElseIfInContinuing) { |
Tint team | f462606 | 2022-05-06 08:50:33 +0000 | [diff] [blame] | 1087 | auto* cont = Block( // continuing { |
| 1088 | If(true, Block(), // if(true) { |
| 1089 | Else(If(Source{{56, 78}}, Expr(true), // } else if (true) { |
| 1090 | Block(Break(Source{{12, 34}})))))); // break; |
| 1091 | // } |
| 1092 | // } |
| 1093 | WrapInFunction(Loop(Block(), cont)); |
| 1094 | EXPECT_FALSE(r()->Resolve()); |
| 1095 | EXPECT_EQ(r()->error(), |
| 1096 | "12:34 error: break statement in a continuing block must be the single " |
| 1097 | "statement of an if statement's true or false block, and that if " |
| 1098 | "statement must be the last statement of the continuing block\n" |
| 1099 | "56:78 note: else has condition"); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 1100 | } |
| 1101 | |
| 1102 | TEST_F(ResolverValidationTest, Stmt_BreakInIfNonEmptyElseInContinuing) { |
Tint team | f462606 | 2022-05-06 08:50:33 +0000 | [diff] [blame] | 1103 | auto* cont = Block( // continuing { |
| 1104 | If(true, // if(true) { |
| 1105 | Block(Break(Source{{12, 34}})), // break; |
| 1106 | Else(Block(Source{{56, 78}}, // } else { |
| 1107 | Assign(Phony(), 1_i))))); // _ = 1i; |
| 1108 | // } |
| 1109 | // } |
| 1110 | WrapInFunction(Loop(Block(), cont)); |
| 1111 | EXPECT_FALSE(r()->Resolve()); |
| 1112 | EXPECT_EQ(r()->error(), |
| 1113 | "12:34 error: break statement in a continuing block must be the single " |
| 1114 | "statement of an if statement's true or false block, and that if " |
| 1115 | "statement must be the last statement of the continuing block\n" |
| 1116 | "56:78 note: non-empty false block"); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 1117 | } |
| 1118 | |
| 1119 | TEST_F(ResolverValidationTest, Stmt_BreakInIfElseNonEmptyTrueInContinuing) { |
Tint team | f462606 | 2022-05-06 08:50:33 +0000 | [diff] [blame] | 1120 | auto* cont = Block( // continuing { |
| 1121 | If(true, // if(true) { |
| 1122 | Block(Source{{56, 78}}, Assign(Phony(), 1_i)), // _ = 1i; |
| 1123 | Else(Block( // } else { |
| 1124 | Break(Source{{12, 34}}))))); // break; |
| 1125 | // } |
| 1126 | // } |
| 1127 | WrapInFunction(Loop(Block(), cont)); |
| 1128 | EXPECT_FALSE(r()->Resolve()); |
| 1129 | EXPECT_EQ(r()->error(), |
| 1130 | "12:34 error: break statement in a continuing block must be the single " |
| 1131 | "statement of an if statement's true or false block, and that if " |
| 1132 | "statement must be the last statement of the continuing block\n" |
| 1133 | "56:78 note: non-empty true block"); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 1134 | } |
| 1135 | |
| 1136 | TEST_F(ResolverValidationTest, Stmt_BreakInIfInContinuingNotLast) { |
Tint team | f462606 | 2022-05-06 08:50:33 +0000 | [diff] [blame] | 1137 | auto* cont = Block( // continuing { |
| 1138 | If(Source{{56, 78}}, true, // if(true) { |
| 1139 | Block(Break(Source{{12, 34}}))), // break; |
| 1140 | // } |
| 1141 | Assign(Phony(), 1_i)); // _ = 1i; |
| 1142 | // } |
| 1143 | WrapInFunction(Loop(Block(), cont)); |
| 1144 | EXPECT_FALSE(r()->Resolve()); |
| 1145 | EXPECT_EQ(r()->error(), |
| 1146 | "12:34 error: break statement in a continuing block must be the single " |
| 1147 | "statement of an if statement's true or false block, and that if " |
| 1148 | "statement must be the last statement of the continuing block\n" |
| 1149 | "56:78 note: if statement containing break statement is not the last " |
| 1150 | "statement of the continuing block"); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 1151 | } |
| 1152 | |
| 1153 | TEST_F(ResolverValidationTest, Stmt_BreakNotInLoopOrSwitch) { |
Tint team | f462606 | 2022-05-06 08:50:33 +0000 | [diff] [blame] | 1154 | WrapInFunction(Break(Source{{12, 34}})); |
| 1155 | EXPECT_FALSE(r()->Resolve()); |
| 1156 | EXPECT_EQ(r()->error(), "12:34 error: break statement must be in a loop or switch case"); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 1157 | } |
| 1158 | |
| 1159 | TEST_F(ResolverValidationTest, StructMemberDuplicateName) { |
Tint team | f462606 | 2022-05-06 08:50:33 +0000 | [diff] [blame] | 1160 | Structure("S", |
| 1161 | {Member(Source{{12, 34}}, "a", ty.i32()), Member(Source{{56, 78}}, "a", ty.i32())}); |
| 1162 | EXPECT_FALSE(r()->Resolve()); |
| 1163 | EXPECT_EQ(r()->error(), |
| 1164 | "56:78 error: redefinition of 'a'\n12:34 note: previous definition " |
| 1165 | "is here"); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 1166 | } |
| 1167 | TEST_F(ResolverValidationTest, StructMemberDuplicateNameDifferentTypes) { |
Tint team | f462606 | 2022-05-06 08:50:33 +0000 | [diff] [blame] | 1168 | Structure("S", {Member(Source{{12, 34}}, "a", ty.bool_()), |
| 1169 | Member(Source{{12, 34}}, "a", ty.vec3<f32>())}); |
| 1170 | EXPECT_FALSE(r()->Resolve()); |
| 1171 | EXPECT_EQ(r()->error(), |
| 1172 | "12:34 error: redefinition of 'a'\n12:34 note: previous definition " |
| 1173 | "is here"); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 1174 | } |
| 1175 | TEST_F(ResolverValidationTest, StructMemberDuplicateNamePass) { |
Tint team | f462606 | 2022-05-06 08:50:33 +0000 | [diff] [blame] | 1176 | Structure("S", {Member("a", ty.i32()), Member("b", ty.f32())}); |
| 1177 | Structure("S1", {Member("a", ty.i32()), Member("b", ty.f32())}); |
| 1178 | EXPECT_TRUE(r()->Resolve()); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 1179 | } |
| 1180 | |
| 1181 | TEST_F(ResolverValidationTest, NonPOTStructMemberAlignAttribute) { |
Tint team | f462606 | 2022-05-06 08:50:33 +0000 | [diff] [blame] | 1182 | Structure("S", { |
| 1183 | Member("a", ty.f32(), {MemberAlign(Source{{12, 34}}, 3)}), |
| 1184 | }); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 1185 | |
Tint team | f462606 | 2022-05-06 08:50:33 +0000 | [diff] [blame] | 1186 | EXPECT_FALSE(r()->Resolve()); |
| 1187 | EXPECT_EQ(r()->error(), "12:34 error: align value must be a positive, power-of-two integer"); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 1188 | } |
| 1189 | |
| 1190 | TEST_F(ResolverValidationTest, ZeroStructMemberAlignAttribute) { |
Tint team | f462606 | 2022-05-06 08:50:33 +0000 | [diff] [blame] | 1191 | Structure("S", { |
| 1192 | Member("a", ty.f32(), {MemberAlign(Source{{12, 34}}, 0)}), |
| 1193 | }); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 1194 | |
Tint team | f462606 | 2022-05-06 08:50:33 +0000 | [diff] [blame] | 1195 | EXPECT_FALSE(r()->Resolve()); |
| 1196 | EXPECT_EQ(r()->error(), "12:34 error: align value must be a positive, power-of-two integer"); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 1197 | } |
| 1198 | |
| 1199 | TEST_F(ResolverValidationTest, ZeroStructMemberSizeAttribute) { |
Tint team | f462606 | 2022-05-06 08:50:33 +0000 | [diff] [blame] | 1200 | Structure("S", { |
| 1201 | Member("a", ty.f32(), {MemberSize(Source{{12, 34}}, 0)}), |
| 1202 | }); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 1203 | |
Tint team | f462606 | 2022-05-06 08:50:33 +0000 | [diff] [blame] | 1204 | EXPECT_FALSE(r()->Resolve()); |
| 1205 | EXPECT_EQ(r()->error(), "12:34 error: size must be at least as big as the type's size (4)"); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 1206 | } |
| 1207 | |
| 1208 | TEST_F(ResolverValidationTest, OffsetAndSizeAttribute) { |
Tint team | f462606 | 2022-05-06 08:50:33 +0000 | [diff] [blame] | 1209 | Structure("S", { |
| 1210 | Member(Source{{12, 34}}, "a", ty.f32(), {MemberOffset(0), MemberSize(4)}), |
| 1211 | }); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 1212 | |
Tint team | f462606 | 2022-05-06 08:50:33 +0000 | [diff] [blame] | 1213 | EXPECT_FALSE(r()->Resolve()); |
| 1214 | EXPECT_EQ(r()->error(), |
| 1215 | "12:34 error: offset attributes cannot be used with align or size " |
| 1216 | "attributes"); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 1217 | } |
| 1218 | |
| 1219 | TEST_F(ResolverValidationTest, OffsetAndAlignAttribute) { |
Tint team | f462606 | 2022-05-06 08:50:33 +0000 | [diff] [blame] | 1220 | Structure("S", { |
| 1221 | Member(Source{{12, 34}}, "a", ty.f32(), {MemberOffset(0), MemberAlign(4)}), |
| 1222 | }); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 1223 | |
Tint team | f462606 | 2022-05-06 08:50:33 +0000 | [diff] [blame] | 1224 | EXPECT_FALSE(r()->Resolve()); |
| 1225 | EXPECT_EQ(r()->error(), |
| 1226 | "12:34 error: offset attributes cannot be used with align or size " |
| 1227 | "attributes"); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 1228 | } |
| 1229 | |
| 1230 | TEST_F(ResolverValidationTest, OffsetAndAlignAndSizeAttribute) { |
Tint team | f462606 | 2022-05-06 08:50:33 +0000 | [diff] [blame] | 1231 | Structure("S", { |
| 1232 | Member(Source{{12, 34}}, "a", ty.f32(), |
| 1233 | {MemberOffset(0), MemberAlign(4), MemberSize(4)}), |
| 1234 | }); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 1235 | |
Tint team | f462606 | 2022-05-06 08:50:33 +0000 | [diff] [blame] | 1236 | EXPECT_FALSE(r()->Resolve()); |
| 1237 | EXPECT_EQ(r()->error(), |
| 1238 | "12:34 error: offset attributes cannot be used with align or size " |
| 1239 | "attributes"); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 1240 | } |
| 1241 | |
| 1242 | TEST_F(ResolverTest, Expr_Constructor_Cast_Pointer) { |
Tint team | f462606 | 2022-05-06 08:50:33 +0000 | [diff] [blame] | 1243 | auto* vf = Var("vf", ty.f32()); |
| 1244 | auto* c = |
| 1245 | Construct(Source{{12, 34}}, ty.pointer<i32>(ast::StorageClass::kFunction), ExprList(vf)); |
| 1246 | auto* ip = Let("ip", ty.pointer<i32>(ast::StorageClass::kFunction), c); |
| 1247 | WrapInFunction(Decl(vf), Decl(ip)); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 1248 | |
Tint team | f462606 | 2022-05-06 08:50:33 +0000 | [diff] [blame] | 1249 | EXPECT_FALSE(r()->Resolve()); |
| 1250 | EXPECT_EQ(r()->error(), "12:34 error: type is not constructible"); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 1251 | } |
| 1252 | |
| 1253 | } // namespace |
Tint team | d2323c5 | 2022-04-11 20:49:10 +0000 | [diff] [blame] | 1254 | } // namespace tint::resolver |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 1255 | |
| 1256 | TINT_INSTANTIATE_TYPEINFO(tint::resolver::FakeStmt); |
| 1257 | TINT_INSTANTIATE_TYPEINFO(tint::resolver::FakeExpr); |