From 82ba7fe548e26fb9a7e00e0e948838f922d612f1 Mon Sep 17 00:00:00 2001 From: David Nadlinger Date: Sat, 16 Mar 2013 09:35:29 +0100 Subject: [PATCH] Fix casting typeof(null) to bool. Covered by the DMD testsuite. --- gen/llvmhelpers.cpp | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/gen/llvmhelpers.cpp b/gen/llvmhelpers.cpp index 8ad26c94..ae4905e6 100644 --- a/gen/llvmhelpers.cpp +++ b/gen/llvmhelpers.cpp @@ -739,6 +739,14 @@ DValue* DtoCastNull(Loc& loc, DValue* val, Type* to) rval = DtoAggrPair(DtoConstSize_t(0), rval, "null_array"); return new DImValue(to, rval); } + else if (totype->ty == Tbool) + { + // In theory, we could return 'false' as a constant here, but DMD + // treats non-null values casted to typeof(null) as true. + LLValue* rval = val->getRVal(); + LLValue* zero = LLConstant::getNullValue(rval->getType()); + return new DImValue(to, gIR->ir->CreateICmpNE(rval, zero, "tmp")); + } else { error(loc, "invalid cast from null to '%s'", to->toChars());