From 20654efecbcbd6631be18fe4c42caf24b657ffd5 Mon Sep 17 00:00:00 2001 From: fangxiao Date: Tue, 26 Aug 2025 11:33:16 +0800 Subject: [PATCH] Fix the compilation issue of catch Issue: https://gitee.com/openharmony/arkcompiler_ets_frontend/issues/ICTJB6 Signed-off-by: fangxiao Change-Id: I27099d558b0e948b6e0d6b1f87335cfdffcf3f0a --- ets2panda/ets2panda | 1 + ets2panda/parser/ETSparser.cpp | 2 +- ets2panda/test/ast/compiler/ets/try_catch.ets | 27 + .../test/ast/compiler/ts/test-interface5.ts | 2 +- .../ast/compiler/ts/test-type-literal5.ts | 2 +- .../ets/catch-soft-keyword-expected.txt | 2 +- .../compiler/ets/catchParamScope-expected.txt | 2 +- .../ets/tryDefaultCatches-expected.txt | 2 +- .../test/parser/ets/try_catch-expected.txt | 793 ++++++++++++++++++ ets2panda/test/runtime/ets/CastReference3.ets | 14 +- ets2panda/test/runtime/ets/CastReference4.ets | 8 +- ets2panda/test/runtime/ets/Enum3.ets | 16 +- ets2panda/test/runtime/ets/Enum4.ets | 19 +- ets2panda/test/runtime/ets/Enum5.ets | 17 +- ets2panda/test/runtime/ets/OptionalChains.ets | 4 +- ets2panda/test/runtime/ets/SmartCast_08.ets | 10 +- .../test/runtime/ets/UnboxingCheckcast.ets | 7 +- .../test/runtime/ets/division-by-zero.ets | 60 +- .../test/runtime/ets/finallyClauseThrow.ets | 13 +- .../test/runtime/ets/generic_Error_class.ets | 12 +- .../test/runtime/ets/generic_exception.ets | 12 +- .../runtime/ets/member-expression-nullptr.ets | 18 +- .../runtime/ets/multi-array-new-catched-1.ets | 6 +- .../runtime/ets/multi-array-new-catched-2.ets | 6 +- ets2panda/test/runtime/ets/notNull.ets | 12 +- .../ets/optional-chaining-function-call.ets | 6 +- .../ets/overload_declaration/notNull.ets | 12 +- ets2panda/test/runtime/ets/top_level_02.ets | 10 +- .../test/runtime/ets/try-catch-error.ets | 40 +- ets2panda/test/runtime/ets/try-catch.ets | 48 +- ets2panda/test/runtime/ets/union_type_RTE.ets | 6 +- ..._verifier_check_scope_declaration_test.cpp | 6 +- 32 files changed, 1051 insertions(+), 144 deletions(-) create mode 120000 ets2panda/ets2panda create mode 100644 ets2panda/test/ast/compiler/ets/try_catch.ets create mode 100644 ets2panda/test/parser/ets/try_catch-expected.txt diff --git a/ets2panda/ets2panda b/ets2panda/ets2panda new file mode 120000 index 0000000000..c60d228fd0 --- /dev/null +++ b/ets2panda/ets2panda @@ -0,0 +1 @@ +../../../ets_frontend/ets2panda \ No newline at end of file diff --git a/ets2panda/parser/ETSparser.cpp b/ets2panda/parser/ETSparser.cpp index e4b25915ea..fc9c9c6541 100644 --- a/ets2panda/parser/ETSparser.cpp +++ b/ets2panda/parser/ETSparser.cpp @@ -1935,7 +1935,7 @@ ir::Expression *ETSParser::ParseCatchParam() void ETSParser::ParseCatchParamTypeAnnotation([[maybe_unused]] ir::AnnotatedExpression *param) { if (Lexer()->TryEatTokenType(lexer::TokenType::PUNCTUATOR_COLON)) { - LogError(diagnostic::MULTI_CATCH_DEPRECATED); + LogUnexpectedToken(Lexer()->GetToken()); TypeAnnotationParsingOptions options = TypeAnnotationParsingOptions::REPORT_ERROR; if (auto *typeAnnotation = ParseTypeAnnotation(&options); typeAnnotation != nullptr) { ES2PANDA_ASSERT(param != nullptr); diff --git a/ets2panda/test/ast/compiler/ets/try_catch.ets b/ets2panda/test/ast/compiler/ets/try_catch.ets new file mode 100644 index 0000000000..c26583baab --- /dev/null +++ b/ets2panda/test/ast/compiler/ets/try_catch.ets @@ -0,0 +1,27 @@ +/* + * 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 Fx extends Exception {} +function main(): int { + let exc : Fx = new Fx(); + try { + if (1 == 2) throw exc; + return 0; + } catch (f : Fx) { + return 1; + } +} + +/* @@? 22:18 Error SyntaxError: Unexpected token 'Fx'. */ diff --git a/ets2panda/test/ast/compiler/ts/test-interface5.ts b/ets2panda/test/ast/compiler/ts/test-interface5.ts index b5b547dc48..f75fa96528 100644 --- a/ets2panda/test/ast/compiler/ts/test-interface5.ts +++ b/ets2panda/test/ast/compiler/ts/test-interface5.ts @@ -15,4 +15,4 @@ interface A { readonly /* @@ label */new(a: number): string }; -/* @@@ label Error SyntaxError: Unexpected token in property key */ +/* @@@ label Error SyntaxError: Unexpected token in property key */ diff --git a/ets2panda/test/ast/compiler/ts/test-type-literal5.ts b/ets2panda/test/ast/compiler/ts/test-type-literal5.ts index 4eeeccc157..964158265e 100644 --- a/ets2panda/test/ast/compiler/ts/test-type-literal5.ts +++ b/ets2panda/test/ast/compiler/ts/test-type-literal5.ts @@ -15,4 +15,4 @@ var a: { readonly /* @@ label */new (a:number):string }; -/* @@@ label Error SyntaxError: Unexpected token in property key */ +/* @@@ label Error SyntaxError: Unexpected token in property key */ diff --git a/ets2panda/test/compiler/ets/catch-soft-keyword-expected.txt b/ets2panda/test/compiler/ets/catch-soft-keyword-expected.txt index 399704b9aa..7d08ad5690 100644 --- a/ets2panda/test/compiler/ets/catch-soft-keyword-expected.txt +++ b/ets2panda/test/compiler/ets/catch-soft-keyword-expected.txt @@ -913,4 +913,4 @@ } } } -Warning: The catch statement with type annotation is deprecated, use catch(e). [catch-soft-keyword.ets:26:19] +SyntaxError: Unexpected token 'Exception'. [catch-soft-keyword.ets:26:19] diff --git a/ets2panda/test/compiler/ets/catchParamScope-expected.txt b/ets2panda/test/compiler/ets/catchParamScope-expected.txt index b1b53cb272..c29b4ece7c 100644 --- a/ets2panda/test/compiler/ets/catchParamScope-expected.txt +++ b/ets2panda/test/compiler/ets/catchParamScope-expected.txt @@ -719,4 +719,4 @@ } } } -Warning: The catch statement with type annotation is deprecated, use catch(e). [catchParamScope.ets:22:25] +SyntaxError: Unexpected token 'Exception'. [catchParamScope.ets:22:25] diff --git a/ets2panda/test/compiler/ets/tryDefaultCatches-expected.txt b/ets2panda/test/compiler/ets/tryDefaultCatches-expected.txt index b93c660e83..be50fbf597 100644 --- a/ets2panda/test/compiler/ets/tryDefaultCatches-expected.txt +++ b/ets2panda/test/compiler/ets/tryDefaultCatches-expected.txt @@ -435,4 +435,4 @@ } } } -Warning: The catch statement with type annotation is deprecated, use catch(e). [tryDefaultCatches.ets:19:15] +SyntaxError: Unexpected token 'Exception'. [tryDefaultCatches.ets:19:15] diff --git a/ets2panda/test/parser/ets/try_catch-expected.txt b/ets2panda/test/parser/ets/try_catch-expected.txt new file mode 100644 index 0000000000..10994195bf --- /dev/null +++ b/ets2panda/test/parser/ets/try_catch-expected.txt @@ -0,0 +1,793 @@ +{ + "type": "Program", + "statements": [ + { + "type": "ClassDeclaration", + "definition": { + "id": { + "type": "Identifier", + "name": "Fx", + "loc": { + "start": { + "line": 16, + "column": 7, + "program": "try_catch.ets" + }, + "end": { + "line": 16, + "column": 9, + "program": "try_catch.ets" + } + } + }, + "superClass": { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "Exception", + "loc": { + "start": { + "line": 16, + "column": 18, + "program": "try_catch.ets" + }, + "end": { + "line": 16, + "column": 27, + "program": "try_catch.ets" + } + } + }, + "loc": { + "start": { + "line": 16, + "column": 18, + "program": "try_catch.ets" + }, + "end": { + "line": 16, + "column": 27, + "program": "try_catch.ets" + } + } + }, + "loc": { + "start": { + "line": 16, + "column": 18, + "program": "try_catch.ets" + }, + "end": { + "line": 16, + "column": 27, + "program": "try_catch.ets" + } + } + }, + "implements": [], + "body": [ + { + "type": "MethodDefinition", + "key": { + "type": "Identifier", + "name": "constructor", + "loc": { + "start": { + "line": 16, + "column": 29, + "program": "try_catch.ets" + }, + "end": { + "line": 16, + "column": 29, + "program": "try_catch.ets" + } + } + }, + "kind": "constructor", + "static": false, + "optional": false, + "computed": false, + "value": { + "type": "FunctionExpression", + "function": { + "type": "ScriptFunction", + "id": { + "type": "Identifier", + "name": "constructor", + "loc": { + "start": { + "line": 16, + "column": 29, + "program": "try_catch.ets" + }, + "end": { + "line": 16, + "column": 29, + "program": "try_catch.ets" + } + } + }, + "generator": false, + "async": false, + "expression": false, + "params": [], + "body": { + "type": "BlockStatement", + "statements": [], + "loc": { + "start": { + "line": 16, + "column": 29, + "program": "try_catch.ets" + }, + "end": { + "line": 16, + "column": 29, + "program": "try_catch.ets" + } + } + }, + "loc": { + "start": { + "line": 16, + "column": 29, + "program": "try_catch.ets" + }, + "end": { + "line": 16, + "column": 29, + "program": "try_catch.ets" + } + } + }, + "loc": { + "start": { + "line": 16, + "column": 29, + "program": "try_catch.ets" + }, + "end": { + "line": 16, + "column": 29, + "program": "try_catch.ets" + } + } + }, + "overloads": [], + "loc": { + "start": { + "line": 1, + "column": 1, + "program": null + }, + "end": { + "line": 1, + "column": 1, + "program": null + } + } + } + ], + "loc": { + "start": { + "line": 16, + "column": 28, + "program": "try_catch.ets" + }, + "end": { + "line": 17, + "column": 9, + "program": "try_catch.ets" + } + } + }, + "loc": { + "start": { + "line": 16, + "column": 1, + "program": "try_catch.ets" + }, + "end": { + "line": 17, + "column": 9, + "program": "try_catch.ets" + } + } + }, + { + "type": "ClassDeclaration", + "definition": { + "id": { + "type": "Identifier", + "name": "ETSGLOBAL", + "loc": { + "start": { + "line": 1, + "column": 1, + "program": "try_catch.ets" + }, + "end": { + "line": 1, + "column": 1, + "program": "try_catch.ets" + } + } + }, + "superClass": null, + "implements": [], + "body": [ + { + "type": "MethodDefinition", + "key": { + "type": "Identifier", + "name": "main", + "loc": { + "start": { + "line": 17, + "column": 10, + "program": "try_catch.ets" + }, + "end": { + "line": 17, + "column": 14, + "program": "try_catch.ets" + } + } + }, + "kind": "method", + "accessibility": "public", + "static": true, + "optional": false, + "computed": false, + "value": { + "type": "FunctionExpression", + "function": { + "type": "ScriptFunction", + "id": { + "type": "Identifier", + "name": "main", + "loc": { + "start": { + "line": 17, + "column": 10, + "program": "try_catch.ets" + }, + "end": { + "line": 17, + "column": 14, + "program": "try_catch.ets" + } + } + }, + "generator": false, + "async": false, + "expression": false, + "params": [], + "returnType": { + "type": "ETSPrimitiveType", + "loc": { + "start": { + "line": 17, + "column": 18, + "program": "try_catch.ets" + }, + "end": { + "line": 17, + "column": 21, + "program": "try_catch.ets" + } + } + }, + "body": { + "type": "BlockStatement", + "statements": [ + { + "type": "VariableDeclaration", + "declarations": [ + { + "type": "VariableDeclarator", + "id": { + "type": "Identifier", + "name": "exc", + "typeAnnotation": { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "Fx", + "loc": { + "start": { + "line": 18, + "column": 15, + "program": "try_catch.ets" + }, + "end": { + "line": 18, + "column": 17, + "program": "try_catch.ets" + } + } + }, + "loc": { + "start": { + "line": 18, + "column": 15, + "program": "try_catch.ets" + }, + "end": { + "line": 18, + "column": 17, + "program": "try_catch.ets" + } + } + }, + "loc": { + "start": { + "line": 18, + "column": 15, + "program": "try_catch.ets" + }, + "end": { + "line": 18, + "column": 17, + "program": "try_catch.ets" + } + } + }, + "loc": { + "start": { + "line": 18, + "column": 9, + "program": "try_catch.ets" + }, + "end": { + "line": 18, + "column": 12, + "program": "try_catch.ets" + } + } + }, + "init": { + "type": "ETSNewClassInstanceExpression", + "typeReference": { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "Fx", + "loc": { + "start": { + "line": 18, + "column": 24, + "program": "try_catch.ets" + }, + "end": { + "line": 18, + "column": 26, + "program": "try_catch.ets" + } + } + }, + "loc": { + "start": { + "line": 18, + "column": 24, + "program": "try_catch.ets" + }, + "end": { + "line": 18, + "column": 26, + "program": "try_catch.ets" + } + } + }, + "loc": { + "start": { + "line": 18, + "column": 24, + "program": "try_catch.ets" + }, + "end": { + "line": 18, + "column": 26, + "program": "try_catch.ets" + } + } + }, + "arguments": [], + "loc": { + "start": { + "line": 18, + "column": 20, + "program": "try_catch.ets" + }, + "end": { + "line": 18, + "column": 29, + "program": "try_catch.ets" + } + } + }, + "loc": { + "start": { + "line": 18, + "column": 9, + "program": "try_catch.ets" + }, + "end": { + "line": 18, + "column": 29, + "program": "try_catch.ets" + } + } + } + ], + "kind": "let", + "loc": { + "start": { + "line": 18, + "column": 5, + "program": "try_catch.ets" + }, + "end": { + "line": 18, + "column": 29, + "program": "try_catch.ets" + } + } + }, + { + "type": "TryStatement", + "block": { + "type": "BlockStatement", + "statements": [ + { + "type": "IfStatement", + "test": { + "type": "BooleanLiteral", + "value": false, + "loc": { + "start": { + "line": 20, + "column": 13, + "program": "try_catch.ets" + }, + "end": { + "line": 20, + "column": 19, + "program": "try_catch.ets" + } + } + }, + "consequent": { + "type": "ThrowStatement", + "argument": { + "type": "Identifier", + "name": "exc", + "loc": { + "start": { + "line": 20, + "column": 27, + "program": "try_catch.ets" + }, + "end": { + "line": 20, + "column": 30, + "program": "try_catch.ets" + } + } + }, + "loc": { + "start": { + "line": 20, + "column": 21, + "program": "try_catch.ets" + }, + "end": { + "line": 20, + "column": 31, + "program": "try_catch.ets" + } + } + }, + "alternate": null, + "loc": { + "start": { + "line": 20, + "column": 9, + "program": "try_catch.ets" + }, + "end": { + "line": 20, + "column": 31, + "program": "try_catch.ets" + } + } + }, + { + "type": "ReturnStatement", + "argument": { + "type": "NumberLiteral", + "value": 0, + "loc": { + "start": { + "line": 21, + "column": 16, + "program": "try_catch.ets" + }, + "end": { + "line": 21, + "column": 17, + "program": "try_catch.ets" + } + } + }, + "loc": { + "start": { + "line": 21, + "column": 9, + "program": "try_catch.ets" + }, + "end": { + "line": 21, + "column": 18, + "program": "try_catch.ets" + } + } + } + ], + "loc": { + "start": { + "line": 19, + "column": 9, + "program": "try_catch.ets" + }, + "end": { + "line": 22, + "column": 6, + "program": "try_catch.ets" + } + } + }, + "handler": [ + { + "type": "CatchClause", + "body": { + "type": "BlockStatement", + "statements": [ + { + "type": "ReturnStatement", + "argument": { + "type": "NumberLiteral", + "value": 1, + "loc": { + "start": { + "line": 23, + "column": 16, + "program": "try_catch.ets" + }, + "end": { + "line": 23, + "column": 17, + "program": "try_catch.ets" + } + } + }, + "loc": { + "start": { + "line": 23, + "column": 9, + "program": "try_catch.ets" + }, + "end": { + "line": 23, + "column": 18, + "program": "try_catch.ets" + } + } + } + ], + "loc": { + "start": { + "line": 22, + "column": 22, + "program": "try_catch.ets" + }, + "end": { + "line": 24, + "column": 6, + "program": "try_catch.ets" + } + } + }, + "param": { + "type": "Identifier", + "name": "f", + "typeAnnotation": { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "Fx", + "loc": { + "start": { + "line": 22, + "column": 18, + "program": "try_catch.ets" + }, + "end": { + "line": 22, + "column": 20, + "program": "try_catch.ets" + } + } + }, + "loc": { + "start": { + "line": 22, + "column": 18, + "program": "try_catch.ets" + }, + "end": { + "line": 22, + "column": 20, + "program": "try_catch.ets" + } + } + }, + "loc": { + "start": { + "line": 22, + "column": 18, + "program": "try_catch.ets" + }, + "end": { + "line": 22, + "column": 20, + "program": "try_catch.ets" + } + } + }, + "loc": { + "start": { + "line": 22, + "column": 14, + "program": "try_catch.ets" + }, + "end": { + "line": 22, + "column": 15, + "program": "try_catch.ets" + } + } + }, + "loc": { + "start": { + "line": 22, + "column": 7, + "program": "try_catch.ets" + }, + "end": { + "line": 24, + "column": 6, + "program": "try_catch.ets" + } + } + } + ], + "finalizer": null, + "loc": { + "start": { + "line": 19, + "column": 5, + "program": "try_catch.ets" + }, + "end": { + "line": 24, + "column": 6, + "program": "try_catch.ets" + } + } + } + ], + "loc": { + "start": { + "line": 17, + "column": 22, + "program": "try_catch.ets" + }, + "end": { + "line": 25, + "column": 2, + "program": "try_catch.ets" + } + } + }, + "loc": { + "start": { + "line": 17, + "column": 10, + "program": "try_catch.ets" + }, + "end": { + "line": 25, + "column": 2, + "program": "try_catch.ets" + } + } + }, + "loc": { + "start": { + "line": 17, + "column": 10, + "program": "try_catch.ets" + }, + "end": { + "line": 25, + "column": 2, + "program": "try_catch.ets" + } + } + }, + "overloads": [], + "loc": { + "start": { + "line": 17, + "column": 1, + "program": "try_catch.ets" + }, + "end": { + "line": 25, + "column": 2, + "program": "try_catch.ets" + } + } + } + ], + "loc": { + "start": { + "line": 1, + "column": 1, + "program": "try_catch.ets" + }, + "end": { + "line": 1, + "column": 1, + "program": "try_catch.ets" + } + } + }, + "loc": { + "start": { + "line": 1, + "column": 1, + "program": "try_catch.ets" + }, + "end": { + "line": 1, + "column": 1, + "program": "try_catch.ets" + } + } + } + ], + "loc": { + "start": { + "line": 1, + "column": 1, + "program": "try_catch.ets" + }, + "end": { + "line": 26, + "column": 1, + "program": "try_catch.ets" + } + } +} +SyntaxError: Unexpected token 'Fx'. [try_catch.ets:22:18] diff --git a/ets2panda/test/runtime/ets/CastReference3.ets b/ets2panda/test/runtime/ets/CastReference3.ets index e4c0300b0c..ec83cc3f33 100644 --- a/ets2panda/test/runtime/ets/CastReference3.ets +++ b/ets2panda/test/runtime/ets/CastReference3.ets @@ -43,13 +43,15 @@ function main() { { let caught = false; try { - let As: FixedArray = new A[1]; - let Bs = As as FixedArray; - } catch (e: ClassCastError) { - caught = true; + let As: FixedArray = new A[1]; + let Bs = As as FixedArray; } catch (e) { - arktest.assertTrue(false) + if (e instanceof ClassCastError) { + caught = true; + } else { + arktest.assertTrue(false); + } } - arktest.assertTrue(caught) + arktest.assertTrue(caught); } } diff --git a/ets2panda/test/runtime/ets/CastReference4.ets b/ets2panda/test/runtime/ets/CastReference4.ets index 1d6541d0c4..ae7b576627 100644 --- a/ets2panda/test/runtime/ets/CastReference4.ets +++ b/ets2panda/test/runtime/ets/CastReference4.ets @@ -32,10 +32,12 @@ function main() { // Element type in array in accumulator is of incompatible type 'A'. // Instruction 'checkcast' will always throw an exception here. It may be a sign of possible error here. let Bs: FixedArray = Is as FixedArray; - } catch (e: ClassCastError) { - caught = true; } catch (e) { - arktest.assertTrue(false) + if (e instanceof ClassCastError) { + caught = true; + } else { + arktest.assertTrue(false) + } } arktest.assertTrue(caught) diff --git a/ets2panda/test/runtime/ets/Enum3.ets b/ets2panda/test/runtime/ets/Enum3.ets index 150f7e76c6..659156291b 100644 --- a/ets2panda/test/runtime/ets/Enum3.ets +++ b/ets2panda/test/runtime/ets/Enum3.ets @@ -43,9 +43,11 @@ function main(): void { try { let yellow: Color = Color.getValueOf("Yellow"); arktest.assertTrue(false) - } catch (e: Exception) { - arktest.assertTrue((e as Object).toString().startsWith("No enum constant Color.Yellow")) - } catch (e) {} + } catch (e) { + if (e instanceof Exception) { + arktest.assertTrue((e as Object).toString().startsWith("No enum constant Color.Yellow")) + } + } let one: int = 1; let green = one as Color; @@ -53,9 +55,11 @@ function main(): void { try { let x = 5 as Color; - arktest.assertTrue( false) - } catch (e: Error) { - arktest.assertTrue( (e as Object).toString().startsWith("Error: No enum Color with value 5")) + arktest.assertTrue(false) + } catch (e) { + if (e instanceof Error) { + arktest.assertTrue( (e as Object).toString().startsWith("Error: No enum Color with value 5")) + } } arktest.assertEQ(2 as Color as int, 2) diff --git a/ets2panda/test/runtime/ets/Enum4.ets b/ets2panda/test/runtime/ets/Enum4.ets index bfac7dcebc..7c5e1f0534 100644 --- a/ets2panda/test/runtime/ets/Enum4.ets +++ b/ets2panda/test/runtime/ets/Enum4.ets @@ -85,10 +85,12 @@ function main(): void { try { let yellow: Color = Color.getValueOf("Yellow"); - arktest.assertTrue( false) - } catch (e: Error) { - arktest.assertTrue( (e as Object).toString().startsWith("Error: No enum constant Color.Yellow")) - } catch (e) {} + arktest.assertTrue(false) + } catch (e) { + if (e instanceof Error) { + arktest.assertTrue( (e as Object).toString().startsWith("Error: No enum constant Color.Yellow")) + } + } ord = 0; let green: Color = ord as Color; @@ -98,8 +100,10 @@ function main(): void { try { let x = 5 as Color; arktest.assertTrue( false) - } catch (e: Error) { - arktest.assertTrue( (e as Object).toString().startsWith("Error: No enum Color with value 5")) + } catch (e) { + if (e instanceof Error) { + arktest.assertTrue( (e as Object).toString().startsWith("Error: No enum Color with value 5")) + } } arktest.assertEQ( 2 as Color as int, 2) @@ -107,6 +111,5 @@ function main(): void { arktest.assertEQ( (Color.Green as int + 2) as Color, (Color.Blue as int - 1) as Color) try { arktest.assertEQ((Color.Green as int + 1) as Color, Color.getValueOf("Red")) - } catch (e) { - } + } catch (e) {} } diff --git a/ets2panda/test/runtime/ets/Enum5.ets b/ets2panda/test/runtime/ets/Enum5.ets index c765271af6..7528e1b08c 100644 --- a/ets2panda/test/runtime/ets/Enum5.ets +++ b/ets2panda/test/runtime/ets/Enum5.ets @@ -77,9 +77,11 @@ function main(): void { try { let yellow: Color = Color.getValueOf("Yellow"); arktest.assertTrue(false) - } catch (e: Exception) { - arktest.assertTrue((e as Object).toString().startsWith("No enum constant Color.Yellow")) - } catch (e) {} + } catch (e) { + if (e instanceof Exception) { + arktest.assertTrue((e as Object).toString().startsWith("No enum constant Color.Yellow")) + } + } ord = "red"; let green: Color = ord as Color; @@ -88,14 +90,15 @@ function main(): void { try { let x = "a" as Color; arktest.assertTrue( false) - } catch (e: Error) { - arktest.assertTrue( (e as Object).toString().startsWith("Error: No enum Color with value a")) + } catch (e) { + if (e instanceof Error) { + arktest.assertTrue( (e as Object).toString().startsWith("Error: No enum Color with value a")) + } } arktest.assertEQ( "red" as Color as string, "red") arktest.assertEQ( Color.Blue as string as Color, Color.Blue) try { arktest.assertEQ( (Color.Green as string) as Color, Color.getValueOf("red")) - } catch (e) { - } + } catch (e) {} } diff --git a/ets2panda/test/runtime/ets/OptionalChains.ets b/ets2panda/test/runtime/ets/OptionalChains.ets index f424388915..ce6c958371 100644 --- a/ets2panda/test/runtime/ets/OptionalChains.ets +++ b/ets2panda/test/runtime/ets/OptionalChains.ets @@ -19,8 +19,10 @@ function assert_o(v: Object | null | undefined) { arktest.assertTrue(v !== null function assert_npe(f: () => void) { try { f(); - } catch (e: NullPointerError) { + } catch (e) { + if (e instanceof NullPointerError) { return; + } } arktest.assertTrue(false, "npe was not thrown") } diff --git a/ets2panda/test/runtime/ets/SmartCast_08.ets b/ets2panda/test/runtime/ets/SmartCast_08.ets index 0e14d65349..0883a08185 100644 --- a/ets2panda/test/runtime/ets/SmartCast_08.ets +++ b/ets2panda/test/runtime/ets/SmartCast_08.ets @@ -25,10 +25,12 @@ function foo(flag: boolean): int { if (flag) { throw new Error(); } - } catch(ex: NullPointerError) { - z = 2; - } catch(ex) { - w = undefined; + } catch (ex) { + if (ex instanceof NullPointerError) { + z = 2; + } else { + w = undefined; + } } return x + y! + z + w!; diff --git a/ets2panda/test/runtime/ets/UnboxingCheckcast.ets b/ets2panda/test/runtime/ets/UnboxingCheckcast.ets index d7ddb16a64..d97dffe185 100644 --- a/ets2panda/test/runtime/ets/UnboxingCheckcast.ets +++ b/ets2panda/test/runtime/ets/UnboxingCheckcast.ets @@ -33,8 +33,9 @@ function main() { try{ c as double; - } catch (e: ClassCastError){ - } catch(other) { - arktest.assertEQ(1, 0) + } catch (e) { + if (e instanceof ClassCastError) {} else { + arktest.assertEQ(1, 0) + } } } diff --git a/ets2panda/test/runtime/ets/division-by-zero.ets b/ets2panda/test/runtime/ets/division-by-zero.ets index e38756ea60..291db8e419 100644 --- a/ets2panda/test/runtime/ets/division-by-zero.ets +++ b/ets2panda/test/runtime/ets/division-by-zero.ets @@ -23,29 +23,37 @@ function main() : void { try { let result : int = INT_ONE / INT_ZERO; - } catch (error: ArithmeticError) { - caught_counter++; + } catch (error) { + if (error instanceof ArithmeticError) { + caught_counter++; + } } arktest.assertEQ(caught_counter, 1) try { let result : long = LONG_ONE / LONG_ZERO; - } catch (error: ArithmeticError) { - caught_counter++; + } catch (error) { + if (error instanceof ArithmeticError) { + caught_counter++; + } } arktest.assertEQ(caught_counter, 2) try { let result : int = INT_ONE % INT_ZERO; - } catch (error: ArithmeticError) { - caught_counter++; + } catch (error) { + if (error instanceof ArithmeticError) { + caught_counter++; + } } arktest.assertEQ(caught_counter, 3) try { let result : long = LONG_ONE % LONG_ZERO; - } catch (error: ArithmeticError) { - caught_counter++; + } catch (error) { + if (error instanceof ArithmeticError) { + caught_counter++; + } } arktest.assertEQ(caught_counter, 4) @@ -54,37 +62,49 @@ function main() : void { try { let result : double = DOUBLE_ONE / DOUBLE_ZERO; - } catch (error: ArithmeticError) { - arktest.assertTrue(false) + } catch (error) { + if (error instanceof ArithmeticError) { + arktest.assertTrue(false) + } } try { let result = INT_ONE / DOUBLE_ZERO; - } catch (error: ArithmeticError) { - arktest.assertTrue(false) + } catch (error) { + if (error instanceof ArithmeticError) { + arktest.assertTrue(false) + } } try { let result = DOUBLE_ONE / INT_ZERO; - } catch (error: ArithmeticError) { - arktest.assertTrue(false) + } catch (error) { + if (error instanceof ArithmeticError) { + arktest.assertTrue(false) + } } try { let result : double = DOUBLE_ONE % DOUBLE_ZERO; - } catch (error: ArithmeticError) { - arktest.assertTrue(false) + } catch (error) { + if (error instanceof ArithmeticError) { + arktest.assertTrue(false) + } } try { let result = INT_ONE % DOUBLE_ZERO; - } catch (error: ArithmeticError) { - arktest.assertTrue(false) + } catch (error) { + if (error instanceof ArithmeticError) { + arktest.assertTrue(false) + } } try { let result = DOUBLE_ONE % INT_ZERO; - } catch (error: ArithmeticError) { - arktest.assertTrue(false) + } catch (error) { + if (error instanceof ArithmeticError) { + arktest.assertTrue(false) + } } } diff --git a/ets2panda/test/runtime/ets/finallyClauseThrow.ets b/ets2panda/test/runtime/ets/finallyClauseThrow.ets index 2b9537b10a..5d0bbf9e09 100644 --- a/ets2panda/test/runtime/ets/finallyClauseThrow.ets +++ b/ets2panda/test/runtime/ets/finallyClauseThrow.ets @@ -17,14 +17,17 @@ function main() : int { try { try { throw new Exception(); - } catch (e:Error) { - arktest.assertTrue(false) + } catch (error) { + if (error instanceof Error) { + arktest.assertTrue(false); + } } finally { throw new Error(); } - } catch (e:Error) { - } catch (e) { - arktest.assertTrue(false) + } catch (error) { + if (error instanceof Error) {} else { + arktest.assertTrue(false) + } } return 0; } \ No newline at end of file diff --git a/ets2panda/test/runtime/ets/generic_Error_class.ets b/ets2panda/test/runtime/ets/generic_Error_class.ets index 50aa86c29d..3533691896 100644 --- a/ets2panda/test/runtime/ets/generic_Error_class.ets +++ b/ets2panda/test/runtime/ets/generic_Error_class.ets @@ -18,12 +18,14 @@ class E extends Error {} function main(): int { try { throw new E(); - } catch (e: E) { - return 0; - } catch (e: E) { - return 0; } catch (e) { - return 0; + if (e instanceof E) { + return 0; + } else if (e instanceof E) { + return 0; + } else { + return 0; + } } } diff --git a/ets2panda/test/runtime/ets/generic_exception.ets b/ets2panda/test/runtime/ets/generic_exception.ets index e5ef48a63f..b298b85889 100644 --- a/ets2panda/test/runtime/ets/generic_exception.ets +++ b/ets2panda/test/runtime/ets/generic_exception.ets @@ -18,12 +18,14 @@ class E extends Exception {} function main(): int { try { throw new E(); - } catch (e: E) { - return 0; - } catch (e: E) { - return 0; } catch (e) { - return 0; + if (e instanceof E) { + return 0; + } else if (e instanceof E) { + return 0; + } else { + return 0; + } } } diff --git a/ets2panda/test/runtime/ets/member-expression-nullptr.ets b/ets2panda/test/runtime/ets/member-expression-nullptr.ets index f0eeafcc76..24711fe364 100644 --- a/ets2panda/test/runtime/ets/member-expression-nullptr.ets +++ b/ets2panda/test/runtime/ets/member-expression-nullptr.ets @@ -44,8 +44,10 @@ function main(): void { try { let residence = (john as Person).residence; - } catch (e: ClassCastError) { - test = true; + } catch (e) { + if (e instanceof ClassCastError) { + test = true; + } } arktest.assertEQ(test, true) @@ -54,8 +56,10 @@ function main(): void { try { let numbers: int = john!.residence.numberOfRooms; - } catch (e: NullPointerError) { - test = true; + } catch (e) { + if (e instanceof NullPointerError) { + test = true; + } } arktest.assertEQ(test, true) @@ -63,8 +67,10 @@ function main(): void { arktest.assertEQ(test, false) try { let numbers: int = foo() + bar() + john!.residence.numberOfRooms; - } catch (e: NullPointerError) { - test = true; + } catch (e) { + if (e instanceof NullPointerError) { + test = true; + } } arktest.assertEQ(test, true) arktest.assertEQ(a, 2) // foo and bar were evaluated diff --git a/ets2panda/test/runtime/ets/multi-array-new-catched-1.ets b/ets2panda/test/runtime/ets/multi-array-new-catched-1.ets index 353980fef8..ab8a06edc7 100644 --- a/ets2panda/test/runtime/ets/multi-array-new-catched-1.ets +++ b/ets2panda/test/runtime/ets/multi-array-new-catched-1.ets @@ -20,8 +20,10 @@ function main(): void { let catched = false; try { a = new Number[A.length + 2.][4][A.length + 3.00001]; - } catch (e: TypeError) { - catched = true; + } catch (e) { + if (e instanceof TypeError) { + catched = true; + } } arktest.assertTrue(catched) diff --git a/ets2panda/test/runtime/ets/multi-array-new-catched-2.ets b/ets2panda/test/runtime/ets/multi-array-new-catched-2.ets index f518d1b800..dfc04b8f38 100644 --- a/ets2panda/test/runtime/ets/multi-array-new-catched-2.ets +++ b/ets2panda/test/runtime/ets/multi-array-new-catched-2.ets @@ -23,8 +23,10 @@ function main(): void { let catched = false; try { a = new Number[baz()][4][A.length + 3.0000]; - } catch (e: TypeError) { - catched = true; + } catch (e) { + if (e instanceof TypeError) { + catched = true; + } } arktest.assertTrue(catched) diff --git a/ets2panda/test/runtime/ets/notNull.ets b/ets2panda/test/runtime/ets/notNull.ets index cf3943bff0..65fe54e615 100644 --- a/ets2panda/test/runtime/ets/notNull.ets +++ b/ets2panda/test/runtime/ets/notNull.ets @@ -101,8 +101,10 @@ function testNPE() : void { try { (o as Object | null)!; arktest.assertTrue(false, "this must not be executed") - } catch (ex: NullPointerError) { - npe_caught = true; + } catch (ex) { + if (ex instanceof NullPointerError) { + npe_caught = true; + } } arktest.assertTrue(npe_caught, "NPE must be caught") @@ -110,8 +112,10 @@ function testNPE() : void { try { baz()!; arktest.assertTrue(false, "this must not be executed") - } catch (ex: NullPointerError) { - npe_caught = true; + } catch (ex) { + if (ex instanceof NullPointerError) { + npe_caught = true; + } } arktest.assertTrue(npe_caught, "NPE must be caught") diff --git a/ets2panda/test/runtime/ets/optional-chaining-function-call.ets b/ets2panda/test/runtime/ets/optional-chaining-function-call.ets index b4b6bac336..0a1e3133c6 100644 --- a/ets2panda/test/runtime/ets/optional-chaining-function-call.ets +++ b/ets2panda/test/runtime/ets/optional-chaining-function-call.ets @@ -34,8 +34,10 @@ function foo(a: funcType | null) { let test = false; try { fruit = a!(); - } catch (e: NullPointerError) { - test = true; + } catch (e) { + if (e instanceof NullPointerError) { + test = true; + } } arktest.assertEQ(test, true) diff --git a/ets2panda/test/runtime/ets/overload_declaration/notNull.ets b/ets2panda/test/runtime/ets/overload_declaration/notNull.ets index 837a42a528..031d8ee927 100644 --- a/ets2panda/test/runtime/ets/overload_declaration/notNull.ets +++ b/ets2panda/test/runtime/ets/overload_declaration/notNull.ets @@ -103,8 +103,10 @@ function testNPE(): void { try { (o as Object | null)!; arktest.assertTrue(false, "this must not be executed") - } catch (ex: NullPointerError) { - npe_caught = true; + } catch (ex) { + if (ex instanceof NullPointerError) { + npe_caught = true; + } } arktest.assertTrue(npe_caught, "NPE must be caught") @@ -112,8 +114,10 @@ function testNPE(): void { try { baz()!; arktest.assertTrue(false, "this must not be executed") - } catch (ex: NullPointerError) { - npe_caught = true; + } catch (ex) { + if (ex instanceof NullPointerError) { + npe_caught = true; + } } arktest.assertTrue(npe_caught, "NPE must be caught") diff --git a/ets2panda/test/runtime/ets/top_level_02.ets b/ets2panda/test/runtime/ets/top_level_02.ets index bbdf6f1608..484bb2b487 100644 --- a/ets2panda/test/runtime/ets/top_level_02.ets +++ b/ets2panda/test/runtime/ets/top_level_02.ets @@ -37,11 +37,11 @@ try { s += "cba"; q += 5; throw new TestException(); -} -catch (e: TestException) { -} -catch (e) { - arktest.assertTrue(false) +} catch (e) { + if (e instanceof TestException) {} + else { + arktest.assertTrue(false) + } } arktest.assertEQ(q, 8) diff --git a/ets2panda/test/runtime/ets/try-catch-error.ets b/ets2panda/test/runtime/ets/try-catch-error.ets index 5428c1e6c6..efd9a4c0c0 100644 --- a/ets2panda/test/runtime/ets/try-catch-error.ets +++ b/ets2panda/test/runtime/ets/try-catch-error.ets @@ -20,40 +20,48 @@ let catchCode = 0; function main(): void { try { throw new DivideByZeroError(); - } catch (p: DivideByZeroError) { - catchCode = 1; - } catch (p: Error) { - arktest.assertTrue(false) + } catch (p) { + if (p instanceof DivideByZeroError) { + catchCode = 1; + } else if (p instanceof Error) { + arktest.assertTrue(false) + } } arktest.assertEQ(catchCode, 1) try { throw new DivideByZeroError(); - } catch (p: Error) { - catchCode = 2; - } catch (p: DivideByZeroError) { - arktest.assertTrue(false) + } catch (p) { + if (p instanceof Error) { + catchCode = 2; + } else if (p instanceof DivideByZeroError) { + arktest.assertTrue(false) + } } arktest.assertEQ(catchCode, 2) try { throw new TestError(); - } catch (p: TestError) { - catchCode = 3; - } catch (p: Error) { - arktest.assertTrue(false) + } catch (p) { + if (p instanceof TestError) { + catchCode = 3; + } else if (p instanceof Error) { + arktest.assertTrue(false) + } } arktest.assertEQ(catchCode, 3) try { throw new TestError(); - } catch (p: Error) { - catchCode = 4; - } catch (p: TestError) { - arktest.assertTrue(false) + } catch (p) { + if (p instanceof Error) { + catchCode = 4; + } else if (p instanceof TestError) { + arktest.assertTrue(false) + } } arktest.assertEQ(catchCode, 4) diff --git a/ets2panda/test/runtime/ets/try-catch.ets b/ets2panda/test/runtime/ets/try-catch.ets index 9415ba2e58..aaa60ef168 100644 --- a/ets2panda/test/runtime/ets/try-catch.ets +++ b/ets2panda/test/runtime/ets/try-catch.ets @@ -20,48 +20,56 @@ let catchCode = 0; function main(): void { try { throw new NullPointerError(); - } catch (e: NullPointerError) { - catchCode = 1; - } catch (e: Error) { - arktest.assertTrue(false) } catch (e) { - arktest.assertTrue(false) + if (e instanceof NullPointerError) { + catchCode = 1; + } else if (e instanceof Error) { + arktest.assertTrue(false) + } else { + arktest.assertTrue(false) + } } arktest.assertEQ(catchCode, 1) try { throw new NullPointerError(); - } catch (e: Error) { - catchCode = 2; - } catch (e: NullPointerError) { - arktest.assertTrue(false) } catch (e) { - arktest.assertTrue(false) + if (e instanceof Error) { + catchCode = 2; + } else if (e instanceof NullPointerError) { + arktest.assertTrue(false) + } else { + arktest.assertTrue(false) + } } arktest.assertEQ(catchCode, 2) try { throw new TestException(); - } catch(e: TestException) { - catchCode = 3; - } catch (e: Exception) { - arktest.assertTrue(false) } catch (e) { - arktest.assertTrue(false) + if (e instanceof TestException) { + catchCode = 3; + } else if (e instanceof Exception) { + arktest.assertTrue(false) + } else { + arktest.assertTrue(false) + } } arktest.assertEQ(catchCode, 3) try { throw new TestException(); - } catch (e: Exception) { - catchCode = 4; - } catch (e: TestException) { - arktest.assertTrue(false) } catch (e) { - arktest.assertTrue(false) + if (e instanceof Exception) { + catchCode = 4; + } else if (e instanceof TestException) { + arktest.assertTrue(false) + } else { + arktest.assertTrue(false) + } } arktest.assertEQ(catchCode, 4) diff --git a/ets2panda/test/runtime/ets/union_type_RTE.ets b/ets2panda/test/runtime/ets/union_type_RTE.ets index 0c73ea88d3..57ff53bc38 100644 --- a/ets2panda/test/runtime/ets/union_type_RTE.ets +++ b/ets2panda/test/runtime/ets/union_type_RTE.ets @@ -29,8 +29,10 @@ function main() { try { foo(new A(11) as Object) - } catch (error: ClassCastError) { - caught_counter++ + } catch (error) { + if (error instanceof ClassCastError) { + caught_counter++; + } } arktest.assertEQ(caught_counter, 1) diff --git a/ets2panda/test/unit/public/ast_verifier_check_scope_declaration_test.cpp b/ets2panda/test/unit/public/ast_verifier_check_scope_declaration_test.cpp index c5d4a0d67c..1112e68ff3 100644 --- a/ets2panda/test/unit/public/ast_verifier_check_scope_declaration_test.cpp +++ b/ets2panda/test/unit/public/ast_verifier_check_scope_declaration_test.cpp @@ -90,8 +90,10 @@ TEST_F(ASTVerifierTest, TryCatch) try { throw new NullPointerError(); - } catch (e: NullPointerError) { - catchCode = 1; + } catch (e) { + if (e instanceof NullPointerError) { + catchCode = 1; + } } } )"; -- Gitee