2021-03-18 21:14:44 +00:00
|
|
|
// Copyright 2021 The Tint Authors.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
#include "src/resolver/resolver.h"
|
|
|
|
|
|
|
|
#include "gmock/gmock.h"
|
|
|
|
#include "src/resolver/resolver_test_helper.h"
|
2021-04-19 22:54:43 +00:00
|
|
|
#include "src/sem/storage_texture_type.h"
|
2021-03-18 21:14:44 +00:00
|
|
|
|
|
|
|
namespace tint {
|
|
|
|
namespace resolver {
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
using ResolverAssignmentValidationTest = ResolverTest;
|
|
|
|
|
|
|
|
TEST_F(ResolverAssignmentValidationTest, AssignIncompatibleTypes) {
|
|
|
|
// {
|
|
|
|
// var a : i32 = 2;
|
|
|
|
// a = 2.3;
|
|
|
|
// }
|
|
|
|
|
|
|
|
auto* var = Var("a", ty.i32(), ast::StorageClass::kNone, Expr(2));
|
|
|
|
|
2021-05-18 10:28:48 +00:00
|
|
|
auto* assign = Assign(Source{{12, 34}}, "a", 2.3f);
|
2021-03-18 21:14:44 +00:00
|
|
|
WrapInFunction(var, assign);
|
|
|
|
|
|
|
|
ASSERT_FALSE(r()->Resolve());
|
|
|
|
|
2021-05-18 10:28:48 +00:00
|
|
|
EXPECT_EQ(r()->error(), "12:34 error: cannot assign 'f32' to 'i32'");
|
2021-03-18 21:14:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(ResolverAssignmentValidationTest,
|
|
|
|
AssignCompatibleTypesInBlockStatement_Pass) {
|
|
|
|
// {
|
|
|
|
// var a : i32 = 2;
|
|
|
|
// a = 2
|
|
|
|
// }
|
|
|
|
auto* var = Var("a", ty.i32(), ast::StorageClass::kNone, Expr(2));
|
2021-05-18 10:28:48 +00:00
|
|
|
WrapInFunction(var, Assign("a", 2));
|
2021-03-18 21:14:44 +00:00
|
|
|
|
2021-04-06 14:07:07 +00:00
|
|
|
ASSERT_TRUE(r()->Resolve()) << r()->error();
|
2021-03-18 21:14:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(ResolverAssignmentValidationTest,
|
|
|
|
AssignIncompatibleTypesInBlockStatement_Fail) {
|
|
|
|
// {
|
|
|
|
// var a : i32 = 2;
|
|
|
|
// a = 2.3;
|
|
|
|
// }
|
|
|
|
|
|
|
|
auto* var = Var("a", ty.i32(), ast::StorageClass::kNone, Expr(2));
|
2021-05-18 10:28:48 +00:00
|
|
|
WrapInFunction(var, Assign(Source{{12, 34}}, "a", 2.3f));
|
2021-03-18 21:14:44 +00:00
|
|
|
|
|
|
|
ASSERT_FALSE(r()->Resolve());
|
|
|
|
|
2021-05-18 10:28:48 +00:00
|
|
|
EXPECT_EQ(r()->error(), "12:34 error: cannot assign 'f32' to 'i32'");
|
2021-03-18 21:14:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(ResolverAssignmentValidationTest,
|
|
|
|
AssignIncompatibleTypesInNestedBlockStatement_Fail) {
|
|
|
|
// {
|
|
|
|
// {
|
|
|
|
// var a : i32 = 2;
|
|
|
|
// a = 2.3;
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
|
|
|
|
auto* var = Var("a", ty.i32(), ast::StorageClass::kNone, Expr(2));
|
2021-05-18 10:28:48 +00:00
|
|
|
auto* inner_block = Block(Decl(var), Assign(Source{{12, 34}}, "a", 2.3f));
|
2021-04-22 13:50:53 +00:00
|
|
|
auto* outer_block = Block(inner_block);
|
2021-04-06 14:07:07 +00:00
|
|
|
WrapInFunction(outer_block);
|
2021-03-18 21:14:44 +00:00
|
|
|
|
|
|
|
ASSERT_FALSE(r()->Resolve());
|
|
|
|
|
2021-05-18 10:28:48 +00:00
|
|
|
EXPECT_EQ(r()->error(), "12:34 error: cannot assign 'f32' to 'i32'");
|
2021-03-18 21:14:44 +00:00
|
|
|
}
|
|
|
|
|
2021-03-31 13:26:43 +00:00
|
|
|
TEST_F(ResolverAssignmentValidationTest, AssignToScalar_Fail) {
|
|
|
|
// var my_var : i32 = 2;
|
|
|
|
// 1 = my_var;
|
|
|
|
|
|
|
|
auto* var = Var("my_var", ty.i32(), ast::StorageClass::kNone, Expr(2));
|
2021-05-18 10:28:48 +00:00
|
|
|
WrapInFunction(var, Assign(Expr(Source{{12, 34}}, 1), "my_var"));
|
2021-03-31 13:26:43 +00:00
|
|
|
|
|
|
|
EXPECT_FALSE(r()->Resolve());
|
2021-05-18 10:28:48 +00:00
|
|
|
EXPECT_EQ(r()->error(), "12:34 error: cannot assign to value of type 'i32'");
|
2021-03-31 13:26:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(ResolverAssignmentValidationTest, AssignCompatibleTypes_Pass) {
|
2021-05-18 10:28:48 +00:00
|
|
|
// var a : i32 = 2;
|
2021-03-31 13:26:43 +00:00
|
|
|
// a = 2
|
|
|
|
auto* var = Var("a", ty.i32(), ast::StorageClass::kNone, Expr(2));
|
2021-05-18 10:28:48 +00:00
|
|
|
WrapInFunction(var, Assign(Source{{12, 34}}, "a", 2));
|
2021-03-31 13:26:43 +00:00
|
|
|
|
|
|
|
EXPECT_TRUE(r()->Resolve()) << r()->error();
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(ResolverAssignmentValidationTest,
|
|
|
|
AssignCompatibleTypesThroughAlias_Pass) {
|
|
|
|
// alias myint = i32;
|
2021-05-18 10:28:48 +00:00
|
|
|
// var a : myint = 2;
|
2021-03-31 13:26:43 +00:00
|
|
|
// a = 2
|
2021-06-09 14:32:14 +00:00
|
|
|
auto* myint = Alias("myint", ty.i32());
|
2021-03-31 13:26:43 +00:00
|
|
|
auto* var = Var("a", myint, ast::StorageClass::kNone, Expr(2));
|
2021-05-18 10:28:48 +00:00
|
|
|
WrapInFunction(var, Assign(Source{{12, 34}}, "a", 2));
|
2021-03-31 13:26:43 +00:00
|
|
|
|
|
|
|
EXPECT_TRUE(r()->Resolve()) << r()->error();
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(ResolverAssignmentValidationTest,
|
|
|
|
AssignCompatibleTypesInferRHSLoad_Pass) {
|
|
|
|
// var a : i32 = 2;
|
|
|
|
// var b : i32 = 3;
|
|
|
|
// a = b;
|
|
|
|
auto* var_a = Var("a", ty.i32(), ast::StorageClass::kNone, Expr(2));
|
|
|
|
auto* var_b = Var("b", ty.i32(), ast::StorageClass::kNone, Expr(3));
|
2021-05-18 10:28:48 +00:00
|
|
|
WrapInFunction(var_a, var_b, Assign(Source{{12, 34}}, "a", "b"));
|
2021-03-31 13:26:43 +00:00
|
|
|
|
|
|
|
EXPECT_TRUE(r()->Resolve()) << r()->error();
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(ResolverAssignmentValidationTest, AssignThroughPointer_Pass) {
|
2021-05-18 10:28:48 +00:00
|
|
|
// var a : i32;
|
|
|
|
// let b : ptr<function,i32> = &a;
|
|
|
|
// *b = 2;
|
2021-03-31 13:26:43 +00:00
|
|
|
const auto func = ast::StorageClass::kFunction;
|
2021-06-04 19:55:08 +00:00
|
|
|
auto* var_a = Var("a", ty.i32(), func, Expr(2));
|
|
|
|
auto* var_b = Const("b", ty.pointer<int>(func), AddressOf(Expr("a")));
|
2021-05-18 10:28:48 +00:00
|
|
|
WrapInFunction(var_a, var_b, Assign(Source{{12, 34}}, Deref("b"), 2));
|
2021-03-31 13:26:43 +00:00
|
|
|
|
|
|
|
EXPECT_TRUE(r()->Resolve()) << r()->error();
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(ResolverAssignmentValidationTest, AssignToConstant_Fail) {
|
|
|
|
// {
|
2021-04-08 15:46:17 +00:00
|
|
|
// let a : i32 = 2;
|
2021-03-31 13:26:43 +00:00
|
|
|
// a = 2
|
|
|
|
// }
|
|
|
|
auto* var = Const("a", ty.i32(), Expr(2));
|
2021-05-18 10:28:48 +00:00
|
|
|
WrapInFunction(var, Assign(Expr(Source{{12, 34}}, "a"), 2));
|
2021-03-31 13:26:43 +00:00
|
|
|
|
|
|
|
EXPECT_FALSE(r()->Resolve());
|
2021-05-18 10:28:48 +00:00
|
|
|
EXPECT_EQ(r()->error(), "12:34 error: cannot assign to value of type 'i32'");
|
2021-03-31 13:26:43 +00:00
|
|
|
}
|
|
|
|
|
2021-06-09 09:12:57 +00:00
|
|
|
// TODO(crbug.com/tint/809): The var has an implicit access::read, and so this
|
|
|
|
// test will pass again when we start validating the access mode on the LHS of
|
|
|
|
// an assignment.
|
|
|
|
TEST_F(ResolverAssignmentValidationTest, DISABLED_AssignNonStorable_Fail) {
|
2021-06-04 20:41:47 +00:00
|
|
|
// var a : texture_storage_1d<rgba8unorm, read>;
|
|
|
|
// var b : texture_storage_1d<rgba8unorm, read>;
|
2021-03-31 13:26:43 +00:00
|
|
|
// a = b;
|
|
|
|
|
2021-05-04 19:02:01 +00:00
|
|
|
auto make_type = [&] {
|
2021-06-04 20:41:47 +00:00
|
|
|
return ty.storage_texture(ast::TextureDimension::k1d,
|
|
|
|
ast::ImageFormat::kRgba8Unorm,
|
|
|
|
ast::Access::kRead);
|
2021-05-04 19:02:01 +00:00
|
|
|
};
|
2021-03-31 13:26:43 +00:00
|
|
|
|
2021-06-04 19:55:08 +00:00
|
|
|
Global("a", make_type(), ast::StorageClass::kNone,
|
|
|
|
ast::DecorationList{
|
2021-05-18 10:28:48 +00:00
|
|
|
create<ast::BindingDecoration>(0),
|
|
|
|
create<ast::GroupDecoration>(0),
|
|
|
|
});
|
2021-06-04 19:55:08 +00:00
|
|
|
Global("b", make_type(), ast::StorageClass::kNone,
|
|
|
|
ast::DecorationList{
|
2021-05-18 10:28:48 +00:00
|
|
|
create<ast::BindingDecoration>(1),
|
|
|
|
create<ast::GroupDecoration>(0),
|
|
|
|
});
|
2021-03-31 13:26:43 +00:00
|
|
|
|
2021-05-18 10:28:48 +00:00
|
|
|
WrapInFunction(Assign("a", Expr(Source{{12, 34}}, "b")));
|
2021-03-31 13:26:43 +00:00
|
|
|
|
|
|
|
EXPECT_FALSE(r()->Resolve());
|
2021-05-18 10:28:48 +00:00
|
|
|
EXPECT_EQ(
|
|
|
|
r()->error(),
|
2021-06-04 20:41:47 +00:00
|
|
|
R"(12:34 error: 'texture_storage_1d<rgba8unorm, read>' is not storable)");
|
2021-03-31 13:26:43 +00:00
|
|
|
}
|
|
|
|
|
2021-03-18 21:14:44 +00:00
|
|
|
} // namespace
|
|
|
|
} // namespace resolver
|
|
|
|
} // namespace tint
|