From d2908bc6a14978dd65de1091f8fdf8d8c8d8718b Mon Sep 17 00:00:00 2001 From: xuxinjie4 Date: Wed, 23 Jul 2025 15:37:28 +0800 Subject: [PATCH] Fix serval crash Issue: https://gitee.com/openharmony/arkcompiler_ets_frontend/issues/ICOAHS?from=project-issue Signed-off-by: xuxinjie4 --- ets2panda/checker/ETSAnalyzer.cpp | 15 ++++++++-- ets2panda/checker/ets/utilityTypeHandlers.cpp | 6 +--- .../test/ast/compiler/ets/bad_call_setter.ets | 1 - .../ast/compiler/ets/same_name_field_err.ets | 1 - .../ets/unresolve_class_with_type_infer.ets | 22 ++++++++++++++ .../compiler/ets/wrong_variable_binding.ets | 30 +++++++++++++++++++ .../spreadexpr_in_newexpr_neg01.ets | 1 - .../class-instance-field-redeclaration.ets | 1 - .../ets/class-static-field-redeclaration.ets | 1 - ets2panda/test/ast/parser/ets/enum22.ets | 1 - ets2panda/test/ast/parser/ets/enum23.ets | 3 +- ets2panda/test/ast/parser/ets/fields.ets | 1 - .../ets/spreadexpr_in_newexpr_neg01.ets | 3 +- 13 files changed, 68 insertions(+), 18 deletions(-) create mode 100644 ets2panda/test/ast/compiler/ets/unresolve_class_with_type_infer.ets create mode 100644 ets2panda/test/ast/compiler/ets/wrong_variable_binding.ets diff --git a/ets2panda/checker/ETSAnalyzer.cpp b/ets2panda/checker/ETSAnalyzer.cpp index 74631ae976..87787c5d78 100644 --- a/ets2panda/checker/ETSAnalyzer.cpp +++ b/ets2panda/checker/ETSAnalyzer.cpp @@ -16,6 +16,7 @@ #include "ETSAnalyzer.h" #include "checker/ETSchecker.h" +#include "compiler/lowering/util.h" #include "generated/diagnostic.h" #include "checker/types/globalTypesHolder.h" #include "checker/types/ets/etsTupleType.h" @@ -23,7 +24,6 @@ #include "evaluate/scopedDebugInfoPlugin.h" #include "types/signature.h" #include "compiler/lowering/ets/setJumpTarget.h" -#include "compiler/lowering/util.h" #include "checker/types/ets/etsAsyncFuncReturnType.h" #include "types/ts/nullType.h" #include "types/type.h" @@ -107,7 +107,18 @@ checker::Type *ETSAnalyzer::Check(ir::ClassProperty *st) const ETSChecker *checker = GetETSChecker(); if (st->Id()->Variable() == nullptr) { - st->Id()->Check(checker); + // Now invalid or dummy nodes obtaining after parsing don't have associated variables at all, that leads to + // incorrect AST and multiple reported errors in AST verifier. Need to create and bind [special]? variables for + // them with default TypeError set[?]. Why can't we directly check the 'Id'? During the process of + // resolveIdentifier, we might obtain the wrong variable, which breaks the consistency between the variable and + // its tsType. see wrong_variable_binding.ets for more details. + auto ident = st->Id(); + auto [decl, var] = checker->VarBinder()->NewVarDecl( + ident->Start(), compiler::GenName(checker->ProgramAllocator()).View()); + var->SetScope(checker->VarBinder()->GetScope()); + ident->SetVariable(var); + decl->BindNode(ident); + ident->SetTsType(var->SetTsType(checker->GlobalTypeError())); } ES2PANDA_ASSERT(st->Id()->Variable() != nullptr); diff --git a/ets2panda/checker/ets/utilityTypeHandlers.cpp b/ets2panda/checker/ets/utilityTypeHandlers.cpp index d8d65c3a54..93f6b5eccc 100644 --- a/ets2panda/checker/ets/utilityTypeHandlers.cpp +++ b/ets2panda/checker/ets/utilityTypeHandlers.cpp @@ -123,11 +123,7 @@ static T *CloneNodeIfNotNullptr(T *node, ArenaAllocator *allocator) Type *ETSChecker::CreatePartialType(Type *const typeToBePartial) { ES2PANDA_ASSERT(typeToBePartial->IsETSReferenceType()); - if (typeToBePartial->IsTypeError()) { - return typeToBePartial; - } - - if (typeToBePartial->IsETSAnyType()) { + if (typeToBePartial->IsTypeError() || typeToBePartial->IsETSNeverType() || typeToBePartial->IsETSAnyType()) { return typeToBePartial; } diff --git a/ets2panda/test/ast/compiler/ets/bad_call_setter.ets b/ets2panda/test/ast/compiler/ets/bad_call_setter.ets index 2bfd2c9474..0e68c90948 100644 --- a/ets2panda/test/ast/compiler/ets/bad_call_setter.ets +++ b/ets2panda/test/ast/compiler/ets/bad_call_setter.ets @@ -31,5 +31,4 @@ function main() { /* @@? 23:5 Error SyntaxError: Unexpected token 'this'. */ /* @@? 23:9 Error SyntaxError: Unexpected token '.'. */ /* @@? 23:10 Error TypeError: Variable 'text' has already been declared. */ -/* @@? 23:10 Error TypeError: Property 'text' must be accessed through 'this' */ /* @@? 23:17 Error TypeError: Property 's' must be accessed through 'this' */ diff --git a/ets2panda/test/ast/compiler/ets/same_name_field_err.ets b/ets2panda/test/ast/compiler/ets/same_name_field_err.ets index 8c8f75dc32..fdfa470be3 100644 --- a/ets2panda/test/ast/compiler/ets/same_name_field_err.ets +++ b/ets2panda/test/ast/compiler/ets/same_name_field_err.ets @@ -24,4 +24,3 @@ function main(): void { } /* @@? 19:12 Error TypeError: Variable 'a' has already been declared. */ -/* @@? 19:12 Error TypeError: Property 'a' must be accessed through 'this' */ diff --git a/ets2panda/test/ast/compiler/ets/unresolve_class_with_type_infer.ets b/ets2panda/test/ast/compiler/ets/unresolve_class_with_type_infer.ets new file mode 100644 index 0000000000..9347301cc3 --- /dev/null +++ b/ets2panda/test/ast/compiler/ets/unresolve_class_with_type_infer.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>(record: Record>){ + +} + +foo({}) + +/* @@? 16:39 Error TypeError: Cannot find type 'A'. */ diff --git a/ets2panda/test/ast/compiler/ets/wrong_variable_binding.ets b/ets2panda/test/ast/compiler/ets/wrong_variable_binding.ets new file mode 100644 index 0000000000..c9f2d161ac --- /dev/null +++ b/ets2panda/test/ast/compiler/ets/wrong_variable_binding.ets @@ -0,0 +1,30 @@ +/* + * 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 { + + public C; + + public C: "+v)"; + + public constructor() {} + + } + + class D extends C{} + +/* @@? 19:13 Error SyntaxError: Field type annotation expected. */ +/* @@? 21:12 Error TypeError: Variable 'C' has already been declared. */ \ No newline at end of file diff --git a/ets2panda/test/ast/parser/ets/FixedArray/spreadexpr_in_newexpr_neg01.ets b/ets2panda/test/ast/parser/ets/FixedArray/spreadexpr_in_newexpr_neg01.ets index ff2284a298..7eef9e9c85 100644 --- a/ets2panda/test/ast/parser/ets/FixedArray/spreadexpr_in_newexpr_neg01.ets +++ b/ets2panda/test/ast/parser/ets/FixedArray/spreadexpr_in_newexpr_neg01.ets @@ -33,6 +33,5 @@ function main() { /* @@? 19:9 Error SyntaxError: Unexpected token 'this'. */ /* @@? 19:13 Error SyntaxError: Unexpected token '.'. */ /* @@? 19:14 Error TypeError: Variable 'fld' has already been declared. */ -/* @@? 19:14 Error TypeError: Property 'fld' must be accessed through 'this' */ /* @@? 19:20 Error TypeError: Unresolved reference p */ /* @@? 21:1 Error SyntaxError: Unexpected token '}'. */ diff --git a/ets2panda/test/ast/parser/ets/class-instance-field-redeclaration.ets b/ets2panda/test/ast/parser/ets/class-instance-field-redeclaration.ets index ef8d91f4ec..5724caccac 100644 --- a/ets2panda/test/ast/parser/ets/class-instance-field-redeclaration.ets +++ b/ets2panda/test/ast/parser/ets/class-instance-field-redeclaration.ets @@ -19,4 +19,3 @@ class C { } /* @@@ label Error TypeError: Variable 'foo' has already been declared. */ -/* @@@ label Error TypeError: Property 'foo' must be accessed through 'this' */ diff --git a/ets2panda/test/ast/parser/ets/class-static-field-redeclaration.ets b/ets2panda/test/ast/parser/ets/class-static-field-redeclaration.ets index 87471276b0..7951f410c9 100644 --- a/ets2panda/test/ast/parser/ets/class-static-field-redeclaration.ets +++ b/ets2panda/test/ast/parser/ets/class-static-field-redeclaration.ets @@ -19,4 +19,3 @@ class C { } /* @@@ label Error TypeError: Variable 'foo' has already been declared. */ -/* @@@ label Error TypeError: Static property 'foo' must be accessed through it's class 'C' */ diff --git a/ets2panda/test/ast/parser/ets/enum22.ets b/ets2panda/test/ast/parser/ets/enum22.ets index 1205a10d2b..b052216cad 100644 --- a/ets2panda/test/ast/parser/ets/enum22.ets +++ b/ets2panda/test/ast/parser/ets/enum22.ets @@ -18,4 +18,3 @@ enum duplicateKeysStringCase { /* @@ label */Gray = "#ff808080" } /* @@@ label Error TypeError: Variable 'Gray' has already been declared. */ -/* @@@ label Error TypeError: Static property 'Gray' must be accessed through it's class 'duplicateKeysStringCase' */ diff --git a/ets2panda/test/ast/parser/ets/enum23.ets b/ets2panda/test/ast/parser/ets/enum23.ets index 800c35446e..fbea4d6c3b 100644 --- a/ets2panda/test/ast/parser/ets/enum23.ets +++ b/ets2panda/test/ast/parser/ets/enum23.ets @@ -17,5 +17,4 @@ enum duplicateKeysNumCase { Red = 1, /* @@ label */Red = 1 } -/* @@@ label Error TypeError: Variable 'Red' has already been declared. */ -/* @@@ label Error TypeError: Static property 'Red' must be accessed through it's class 'duplicateKeysNumCase' */ +/* @@@ label Error TypeError: Variable 'Red' has already been declared. */ \ No newline at end of file diff --git a/ets2panda/test/ast/parser/ets/fields.ets b/ets2panda/test/ast/parser/ets/fields.ets index 414efc3575..31cdc48edb 100644 --- a/ets2panda/test/ast/parser/ets/fields.ets +++ b/ets2panda/test/ast/parser/ets/fields.ets @@ -28,4 +28,3 @@ class C { } /* @@@ label Error TypeError: Variable 'f' has already been declared. */ -/* @@@ label Error TypeError: Static property 'f' must be accessed through it's class 'C' */ diff --git a/ets2panda/test/ast/parser/ets/spreadexpr_in_newexpr_neg01.ets b/ets2panda/test/ast/parser/ets/spreadexpr_in_newexpr_neg01.ets index 2a6b971d87..346e038a11 100644 --- a/ets2panda/test/ast/parser/ets/spreadexpr_in_newexpr_neg01.ets +++ b/ets2panda/test/ast/parser/ets/spreadexpr_in_newexpr_neg01.ets @@ -33,6 +33,5 @@ function main() { /* @@? 19:9 Error SyntaxError: Unexpected token 'this'. */ /* @@? 19:13 Error SyntaxError: Unexpected token '.'. */ /* @@? 19:14 Error TypeError: Variable 'fld' has already been declared. */ -/* @@? 19:14 Error TypeError: Property 'fld' must be accessed through 'this' */ /* @@? 19:20 Error TypeError: Unresolved reference p */ -/* @@? 21:1 Error SyntaxError: Unexpected token '}'. */ +/* @@? 21:1 Error SyntaxError: Unexpected token '}'. */ \ No newline at end of file -- Gitee