diff --git a/ets2panda/checker/ETSAnalyzer.cpp b/ets2panda/checker/ETSAnalyzer.cpp index 9af93405a21175a9610ce64decd8a3b6ba206b65..515f962441ae17bab5115a8307d1e81a4c31139a 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 0000000000000000000000000000000000000000..ee7b46aabf12ce6f2b615ef62edc6067333cf5d8 --- /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 0000000000000000000000000000000000000000..5676324ca1bd8a50b06d652ccbd11136630673ca --- /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 0000000000000000000000000000000000000000..eaf899d40ce736f4e64719fe4919d3b676c2395e --- /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 0000000000000000000000000000000000000000..030e6da0b06a8c20bc58d7100e22d6e31199acce --- /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 0000000000000000000000000000000000000000..728f82f7f6b29c0eec9cdb4010f5054f0447828c --- /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' */