diff --git a/ets2panda/checker/ets/typeCheckingHelpers.cpp b/ets2panda/checker/ets/typeCheckingHelpers.cpp index dbc753ee3c1c88cf2c8d0a3643103271d49d368e..087c77677e4ea247e6ff02eb4d6bb0ab97a14510 100644 --- a/ets2panda/checker/ets/typeCheckingHelpers.cpp +++ b/ets2panda/checker/ets/typeCheckingHelpers.cpp @@ -666,7 +666,11 @@ Type *ETSChecker::GuaranteedTypeForUnionFieldAccess(ir::MemberExpression *member { const auto &types = etsUnionType->ConstituentTypes(); ArenaVector apparentTypes {Allocator()->Adapter()}; - const auto &propertyName = memberExpression->Property()->AsIdentifier()->Name(); + const auto *prop = memberExpression->Property(); + if (!prop->IsIdentifier() && !prop->IsStringLiteral()) { + return GlobalTypeError(); + } + const auto &propertyName = prop->IsIdentifier() ? prop->AsIdentifier()->Name() : prop->AsStringLiteral()->Str(); for (auto *type : types) { auto searchFlags = PropertySearchFlags::SEARCH_FIELD | PropertySearchFlags::SEARCH_METHOD | PropertySearchFlags::SEARCH_IN_BASE; diff --git a/ets2panda/checker/types/typeError.h b/ets2panda/checker/types/typeError.h index 9b2d87c4075983d6df1c4a553fad6b2c8f7b3a55..86f49b40cce7ce36b426c122935ace1da5a8d4e6 100644 --- a/ets2panda/checker/types/typeError.h +++ b/ets2panda/checker/types/typeError.h @@ -42,6 +42,12 @@ public: { ss << ERROR_TYPE; } + + Type *Instantiate([[maybe_unused]] ArenaAllocator *allocator, [[maybe_unused]] TypeRelation *relation, + [[maybe_unused]] GlobalTypesHolder *globalTypes) override + { + return this; + } }; } // namespace ark::es2panda::checker diff --git a/ets2panda/test/ast/compiler/ets/forof_bad_type.ets b/ets2panda/test/ast/compiler/ets/forof_bad_type.ets new file mode 100644 index 0000000000000000000000000000000000000000..1f20a9aece0f3212230314dabc6e0dc21983254d --- /dev/null +++ b/ets2panda/test/ast/compiler/ets/forof_bad_type.ets @@ -0,0 +1,28 @@ +/* + * Copyright (c) 2024-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. + */ + +declare class A { + a: A + foo(f: (a: FixedArray | A[]) => void):void + bar():A +} + +new A().bar().a.foo((x) => { + for (let e of x) { + console.log(e) + } +}) + +/* @@? 18:27 Error TypeError: Cannot find type 'Tadad'. */ diff --git a/ets2panda/test/ast/compiler/ets/union_access.ets b/ets2panda/test/ast/compiler/ets/union_access.ets new file mode 100644 index 0000000000000000000000000000000000000000..8132d86e19c9da26c90e0bf86d3df80cd89125c8 --- /dev/null +++ b/ets2panda/test/ast/compiler/ets/union_access.ets @@ -0,0 +1,23 @@ +/* + * 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. + */ + +class C{} +class A{} + +function foo(x: A | C) { + x[x+1] +} + +/* @@? 20:5 Error TypeError: Indexed access is not supported for such expression type. */ \ No newline at end of file