-
-
Notifications
You must be signed in to change notification settings - Fork 740
Work On Issue 18172 - add scope to pointers in getopt to allow @safe #6281
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
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -422,7 +422,7 @@ parses foo but leaves "--bar" in $(D args). The double-dash itself is | |
removed from the argument array unless the $(D std.getopt.config.keepEndOfOptions) | ||
directive is given. | ||
*/ | ||
GetoptResult getopt(T...)(ref string[] args, T opts) | ||
GetoptResult getopt(T...)(ref string[] args, scope T opts) | ||
{ | ||
import std.exception : enforce; | ||
enforce(args.length, | ||
|
@@ -459,6 +459,19 @@ GetoptResult getopt(T...)(ref string[] args, T opts) | |
} | ||
} | ||
|
||
version(DIP1000) | ||
{ | ||
@safe unittest | ||
{ | ||
auto args = ["prog", "--foo", "-b"]; | ||
|
||
bool foo; | ||
bool bar; | ||
auto rslt = getopt(args, "foo|f", "Some information about foo.", &foo, "bar|b", | ||
"Some help message about bar.", &bar); | ||
} | ||
} | ||
|
||
/** | ||
Configuration options for $(D getopt). | ||
|
||
|
@@ -681,7 +694,7 @@ private template optionValidator(A...) | |
|
||
private void getoptImpl(T...)(ref string[] args, ref configuration cfg, | ||
ref GetoptResult rslt, ref GetOptException excep, | ||
void[][string] visitedLongOpts, void[][string] visitedShortOpts, T opts) | ||
void[][string] visitedLongOpts, void[][string] visitedShortOpts, scope T opts) | ||
{ | ||
enum validationMessage = optionValidator!T; | ||
static assert(validationMessage == "", validationMessage); | ||
|
@@ -700,7 +713,16 @@ private void getoptImpl(T...)(ref string[] args, ref configuration cfg, | |
else | ||
{ | ||
// it's an option string | ||
auto option = to!string(opts[0]); | ||
static if (is(typeof(opts[0]) == string)) | ||
{ | ||
// allocate a new string so as to remove the | ||
// scope restriction | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That really sucks. Safety shouldn't result in a performance overhead :/
|
||
string option = opts[0].idup; | ||
} | ||
else | ||
{ | ||
string option = to!string(opts[0]); | ||
} | ||
if (option.length == 0) | ||
{ | ||
excep = new GetOptException("An option name may not be an empty string", excep); | ||
|
@@ -729,7 +751,9 @@ private void getoptImpl(T...)(ref string[] args, ref configuration cfg, | |
static if (is(typeof(opts[1]) : string)) | ||
{ | ||
auto receiver = opts[2]; | ||
optionHelp.help = opts[1]; | ||
// allocate a new string so as to remove the | ||
// scope restriction | ||
optionHelp.help = opts[1].idup; | ||
immutable lowSliceIdx = 3; | ||
} | ||
else | ||
|
@@ -802,7 +826,7 @@ private void getoptImpl(T...)(ref string[] args, ref configuration cfg, | |
} | ||
} | ||
|
||
private bool handleOption(R)(string option, R receiver, ref string[] args, | ||
private bool handleOption(R)(string option, scope R receiver, ref string[] args, | ||
ref configuration cfg, bool incremental) | ||
{ | ||
import std.algorithm.iteration : map, splitter; | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure how we want to handle DIP1000 regressions, but we need a way to ensure things stay
@safe
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
By #6278 and thus gradually enabling -dip1000?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
But this
unittest
needs to be annotated with@safe
when-dip1000
is passed and@system
when it's not to prevent any DIP1000 regressions. #6278 Doesn't have a mechanism for that.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Argh fair enough - there still might be issues as long as
-deps
compiles all unittests of its dependencies :/Anyhow, for the time being #6278 (or similar could set
DIP1000
by hand).