tint: Rename 'static_assert' to 'const_assert'

Bug: tint:1807
Change-Id: I2c2a205ada01ad14d0bf6620a3dc3ec84dd7ee67
Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/117212
Kokoro: Kokoro <noreply+kokoro@google.com>
Reviewed-by: Dan Sinclair <dsinclair@chromium.org>
Commit-Queue: Ben Clayton <bclayton@google.com>
This commit is contained in:
Ben Clayton
2023-01-24 14:59:43 +00:00
committed by Dawn LUCI CQ
parent 9dc48bcef3
commit c98d57d662
38 changed files with 565 additions and 302 deletions

View File

@@ -36,7 +36,7 @@ struct ResolverAliasAnalysisTest : public resolver::TestHelper, public testing::
// }
struct TwoPointerConfig {
type::AddressSpace address_space; // The address space for the pointers.
bool aliased; // Whether the pointers alias or not.
bool aliased; // Whether the pointers alias or not.
};
class TwoPointers : public ResolverTestWithParam<TwoPointerConfig> {
protected:

View File

@@ -0,0 +1,107 @@
// 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 ResolverConstAssertTest = ResolverTest;
TEST_F(ResolverConstAssertTest, Global_True_Pass) {
GlobalConstAssert(true);
ASSERT_TRUE(r()->Resolve()) << r()->error();
}
TEST_F(ResolverConstAssertTest, Global_False_Fail) {
GlobalConstAssert(Source{{12, 34}}, false);
EXPECT_FALSE(r()->Resolve());
EXPECT_EQ(r()->error(), "12:34 error: const assertion failed");
}
TEST_F(ResolverConstAssertTest, Global_Const_Pass) {
GlobalConst("C", ty.bool_(), Expr(true));
GlobalConstAssert("C");
ASSERT_TRUE(r()->Resolve()) << r()->error();
}
TEST_F(ResolverConstAssertTest, Global_Const_Fail) {
GlobalConst("C", ty.bool_(), Expr(false));
GlobalConstAssert(Source{{12, 34}}, "C");
EXPECT_FALSE(r()->Resolve());
EXPECT_EQ(r()->error(), "12:34 error: const assertion failed");
}
TEST_F(ResolverConstAssertTest, Global_LessThan_Pass) {
GlobalConstAssert(LessThan(2_i, 3_i));
ASSERT_TRUE(r()->Resolve()) << r()->error();
}
TEST_F(ResolverConstAssertTest, Global_LessThan_Fail) {
GlobalConstAssert(Source{{12, 34}}, LessThan(4_i, 3_i));
EXPECT_FALSE(r()->Resolve());
EXPECT_EQ(r()->error(), "12:34 error: const assertion failed");
}
TEST_F(ResolverConstAssertTest, Local_True_Pass) {
WrapInFunction(ConstAssert(true));
ASSERT_TRUE(r()->Resolve()) << r()->error();
}
TEST_F(ResolverConstAssertTest, Local_False_Fail) {
WrapInFunction(ConstAssert(Source{{12, 34}}, false));
EXPECT_FALSE(r()->Resolve());
EXPECT_EQ(r()->error(), "12:34 error: const assertion failed");
}
TEST_F(ResolverConstAssertTest, Local_Const_Pass) {
GlobalConst("C", ty.bool_(), Expr(true));
WrapInFunction(ConstAssert("C"));
ASSERT_TRUE(r()->Resolve()) << r()->error();
}
TEST_F(ResolverConstAssertTest, Local_Const_Fail) {
GlobalConst("C", ty.bool_(), Expr(false));
WrapInFunction(ConstAssert(Source{{12, 34}}, "C"));
EXPECT_FALSE(r()->Resolve());
EXPECT_EQ(r()->error(), "12:34 error: const assertion failed");
}
TEST_F(ResolverConstAssertTest, Local_NonConst) {
GlobalVar("V", ty.bool_(), Expr(true), type::AddressSpace::kPrivate);
WrapInFunction(ConstAssert(Expr(Source{{12, 34}}, "V")));
EXPECT_FALSE(r()->Resolve());
EXPECT_EQ(r()->error(),
"12:34 error: const assertion requires a const-expression, but expression is a "
"runtime-expression");
}
TEST_F(ResolverConstAssertTest, Local_LessThan_Pass) {
WrapInFunction(ConstAssert(LessThan(2_i, 3_i)));
ASSERT_TRUE(r()->Resolve()) << r()->error();
}
TEST_F(ResolverConstAssertTest, Local_LessThan_Fail) {
WrapInFunction(ConstAssert(Source{{12, 34}}, LessThan(4_i, 3_i)));
EXPECT_FALSE(r()->Resolve());
EXPECT_EQ(r()->error(), "12:34 error: const assertion failed");
}
} // namespace
} // namespace tint::resolver

View File

