diff --git a/ets2panda/checker/ets/typeCheckingHelpers.cpp b/ets2panda/checker/ets/typeCheckingHelpers.cpp index 6496cdb8ce75eb3a25d6fc59349a8ce4840f40fc..c225e9188dce53612c1132f2d7730ca733ba5c0f 100644 --- a/ets2panda/checker/ets/typeCheckingHelpers.cpp +++ b/ets2panda/checker/ets/typeCheckingHelpers.cpp @@ -731,7 +731,15 @@ Type *ETSChecker::GuaranteedTypeForUnionFieldAccess(ir::MemberExpression *member const auto &types = etsUnionType->ConstituentTypes(); ArenaVector apparentTypes {ProgramAllocator()->Adapter()}; const auto *prop = memberExpression->Property(); - const auto &propertyName = prop->IsIdentifier() ? prop->AsIdentifier()->Name() : prop->AsStringLiteral()->Str(); + util::StringView propertyName; + if (prop->IsIdentifier()) { + propertyName = prop->AsIdentifier()->Name(); + } else if (prop->IsStringLiteral()) { + propertyName = prop->AsStringLiteral()->Str(); + } else { + return GlobalTypeError(); + } + for (auto *type : types) { auto searchFlags = PropertySearchFlags::SEARCH_FIELD | PropertySearchFlags::SEARCH_METHOD | PropertySearchFlags::SEARCH_IN_BASE; diff --git a/ets2panda/test/ast/compiler/ets/union_field_access_2.ets b/ets2panda/test/ast/compiler/ets/union_field_access_2.ets new file mode 100644 index 0000000000000000000000000000000000000000..f3810056329f128bf4653a78446cd05f6a3cd3c2 --- /dev/null +++ b/ets2panda/test/ast/compiler/ets/union_field_access_2.ets @@ -0,0 +1,29 @@ +/* + * 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. + */ + +// ensure compiler doesn't crash when compiling invalid code. + +(a ? delete arguments==({: false, Float64Array: []}) : [ArrayBuffer[false]]) + +/* @@? 18:1 Error TypeError: Indexed access is not supported for such expression type. */ +/* @@? 18:2 Error TypeError: Unresolved reference a */ +/* @@? 18:6 Error TypeError: Unresolved reference delete */ +/* @@? 18:13 Error SyntaxError: Unexpected token. */ +/* @@? 18:13 Error TypeError: Unresolved reference arguments */ +/* @@? 18:24 Error TypeError: need to specify target type for class composite */ +/* @@? 18:26 Error SyntaxError: Unexpected token. */ +/* @@? 18:28 Error SyntaxError: Unexpected token. */ +/* @@? 18:54 Error SyntaxError: Unexpected token, expected ')'. */ +/* @@? 18:76 Error SyntaxError: Unexpected token ')'. */