Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: add deprecation warnings when casting string/bytes to regex #20944

Merged
merged 3 commits into from
Nov 29, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion modules/internal/ChapelBase.chpl
Original file line number Diff line number Diff line change
Expand Up @@ -1444,7 +1444,10 @@ module ChapelBase {
// string to a type. Otherwise, we can't resolve chpl_debug_writeln in
// `range.these`
{ var dummyRange = 1..0; for i in dummyRange {} }
return str:t;
if t:string == "regex(string)" || t:string == "regex(bytes)" then
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

how about if t == regex(string) || t == regex(bytes) ?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I attempted to make this change and ran into issues. I added some comments to document the problems encountered and noted that we would prefer to use the type comparison rather than string comparison here.

return compile(str);
else
return str:t;
}
}
// param s is used for error reporting
Expand Down
2 changes: 2 additions & 0 deletions modules/standard/Regex.chpl
Original file line number Diff line number Diff line change
Expand Up @@ -1142,12 +1142,14 @@ inline operator :(x: regex(bytes), type t: bytes) {

// Cast string to regex
pragma "no doc"
deprecated "Casting strings to regex is deprecated. Use regex.compile(string) instead."
inline operator :(x: string, type t: regex(string)) throws {
return compile(x);
}

// Cast bytes to regex
pragma "no doc"
deprecated "Casting bytes to regex is deprecated. Use regex.compile(bytes) instead."
inline operator :(x: bytes, type t: regex(bytes)) throws {
return compile(x);
}
Expand Down
2 changes: 2 additions & 0 deletions test/deprecated/regexTertiary.good
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
regexTertiary.chpl:8: warning: Casting strings to regex is deprecated. Use regex.compile(string) instead.
regexTertiary.chpl:9: warning: Casting bytes to regex is deprecated. Use regex.compile(bytes) instead.
regexTertiary.chpl:11: warning: string.search is deprecated, use regex search instead
regexTertiary.chpl:12: warning: bytes.search is deprecated, use regex search instead
regexTertiary.chpl:14: warning: string.search is deprecated, use regex search instead
Expand Down
10 changes: 10 additions & 0 deletions test/regex/bytes/cast.chpl
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,13 @@ writeln(b"contains regular expression".find(rb));

writeln("doesn't contain regular expression".find(rs));
writeln(b"doesn't contain regular expression".find(rb));


var rcs = compile(s);
var rcb = compile(b);

writeln("contains regular expression".find(rcs));
writeln(b"contains regular expression".find(rcb));

writeln("doesn't contain regular expression".find(rcs));
writeln(b"doesn't contain regular expression".find(rcb));
6 changes: 6 additions & 0 deletions test/regex/bytes/cast.good
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
cast.chpl:8: warning: Casting strings to regex is deprecated. Use regex.compile(string) instead.
cast.chpl:9: warning: Casting bytes to regex is deprecated. Use regex.compile(bytes) instead.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I might remove lines 8-15 from this test since the deprecated cast is still tested in /deprecated/regexTertiary

0
0
-1
-1
0
0
-1
Expand Down