Skip to content

Commit

Permalink
stringify and universal cast-to-string deprecation (#22068)
Browse files Browse the repository at this point in the history
As discussed [here](#20256),
`stringify` is being deprecated in favor of `"%?".format`. And the
catch-all `: string` discussed
[here](#19893) (which relied
on `stringify`) is also being deprecated in favor of `"%?".format`.

Specifically, this PR makes the following changes:
- deprecate `stringify`
- deprecate the "catch-all" cast-to-string in ChapelIO
- adds a cast-to-string specifically for FCF types s.t. they don't
trigger the deprecation warning from the "catch-all" cast
- fixes a bug in Formatted IO that caused the results of `stringify(x)`
and `"%?".format(x)` to differ for a few types
- modifies standard library, package modules, and tests to accommodate
these changes
- removes some unused regex functionality from `writef` as it was only
applicable for `readf`

testing:
- [x] paratest
- [x] gasnet paratest
- [x] inspected docs
  • Loading branch information
jeremiah-corrado authored Jul 27, 2023
2 parents 498e174 + c38e7f6 commit c4bfa69
Show file tree
Hide file tree
Showing 41 changed files with 237 additions and 180 deletions.
1 change: 1 addition & 0 deletions compiler/AST/checkAST.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ void checkPrimitives()
case PRIM_FIELD_NAME_TO_NUM:
case PRIM_FIELD_BY_NUM:
case PRIM_IS_RECORD_TYPE:
case PRIM_IS_FCF_TYPE:
case PRIM_IS_UNION_TYPE:
case PRIM_IS_EXTERN_UNION_TYPE:
case PRIM_IS_ATOMIC_TYPE:
Expand Down
1 change: 1 addition & 0 deletions compiler/AST/primitive.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1130,6 +1130,7 @@ initPrimitive() {
prim_def(PRIM_IS_NILABLE_CLASS_TYPE, "is nilable class type", returnInfoBool);
prim_def(PRIM_IS_NON_NILABLE_CLASS_TYPE, "is non nilable class type", returnInfoBool);
prim_def(PRIM_IS_RECORD_TYPE, "is record type", returnInfoBool);
prim_def(PRIM_IS_FCF_TYPE, "is fcf type", returnInfoBool);
prim_def(PRIM_IS_UNION_TYPE, "is union type", returnInfoBool);
prim_def(PRIM_IS_EXTERN_UNION_TYPE, "is extern union type", returnInfoBool);
prim_def(PRIM_IS_ATOMIC_TYPE, "is atomic type", returnInfoBool);
Expand Down
14 changes: 13 additions & 1 deletion compiler/resolution/preFold.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1551,6 +1551,19 @@ static Expr* preFoldPrimOp(CallExpr* call) {
break;
}

case PRIM_IS_FCF_TYPE: {
Type* t = call->get(1)->typeInfo();

if (t->symbol->hasFlag(FLAG_FUNCTION_CLASS))
retval = new SymExpr(gTrue);
else
retval = new SymExpr(gFalse);

call->replace(retval);

break;
}

case PRIM_IS_UNION_TYPE: {
AggregateType* classType = toAggregateType(call->get(1)->typeInfo());

Expand Down Expand Up @@ -3007,4 +3020,3 @@ static bool isNormalField(Symbol* field)

return true;
}

1 change: 1 addition & 0 deletions compiler/util/exprAnalysis.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,7 @@ bool SafeExprAnalysis::fnHasNoSideEffects(FnSymbol* fnSym) {
case PRIM_FIELD_NAME_TO_NUM:
case PRIM_FIELD_BY_NUM:
case PRIM_IS_RECORD_TYPE:
case PRIM_IS_FCF_TYPE:
case PRIM_IS_UNION_TYPE:
case PRIM_IS_ATOMIC_TYPE:
case PRIM_IS_REF_ITER_TYPE:
Expand Down
1 change: 1 addition & 0 deletions frontend/include/chpl/uast/prim-ops-list.h
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,7 @@ PRIMITIVE_R(IS_CLASS_TYPE, "is class type")
PRIMITIVE_R(IS_NILABLE_CLASS_TYPE, "is nilable class type")
PRIMITIVE_R(IS_NON_NILABLE_CLASS_TYPE, "is non nilable class type")
PRIMITIVE_R(IS_RECORD_TYPE, "is record type")
PRIMITIVE_R(IS_FCF_TYPE, "is fcf type")
PRIMITIVE_R(IS_UNION_TYPE, "is union type")
PRIMITIVE_R(IS_EXTERN_UNION_TYPE, "is extern union type")
PRIMITIVE_R(IS_ATOMIC_TYPE, "is atomic type")
Expand Down
1 change: 1 addition & 0 deletions frontend/lib/resolution/prims.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -351,6 +351,7 @@ CallResolutionResult resolvePrimCall(Context* context,
case PRIM_IS_NILABLE_CLASS_TYPE:
case PRIM_IS_NON_NILABLE_CLASS_TYPE:
case PRIM_IS_RECORD_TYPE:
case PRIM_IS_FCF_TYPE:
case PRIM_IS_UNION_TYPE:
case PRIM_IS_EXTERN_UNION_TYPE:
case PRIM_IS_ATOMIC_TYPE:
Expand Down
4 changes: 2 additions & 2 deletions modules/internal/ChapelArray.chpl
Original file line number Diff line number Diff line change
Expand Up @@ -2156,8 +2156,8 @@ module ChapelArray {
// How to cast arrays to strings
@chpldoc.nodoc
operator :(x: [], type t:string) {
use IO;
return stringify(x);
import IO.FormattedIO.string;
return try! "%?".format(x);
}

pragma "fn returns aliasing array"
Expand Down
15 changes: 8 additions & 7 deletions modules/internal/ChapelDomain.chpl
Original file line number Diff line number Diff line change
Expand Up @@ -1382,9 +1382,10 @@ module ChapelDomain {
*/
proc dim(d : int) {
use HaltWrappers;
import IO.FormattedIO.string;
if boundsChecking then
if (d < 0 || d >= rank) then
HaltWrappers.boundsCheckHalt("dim(" + d:string + ") is out-of-bounds; must be 0.." + (rank-1):string);
HaltWrappers.boundsCheckHalt(try! "dim(%i) is out-of-bounds; must be 0..%i".format(d, rank-1));
return _value.dsiDim(d);
}

Expand Down Expand Up @@ -1743,10 +1744,11 @@ module ChapelDomain {
It is an error if `idx` is not a valid index in `arr`.
*/
proc initialize(arr: [?d], idx, in value: arr.eltType) {
import IO.FormattedIO.string;

// Check to make sure value and array element types match.
if arr.eltType != value.type then
compilerError('Initialization value type \'' + value:string +
compilerError('Initialization value type \'' + value.type:string +
'\' does not match array element type \'' +
arr.eltType:string + '\'');

Expand All @@ -1770,15 +1772,14 @@ module ChapelDomain {
'the domain being resized');

if !arr.domain.contains(idx) then
halt('Array index out of bounds: ' + idx:string);
halt(try! 'Array index out of bounds: %?'.format(idx));

// Get a reference to the array slot.
ref elem = arr[idx];

if _checks {
if isElementInitialized(arr, idx) {
halt('Element at array index \'' + idx:string + '\' ' +
'is already initialized');
halt(try! "Element at array index '%?' is already initialized".format(idx));
}
}

Expand Down Expand Up @@ -2718,8 +2719,8 @@ module ChapelDomain {
if canResolveMethod(val._value, "doiToString") {
return val._value.doiToString();
} else {
use IO;
return stringify(val);
import IO.FormattedIO.string;
return try! "%?".format(val);
}
}

Expand Down
10 changes: 5 additions & 5 deletions modules/internal/DefaultRectangular.chpl
Original file line number Diff line number Diff line change
Expand Up @@ -2309,11 +2309,11 @@ module DefaultRectangular {
private proc complexTransferComm(A, B, stridelevels:int(32), dstStride, srcStride, count, AFirst, BFirst) {
if debugDefaultDistBulkTransfer {
chpl_debug_writeln("BulkTransferStride with values:\n",
"\tLocale = ", stringify(here.id), "\n",
"\tStride levels = ", stringify(stridelevels), "\n",
"\tdstStride = ", stringify(dstStride), "\n",
"\tsrcStride = ", stringify(srcStride), "\n",
"\tcount = ", stringify(count));
try! "\tLocale = %?\n".format(here.id),
try! "\tStride levels = %?\n".format(stridelevels),
try! "\tdstStride = %?\n".format(dstStride),
try! "\tsrcStride = %?\n".format(srcStride),
try! "\tcount = %?".format(count));
}

const AO = A.getDataIndex(AFirst, getShifted = false);
Expand Down
12 changes: 12 additions & 0 deletions modules/internal/StringCasts.chpl
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,18 @@ module StringCasts {
return false;
}

// homogenous tuples of primitive type
operator :(x: ?k*?t, type s:string) where isPrimitiveType(t) && isHomogeneousTupleType(x.type) {
var ret = "(";
for param i in 0..#k {
if i != 0 then ret += ", ";
ret += x[i]:string;
}
if k == 1 then ret += ",";
ret += ")";
return ret;
}

//
// int
//
Expand Down
4 changes: 2 additions & 2 deletions modules/packages/SortedMap.chpl
Original file line number Diff line number Diff line change
Expand Up @@ -343,7 +343,7 @@ module SortedMap {
var found: bool;
(found, result) = _set.lowerBound((k, nil));
if !found || comparator.compare(result[0], k) != 0 then
boundsCheckHalt("sortedMap index " + k:string + " out of bounds");
boundsCheckHalt(try! "sortedMap index %? out of bounds".format(k));
return result[1]!.val;
}
/*
Expand All @@ -356,7 +356,7 @@ module SortedMap {
var found: bool;
(found, result) = _set.lowerBound((k, nil));
if !found || comparator.compare(result[0], k) != 0 then
boundsCheckHalt("sortedMap index " + k:string + " out of bounds");
boundsCheckHalt(try! "sortedMap index %? out of bounds".format(k));

_set.remove((k, nil));

Expand Down
4 changes: 2 additions & 2 deletions modules/packages/SortedSet/Treap.chpl
Original file line number Diff line number Diff line change
Expand Up @@ -368,7 +368,7 @@ module Treap {
proc _getReference(element: eltType) ref {
var node = _findRef(_root, element);
if node == nil then
boundsCheckHalt("index " + element:string + " out of bounds");
boundsCheckHalt(try! "index %? out of bounds".format(element));
ref result = node!.element;
return result;
}
Expand All @@ -384,7 +384,7 @@ module Treap {
proc const _getValue(element: eltType) const {
var node = _find(_root, element);
if node == nil then
boundsCheckHalt("index " + element:string + " out of bounds");
boundsCheckHalt(try! "index %? out of bounds".format(element));
var result = node!.element;
return result;
}
Expand Down
Loading

0 comments on commit c4bfa69

Please sign in to comment.