Skip to content

Commit

Permalink
Fix unittests of toDelegate
Browse files Browse the repository at this point in the history
As discovered by @Bolpat:
<dlang#10599 (comment)>
  • Loading branch information
0xEAB committed Jan 24, 2025
1 parent 488ea69 commit 02b1ca0
Showing 1 changed file with 14 additions and 6 deletions.
20 changes: 14 additions & 6 deletions std/functional.d
Original file line number Diff line number Diff line change
Expand Up @@ -1886,16 +1886,21 @@ private template buildDelegate(F)

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

uint myNum = 0x1337;
struct S1 { int opCall() { inc(myNum); return myNum; } }
static assert(!is(typeof(&s1.opCall) == delegate));
struct S1
{
static int myNum = 0x1337;
static int opCall() { inc(myNum); return myNum; }
}

S1 s1;
auto getvals1 = toDelegate(s1);
static assert(!is(typeof(&s1.opCall) == delegate));
static assert( is(typeof(toDelegate(s1)) == delegate));
assert(getvals1() == 0x1338);
}

Expand Down Expand Up @@ -1924,15 +1929,18 @@ private template buildDelegate(F)
assert(getvali() == 3);

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

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

/* test for attributes */
Expand Down

0 comments on commit 02b1ca0

Please sign in to comment.