View mode: basic / threaded / horizontal-split · Log in · Help
March 13, 2013
Casting delegates to void*
Hi all,

A quick quiz: Does the following function compile, and if yes,
what will it return?

---
void* delegateToPtr(void delegate() dg) {
    return cast(void*)dg;
}
---

I am working on weeding out the last test suite failures in the
2.062 branch of LDC right now (release to be expected soon(tm)),
and was surprised to find that current DMD indeed accepts the
snippet. The result of the cast is the context pointer of the
delegate.

LDC, on the other hand, refuses to compile the cast. It would be
trivial to change its behavior to match DMD, but I am not sure
why that behavior would be desirable in the first place. It seems
rather like an accepts-invalid bug to me.

An instance of such a cast has been added to the test suite
while fixing issue 7159: 
https://github.com/D-Programming-Language/dmd/blob/18da33b4d303f1dd020576a6a671f91fd6b06c10/test/runnable/xtest46.d#L5011

David
March 13, 2013
Re: Casting delegates to void*
On 13 March 2013 19:18, David Nadlinger <see@klickverbot.at> wrote:

> Hi all,
>
> A quick quiz: Does the following function compile, and if yes,
> what will it return?
>
> ---
> void* delegateToPtr(void delegate() dg) {
>     return cast(void*)dg;
> }
> ---
>
>
GDC's is also:

{
 return <retval> = dg.object;
}

I think it would be better to be explicit in the case though.

eg:
---
// Returns context.
void* delegateToPtr(void delegate() dg) {
   return cast(void*)dg.ptr;
}

// Returns function pointer.
void* delegateToPtr(void delegate() dg) {
   return cast(void*)dg.funcptr;
}
---

So you aren't left guessing which one it could be.


Regards
-- 
Iain Buclaw

*(p < e ? p++ : p) = (c & 0x0f) + '0';
March 14, 2013
Re: Casting delegates to void*
"David Nadlinger" <see@klickverbot.at> wrote in message 
news:kibnaskimiqnmzziegjo@forum.dlang.org...
> Hi all,
>
> A quick quiz: Does the following function compile, and if yes,
> what will it return?
>
> ---
> void* delegateToPtr(void delegate() dg) {
>     return cast(void*)dg;
> }
> ---
>

IIRC dg.ptr will be lowered to this, and that is how the glue layer knows 
you want to extract the pointer.  My guess is this predates the .ptr 
property, and much like the array -> ulong cast, should be squashed.
March 15, 2013
Re: Casting delegates to void*
On 3/13/2013 7:32 PM, Daniel Murphy wrote:
> "David Nadlinger" <see@klickverbot.at> wrote in message
> news:kibnaskimiqnmzziegjo@forum.dlang.org...
>> Hi all,
>>
>> A quick quiz: Does the following function compile, and if yes,
>> what will it return?
>>
>> ---
>> void* delegateToPtr(void delegate() dg) {
>>      return cast(void*)dg;
>> }
>> ---
>>
>
> IIRC dg.ptr will be lowered to this, and that is how the glue layer knows
> you want to extract the pointer.  My guess is this predates the .ptr
> property, and much like the array -> ulong cast, should be squashed.
>
>

I agree. Please file an enhancement request for this in bugzilla.
March 16, 2013
Re: Casting delegates to void*
On Friday, 15 March 2013 at 21:01:26 UTC, Walter Bright wrote:
> On 3/13/2013 7:32 PM, Daniel Murphy wrote:
>> "David Nadlinger" <see@klickverbot.at> wrote in message
>> news:kibnaskimiqnmzziegjo@forum.dlang.org...
>>> Hi all,
>>>
>>> A quick quiz: Does the following function compile, and if yes,
>>> what will it return?
>>>
>>> ---
>>> void* delegateToPtr(void delegate() dg) {
>>>     return cast(void*)dg;
>>> }
>>> ---
>>>
>>
>> IIRC dg.ptr will be lowered to this, and that is how the glue 
>> layer knows
>> you want to extract the pointer.  My guess is this predates 
>> the .ptr
>> property, and much like the array -> ulong cast, should be 
>> squashed.
>>
>>
>
> I agree. Please file an enhancement request for this in 
> bugzilla.

http://d.puremagic.com/issues/show_bug.cgi?id=9735

David
Top | Discussion index | About this forum | D home