Skip to content

Commit

Permalink
Deprecate array->string cast (#23380)
Browse files Browse the repository at this point in the history
Deprecate casting arrays to string. This is a continuation of the
deprecation of the universal string cast from [this
PR](#22068). See
#19893 for more context.

This deprecation recommends replacing:
```chapel
var s = myArray: string;
```
with:
```chapel
var s = "%?".format(myArray);
```

- [x] paratest

[ reviewed by @riftEmber ] - Thanks!
  • Loading branch information
jeremiah-corrado authored Sep 14, 2023
2 parents 05e8c98 + 51ef5ad commit 23e35cd
Show file tree
Hide file tree
Showing 12 changed files with 46 additions and 25 deletions.
1 change: 1 addition & 0 deletions modules/internal/ChapelArray.chpl
Original file line number Diff line number Diff line change
Expand Up @@ -2210,6 +2210,7 @@ module ChapelArray {

// How to cast arrays to strings
@chpldoc.nodoc
@deprecated(notes="casting arrays to string is deprecated; please use 'try! \"%?\".format()' from IO.FormattedIO instead")
operator :(x: [], type t:string) {
import IO.FormattedIO.string;
return try! "%?".format(x);
Expand Down
16 changes: 16 additions & 0 deletions test/arrays/cast/cast-to-string.chpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
var a = [i in 1..10] i;

operator :(arr: [], type t:string) {
var s = "---";
var first = true;
for x in arr {
if first
then first = false;
else s += " ";
s += x:string;
};
s += "---";
return s;
}

writeln(a:string);
1 change: 1 addition & 0 deletions test/arrays/cast/cast-to-string.good
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
---1 2 3 4 5 6 7 8 9 10---
6 changes: 0 additions & 6 deletions test/arrays/cast/castArrToString.chpl

This file was deleted.

4 changes: 0 additions & 4 deletions test/arrays/cast/castArrToString.good

This file was deleted.

6 changes: 0 additions & 6 deletions test/arrays/cast/castArrToString2.chpl

This file was deleted.

4 changes: 0 additions & 4 deletions test/arrays/cast/castArrToString2.good

This file was deleted.

18 changes: 18 additions & 0 deletions test/arrays/cast/promoted-cast-to-string.chpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
record R {
var x: int;

operator: (r: R, type t:string) {
return "-" + r.x:string + "-";
}
}

proc string.init=(r: R) {
init this;
this = r:string;
}

const d = {1..10},
a = [i in d] new R(i),
b: [d] string = a;

writeln(b);
1 change: 1 addition & 0 deletions test/arrays/cast/promoted-cast-to-string.good
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
-1- -2- -3- -4- -5- -6- -7- -8- -9- -10-
2 changes: 2 additions & 0 deletions test/deprecated/IO/array-string-cast.chpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
const x: [1..10] int;
writeln(x:string);
2 changes: 2 additions & 0 deletions test/deprecated/IO/array-string-cast.good
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
array-string-cast.chpl:2: warning: casting arrays to string is deprecated; please use 'try! "%?".format()' from IO.FormattedIO instead
0 0 0 0 0 0 0 0 0 0
10 changes: 5 additions & 5 deletions tools/mason/MasonTest.chpl
Original file line number Diff line number Diff line change
Expand Up @@ -591,11 +591,11 @@ proc runAndLog(executable, fileName, ref result, reqNumLocales: int = numLocales
// (albeit wasteful) thing we can do here is just cast the lists to
// array here.
//
if testNames.size != 0 then testNamesStr = testNames.toArray(): string;
if failedTestNames.size != 0 then failedTestNamesStr = failedTestNames.toArray(): string;
if erroredTestNames.size != 0 then erroredTestNamesStr = erroredTestNames.toArray(): string;
if testsPassed.size != 0 then passedTestStr = testsPassed.toArray(): string;
if skippedTestNames.size != 0 then skippedTestNamesStr = skippedTestNames.toArray(): string;
if testNames.size != 0 then testNamesStr = try! "%?".format(testNames.toArray());
if failedTestNames.size != 0 then failedTestNamesStr = try! "%?".format(failedTestNames.toArray());
if erroredTestNames.size != 0 then erroredTestNamesStr = try! "%?".format(erroredTestNames.toArray());
if testsPassed.size != 0 then passedTestStr = try! "%?".format(testsPassed.toArray());
if skippedTestNames.size != 0 then skippedTestNamesStr = try! "%?".format(skippedTestNames.toArray());
var exec = spawn([executable, "-nl", reqNumLocales: string, "--testNames",
testNamesStr,"--failedTestNames", failedTestNamesStr, "--errorTestNames",
erroredTestNamesStr, "--ranTests", passedTestStr, "--skippedTestNames",
Expand Down

0 comments on commit 23e35cd

Please sign in to comment.