Skip to content

Commit

Permalink
Merge pull request #3043 from 9rnsr/fix14213
Browse files Browse the repository at this point in the history
[REG2.067a] Issue 14213 - Strange deprecated message in std.typecons.Proxy with using method
  • Loading branch information
WalterBright committed Mar 9, 2015
2 parents 74d2c8a + 6a8345b commit 50c65e5
Showing 1 changed file with 46 additions and 9 deletions.
55 changes: 46 additions & 9 deletions std/typecons.d
Expand Up @@ -4323,6 +4323,10 @@ Make proxy for $(D a).
*/
mixin template Proxy(alias a)
{
private alias ValueType = typeof({ return a; }());
private enum bool accessibleFrom(T) =
is(typeof((ref T self){ cast(void)mixin("self." ~ a.stringof); }));

static if (is(typeof(this) == class))
{
override bool opEquals(Object o)
Expand All @@ -4337,7 +4341,7 @@ mixin template Proxy(alias a)
}

bool opEquals(T)(T b)
if (is(typeof(a):T) || is(typeof(a.opEquals(b))) || is(typeof(b.opEquals(a))))
if (is(ValueType : T) || is(typeof(a.opEquals(b))) || is(typeof(b.opEquals(a))))
{
static if (is(typeof(a.opEquals(b))))
return a.opEquals(b);
Expand All @@ -4356,14 +4360,14 @@ mixin template Proxy(alias a)
return a < mixin("b."~a.stringof[5..$]) ? -1
: a > mixin("b."~a.stringof[5..$]) ? +1 : 0;
}
static if (is(typeof(a) == class))
static if (is(ValueType == class))
return a.opCmp(o);
else
throw new Exception("Attempt to compare a "~typeid(this).toString~" and a "~typeid(o).toString);
}

int opCmp(T)(auto ref const T b)
if (is(typeof(a):T) || is(typeof(a.opCmp(b))) || is(typeof(b.opCmp(a))))
if (is(ValueType : T) || is(typeof(a.opCmp(b))) || is(typeof(b.opCmp(a))))
{
static if (is(typeof(a.opCmp(b))))
return a.opCmp(b);
Expand All @@ -4373,9 +4377,16 @@ mixin template Proxy(alias a)
return a < b ? -1 : a > b ? +1 : 0;
}

override hash_t toHash() const nothrow @trusted
static if (accessibleFrom!(const typeof(this)))
{
return typeid(typeof(a)).getHash(cast(const void*)&a);
override hash_t toHash() const nothrow @trusted
{
static if (is(typeof(&a) == ValueType*))
alias v = a;
else
auto v = a; // if a is (property) function
return typeid(ValueType).getHash(cast(const void*)&v);
}
}
}
else
Expand Down Expand Up @@ -4403,9 +4414,16 @@ mixin template Proxy(alias a)
return a < b ? -1 : a > b ? +1 : 0;
}

hash_t toHash() const nothrow @trusted
static if (accessibleFrom!(const typeof(this)))
{
return typeid(typeof(a)).getHash(cast(const void*)&a);
hash_t toHash() const nothrow @trusted
{
static if (is(typeof(&a) == ValueType*))
alias v = a;
else
auto v = a; // if a is (property) function
return typeid(ValueType).getHash(cast(const void*)&v);
}
}
}

Expand All @@ -4432,7 +4450,7 @@ mixin template Proxy(alias a)
static if (!is(typeof(this) == class))
{
private import std.traits;
static if (isAssignable!(typeof(a)))
static if (isAssignable!ValueType)
{
auto ref opAssign(this X)(auto ref typeof(this) v)
{
Expand Down Expand Up @@ -4487,7 +4505,7 @@ mixin template Proxy(alias a)

import std.traits : isArray;

static if (isArray!(typeof(a)))
static if (isArray!ValueType)
{
auto opDollar() const { return a.length; }
}
Expand Down Expand Up @@ -4858,6 +4876,25 @@ unittest
bool* b = Name("a") in names;
}

unittest
{
// bug14213, using function for the payload
static struct S
{
int foo() { return 12; }
mixin Proxy!foo;
}
static class C
{
int foo() { return 12; }
mixin Proxy!foo;
}
S s;
assert(s + 1 == 13);
C c = new C();
assert(s * 2 == 24);
}

/**
$(B Typedef) allows the creation of a unique type which is
based on an existing type. Unlike the $(D alias) feature,
Expand Down

0 comments on commit 50c65e5

Please sign in to comment.