From 18093cdbbf5f6e6c61ef3ee3d64f30e7ee27473e Mon Sep 17 00:00:00 2001 From: oh-rgx Date: Mon, 11 Aug 2025 09:35:30 +0800 Subject: [PATCH] Fix crash issue Issue: #ICS957 Signed-off-by: oh-rgx --- ets2panda/checker/ets/function.cpp | 9 +++- ets2panda/checker/ets/helpers.cpp | 4 +- .../ast/compiler/ets/index_callExpression.ets | 22 +++++++++ .../ets/resolve_class_declaration.ets | 28 +++++++++++ .../ast/compiler/ets/signature_info_param.ets | 30 ++++++++++++ .../test/ast/compiler/ets/signature_param.ets | 48 +++++++++++++++++++ .../ets/FixedArray/unexpected_token_31.ets | 2 +- .../ast/parser/ets/unexpected_token_31.ets | 2 +- .../parser/ets/wrong_context_function_1.ets | 1 - .../parser/ets/wrong_context_function_3.ets | 1 - .../parser/ets/test_type_alias6-expected.txt | 1 - 11 files changed, 140 insertions(+), 8 deletions(-) create mode 100644 ets2panda/test/ast/compiler/ets/index_callExpression.ets create mode 100644 ets2panda/test/ast/compiler/ets/resolve_class_declaration.ets create mode 100644 ets2panda/test/ast/compiler/ets/signature_info_param.ets create mode 100644 ets2panda/test/ast/compiler/ets/signature_param.ets diff --git a/ets2panda/checker/ets/function.cpp b/ets2panda/checker/ets/function.cpp index 0719ed1705..6f65e600f6 100644 --- a/ets2panda/checker/ets/function.cpp +++ b/ets2panda/checker/ets/function.cpp @@ -24,6 +24,7 @@ #include "checker/types/ets/etsAsyncFuncReturnType.h" #include "checker/types/ets/etsObjectType.h" #include "checker/types/gradualType.h" +#include "checker/types/typeError.h" #include "compiler/lowering/scopesInit/scopesInitPhase.h" #include "ir/base/catchClause.h" #include "ir/base/classDefinition.h" @@ -484,6 +485,7 @@ bool ETSChecker::ValidateSignatureRequiredParams(Signature *substitutedSig, // Note: If the signatures are from lambdas, then they have no `Function`. ir::ScriptFunction *const lambda = argument->AsArrowFunctionExpression()->Function(); auto targetParm = substitutedSig->GetSignatureInfo()->params[index]->Declaration()->Node(); + ERROR_SANITY_CHECK(this, targetParm->IsETSParameterExpression(), return false); if (CheckLambdaAssignable(targetParm->AsETSParameterExpression(), lambda)) { continue; } @@ -1632,8 +1634,11 @@ static bool AppendSignatureInfoParam(ETSChecker *checker, SignatureInfo *sigInfo if (!param->IsOptional()) { ++sigInfo->minArgCount; } - ES2PANDA_ASSERT(!param->IsOptional() || param->Ident()->TsType()->IsTypeError() || - checker->Relation()->IsSupertypeOf(param->Ident()->TsType(), checker->GlobalETSUndefinedType())); + ERROR_SANITY_CHECK( + checker, + !param->IsOptional() || param->Ident()->TsType()->IsTypeError() || + checker->Relation()->IsSupertypeOf(param->Ident()->TsType(), checker->GlobalETSUndefinedType()), + return false); return true; } diff --git a/ets2panda/checker/ets/helpers.cpp b/ets2panda/checker/ets/helpers.cpp index cb93f2bada..6fe0fe66c0 100644 --- a/ets2panda/checker/ets/helpers.cpp +++ b/ets2panda/checker/ets/helpers.cpp @@ -152,6 +152,7 @@ void ETSChecker::WrongContextErrorClassifyByType(ir::Identifier *ident) LogError(diagnostic::ID_WRONG_CTX, {ident->Name()}, ident->Start()); return; } + ident->SetTsType(GlobalTypeError()); LogError(diagnostic::ID_IN_WRONG_CTX, {identCategoryName.c_str(), ident->Name()}, ident->Start()); } @@ -1874,7 +1875,8 @@ Type *ETSChecker::ResolveReferencedType(varbinder::LocalVariable *refVar, const case ir::AstNodeType::CLASS_DECLARATION: case ir::AstNodeType::STRUCT_DECLARATION: case ir::AstNodeType::CLASS_DEFINITION: - if (refVar->Declaration()->Node()->AsClassDefinition()->IsNamespaceTransformed()) { + if (refVar->Declaration()->Node()->IsClassDefinition() && + refVar->Declaration()->Node()->AsClassDefinition()->IsNamespaceTransformed()) { LogError(diagnostic::NAMESPACE_AS_TYPE, {refVar->Name()}, name->Start()); return GlobalTypeError(); } diff --git a/ets2panda/test/ast/compiler/ets/index_callExpression.ets b/ets2panda/test/ast/compiler/ets/index_callExpression.ets new file mode 100644 index 0000000000..9ce5b1325d --- /dev/null +++ b/ets2panda/test/ast/compiler/ets/index_callExpression.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 testInfo() { + DataView => ""[main]() +} + +/* @@? 17:5 Error TypeError: The type of parameter 'DataView' cannot be inferred */ +/* @@? 17:17 Error TypeError: Type 'String' has no call signatures. */ +/* @@? 17:20 Error TypeError: Function name 'main' used in the wrong context */ diff --git a/ets2panda/test/ast/compiler/ets/resolve_class_declaration.ets b/ets2panda/test/ast/compiler/ets/resolve_class_declaration.ets new file mode 100644 index 0000000000..97180f8beb --- /dev/null +++ b/ets2panda/test/ast/compiler/ets/resolve_class_declaration.ets @@ -0,0 +1,28 @@ +/* + * 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. + */ + +interface I { + b5 : number;r_2.ETSGLOBAL::hot_catc { + return; +} + +/* @@? 17:20 Error SyntaxError: Interface fields must have type annotation. */ +/* @@? 17:30 Error SyntaxError: Unexpected token, expected ','. */ +/* @@? 17:30 Error SyntaxError: Unexpected token, expected 'private' or identifier. */ +/* @@? 17:31 Error SyntaxError: Identifier expected. */ +/* @@? 17:31 Error SyntaxError: Unexpected token, expected ','. */ +/* @@? 17:31 Error SyntaxError: Unexpected token, expected 'private' or identifier. */ +/* @@? 17:41 Error SyntaxError: Interface fields must have type annotation. */ +/* @@? 18:5 Error SyntaxError: Invalid Type. */ diff --git a/ets2panda/test/ast/compiler/ets/signature_info_param.ets b/ets2panda/test/ast/compiler/ets/signature_info_param.ets new file mode 100644 index 0000000000..86eadbdc4a --- /dev/null +++ b/ets2panda/test/ast/compiler/ets/signature_info_param.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. + */ + +function testInfo() : void { + let iniP : Promise = intFunc(); + let pThen = iniP.then((d = e : Object) : Object => { return else;}); +} + +/* @@? 17:34 Error TypeError: Unresolved reference intFunc */ +/* @@? 18:34 Error SyntaxError: Unexpected token, expected ',' or ')'. */ +/* @@? 18:42 Error SyntaxError: Expected '=>', got ')'. */ +/* @@? 18:42 Error SyntaxError: Unexpected token ')'. */ +/* @@? 18:44 Error SyntaxError: Unexpected token, expected ',' or ')'. */ +/* @@? 18:44 Error SyntaxError: Unexpected token ':'. */ +/* @@? 18:46 Error SyntaxError: Unexpected token 'Object'. */ +/* @@? 18:46 Error TypeError: The type of parameter 'Object' cannot be inferred */ +/* @@? 18:65 Error SyntaxError: Unexpected token 'else'. */ +/* @@? 18:71 Error SyntaxError: Unexpected token ')'. */ diff --git a/ets2panda/test/ast/compiler/ets/signature_param.ets b/ets2panda/test/ast/compiler/ets/signature_param.ets new file mode 100644 index 0000000000..9c758d7953 --- /dev/null +++ b/ets2panda/test/ast/compiler/ets/signature_param.ets @@ -0,0 +1,48 @@ +/* + * 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 foo1(f: () => void) { + try { + + } catch (e) { + arktest.assertTrue(false, "expected} +} + +class A {} + +function foo2(let f = ((x:A {} + +function foo3() { + foo2(() => {}}); + foo2(() => {}); +} + +/* @@? 20:35 Error SyntaxError: Newline is not allowed in strings */ +/* @@? 20:45 Error SyntaxError: Unexpected token, expected ',' or ')'. */ +/* @@? 22:1 Error SyntaxError: Illegal start of CLASS expression. */ +/* @@? 24:1 Error SyntaxError: Unexpected token, expected ')'. */ +/* @@? 24:1 Error SyntaxError: Unexpected token '{'. */ +/* @@? 24:1 Error SyntaxError: Unexpected token, expected ')'. */ +/* @@? 24:1 Error SyntaxError: Expected '{', got 'let'. */ +/* @@? 24:1 Error SyntaxError: Unexpected token, expected ',' or ')'. */ +/* @@? 24:1 Error SyntaxError: Parameter declaration should have an explicit type annotation. */ +/* @@? 24:1 Error SyntaxError: Unexpected token, expected an identifier. */ +/* @@? 24:1 Error SyntaxError: Nested functions are not allowed. */ +/* @@? 24:1 Error TypeError: Variable 'f' has already been declared. */ +/* @@? 24:1 Error TypeError: Unresolved reference x */ +/* @@? 26:1 Error SyntaxError: Nested functions are not allowed. */ +/* @@? 27:18 Error SyntaxError: Unexpected token, expected ',' or ')'. */ +/* @@? 27:18 Error SyntaxError: Unexpected token ')'. */ +/* @@? 28:21 Error TypeError: Unresolved reference foo2 */ diff --git a/ets2panda/test/ast/parser/ets/FixedArray/unexpected_token_31.ets b/ets2panda/test/ast/parser/ets/FixedArray/unexpected_token_31.ets index 8190270528..37de6fe998 100644 --- a/ets2panda/test/ast/parser/ets/FixedArray/unexpected_token_31.ets +++ b/ets2panda/test/ast/parser/ets/FixedArray/unexpected_token_31.ets @@ -28,5 +28,5 @@ function foo(...^number: FixedArray): int { /* @@? 16:48 Error SyntaxError: Unexpected token '{'. */ /* @@? 17:5 Error SyntaxError: return keyword should be used in function body. */ /* @@? 17:12 Error TypeError: Type name 'number' used in the wrong context */ -/* @@? 17:12 Error TypeError: Object type doesn't have proper index access method. */ +/* @@? 17:12 Error TypeError: Indexed access is not supported for such expression type. */ /* @@? 17:12 Error TypeError: All return statements in the function should be empty or have a value. */ diff --git a/ets2panda/test/ast/parser/ets/unexpected_token_31.ets b/ets2panda/test/ast/parser/ets/unexpected_token_31.ets index d7442c979c..01374410c3 100644 --- a/ets2panda/test/ast/parser/ets/unexpected_token_31.ets +++ b/ets2panda/test/ast/parser/ets/unexpected_token_31.ets @@ -28,5 +28,5 @@ function foo(...^number: int[]): int { /* @@? 16:38 Error SyntaxError: Unexpected token '{'. */ /* @@? 17:5 Error SyntaxError: return keyword should be used in function body. */ /* @@? 17:12 Error TypeError: Type name 'number' used in the wrong context */ -/* @@? 17:12 Error TypeError: Object type doesn't have proper index access method. */ +/* @@? 17:12 Error TypeError: Indexed access is not supported for such expression type. */ /* @@? 17:12 Error TypeError: All return statements in the function should be empty or have a value. */ diff --git a/ets2panda/test/ast/parser/ets/wrong_context_function_1.ets b/ets2panda/test/ast/parser/ets/wrong_context_function_1.ets index 4e2bf2e259..530534fa78 100644 --- a/ets2panda/test/ast/parser/ets/wrong_context_function_1.ets +++ b/ets2panda/test/ast/parser/ets/wrong_context_function_1.ets @@ -23,4 +23,3 @@ function main() } /* @@@ label Error TypeError: Function name 'a' used in the wrong context */ -/* @@@ label1 Error TypeError: Type 'Int' cannot be assigned to type '() => void' */ diff --git a/ets2panda/test/ast/parser/ets/wrong_context_function_3.ets b/ets2panda/test/ast/parser/ets/wrong_context_function_3.ets index 74b8296e56..293065f2be 100644 --- a/ets2panda/test/ast/parser/ets/wrong_context_function_3.ets +++ b/ets2panda/test/ast/parser/ets/wrong_context_function_3.ets @@ -23,4 +23,3 @@ function main() } /* @@? 22:5 Error TypeError: Function name 'a' used in the wrong context */ -/* @@? 22:5 Error TypeError: Bad operand type, the type of the operand must be numeric type. */ diff --git a/ets2panda/test/parser/ets/test_type_alias6-expected.txt b/ets2panda/test/parser/ets/test_type_alias6-expected.txt index 417fe11ce0..315692355d 100644 --- a/ets2panda/test/parser/ets/test_type_alias6-expected.txt +++ b/ets2panda/test/parser/ets/test_type_alias6-expected.txt @@ -447,4 +447,3 @@ } TypeError: Variable 'x' has already been declared. [test_type_alias6.ets:17:5] TypeError: Type name 'x' used in the wrong context [test_type_alias6.ets:17:5] -TypeError: Type 'Double' cannot be assigned to type 'Int' [test_type_alias6.ets:17:9] -- Gitee