blob: 9f41f3a7a041ba77eadcca84b4d6d66e7ca1c02d [file] [log] [blame] [edit]
// Copyright 2022 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/tint/resolver/resolver.h"
#include "gmock/gmock.h"
#include "src/tint/resolver/resolver_test_helper.h"
using namespace tint::number_suffixes; // NOLINT
namespace tint::resolver {
namespace {
using ResolverIncrementDecrementValidationTest = ResolverTest;
TEST_F(ResolverIncrementDecrementValidationTest, Increment_Signed) {
// var a : i32 = 2;
// a++;
auto* var = Var("a", ty.i32(), Expr(2_i));
WrapInFunction(var, Increment(Source{{12, 34}}, "a"));
EXPECT_TRUE(r()->Resolve()) << r()->error();
}
TEST_F(ResolverIncrementDecrementValidationTest, Decrement_Signed) {
// var a : i32 = 2;
// a--;
auto* var = Var("a", ty.i32(), Expr(2_i));
WrapInFunction(var, Decrement(Source{{12, 34}}, "a"));
EXPECT_TRUE(r()->Resolve()) << r()->error();
}
TEST_F(ResolverIncrementDecrementValidationTest, Increment_Unsigned) {
// var a : u32 = 2u;
// a++;
auto* var = Var("a", ty.u32(), Expr(2_u));
WrapInFunction(var, Increment(Source{{12, 34}}, "a"));
EXPECT_TRUE(r()->Resolve()) << r()->error();
}
TEST_F(ResolverIncrementDecrementValidationTest, Decrement_Unsigned) {
// var a : u32 = 2u;
// a--;
auto* var = Var("a", ty.u32(), Expr(2_u));
WrapInFunction(var, Decrement(Source{{12, 34}}, "a"));
EXPECT_TRUE(r()->Resolve()) << r()->error();
}
TEST_F(ResolverIncrementDecrementValidationTest, ThroughPointer) {
// var a : i32;
// let b : ptr<function,i32> = &a;
// *b++;
auto* var_a = Var("a", ty.i32(), type::AddressSpace::kFunction);
auto* var_b = Let("b", ty.pointer<i32>(type::AddressSpace::kFunction), AddressOf(Expr("a")));
WrapInFunction(var_a, var_b, Increment(Source{{12, 34}}, Deref("b")));
EXPECT_TRUE(r()->Resolve()) << r()->error();
}
TEST_F(ResolverIncrementDecrementValidationTest, ThroughArray) {
// var a : array<i32, 4_u>;
// a[1i]++;
auto* var_a = Var("a", ty.array(ty.i32(), 4_u));
WrapInFunction(var_a, Increment(Source{{12, 34}}, IndexAccessor("a", 1_i)));
EXPECT_TRUE(r()->Resolve()) << r()->error();
}
TEST_F(ResolverIncrementDecrementValidationTest, ThroughVector_Index) {
// var a : vec4<i32>;
// a[1i]++;
auto* var_a = Var("a", ty.vec4(ty.i32()));
WrapInFunction(var_a, Increment(Source{{12, 34}}, IndexAccessor("a", 1_i)));
EXPECT_TRUE(r()->Resolve()) << r()->error();
}
TEST_F(ResolverIncrementDecrementValidationTest, ThroughVector_Member) {
// var a : vec4<i32>;
// a.y++;
auto* var_a = Var("a", ty.vec4(ty.i32()));
WrapInFunction(var_a, Increment(Source{{12, 34}}, MemberAccessor("a", "y")));
EXPECT_TRUE(r()->Resolve()) << r()->error();
}
TEST_F(ResolverIncrementDecrementValidationTest, Float) {
// var a : f32 = 2.0;
// a++;
auto* var = Var("a", ty.f32(), Expr(2_f));
auto* inc = Increment(Expr(Source{{12, 34}}, "a"));
WrapInFunction(var, inc);
ASSERT_FALSE(r()->Resolve());
EXPECT_EQ(r()->error(),
"12:34 error: increment statement can only be applied to an "
"integer scalar");
}
TEST_F(ResolverIncrementDecrementValidationTest, Vector) {
// var a : vec4<f32>;
// a++;
auto* var = Var("a", ty.vec4<i32>());
auto* inc = Increment(Expr(Source{{12, 34}}, "a"));
WrapInFunction(var, inc);
ASSERT_FALSE(r()->Resolve());
EXPECT_EQ(r()->error(),
"12:34 error: increment statement can only be applied to an "
"integer scalar");
}
TEST_F(ResolverIncrementDecrementValidationTest, Atomic) {
// var<workgroup> a : atomic<i32>;
// a++;
GlobalVar(Source{{12, 34}}, "a", ty.atomic(ty.i32()), type::AddressSpace::kWorkgroup);
WrapInFunction(Increment(Expr(Source{{56, 78}}, "a")));
EXPECT_FALSE(r()->Resolve());
EXPECT_EQ(r()->error(),
"56:78 error: increment statement can only be applied to an "
"integer scalar");
}
TEST_F(ResolverIncrementDecrementValidationTest, Literal) {
// 1++;
WrapInFunction(Increment(Expr(Source{{56, 78}}, 1_i)));
EXPECT_FALSE(r()->Resolve());
EXPECT_EQ(r()->error(), "56:78 error: cannot modify value of type 'i32'");
}
TEST_F(ResolverIncrementDecrementValidationTest, Constant) {
// let a = 1;
// a++;
auto* a = Let(Source{{12, 34}}, "a", Expr(1_i));
WrapInFunction(a, Increment(Expr(Source{{56, 78}}, "a")));
EXPECT_FALSE(r()->Resolve());
EXPECT_EQ(r()->error(), R"(56:78 error: cannot modify 'let'
12:34 note: 'a' is declared here:)");
}
TEST_F(ResolverIncrementDecrementValidationTest, Parameter) {
// fn func(a : i32)
// {
// a++;
// }
auto* a = Param(Source{{12, 34}}, "a", ty.i32());
Func("func", utils::Vector{a}, ty.void_(),
utils::Vector{
Increment(Expr(Source{{56, 78}}, "a")),
});
EXPECT_FALSE(r()->Resolve());
EXPECT_EQ(r()->error(), R"(56:78 error: cannot modify function parameter
12:34 note: 'a' is declared here:)");
}
TEST_F(ResolverIncrementDecrementValidationTest, ReturnValue) {
// fn func() -> i32 {
// return 0;
// }
// {
// a++;
// }
Func("func", utils::Empty, ty.i32(),
utils::Vector{
Return(0_i),
});
WrapInFunction(Increment(Call(Source{{56, 78}}, "func")));
EXPECT_FALSE(r()->Resolve());
EXPECT_EQ(r()->error(), R"(56:78 error: cannot modify value of type 'i32')");
}
TEST_F(ResolverIncrementDecrementValidationTest, ReadOnlyBuffer) {
// @group(0) @binding(0) var<storage,read> a : i32;
// {
// a++;
// }
GlobalVar(Source{{12, 34}}, "a", ty.i32(), type::AddressSpace::kStorage, type::Access::kRead,
Group(0_a), Binding(0_a));
WrapInFunction(Increment(Source{{56, 78}}, "a"));
EXPECT_FALSE(r()->Resolve());
EXPECT_EQ(r()->error(), "56:78 error: cannot modify read-only type 'ref<storage, i32, read>'");
}
TEST_F(ResolverIncrementDecrementValidationTest, Phony) {
// _++;
WrapInFunction(Increment(Phony(Source{{56, 78}})));
EXPECT_FALSE(r()->Resolve());
EXPECT_EQ(r()->error(), "56:78 error: cannot modify value of type 'void'");
}
TEST_F(ResolverIncrementDecrementValidationTest, InForLoopInit) {
// var a : i32 = 2;
// for (a++; ; ) {
// break;
// }
auto* a = Var("a", ty.i32(), Expr(2_i));
auto* loop = For(Increment(Source{{56, 78}}, "a"), nullptr, nullptr, Block(Break()));
WrapInFunction(a, loop);
EXPECT_TRUE(r()->Resolve()) << r()->error();
}
TEST_F(ResolverIncrementDecrementValidationTest, InForLoopCont) {
// var a : i32 = 2;
// for (; ; a++) {
// break;
// }
auto* a = Var("a", ty.i32(), Expr(2_i));
auto* loop = For(nullptr, nullptr, Increment(Source{{56, 78}}, "a"), Block(Break()));
WrapInFunction(a, loop);
EXPECT_TRUE(r()->Resolve()) << r()->error();
}
} // namespace
} // namespace tint::resolver