View mode: basic / threaded / horizontal-split · Log in · Help
May 23, 2009
Asserts inside nothrow function
I just started using nothrow functions a little bit and came across the following issue.
Some of the functions that I'd like to mark as nothrow have debug asserts inside them.

They used to throw an Exception, but I'd like to change their behavior and terminate an application whenever an assertion fails inside a nothrow function.

It's not very handy to write

try { assert(condition, errorMessage); } catch { abort(); }

So, how about criticalEnforce inside std.contracts? Something like this:

import std.stdio;

extern (C) void abort() nothrow;

auto tryOrDie(T)(scope lazy T dg) nothrow
{
   try {
       return dg();
   } catch (Exception e) {
       try {
           writeln("Exception occurred: ", e);
       } catch {
       }
       abort();
   }
}

auto criticalEnforce(T...)(T args) nothrow
{
   return tryOrDie(enforce(args));
}

BTW, an attempts to use it leads to an ICE:
Assertion failure: '0' on line 939 in file 'glue.c'
May 23, 2009
Re: Asserts inside nothrow function
Denis Koroskin, el 23 de mayo a las 18:12 me escribiste:
> I just started using nothrow functions a little bit and came across the following issue.
> Some of the functions that I'd like to mark as nothrow have debug asserts inside them.
> 
> They used to throw an Exception, but I'd like to change their behavior and terminate an application whenever an assertion fails inside a nothrow function.
> 
> It's not very handy to write
> 
> try { assert(condition, errorMessage); } catch { abort(); }

I don't know what happened with OutOfMemoty errors and nothrow (I guess
OutOfMemory was not considered an exception in terms of nothrow),  but
I think and assertion should have the same treatment.

-- 
Leandro Lucarella (luca) | Blog colectivo: http://www.mazziblog.com.ar/blog/
----------------------------------------------------------------------------
GPG Key: 5F5A8D05 (F8CD F9A7 BF00 5431 4145  104C 949E BFB6 5F5A 8D05)
----------------------------------------------------------------------------
Y nosotros? Y nosotros qué tenemos, eh? Yo te pregunto qué tenemos? El
kani kama es extrangero y los picles, los picles ya no son nuestros...
Tenemos el 'ulce de leche, que es argentino y machazo, como un pampeano
en su rancho. Tenemos el clásico, el colonial y el repostero y en
Santiago del Estero, se lo comen a pan lactal.
May 23, 2009
Re: Asserts inside nothrow function
Denis Koroskin wrote:
> I just started using nothrow functions a little bit and came across the 
> following issue.
> Some of the functions that I'd like to mark as nothrow have debug 
> asserts inside them.
> 
> They used to throw an Exception, but I'd like to change their behavior 
> and terminate an application whenever an assertion fails inside a 
> nothrow function.

At the moment, any exception that passes beyond user control will result 
in that thread terminating rather than the entire app.  This goes for 
the main thread also--if you have other threads running then the app 
won't exit until those complete as well.  I suppose I could add an 
"unhandled exception handler" that lets the user specify what should 
happen, though the only options are really either letting only the 
thread terminate or calling exit() to terminate the entire app.  The 
only advantage of a handler over setting a boolean for behavior is that 
you could do custom error reporting and the like as well.  There's no 
harm in providing such a feature, so I think I'll look into it for 2.031.
May 23, 2009
Re: Asserts inside nothrow function
Denis Koroskin wrote:
> BTW, an attempts to use it leads to an ICE:
> Assertion failure: '0' on line 939 in file 'glue.c'

Please add these with a reproducible test case to bugzilla.
May 23, 2009
Re: Asserts inside nothrow function
Walter Bright wrote:
> Denis Koroskin wrote:
>> BTW, an attempts to use it leads to an ICE:
>> Assertion failure: '0' on line 939 in file 'glue.c'
> 
> Please add these with a reproducible test case to bugzilla.
It's probably a duplicate of 854=2863=2251?, which there's already a 
patch for. It's the most duplicated bug in the history of D bugzilla.

Here's the minimal case for 854.

template Foo(T...) {
    alias T Foo;
}
void main() {
    auto y = (Foo!(int) x){ return 0; };
}
May 23, 2009
Re: Asserts inside nothrow function
== Quote from Walter Bright (newshound1@digitalmars.com)'s article
> Denis Koroskin wrote:
> > BTW, an attempts to use it leads to an ICE:
> > Assertion failure: '0' on line 939 in file 'glue.c'
> Please add these with a reproducible test case to bugzilla.

This is just another case of the infamous bug 2251.

http://d.puremagic.com/issues/show_bug.cgi?id=2251
May 25, 2009
Re: Asserts inside nothrow function
Denis Koroskin wrote:
> I just started using nothrow functions a little bit and came across the
> following issue.
> Some of the functions that I'd like to mark as nothrow have debug
> asserts inside them.
> 
> They used to throw an Exception, but I'd like to change their behavior
> and terminate an application whenever an assertion fails inside a
> nothrow function.
> 
> It's not very handy to write
> 
> try { assert(condition, errorMessage); } catch { abort(); }
> 

Um .. am I missing something here?

void check(bool condition, lazy string msg) { if (!condition) { printf(/* print msg here */); abort; } }
Top | Discussion index | About this forum | D home