diff --git a/ets2panda/checker/ets/helpers.cpp b/ets2panda/checker/ets/helpers.cpp index 5e11b6fe85bcd2bd0d6a3a5efd8020b9a5f2240a..e2c83a6116993214c92b69ec2ec19efbbcec211f 100644 --- a/ets2panda/checker/ets/helpers.cpp +++ b/ets2panda/checker/ets/helpers.cpp @@ -77,11 +77,15 @@ void ETSChecker::CheckTruthinessOfType(ir::Expression *expr) checker::Type *type = expr->Check(this); auto *unboxed_type = ETSBuiltinTypeAsConditionalType(type); + if (unboxed_type == nullptr) { + ThrowTypeError("Condition must be of possible condition type", expr->Start()); + } + if (unboxed_type == GlobalBuiltinVoidType() || unboxed_type->IsETSVoidType()) { ThrowTypeError("An expression of type 'void' cannot be tested for truthiness", expr->Start()); } - if (unboxed_type != nullptr && !unboxed_type->IsConditionalExprType()) { + if (!unboxed_type->IsConditionalExprType()) { ThrowTypeError("Condition must be of possible condition type", expr->Start()); } diff --git a/ets2panda/test/CMakeLists.txt b/ets2panda/test/CMakeLists.txt index 9bb12430391fd061eb05526770eeef7f02e5d3f7..a76d250a09ea8132b6c0d31c5362cdd00cd5978b 100644 --- a/ets2panda/test/CMakeLists.txt +++ b/ets2panda/test/CMakeLists.txt @@ -133,7 +133,19 @@ if(PANDA_WITH_ETS) SANITIZERS ${PANDA_SANITIZERS_LIST} - ) + ) + panda_add_gtest( + NAME es2panda_checker_tests + SOURCES + unit/checker_test.cpp + LIBRARIES + es2panda-public es2panda-lib + INCLUDE_DIRS + ${ES2PANDA_ROOT} + SANITIZERS + ${PANDA_SANITIZERS_LIST} + ) + endif() panda_add_gtest( diff --git a/ets2panda/test/unit/checker_test.cpp b/ets2panda/test/unit/checker_test.cpp new file mode 100644 index 0000000000000000000000000000000000000000..69a0519e62ef2ae3fe2fe6d5048bfb411b431616 --- /dev/null +++ b/ets2panda/test/unit/checker_test.cpp @@ -0,0 +1,59 @@ +/** + * Copyright (c) 2021-2023 Huawei Device Co., Ltd. + * 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 +#include "macros.h" +#include "public/es2panda_lib.h" + +class CheckerTest : public testing::Test { +public: + CheckerTest() + { + impl_ = es2panda_GetImpl(ES2PANDA_LIB_VERSION); + // NOLINTNEXTLINE(modernize-avoid-c-arrays) + char const *argv[] = {"test"}; + cfg_ = impl_->CreateConfig(1, argv); + } + + ~CheckerTest() override + { + impl_->DestroyConfig(cfg_); + } + + NO_COPY_SEMANTIC(CheckerTest); + NO_MOVE_SEMANTIC(CheckerTest); + +protected: + // NOLINTBEGIN(misc-non-private-member-variables-in-classes) + es2panda_Impl const *impl_; + es2panda_Config *cfg_; + // NOLINTEND(misc-non-private-member-variables-in-classes) +}; + +TEST_F(CheckerTest, ExtendedConditionalExpressionFunctor) +{ + char const *text = R"XXX( +class A { + m() {} + m2() { this.m ? "a": "b" } +} +)XXX"; + es2panda_Context *ctx = impl_->CreateContextFromString(cfg_, text, "dummy.ets"); + ctx = impl_->ProceedToState(ctx, ES2PANDA_STATE_CHECKED); + ASSERT_EQ(std::string(impl_->ContextErrorMessage(ctx)), + "TypeError: Condition must be of possible condition type[dummy.ets:4,12]"); + + impl_->DestroyContext(ctx); +} \ No newline at end of file