assume, assert, enforce, @safe

ponce via Digitalmars-d digitalmars-d at puremagic.com
Thu Jul 31 03:24:06 PDT 2014


On Thursday, 31 July 2014 at 09:13:53 UTC, Walter Bright wrote:
> On 7/31/2014 1:23 AM, Daniel Murphy wrote:
>> "Walter Bright"  wrote in message 
>> news:lrbpvj$mih$1 at digitalmars.com...
>>
>>> 5. assert(0); is equivalent to a halt, and the compiler won't 
>>> remove it.
>>
>> This is not the same definition the spec gives.  The spec says 
>> assert(0) can be
>> treated as unreachable, and the compiler is allowed to 
>> optimize accordingly.
>
> It says more than that:
>
> "The expression assert(0) is a special case; it signifies that 
> it is unreachable code. Either AssertError is thrown at runtime 
> if it is reachable, or the execution is halted (on the x86 
> processor, a HLT instruction can be used to halt execution). 
> The optimization and code generation phases of compilation may 
> assume that it is unreachable code."
>
>   -- http://dlang.org/expression.html#AssertExpression

You said "the compiler won't remove it".
http://dlang.org/expression.html#AssertExpression says: "The 
optimization and code generation phases of compilation may assume 
that it is unreachable code."

Who is right?

If I write:

---
switch(expr())
{
case 0: doIt();
case 1: doThat();
default:
   assert(0);
}
---

Will the optimizer be able to remove the default: case?
Because If I use assert(0) it's on purpose and do not want it to 
be elided, ever.
MSVC has __assume(0); for unreachable code, GCC has 
__builtin_unreachable()


More information about the Digitalmars-d mailing list