@@ -207,7 +207,7 @@ class DependencyScanner {
[&](const ast::Enable*) {
// Enable directives do not effect the dependency graph.
},
[&](const ast::StaticAssert* assertion) { TraverseExpression(assertion->condition); },
[&](const ast::ConstAssert* assertion) { TraverseExpression(assertion->condition); },
[&](Default) { UnhandledNode(diagnostics_, global->node); });
}
@@ -319,7 +319,7 @@ class DependencyScanner {
TraverseExpression(w->condition);
TraverseStatement(w->body);
},
[&](const ast::StaticAssert* assertion) { TraverseExpression(assertion->condition); },
[&](const ast::ConstAssert* assertion) { TraverseExpression(assertion->condition); },
[&](Default) {
if (TINT_UNLIKELY((!stmt->IsAnyOf<ast::BreakStatement, ast::ContinueStatement,
ast::DiscardStatement>()))) {
@@ -556,7 +556,7 @@ struct DependencyAnalysis {
[&](const ast::Function* func) { return func->symbol; },
[&](const ast::Variable* var) { return var->symbol; },
[&](const ast::Enable*) { return Symbol(); },
[&](const ast::StaticAssert*) { return Symbol(); },
[&](const ast::ConstAssert*) { return Symbol(); },
[&](Default) {
UnhandledNode(diagnostics_, node);
return Symbol{};
@@ -575,12 +575,12 @@ struct DependencyAnalysis {
/// declaration
std::string KindOf(const ast::Node* node) {
return Switch(
node, //
[&](const ast::Struct*) { return "struct"; }, //
[&](const ast::Alias*) { return "alias"; }, //
[&](const ast::Function*) { return "function"; }, //
[&](const ast::Variable* v) { return v->Kind(); }, //
[&](const ast::StaticAssert*) { return "static_assert"; }, //
node, //
[&](const ast::Struct*) { return "struct"; }, //
[&](const ast::Alias*) { return "alias"; }, //
[&](const ast::Function*) { return "function"; }, //
[&](const ast::Variable* v) { return v->Kind(); }, //
[&](const ast::ConstAssert*) { return "const_assert"; }, //
[&](Default) {
UnhandledNode(diagnostics_, node);
return "<error>";

View File

@@ -155,7 +155,7 @@ bool Resolver::ResolveInternal() {
[&](const ast::TypeDecl* td) { return TypeDecl(td); },
[&](const ast::Function* func) { return Function(func); },
[&](const ast::Variable* var) { return GlobalVariable(var); },
[&](const ast::StaticAssert* sa) { return StaticAssert(sa); },
[&](const ast::ConstAssert* ca) { return ConstAssert(ca); },
[&](Default) {
TINT_UNREACHABLE(Resolver, diagnostics_)
<< "unhandled global declaration: " << decl->TypeInfo().name;
@@ -936,8 +936,8 @@ sem::GlobalVariable* Resolver::GlobalVariable(const ast::Variable* v) {
return sem;
}
sem::Statement* Resolver::StaticAssert(const ast::StaticAssert* assertion) {
ExprEvalStageConstraint constraint{sem::EvaluationStage::kConstant, "static assertion"};
sem::Statement* Resolver::ConstAssert(const ast::ConstAssert* assertion) {
ExprEvalStageConstraint constraint{sem::EvaluationStage::kConstant, "const assertion"};
TINT_SCOPED_ASSIGNMENT(expr_eval_stage_constraint_, constraint);
auto* expr = Expression(assertion->condition);
if (!expr) {
@@ -946,12 +946,12 @@ sem::Statement* Resolver::StaticAssert(const ast::StaticAssert* assertion) {
auto* cond = expr->ConstantValue();
if (auto* ty = cond->Type(); !ty->Is<type::Bool>()) {
AddError(
"static assertion condition must be a bool, got '" + builder_->FriendlyName(ty) + "'",
"const assertion condition must be a bool, got '" + builder_->FriendlyName(ty) + "'",
assertion->condition->source);
return nullptr;
}
if (!cond->ValueAs<bool>()) {
AddError("static assertion failed", assertion->source);
AddError("const assertion failed", assertion->source);
return nullptr;
}
auto* sem =
@@ -1258,7 +1258,7 @@ sem::Statement* Resolver::Statement(const ast::Statement* stmt) {
[&](const ast::IncrementDecrementStatement* i) { return IncrementDecrementStatement(i); },
[&](const ast::ReturnStatement* r) { return ReturnStatement(r); },
[&](const ast::VariableDeclStatement* v) { return VariableDeclStatement(v); },
[&](const ast::StaticAssert* sa) { return StaticAssert(sa); },
[&](const ast::ConstAssert* sa) { return ConstAssert(sa); },
// Error cases
[&](const ast::CaseStatement*) {

View File

@@ -230,6 +230,7 @@ class Resolver {
sem::CaseStatement* CaseStatement(const ast::CaseStatement*, const type::Type*);
sem::Statement* CompoundAssignmentStatement(const ast::CompoundAssignmentStatement*);
sem::Statement* ContinueStatement(const ast::ContinueStatement*);
sem::Statement* ConstAssert(const ast::ConstAssert*);
sem::Statement* DiscardStatement(const ast::DiscardStatement*);
sem::ForLoopStatement* ForLoopStatement(const ast::ForLoopStatement*);
sem::WhileStatement* WhileStatement(const ast::WhileStatement*);
@@ -240,7 +241,6 @@ class Resolver {
sem::LoopStatement* LoopStatement(const ast::LoopStatement*);
sem::Statement* ReturnStatement(const ast::ReturnStatement*);
sem::Statement* Statement(const ast::Statement*);
sem::Statement* StaticAssert(const ast::StaticAssert*);
sem::SwitchStatement* SwitchStatement(const ast::SwitchStatement* s);
sem::Statement* VariableDeclStatement(const ast::VariableDeclStatement*);
bool Statements(utils::VectorRef<const ast::Statement*>);

View File

@@ -1,107 +0,0 @@
// 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 ResolverStaticAssertTest = ResolverTest;
TEST_F(ResolverStaticAssertTest, Global_True_Pass) {
GlobalStaticAssert(true);
ASSERT_TRUE(r()->Resolve()) << r()->error();
}
TEST_F(ResolverStaticAssertTest, Global_False_Fail) {
GlobalStaticAssert(Source{{12, 34}}, false);
EXPECT_FALSE(r()->Resolve());
EXPECT_EQ(r()->error(), "12:34 error: static assertion failed");
}
TEST_F(ResolverStaticAssertTest, Global_Const_Pass) {
GlobalConst("C", ty.bool_(), Expr(true));
GlobalStaticAssert("C");
ASSERT_TRUE(r()->Resolve()) << r()->error();
}
TEST_F(ResolverStaticAssertTest, Global_Const_Fail) {
GlobalConst("C", ty.bool_(), Expr(false));
GlobalStaticAssert(Source{{12, 34}}, "C");
EXPECT_FALSE(r()->Resolve());
EXPECT_EQ(r()->error(), "12:34 error: static assertion failed");
}
TEST_F(ResolverStaticAssertTest, Global_LessThan_Pass) {
GlobalStaticAssert(LessThan(2_i, 3_i));
ASSERT_TRUE(r()->Resolve()) << r()->error();
}
TEST_F(ResolverStaticAssertTest, Global_LessThan_Fail) {
GlobalStaticAssert(Source{{12, 34}}, LessThan(4_i, 3_i));
EXPECT_FALSE(r()->Resolve());
EXPECT_EQ(r()->error(), "12:34 error: static assertion failed");
}
TEST_F(ResolverStaticAssertTest, Local_True_Pass) {
WrapInFunction(StaticAssert(true));
ASSERT_TRUE(r()->Resolve()) << r()->error();
}
TEST_F(ResolverStaticAssertTest, Local_False_Fail) {
WrapInFunction(StaticAssert(Source{{12, 34}}, false));
EXPECT_FALSE(r()->Resolve());
EXPECT_EQ(r()->error(), "12:34 error: static assertion failed");
}
TEST_F(ResolverStaticAssertTest, Local_Const_Pass) {
GlobalConst("C", ty.bool_(), Expr(true));
WrapInFunction(StaticAssert("C"));
ASSERT_TRUE(r()->Resolve()) << r()->error();
}
TEST_F(ResolverStaticAssertTest, Local_Const_Fail) {
GlobalConst("C", ty.bool_(), Expr(false));
WrapInFunction(StaticAssert(Source{{12, 34}}, "C"));
EXPECT_FALSE(r()->Resolve());
EXPECT_EQ(r()->error(), "12:34 error: static assertion failed");
}
TEST_F(ResolverStaticAssertTest, Local_NonConst) {
GlobalVar("V", ty.bool_(), Expr(true), type::AddressSpace::kPrivate);
WrapInFunction(StaticAssert(Expr(Source{{12, 34}}, "V")));
EXPECT_FALSE(r()->Resolve());
EXPECT_EQ(r()->error(),
"12:34 error: static assertion requires a const-expression, but expression is a "
"runtime-expression");
}
TEST_F(ResolverStaticAssertTest, Local_LessThan_Pass) {
WrapInFunction(StaticAssert(LessThan(2_i, 3_i)));
ASSERT_TRUE(r()->Resolve()) << r()->error();
}
TEST_F(ResolverStaticAssertTest, Local_LessThan_Fail) {
WrapInFunction(StaticAssert(Source{{12, 34}}, LessThan(4_i, 3_i)));
EXPECT_FALSE(r()->Resolve());
EXPECT_EQ(r()->error(), "12:34 error: static assertion failed");
}
} // namespace
} // namespace tint::resolver

View File

@@ -1067,7 +1067,7 @@ class UniformityGraph {
return cf;
},
[&](const ast::StaticAssert*) {
[&](const ast::ConstAssert*) {
return cf; // No impact on uniformity
},