diff --git a/compiler/src/dmd/expressionsem.d b/compiler/src/dmd/expressionsem.d index d191e9b5af20..06124e181eaf 100644 --- a/compiler/src/dmd/expressionsem.d +++ b/compiler/src/dmd/expressionsem.d @@ -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) + { + 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; diff --git a/compiler/test/compilable/ctests2.c b/compiler/test/compilable/ctests2.c index d01fa7df746a..16da88ba378b 100644 --- a/compiler/test/compilable/ctests2.c +++ b/compiler/test/compilable/ctests2.c @@ -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); +} + +/*************************************************/