2 days ago Re: CTFE limitation on reinterpreting cast | ||||
|---|---|---|---|---|
| ||||
![]() Posted in reply to Rambutan | On 9/28/2026 3:50 PM, Rambutan wrote:
> struct B {}
> struct C {}
>
> __gshared B Bobj;
>
> auto ptr = cast(C*)&Bobj;
CTFE does not do anything that is not memory safe. Consider:
```d
struct B { int b; }
struct C { long c; }
__gshared B Bobj;
auto ptr = cast(C*)&Bobj;
```
This fails, because the structs are laid out differently, and `ptr.c` will produce memory unsafe behavior.
You could make the point that if the layouts of B and C are identical, this would be memory safe and should work.
Feel free to make the case for it!
| |||
1 day ago Re: CTFE limitation on reinterpreting cast | ||||
|---|---|---|---|---|
| ||||
Rambutan ![]() Posted in reply to Walter Bright | On Wednesday, 30 September 2026 at 00:17:25 UTC, Walter Bright wrote:
> On 9/28/2026 3:50 PM, Rambutan wrote:
>> struct B {}
>> struct C {}
>>
>> __gshared B Bobj;
>>
>> auto ptr = cast(C*)&Bobj;
>
> CTFE does not do anything that is not memory safe. Consider:
>
> ```d
> struct B { int b; }
> struct C { long c; }
>
> __gshared B Bobj;
>
> auto ptr = cast(C*)&Bobj;
> ```
Thank you for confirming what I speculated earlier in this thread when I said this is dangerous and so may be the motive for banning reinterpreting casts in CTFE.
However I also pointed out that there is no problem with such a cast if the new pointer is never indirected in any way at compile time. So a better rule would be to ban any kind of indirection using the result of a reinterpreting cast, and NOT to ban the cast itself.
That way placing cast pointers to globals in global struct initializers would be legal, as it is in C.
| |||
1 day ago Re: CTFE limitation on reinterpreting cast | ||||
|---|---|---|---|---|
| ||||
Nick Treleaven ![]() Posted in reply to Rambutan | On Monday, 28 September 2026 at 23:35:24 UTC, Rambutan wrote: > I have had no trouble doing this with function pointers earlier. Here's a meaningless small example.
This compiles just fine. I think its a compiler bug. The cast above should be a compile error, because the pointer types are not covariant. Now filed: https://github.com/dlang/dmd/issues/23950. The compiler was ignoring the parameter types and linkage, but it did check the return type. The following errors with stable dmd:
| |||
19 hours ago Re: CTFE limitation on reinterpreting cast | ||||
|---|---|---|---|---|
| ||||
Walter Bright ![]() Posted in reply to Rambutan | On 9/29/2026 8:38 PM, Rambutan wrote:
> Thank you for confirming what I speculated earlier in this thread when I said this is dangerous and so may be the motive for banning reinterpreting casts in CTFE.
>
> However I also pointed out that there is no problem with such a cast if the new pointer is never indirected in any way at compile time. So a better rule would be to ban any kind of indirection using the result of a reinterpreting cast, and NOT to ban the cast itself.
>
> That way placing cast pointers to globals in global struct initializers would be legal, as it is in C.
I understand your point, and it has merit. But I am apprehensive in allowing CTFE to make bad pointers, even if they are not dereferenced. Perhaps consider a union instead?
| |||
18 hours ago Re: CTFE limitation on reinterpreting cast | ||||
|---|---|---|---|---|
| ||||
Richard (Rikki) Andrew Cattermole ![]() Posted in reply to Walter Bright | On 01/10/2026 7:44 PM, Walter Bright wrote:
> On 9/29/2026 8:38 PM, Rambutan wrote:
>> Thank you for confirming what I speculated earlier in this thread when I said this is dangerous and so may be the motive for banning reinterpreting casts in CTFE.
>>
>> However I also pointed out that there is no problem with such a cast if the new pointer is never indirected in any way at compile time. So a better rule would be to ban any kind of indirection using the result of a reinterpreting cast, and NOT to ban the cast itself.
>>
>> That way placing cast pointers to globals in global struct initializers would be legal, as it is in C.
>
> I understand your point, and it has merit. But I am apprehensive in allowing CTFE to make bad pointers, even if they are not dereferenced. Perhaps consider a union instead?
Ya know, C has rules for how structs can be merged.
Perhaps that is a good place to look at.
| |||
30 minutes ago Re: CTFE limitation on reinterpreting cast | ||||
|---|---|---|---|---|
| ||||
Walter Bright ![]() Posted in reply to Richard (Rikki) Andrew Cattermole | On 10/1/2026 12:15 AM, Richard (Rikki) Andrew Cattermole wrote:
> Ya know, C has rules for how structs can be merged.
>
> Perhaps that is a good place to look at.
Perhaps. But we'd also need a case for why this capability is needed.
| |||
18 minutes ago Re: CTFE limitation on reinterpreting cast | ||||
|---|---|---|---|---|
| ||||
Richard (Rikki) Andrew Cattermole ![]() Posted in reply to Walter Bright | On 02/10/2026 2:22 PM, Walter Bright wrote:
> On 10/1/2026 12:15 AM, Richard (Rikki) Andrew Cattermole wrote:
>> Ya know, C has rules for how structs can be merged.
>>
>> Perhaps that is a good place to look at.
>
> Perhaps. But we'd also need a case for why this capability is needed.
Can and should are two separate questions, we have not shown can, let alone should.
One problem at a time!
| |||
Permalink
Reply


