Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions compiler/src/dmd/expressionsem.d
Original file line number Diff line number Diff line change
Expand Up @@ -12319,6 +12319,29 @@ private extern (C++) final class ExpressionSemanticVisitor : Visitor

Type t1 = exp.e1.type;
Type t2 = exp.e2.type;

// https://issues.dlang.org/show_bug.cgi?id=23767
// `cast(void*) 0` should be treated as `null` so the ternary expression
// gets the pointer type of the other branch
if (sc.flags & SCOPE.Cfile)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a way to change the way the types are merged rather than rewriting them? Not sure if you have the scope available but you could always just have a different function for finding the common types of C.

The C behaviour (in a sense) was almost what D used to do before I fixed it a while back.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

void* + T* merges to void* in general, both in C and D, only in C there is special treatment when the void* is the NULL constant

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Previously it went to T* until I fixed it (I think ldc 1.30 still has this bug)

Could potentially use TypeNull

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am rewriting (void*)0 to use TypeNull

{
static void rewriteCNull(ref Expression e, ref Type t)
{
if (!t.isTypePointer())
return;
if (auto ie = e.optimize(WANTvalue).isIntegerExp())
{
if (ie.getInteger() == 0)
{
e = new NullExp(e.loc, Type.tnull);
t = Type.tnull;
}
}
}
rewriteCNull(exp.e1, t1);
rewriteCNull(exp.e2, t2);
}

if (t1.ty == Tnoreturn)
{
exp.type = t2;
Expand Down
11 changes: 11 additions & 0 deletions compiler/test/compilable/ctests2.c
Original file line number Diff line number Diff line change
Expand Up @@ -195,3 +195,14 @@ _Static_assert(sizeof(testTypeofB) == sizeof(short), "17");
void *c23752 = &*((void*)(0));

/*************************************************/

// https://issues.dlang.org/show_bug.cgi?id=23767
const int arr23767[4];
void f23767(void)
{
int x = *(0 ? (void*)0 : arr23767);
int y = *(1 ? arr23767 : (void*)(3-3));
int* p = (1 ? (void*)0 : (void*)0);
}

/*************************************************/