www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - cast oddities - void* <-> AA

reply "Iain Buclaw" <ibuclaw ubuntu.com> writes:
Discovered this through some other issue when finishing up the 
latest frontend merge.


In the following example, I feel that this should not compile at 
all.

void main()
{
   int[int] a;
   void* b = cast(void*)a;
}


Given that the definition of an AA is (as defined in rt.aaA):

struct AA
{
     void* a;
}


Similarly for this code:

void main()
{
   int[int] a;
   void* b;
   a = cast(int[int]) b;   // fails
   a = cast(int[int]) &b;  // works
}

Correct me if I'm wrong, but both examples should warrant bug 
reports.


NB: These examples don't compile on GDC because of the safe 
guards from directly converting between scalar and pointer types.


Regards
Iain
Aug 17 2012
next sibling parent "Iain Buclaw" <ibuclaw ubuntu.com> writes:
On Friday, 17 August 2012 at 22:15:10 UTC, Iain Buclaw wrote:

 NB: These examples don't compile on GDC because of the safe 
 guards from directly converting between scalar and pointer 
 types.
s/scalar/non-scalar/
Aug 17 2012
prev sibling next sibling parent reply "bearophile" <bearophileHUGS lycos.com> writes:
Iain Buclaw:

 Correct me if I'm wrong, but both examples should warrant bug 
 reports.
Accepting the cast of an AA to void* seems bad. Bye, bearophile
Aug 17 2012
parent "Richard Webb" <webby beardmouse.org.uk> writes:
fwiw, I raised a bug about this on Orange a while back: 
https://github.com/jacob-carlborg/orange/issues/17.

I thought it a bit odd that it compiled in DMD at the time, but i 
don't think i opened a bug about it.
Aug 18 2012
prev sibling parent "David Nadlinger" <see klickverbot.at> writes:
On Friday, 17 August 2012 at 22:15:10 UTC, Iain Buclaw wrote:
 In the following example, I feel that this should not compile 
 at all.

 void main()
 {
   int[int] a;
   void* b = cast(void*)a;
 }


 Given that the definition of an AA is (as defined in rt.aaA):

 struct AA
 {
     void* a;
 }
I also once encountered some cases where the DMD frontend would allow conversions between structs and scalar types or other unrelated structs which happened to have the same member layout – which broke LDC, as the LLVM IR is typed. David
Aug 18 2012