try/catch idiom in std.datetime

Jonathan M Davis jmdavisProg at gmx.com
Mon Nov 18 08:53:07 PST 2013


On Monday, November 18, 2013 13:23:23 Jacob Carlborg wrote:
> On 2013-11-18 12:03, Jonathan M Davis wrote:
> > assert(0) is intended specifically for use in cases where a line is
> > supposed to be unreachable, and it wouldn't make any sense to use it in
> > any other case, because assertion failures are intended to kill the
> > program, and assert(0) always fails. It happens that it throws an
> > AssertError in non-release mode in order to give you better debug
> > information on failure, and it's a HLT instruction in release, but in
> > either case, it's intended to be unreachable code.
> > 
> > The spec really should be updated to make it clear that when assertions
> > are
> > compiled in, assert(0) throws an AssertError and that when assertions are
> > supposed to be compiled out, it becomes a HLT instruction. And if need be,
> > we can update the spec to require that try-catches be compiled out when
> > assertions are compiled out, and the catch's body only contains an
> > assert(0).
> Does all architectures support the HLT instruction or equivalent? The
> spec explicitly says HLT is used on x86.

I don't know. My knowledge of stuff at that level on x86 is already poor. I 
have pretty much zero knowledge about that sort of thing on other 
architectures. The extent of my knowledge in that area tends to be restricted 
to big endian vs little endian. The only reason that I even know that HLT 
exists is thanks to this feature in D.

If it's not possible to require HLT or an equivalent, then it wouldn't make 
sense to put that in the spec, which would be unfortunate, but ideally that 
would be required. However, even if it can't be, that doesn't change the fact 
that assert(0) is intended to indicate unreachable code and doesn't make sense 
for anything else (as I recall, the compiler itself inserts it for that 
purpose at the end of functions in some circumstances to catch missing return 
statements). So, I see no problem with the compiler making optimizations in 
release based on the assumption that assert(0) should never be reached.

- Jonathan M Davis


More information about the Digitalmars-d mailing list