Skip to content

Commit aa2b739

Browse files
committed
Work On Issue 18172 - add scope to pointers to allow @safe
1 parent d51fd2a commit aa2b739

File tree

1 file changed

+29
-5
lines changed

1 file changed

+29
-5
lines changed

std/getopt.d

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -422,7 +422,7 @@ parses foo but leaves "--bar" in $(D args). The double-dash itself is
422422
removed from the argument array unless the $(D std.getopt.config.keepEndOfOptions)
423423
directive is given.
424424
*/
425-
GetoptResult getopt(T...)(ref string[] args, T opts)
425+
GetoptResult getopt(T...)(ref string[] args, scope T opts)
426426
{
427427
import std.exception : enforce;
428428
enforce(args.length,
@@ -459,6 +459,19 @@ GetoptResult getopt(T...)(ref string[] args, T opts)
459459
}
460460
}
461461

462+
version(DIP1000)
463+
{
464+
@safe unittest
465+
{
466+
auto args = ["prog", "--foo", "-b"];
467+
468+
bool foo;
469+
bool bar;
470+
auto rslt = getopt(args, "foo|f", "Some information about foo.", &foo, "bar|b",
471+
"Some help message about bar.", &bar);
472+
}
473+
}
474+
462475
/**
463476
Configuration options for $(D getopt).
464477
@@ -681,7 +694,7 @@ private template optionValidator(A...)
681694

682695
private void getoptImpl(T...)(ref string[] args, ref configuration cfg,
683696
ref GetoptResult rslt, ref GetOptException excep,
684-
void[][string] visitedLongOpts, void[][string] visitedShortOpts, T opts)
697+
void[][string] visitedLongOpts, void[][string] visitedShortOpts, scope T opts)
685698
{
686699
enum validationMessage = optionValidator!T;
687700
static assert(validationMessage == "", validationMessage);
@@ -700,7 +713,16 @@ private void getoptImpl(T...)(ref string[] args, ref configuration cfg,
700713
else
701714
{
702715
// it's an option string
703-
auto option = to!string(opts[0]);
716+
static if (is(typeof(opts[0]) == string))
717+
{
718+
// allocate a new string so as to remove the
719+
// scope restriction
720+
string option = "" ~ opts[0];
721+
}
722+
else
723+
{
724+
string option = to!string(opts[0]);
725+
}
704726
if (option.length == 0)
705727
{
706728
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,
729751
static if (is(typeof(opts[1]) : string))
730752
{
731753
auto receiver = opts[2];
732-
optionHelp.help = opts[1];
754+
// allocate a new string so as to remove the
755+
// scope restriction
756+
optionHelp.help = "" ~ opts[1];
733757
immutable lowSliceIdx = 3;
734758
}
735759
else
@@ -802,7 +826,7 @@ private void getoptImpl(T...)(ref string[] args, ref configuration cfg,
802826
}
803827
}
804828

805-
private bool handleOption(R)(string option, R receiver, ref string[] args,
829+
private bool handleOption(R)(string option, scope R receiver, ref string[] args,
806830
ref configuration cfg, bool incremental)
807831
{
808832
import std.algorithm.iteration : map, splitter;

0 commit comments

Comments
 (0)