From 8ab1bab92dfdea27a8d461a936edf7c5a10667fd Mon Sep 17 00:00:00 2001 From: mustafadinc Date: Fri, 8 Aug 2025 01:46:04 +0300 Subject: [PATCH] Crash with foo Issue : https://gitee.com/openharmony/arkcompiler_ets_frontend/issues/ICK4MC Signed-off-by: mustafadinc --- ets2panda/checker/ETSAnalyzer.cpp | 20 +++++++++++++ .../generic_method_with_default_short_neg.ets | 28 +++++++++++++++++ ...generic_method_with_promise_return_neg.ets | 30 +++++++++++++++++++ .../ets/invalid_cast_generic_type_neg_0.ets | 20 +++++++++++++ .../ets/invalid_cast_generic_type_neg_1.ets | 20 +++++++++++++ .../ets/invalid_cast_generic_type_neg_2.ets | 18 +++++++++++ 6 files changed, 136 insertions(+) create mode 100644 ets2panda/test/ast/compiler/ets/generic_method_with_default_short_neg.ets create mode 100644 ets2panda/test/ast/compiler/ets/generic_method_with_promise_return_neg.ets create mode 100644 ets2panda/test/ast/compiler/ets/invalid_cast_generic_type_neg_0.ets create mode 100644 ets2panda/test/ast/compiler/ets/invalid_cast_generic_type_neg_1.ets create mode 100644 ets2panda/test/ast/compiler/ets/invalid_cast_generic_type_neg_2.ets diff --git a/ets2panda/checker/ETSAnalyzer.cpp b/ets2panda/checker/ETSAnalyzer.cpp index 9af93405a2..515f962441 100644 --- a/ets2panda/checker/ETSAnalyzer.cpp +++ b/ets2panda/checker/ETSAnalyzer.cpp @@ -3806,6 +3806,26 @@ checker::Type *ETSAnalyzer::Check(ir::TSAsExpression *expr) const return expr->SetTsType(checker->TypeError(expr, diagnostic::NULLISH_CAST_TO_NONNULLISH, expr->Start())); } + if (expr->Expr()->IsLiteral() && sourceType->IsBuiltinNumeric() && targetType->IsETSTypeParameter()) { + checker->LogError(diagnostic::INVALID_CAST, {sourceType->ToString(), targetType->ToString()}, + expr->Expr()->Start()); + return checker->InvalidateType(expr); + } + + if (expr->Expr()->IsLiteral() && sourceType->IsBuiltinNumeric() && targetType->IsETSUnionType()) { + bool allAreTypeParams = true; + for (auto *sub : targetType->AsETSUnionType()->ConstituentTypes()) { + if (!sub->IsETSTypeParameter()) { + allAreTypeParams = false; + } + } + if (allAreTypeParams) { + checker->LogError(diagnostic::INVALID_CAST, {sourceType->ToString(), targetType->ToString()}, + expr->Expr()->Start()); + return checker->InvalidateType(expr); + } + } + const checker::CastingContext ctx( checker->Relation(), sourceType->IsBuiltinNumeric() && targetType->IsBuiltinNumeric() ? diagnostic::IMPROPER_NUMERIC_CAST diff --git a/ets2panda/test/ast/compiler/ets/generic_method_with_default_short_neg.ets b/ets2panda/test/ast/compiler/ets/generic_method_with_default_short_neg.ets new file mode 100644 index 0000000000..ee7b46aabf --- /dev/null +++ b/ets2panda/test/ast/compiler/ets/generic_method_with_default_short_neg.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. + */ + +class Point { + foo(i:X = 0 as X): X { + return i; + } +} + +function main(): void { + let p: Point = new Point(); + let result: short = p.foo(); + arktest.assertEQ(result, 0 as short); +} + +/* @@? 17:32 Error TypeError: Cannot cast type 'Int' to 'X' */ diff --git a/ets2panda/test/ast/compiler/ets/generic_method_with_promise_return_neg.ets b/ets2panda/test/ast/compiler/ets/generic_method_with_promise_return_neg.ets new file mode 100644 index 0000000000..5676324ca1 --- /dev/null +++ b/ets2panda/test/ast/compiler/ets/generic_method_with_promise_return_neg.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 Point { + foo(): Promise { + return Promise.resolve(1 as X); + } +} + +function main(): void { + const p = new Point(); + p.foo().then((value: short) => { + arktest.assertEQ(value, 1 as short); + }); +} + +/* @@? 18:15 Error TypeError: Type 'Promise<*ERROR_TYPE*>' is not compatible with the enclosing method's return type 'Promise' */ +/* @@? 18:31 Error TypeError: Cannot cast type 'Int' to 'X' */ diff --git a/ets2panda/test/ast/compiler/ets/invalid_cast_generic_type_neg_0.ets b/ets2panda/test/ast/compiler/ets/invalid_cast_generic_type_neg_0.ets new file mode 100644 index 0000000000..eaf899d40c --- /dev/null +++ b/ets2panda/test/ast/compiler/ets/invalid_cast_generic_type_neg_0.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 foo() { + let x = 1 as T; +} + +/* @@? 17:13 Error TypeError: Cannot cast type 'Int' to 'T' */ diff --git a/ets2panda/test/ast/compiler/ets/invalid_cast_generic_type_neg_1.ets b/ets2panda/test/ast/compiler/ets/invalid_cast_generic_type_neg_1.ets new file mode 100644 index 0000000000..030e6da0b0 --- /dev/null +++ b/ets2panda/test/ast/compiler/ets/invalid_cast_generic_type_neg_1.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 foo() { + let x = 1 as (T | U); +} + +/* @@? 17:13 Error TypeError: Cannot cast type 'Int' to 'T|U' */ diff --git a/ets2panda/test/ast/compiler/ets/invalid_cast_generic_type_neg_2.ets b/ets2panda/test/ast/compiler/ets/invalid_cast_generic_type_neg_2.ets new file mode 100644 index 0000000000..728f82f7f6 --- /dev/null +++ b/ets2panda/test/ast/compiler/ets/invalid_cast_generic_type_neg_2.ets @@ -0,0 +1,18 @@ +/* + * 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(v: X = 1 as (X | Y)){} + +/* @@? 16:55 Error TypeError: Cannot cast type 'Int' to 'X|Y' */ -- Gitee