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

Make toDelegate safe for function pointers #10599

Merged
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
61 changes: 53 additions & 8 deletions std/functional.d
Original file line number Diff line number Diff line change
Expand Up @@ -1814,6 +1814,13 @@ if (isCallable!(F))
{
return fp;
}
else static if (is(F Func == Func*) && is(Func == function))
{
return function(ref F fp) @trusted
{
return buildDelegate(fp);
0xEAB marked this conversation as resolved.
Show resolved Hide resolved
}(fp);
}
else static if (is(typeof(&F.opCall) == delegate)
|| (is(typeof(&F.opCall) V : V*) && is(V == function)))
{
Expand All @@ -1825,6 +1832,27 @@ if (isCallable!(F))
}
else
{
static assert(false, "Unsupported type of callable, please open an issue.");
}
}

///
@safe unittest
{
static int inc(ref uint num) {
num++;
return 8675309;
}

uint myNum = 0;
auto incMyNumDel = toDelegate(&inc);
auto returnVal = incMyNumDel(myNum);
assert(myNum == 1);
}

private template buildDelegate(F)
{
auto buildDelegate(auto ref F fp) {
alias DelType = typeof(&(new DelegateFaker!(F)).doIt);

static struct DelegateFields {
Expand Down Expand Up @@ -1854,21 +1882,22 @@ if (isCallable!(F))
}
}

///
@system unittest
@safe unittest
{
static int inc(ref uint num) {
num++;
return 8675309;
}

uint myNum = 0;
auto incMyNumDel = toDelegate(&inc);
auto returnVal = incMyNumDel(myNum);
assert(myNum == 1);
uint myNum = 0x1337;
struct S1 { int opCall() { inc(myNum); return myNum; } }
static assert(!is(typeof(&s1.opCall) == delegate));
S1 s1;
Comment on lines +1894 to +1895
Copy link
Contributor

Choose a reason for hiding this comment

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

What’s that doing?
In static assert(!is(typeof(&s1.opCall) == delegate));, the local variable s1 isn’t in scope.
If you swap the lines, the static assert passes.

Copy link
Member Author

Choose a reason for hiding this comment

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

good catch

Copy link
Member Author

Choose a reason for hiding this comment

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

Apparently, it’s a copy&paste of this code:

phobos/std/functional.d

Lines 1891 to 1901 in 8c6fca9

struct S1 { int opCall() { inc(myNum); return myNum; } }
static assert(!is(typeof(&s1.opCall) == delegate));
S1 s1;
auto getvals1 = toDelegate(s1);
assert(getvals1() == 4);
struct S2 { static int opCall() { return 123456; } }
static assert(!is(typeof(&S2.opCall) == delegate));
S2 s2;
auto getvals2 =&S2.opCall;
assert(getvals2() == 123456);

https://github.com/dlang/phobos/blame/8c6fca9ccdb30b69b627808c698526a76f7c4248/std/functional.d#L1891-L1901

auto getvals1 = toDelegate(s1);
assert(getvals1() == 0x1338);
}

@system unittest // not @safe due to toDelegate
@system unittest
{
static int inc(ref uint num) {
num++;
Expand Down Expand Up @@ -1954,7 +1983,7 @@ if (isCallable!(F))
}


@system unittest
@safe unittest
{
static struct S1 { static void opCall()() { } }
static struct S2 { static T opCall(T = int)(T x) {return x; } }
Expand All @@ -1967,6 +1996,22 @@ if (isCallable!(F))
assert(toDelegate(i2)(0xBED) == 0xBED);
}

@safe unittest
{
static void fun() @system pure nothrow @nogc
{
return;
}

auto fn = &fun;
static assert( is(typeof(fn) == void function() @system pure nothrow @nogc));
static assert(!is(typeof(fn) == void function() @safe pure nothrow @nogc));

auto dg = fn.toDelegate();
static assert( is(typeof(dg) == void delegate() @system pure nothrow @nogc));
static assert(!is(typeof(dg) == void delegate() @safe pure nothrow @nogc));
}

/**
* Passes the fields of a struct as arguments to a function.
*
Expand Down
Loading