Skip to content

Use moved core.lifetime.forward for std.functional.forward #6847

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

Merged
merged 1 commit into from
Feb 3, 2019
Merged
Changes from all commits
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
113 changes: 2 additions & 111 deletions std/functional.d
Original file line number Diff line number Diff line change
Expand Up @@ -1613,28 +1613,8 @@ Returns:
*/
template forward(args...)
{
static if (args.length)
{
import std.algorithm.mutation : move;

alias arg = args[0];
// by ref || lazy || const/immutable
static if (__traits(isRef, arg) ||
__traits(isOut, arg) ||
__traits(isLazy, arg) ||
!is(typeof(move(arg))))
alias fwd = arg;
// (r)value
else
@property auto fwd(){ return move(arg); }

static if (args.length == 1)
alias forward = fwd;
else
alias forward = AliasSeq!(fwd, forward!(args[1..$]));
}
else
alias forward = AliasSeq!();
import core.lifetime : fun = forward;
alias forward = fun!args;
}

///
Expand Down Expand Up @@ -1678,49 +1658,6 @@ template forward(args...)
assert(s == "HelloHello");
}

@safe unittest
{
auto foo(TL...)(auto ref TL args)
{
string result = "";
foreach (i, _; args)
{
//pragma(msg, "[",i,"] ", __traits(isRef, args[i]) ? "L" : "R");
result ~= __traits(isRef, args[i]) ? "L" : "R";
}
return result;
}

string bar(TL...)(auto ref TL args)
{
return foo(forward!args);
}
string baz(TL...)(auto ref TL args)
{
int x;
return foo(forward!args[3], forward!args[2], 1, forward!args[1], forward!args[0], x);
}

struct S {}
S makeS(){ return S(); }
int n;
string s;
assert(bar(S(), makeS(), n, s) == "RRLL");
assert(baz(S(), makeS(), n, s) == "LLRRRL");
}

@safe unittest
{
ref int foo(return ref int a) { return a; }
ref int bar(Args)(auto ref Args args)
{
return foo(forward!args);
}
static assert(!__traits(compiles, { auto x1 = bar(3); })); // case of NG
int value = 3;
auto x2 = bar(value); // case of OK
}

///
@safe unittest
{
Expand Down Expand Up @@ -1787,49 +1724,3 @@ template forward(args...)
// const rvalue, copy
assert(z4.x_.i == 1);
}

// lazy -> lazy
@safe unittest
{
int foo1(lazy int i) { return i; }
int foo2(A)(auto ref A i) { return foo1(forward!i); }
int foo3(lazy int i) { return foo2(i); }

int numCalls = 0;
assert(foo3({ ++numCalls; return 42; }()) == 42);
assert(numCalls == 1);
}

// lazy -> non-lazy
@safe unittest
{
int foo1(int a, int b) { return a + b; }
int foo2(A...)(auto ref A args) { return foo1(forward!args); }
int foo3(int a, lazy int b) { return foo2(a, b); }

int numCalls;
assert(foo3(11, { ++numCalls; return 31; }()) == 42);
assert(numCalls == 1);
}

// non-lazy -> lazy
@safe unittest
{
int foo1(int a, lazy int b) { return a + b; }
int foo2(A...)(auto ref A args) { return foo1(forward!args); }
int foo3(int a, int b) { return foo2(a, b); }

assert(foo3(11, 31) == 42);
}

// out
@safe unittest
{
void foo1(int a, out int b) { b = a; }
void foo2(A...)(auto ref A args) { foo1(forward!args); }
void foo3(int a, out int b) { foo2(a, b); }

int b;
foo3(42, b);
assert(b == 42);
}