From b6086b7c161e0c24f0d7f471f1c2ed06ce2645b2 Mon Sep 17 00:00:00 2001 From: "584648456@qq.com" <584648456@qq.com> Date: Sat, 7 Jun 2025 16:29:46 +0800 Subject: [PATCH] Title: for-of union type recognition Description(optional): for-of union type recognition Issue: https://gitee.com/openharmony/arkcompiler_ets_frontend/issues/IC7OBY Signed-off-by: semon <584648456@qq.com> --- .../plugins/ets/stdlib/escompat/Date.ets | 2 +- .../plugins/ets/stdlib/std/core/String.ets | 4 ++++ .../09.for_of_statements/for_of.params.yaml | 20 +++++++++++-------- .../01.smart_types/for_of_stmt.params.yaml | 4 ++-- .../for_of.params.yaml | 20 +++++++++---------- .../Iterable_Types/for-of-s01-string-01.ets | 4 ++-- .../Iterable_Types/for-of-s01-string-02.ets | 2 +- .../Iterable_Types/for-of-s01-string-03.ets | 4 ++-- .../Iterable_Types/for-of-s01-string-04.ets | 2 +- 9 files changed, 35 insertions(+), 27 deletions(-) diff --git a/static_core/plugins/ets/stdlib/escompat/Date.ets b/static_core/plugins/ets/stdlib/escompat/Date.ets index c96e782250..7a5c9989ef 100644 --- a/static_core/plugins/ets/stdlib/escompat/Date.ets +++ b/static_core/plugins/ets/stdlib/escompat/Date.ets @@ -2025,7 +2025,7 @@ class ISODateParser { const literalText = this.dateStr.substring(startPos, endPos) for (const literalChar of literalText) { - if (!Char.isDecDigit(literalChar)) { + if (!Char.isDecDigit(literalChar.charAt(0))) { throw new ISODateParseError() } } diff --git a/static_core/plugins/ets/stdlib/std/core/String.ets b/static_core/plugins/ets/stdlib/std/core/String.ets index d9bbe6b3f4..bcd2a09c2c 100644 --- a/static_core/plugins/ets/stdlib/std/core/String.ets +++ b/static_core/plugins/ets/stdlib/std/core/String.ets @@ -139,6 +139,10 @@ export final class String extends Object implements Comparable, JSONable return this.getLength().toDouble() } + public charAtString(index: int): string { + return this.charAt(index as number).toString(); + } + /** * Getter for char at some index * diff --git a/static_core/plugins/ets/tests/ets-templates/08.statements/09.for_of_statements/for_of.params.yaml b/static_core/plugins/ets/tests/ets-templates/08.statements/09.for_of_statements/for_of.params.yaml index 504de9f26c..244ff48334 100644 --- a/static_core/plugins/ets/tests/ets-templates/08.statements/09.for_of_statements/for_of.params.yaml +++ b/static_core/plugins/ets/tests/ets-templates/08.statements/09.for_of_statements/for_of.params.yaml @@ -77,32 +77,36 @@ cases: assertEQ( res, "1234" ) - use: |- - const ch: char = c'X' + const ch: string = 'X' for (let ch of "A" + "B" + "CD") { res += ch - ch = c' ' + ch = ' ' res += ch } assertEQ( res, "A B C D " ) - use: |- - let ch: char + let ch: string for (ch of "ABCD") res += ch assertEQ( res, "ABCD" ) - use: |- - let ch: char + let ch: string for (ch of "ABCD") { res += ch - ch = c' ' + ch = ' ' res += ch } assertEQ( res, "A B C D " ) - use: |- - let foo: () => String = ():String => { return "ABCD" } - for (let ch of foo()) res += ++ch - assertEQ( res, "BCDE" ) + res = "" + let foo: () => String = ():String => { return "ABCD" } + for (let str of foo()) { + let ch = str.charAt(0); + res += ++ch; + } + assertEQ( res, "BCDE" ) - use: |- let arr: Long[] = new Long[3] diff --git a/static_core/plugins/ets/tests/ets-templates/15.semantic_rules/06.type_inference/01.smart_types/for_of_stmt.params.yaml b/static_core/plugins/ets/tests/ets-templates/15.semantic_rules/06.type_inference/01.smart_types/for_of_stmt.params.yaml index 3afc33611d..3bb4a03f4f 100644 --- a/static_core/plugins/ets/tests/ets-templates/15.semantic_rules/06.type_inference/01.smart_types/for_of_stmt.params.yaml +++ b/static_core/plugins/ets/tests/ets-templates/15.semantic_rules/06.type_inference/01.smart_types/for_of_stmt.params.yaml @@ -13,10 +13,10 @@ cases: - code: |- - let v: char|null + let v: string|null let res: char for (v of 'a') { - res = v + res = v!.charAt(0); } return res == c'a' ? 0 : 1 diff --git a/static_core/plugins/ets/tests/ets-templates/17.experimental_features/07.statements/01.for-of_type_annotation/for_of.params.yaml b/static_core/plugins/ets/tests/ets-templates/17.experimental_features/07.statements/01.for-of_type_annotation/for_of.params.yaml index 481c35cdf3..815321dffd 100644 --- a/static_core/plugins/ets/tests/ets-templates/17.experimental_features/07.statements/01.for-of_type_annotation/for_of.params.yaml +++ b/static_core/plugins/ets/tests/ets-templates/17.experimental_features/07.statements/01.for-of_type_annotation/for_of.params.yaml @@ -35,21 +35,21 @@ cases: - use: |- // empty string - for (let v: char of "") res += v + for (let v: string of "") res += v.charAt(0) if (res == "") return 0; - decl: |- // empty nullable string let s: string|null = "" use: |- - for (let v: char of s!) res += v + for (let v: string of s!) res += v.charAt(0) if (res == "") return 0; - decl: |- // empty nullable string let s: string|undefined = "" use: |- - for (const v: char of s!) res += v + for (const v: string of s!) res += v.charAt(0) if (res == "") return 0; - decl: |- @@ -208,7 +208,7 @@ cases: let arr: string[] = ["ABC", "DEF", "GHJ"] use: |- for (let v: string of arr) - for (let q: char of v) + for (let q: string of v) res += q + " " if (res == "A B C D E F G H J ") return 0 @@ -221,13 +221,13 @@ cases: - use: |- // expression with strings - let ch: char = c'X' - for (let ch: char of "A" + "B" + "CD") { + let ch: string = 'X' + for (let ch: string of "A" + "B" + "CD") { res += ch - ch = c' ' + ch = ' ' res += ch } - if (res == "A B C D " && ch == c'X') return 0; + if (res == "A B C D " && ch == 'X') return 0; - decl: |- function foo(s: T): T[] { @@ -237,8 +237,8 @@ cases: // function with generics, String array for (let v: String of foo("AZ")) { res += "-" - for (let q: Char of v) { - res += q.unboxed() + for (let q: string of v) { + res += (q.charAt(0) as Char).unboxed() } } if (res == "-AZ-AZ-AZ") return 0; diff --git a/static_core/plugins/ets/tests/ets_func_tests/spec/17.Experimental_Features/Iterable_Types/for-of-s01-string-01.ets b/static_core/plugins/ets/tests/ets_func_tests/spec/17.Experimental_Features/Iterable_Types/for-of-s01-string-01.ets index 3bd7ed2920..fa62c47489 100644 --- a/static_core/plugins/ets/tests/ets_func_tests/spec/17.Experimental_Features/Iterable_Types/for-of-s01-string-01.ets +++ b/static_core/plugins/ets/tests/ets_func_tests/spec/17.Experimental_Features/Iterable_Types/for-of-s01-string-01.ets @@ -19,11 +19,11 @@ tags: [] ---*/ function main() { - let ch : char = c'z'; + let ch : string = 'z'; let index : int = 0; let result : int = 0; let testStr : String = "a string object"; - let testArray : char[] = [c'a', c' ', c's', c't', c'r', c'i', c'n', c'g', c' ', c'o', c'b', c'j', c'e', c'c', c't']; + let testArray : string[] = ['a', ' ', 's', 't', 'r', 'i', 'n', 'g', ' ', 'o', 'b', 'j', 'e', 'c', 't']; for (ch of testStr) { if (ch != testArray[index]) { console.log("Incorrect element at index : " + index); diff --git a/static_core/plugins/ets/tests/ets_func_tests/spec/17.Experimental_Features/Iterable_Types/for-of-s01-string-02.ets b/static_core/plugins/ets/tests/ets_func_tests/spec/17.Experimental_Features/Iterable_Types/for-of-s01-string-02.ets index b117da4bda..2b6507eab5 100644 --- a/static_core/plugins/ets/tests/ets_func_tests/spec/17.Experimental_Features/Iterable_Types/for-of-s01-string-02.ets +++ b/static_core/plugins/ets/tests/ets_func_tests/spec/17.Experimental_Features/Iterable_Types/for-of-s01-string-02.ets @@ -19,7 +19,7 @@ tags: [] ---*/ function main() : int { - let tmp : char = c'z'; + let tmp : string = 'z'; let initialString : String = "abcdef"; let workString : String = ""; for (tmp of initialString) { diff --git a/static_core/plugins/ets/tests/ets_func_tests/spec/17.Experimental_Features/Iterable_Types/for-of-s01-string-03.ets b/static_core/plugins/ets/tests/ets_func_tests/spec/17.Experimental_Features/Iterable_Types/for-of-s01-string-03.ets index ec86dac176..43ed100ebf 100644 --- a/static_core/plugins/ets/tests/ets_func_tests/spec/17.Experimental_Features/Iterable_Types/for-of-s01-string-03.ets +++ b/static_core/plugins/ets/tests/ets_func_tests/spec/17.Experimental_Features/Iterable_Types/for-of-s01-string-03.ets @@ -23,11 +23,11 @@ function doit(ch : char) : FixedArray { } function main() : int { - let tmp : char = c'z'; + let tmp : string = 'z'; let initialString : String = "abcdef"; let workString : String = ""; for (tmp of initialString) { - workString = workString + new String(doit(tmp)); + workString = workString + new String(doit(tmp.charAt(0))); } if (workString == "aabbccddeeff") { diff --git a/static_core/plugins/ets/tests/ets_func_tests/spec/17.Experimental_Features/Iterable_Types/for-of-s01-string-04.ets b/static_core/plugins/ets/tests/ets_func_tests/spec/17.Experimental_Features/Iterable_Types/for-of-s01-string-04.ets index bae8b01ee2..6a2524d443 100644 --- a/static_core/plugins/ets/tests/ets_func_tests/spec/17.Experimental_Features/Iterable_Types/for-of-s01-string-04.ets +++ b/static_core/plugins/ets/tests/ets_func_tests/spec/17.Experimental_Features/Iterable_Types/for-of-s01-string-04.ets @@ -20,7 +20,7 @@ tags: [] function main() : int { let index : int = 0; - let tmp : char = c'z'; + let tmp : string = 'z'; let initialString : String = ""; for (tmp of initialString) { index++; -- Gitee