From 3e73f5b06382b70d2b73483787d7469feb8beb5b Mon Sep 17 00:00:00 2001 From: Martin Sajti Date: Wed, 16 Apr 2025 17:01:28 +0200 Subject: [PATCH] Fix wrong optional param types in lambdas In the cases of optional params in a function type, the type of the parameter is not checked correctly. Fix the erroneous types in the stdlib and tests. Issue: https://gitee.com/openharmony/arkcompiler_ets_frontend/issues/IC4121 Internal issue: #24487 Test: build, runtime test, cts test Change-Id: Ifba7614dc3c6f7c35c4a57f6bc8b18e6b51073ba Signed-off-by: Martin Sajti --- .../fcall.params.yaml | 9 ++++++++- .../03.asynchronous_api/05.promise/p.params.yaml | 16 ++++++---------- .../tests/interop_js/eacoro/eaworker_test.ets | 2 +- .../interop_js/tests/promise/promise_tests.ets | 6 ++++-- 4 files changed, 19 insertions(+), 14 deletions(-) diff --git a/static_core/plugins/ets/tests/ets-templates/07.expressions/11.function_call_expression/fcall.params.yaml b/static_core/plugins/ets/tests/ets-templates/07.expressions/11.function_call_expression/fcall.params.yaml index 50b3f1b622..d21323afc8 100644 --- a/static_core/plugins/ets/tests/ets-templates/07.expressions/11.function_call_expression/fcall.params.yaml +++ b/static_core/plugins/ets/tests/ets-templates/07.expressions/11.function_call_expression/fcall.params.yaml @@ -216,7 +216,7 @@ cases: let r: Boolean | undefined = ll?.('Hello', 42, true) arktest.assertEQ(undefined, r) arktest.assertEQ(0, calls) - + ll = getLambdaOrNull(true) r = ll?.('Hello', 42, true) arktest.assertEQ(true, r) @@ -294,3 +294,10 @@ cases: let value = ((up: LP): User => { return up() }) () { return {name: 'Alice', age: 16 } } arktest.assertEQ('Alice', value.name) arktest.assertTrue(value instanceof User) + + - decl: |- + function bar(callee: (a: int) => void): void { + callee(1) + } + use: |- + bar((): void => {}) diff --git a/static_core/plugins/ets/tests/ets-templates/16.concurrency/03.asynchronous_api/05.promise/p.params.yaml b/static_core/plugins/ets/tests/ets-templates/16.concurrency/03.asynchronous_api/05.promise/p.params.yaml index 5ed304c6a0..1621d83e6f 100644 --- a/static_core/plugins/ets/tests/ets-templates/16.concurrency/03.asynchronous_api/05.promise/p.params.yaml +++ b/static_core/plugins/ets/tests/ets-templates/16.concurrency/03.asynchronous_api/05.promise/p.params.yaml @@ -14,12 +14,11 @@ --- cases: - decl: |- - type ON = Object|null let s = 'XYZ' use: |- // test resolving path let r: Promise = new Promise( - (resolve: (p: string) => void, reject: (p: ON) => void): void => { + (resolve: (p: string) => void, reject: (p: Error) => void): void => { if (s == 'XYZ') { resolve(s) } else { @@ -30,12 +29,11 @@ cases: arktest.assertTrue((await r) == 'XYZ') - decl: |- - type ON = Object|null let s = 'XYZ' use: |- // test rejection path let r: Promise = new Promise( - (resolve: (p: string) => void, reject: (p: ON) => void): void => { + (resolve: (p: string) => void, reject: (p: Error) => void): void => { if (s != 'XYZ') { resolve(s) } else { @@ -53,12 +51,11 @@ cases: arktest.assertTrue(false) - decl: |- - type ON = Object|null let s = 'XYZ' use: |- // test throw from resolving path let r: Promise = new Promise( - (resolve: (p: string) => void, reject: (p: ON) => void): void => { + (resolve: (p: string) => void, reject: (p: Error) => void): void => { if (s == 'XYZ') { throw new Error(s) } else { @@ -76,12 +73,11 @@ cases: arktest.assertTrue(false) - decl: |- - type ON = Object|null let initVal: number = 1.0 use: |- // test then chain, resolved let r = new Promise( - (resolve: (p: number) => void, reject: (p: ON) => void): void => { + (resolve: (p: number) => void, reject: (p: Error) => void): void => { if (initVal > 0) { resolve(initVal) } else { @@ -105,7 +101,7 @@ cases: use: |- // test catch chain let r: Promise = new Promise( - (resolve: (p: number) => void, reject: (p: ON) => void): void => { + (resolve: (p: number) => void, reject: (p: Error) => void): void => { if (initVal < 0) { resolve(initVal) } else { @@ -144,7 +140,7 @@ cases: use: |- // test mixed then and catch chain let r: Promise = new Promise( - (resolve: (p: string) => void, reject: (p: ON) => void): void => { + (resolve: (p: string) => void, reject: (p: Error) => void): void => { if (initVal > 0) { resolve('A') } else { diff --git a/static_core/plugins/ets/tests/interop_js/eacoro/eaworker_test.ets b/static_core/plugins/ets/tests/interop_js/eacoro/eaworker_test.ets index 0ce3e701d0..b1d748ab5a 100644 --- a/static_core/plugins/ets/tests/interop_js/eacoro/eaworker_test.ets +++ b/static_core/plugins/ets/tests/interop_js/eacoro/eaworker_test.ets @@ -68,7 +68,7 @@ function RunTasksWithJsAsyncCallTest(): void { let modified = false; let method = JSRuntime.getPropertyJSValue(module, 'asyncWithAwait'); let promise = (ESValue.wrap(JSRuntime.invoke(module, method))).toPromise(); - let promise1 = promise.then((x:Object): void => { + let promise1 = promise.then((x:Any): void => { modified = true; }); diff --git a/static_core/plugins/ets/tests/interop_js/tests/promise/promise_tests.ets b/static_core/plugins/ets/tests/interop_js/tests/promise/promise_tests.ets index 22bbdc25f8..02a7fbf232 100644 --- a/static_core/plugins/ets/tests/interop_js/tests/promise/promise_tests.ets +++ b/static_core/plugins/ets/tests/interop_js/tests/promise/promise_tests.ets @@ -755,9 +755,11 @@ function testPromiseRaceRejected(): void { function testPromiseThenOnRejection(): void { globalTest = new Test(); - Promise.reject(new Error("test")).then( - (value: Object): void => { + Promise.reject(new Error("test")).then( + (value: Object): string => { globalTest!.fail(); + // String return type needed, until 'String|void' type is supported + return ""; }, (error: Error): string => { return "correct flow"; }).then((value): string => { -- Gitee