From 9d6352a4f0c5a19d4e1c21b744008322ac31485a Mon Sep 17 00:00:00 2001 From: zmw Date: Wed, 6 Aug 2025 10:39:48 +0800 Subject: [PATCH] Fix some fuzz cherry-pick to 0728 Issue: https://gitee.com/openharmony/arkcompiler_ets_frontend/issues/ICRFND Signed-off-by: zmw --- ets2panda/checker/checkerContext.cpp | 3 ++- .../ets/constantExpressionLowering.cpp | 5 ++++ .../compiler/ets/stwitch_with_non_case.ets | 21 ++++++++++++++++ .../runtime/ets/const_fold_ts_non_null_01.ets | 20 ++++++++++++++++ .../runtime/ets/const_fold_ts_non_null_02.ets | 20 ++++++++++++++++ .../runtime/ets/const_fold_ts_non_null_03.ets | 20 ++++++++++++++++ .../runtime/ets/const_fold_ts_non_null_04.ets | 20 ++++++++++++++++ .../runtime/ets/non_null_number_as_index.ets | 23 ++++++++++++++++++ .../test/runtime/ets/switch_with_non_case.ets | 24 +++++++++++++++++++ 9 files changed, 155 insertions(+), 1 deletion(-) create mode 100644 ets2panda/test/ast/compiler/ets/stwitch_with_non_case.ets create mode 100644 ets2panda/test/runtime/ets/const_fold_ts_non_null_01.ets create mode 100644 ets2panda/test/runtime/ets/const_fold_ts_non_null_02.ets create mode 100644 ets2panda/test/runtime/ets/const_fold_ts_non_null_03.ets create mode 100644 ets2panda/test/runtime/ets/const_fold_ts_non_null_04.ets create mode 100644 ets2panda/test/runtime/ets/non_null_number_as_index.ets create mode 100644 ets2panda/test/runtime/ets/switch_with_non_case.ets diff --git a/ets2panda/checker/checkerContext.cpp b/ets2panda/checker/checkerContext.cpp index 28bf22ca92..fa10d82fac 100644 --- a/ets2panda/checker/checkerContext.cpp +++ b/ets2panda/checker/checkerContext.cpp @@ -494,7 +494,8 @@ void CheckerContext::AddBreakSmartCasts(ir::Statement const *targetStatement, Sm void CheckerContext::CombineBreakSmartCasts(ir::Statement const *targetStatement) { - ES2PANDA_ASSERT(smartCasts_.empty()); + ES2PANDA_ASSERT((targetStatement->IsSwitchStatement() && targetStatement->AsSwitchStatement()->Cases().empty()) || + smartCasts_.empty()); if (!breakSmartCasts_.empty()) { bool firstCase = true; diff --git a/ets2panda/compiler/lowering/ets/constantExpressionLowering.cpp b/ets2panda/compiler/lowering/ets/constantExpressionLowering.cpp index 497cf9a0b9..4cfd12a178 100644 --- a/ets2panda/compiler/lowering/ets/constantExpressionLowering.cpp +++ b/ets2panda/compiler/lowering/ets/constantExpressionLowering.cpp @@ -1414,6 +1414,11 @@ ir::AstNode *ConstantExpressionLowering::Fold(ir::AstNode *constantNode) return FoldTernaryConstant(condExp); } } + if (node->IsTSNonNullExpression() && IsSupportedLiteral(node->AsTSNonNullExpression()->Expr())) { + auto expr = node->AsTSNonNullExpression()->Expr()->Clone(context_->allocator, node->Parent()); + expr->SetRange(node->Range()); + return expr; + } return node; }; constantNode->TransformChildrenRecursivelyPostorder(handleFoldConstant, Name()); diff --git a/ets2panda/test/ast/compiler/ets/stwitch_with_non_case.ets b/ets2panda/test/ast/compiler/ets/stwitch_with_non_case.ets new file mode 100644 index 0000000000..9741d6c34d --- /dev/null +++ b/ets2panda/test/ast/compiler/ets/stwitch_with_non_case.ets @@ -0,0 +1,21 @@ +/* + * 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(x: int): string { + let rc = "default"; + switch (x) { } +} + +/* @@? 16:10 Error TypeError: Function with a non void return type must return a value. */ diff --git a/ets2panda/test/runtime/ets/const_fold_ts_non_null_01.ets b/ets2panda/test/runtime/ets/const_fold_ts_non_null_01.ets new file mode 100644 index 0000000000..eba259c225 --- /dev/null +++ b/ets2panda/test/runtime/ets/const_fold_ts_non_null_01.ets @@ -0,0 +1,20 @@ +/* + * 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 main() { + const a = 1; + let b = a!; + arktest.assertEQ(b, 1); +} diff --git a/ets2panda/test/runtime/ets/const_fold_ts_non_null_02.ets b/ets2panda/test/runtime/ets/const_fold_ts_non_null_02.ets new file mode 100644 index 0000000000..0233128434 --- /dev/null +++ b/ets2panda/test/runtime/ets/const_fold_ts_non_null_02.ets @@ -0,0 +1,20 @@ +/* + * 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 main() { + const a = 'a'; + let b = a!; + arktest.assertEQ(b, 'a'); +} diff --git a/ets2panda/test/runtime/ets/const_fold_ts_non_null_03.ets b/ets2panda/test/runtime/ets/const_fold_ts_non_null_03.ets new file mode 100644 index 0000000000..bbb8cfc24b --- /dev/null +++ b/ets2panda/test/runtime/ets/const_fold_ts_non_null_03.ets @@ -0,0 +1,20 @@ +/* + * 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 main() { + const a = 'hello'; + let b = a!; + arktest.assertEQ(b, 'hello'); +} diff --git a/ets2panda/test/runtime/ets/const_fold_ts_non_null_04.ets b/ets2panda/test/runtime/ets/const_fold_ts_non_null_04.ets new file mode 100644 index 0000000000..08e4971ccb --- /dev/null +++ b/ets2panda/test/runtime/ets/const_fold_ts_non_null_04.ets @@ -0,0 +1,20 @@ +/* + * 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 main() { + const a = true; + let b = a!; + arktest.assertEQ(b, true); +} diff --git a/ets2panda/test/runtime/ets/non_null_number_as_index.ets b/ets2panda/test/runtime/ets/non_null_number_as_index.ets new file mode 100644 index 0000000000..c821a6f6f3 --- /dev/null +++ b/ets2panda/test/runtime/ets/non_null_number_as_index.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. + */ + +function foo(a:string):string { + return a; +} +function main(): void { + let tup: Array<[number, string]>; + tup = [[1, "E"], [2, "F"], [3, "G"]]; + arktest.assertEQ(foo(tup[1][1!]), "F") +} diff --git a/ets2panda/test/runtime/ets/switch_with_non_case.ets b/ets2panda/test/runtime/ets/switch_with_non_case.ets new file mode 100644 index 0000000000..12c625abe8 --- /dev/null +++ b/ets2panda/test/runtime/ets/switch_with_non_case.ets @@ -0,0 +1,24 @@ +/* + * 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(x: int): string { + let rc = "default"; + switch (x) { } + return rc; +} + +function main() { + arktest.assertEQ(foo(1), "default"); +} -- Gitee