diff --git a/ets2panda/checker/ets/typeCheckingHelpers.cpp b/ets2panda/checker/ets/typeCheckingHelpers.cpp index 345bd6ce974ab2da9dfe9b6e497fc1ab070057fe..35b0f1d1e82d14065ff43754a1aa911f06632c61 100644 --- a/ets2panda/checker/ets/typeCheckingHelpers.cpp +++ b/ets2panda/checker/ets/typeCheckingHelpers.cpp @@ -1445,7 +1445,7 @@ bool ETSChecker::CheckLambdaAssignable(ir::Expression *param, ir::ScriptFunction if (typeAnn == nullptr) { return false; } - if (typeAnn->IsETSTypeReference() && !typeAnn->AsETSTypeReference()->TsType()->IsETSArrayType()) { + if (typeAnn->IsETSTypeReference()) { typeAnn = util::Helpers::DerefETSTypeReference(typeAnn); } diff --git a/ets2panda/test/runtime/ets/any_lambda.ets b/ets2panda/test/runtime/ets/any_lambda.ets new file mode 100644 index 0000000000000000000000000000000000000000..1fa27d79b1090f60de6c55020c380e6c19752e4b --- /dev/null +++ b/ets2panda/test/runtime/ets/any_lambda.ets @@ -0,0 +1,22 @@ +/* + * Copyright (c) 2025 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. + */ + +function foo(a: Any): string { + return "success"; +} + +function main() { + arktest.assertTrue(foo(() => {}) === "success"); +} diff --git a/ets2panda/test/runtime/ets/deref_array_type.ets b/ets2panda/test/runtime/ets/deref_array_type.ets new file mode 100644 index 0000000000000000000000000000000000000000..525a674479b6f0a07eab3877eaa7b1dadab1fc30 --- /dev/null +++ b/ets2panda/test/runtime/ets/deref_array_type.ets @@ -0,0 +1,22 @@ +/* + * Copyright (c) 2025 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. + */ + +function foo(a: Any): string { + return "success"; +} + +function main() { + arktest.assertTrue(foo(() : Array => { return [1, 2]; }) === "success"); +} diff --git a/ets2panda/util/helpers.cpp b/ets2panda/util/helpers.cpp index 37b43e97a80344974236972ce83daa8178b55935..8d6ab0024b3aa542ca6c09ae6d8b2d697dc367d5 100644 --- a/ets2panda/util/helpers.cpp +++ b/ets2panda/util/helpers.cpp @@ -61,6 +61,13 @@ bool Helpers::IsGlobalIdentifier(const util::StringView &str) return (str.Is("NaN") || str.Is("undefined") || str.Is("Infinity")); } +bool Helpers::IsBuiltinType(const util::StringView &name) +{ + return name.Is(compiler::Signatures::PARTIAL_TYPE_NAME) || name.Is(compiler::Signatures::REQUIRED_TYPE_NAME) || + name.Is(compiler::Signatures::READONLY_TYPE_NAME) || name.Is(compiler::Signatures::FIXED_ARRAY_TYPE_NAME) || + name.Is(compiler::Signatures::ANY_TYPE_NAME); +} + bool Helpers::ContainSpreadElement(const ArenaVector &args) { return std::any_of(args.begin(), args.end(), [](const auto *it) { return it->IsSpreadElement(); }); @@ -820,9 +827,15 @@ ir::AstNode *Helpers::DerefETSTypeReference(ir::AstNode *node) do { auto *name = node->AsETSTypeReference()->Part()->GetIdent(); - ES2PANDA_ASSERT(name->IsIdentifier()); + if (name->IsBrokenExpression()) { + return node; + } auto *var = name->AsIdentifier()->Variable(); - ES2PANDA_ASSERT(var != nullptr); + if (var == nullptr) { + // var == nullptr means it might be built-in type like Any type, which would cause unexpected crash below. + ES2PANDA_ASSERT(IsBuiltinType(name->AsIdentifier()->Name())); + return node; + } auto *declNode = var->Declaration()->Node(); if (!declNode->IsTSTypeAliasDeclaration()) { return declNode; diff --git a/ets2panda/util/helpers.h b/ets2panda/util/helpers.h index a1a7ed6cd7fc03fda853aaa9bef04922cd4d31fd..2779b7d34c984c9be18d177de79770a84df3825a 100644 --- a/ets2panda/util/helpers.h +++ b/ets2panda/util/helpers.h @@ -101,6 +101,7 @@ public: Helpers() = delete; static bool IsGlobalIdentifier(const util::StringView &str); + static bool IsBuiltinType(const util::StringView &name); static bool ContainSpreadElement(const ArenaVector &args); static util::StringView LiteralToPropName(const ir::Expression *lit);