View mode: basic / threaded / horizontal-split · Log in · Help
February 07, 2012
Should C functions automatically be nothrow?
Can C functions throw? I don't know of any way that a C function could throw. 
Is it possible if you have a C function which calls a D function or something 
like that? I don't know. I wouldn't really expect the C function to be able to 
handle the D exception, in which case, it wouldn't end up throwing the 
exception to whatever code calls it.

Assuming that C functions can't throw, is there any reason _not_ to have the 
compiler automatically treat C functions as nothrow?

If we can't treat C funtions in general as nothrow (for whatever reason that 
may be), is there a reason why we can't explicitly mark the various C bindings 
in druntime as nothrow at the very least?

- Jonathan M Davis
February 07, 2012
Re: Should C functions automatically be nothrow?
On 2/6/2012 6:04 PM, Jonathan M Davis wrote:
> Can C functions throw?

Yes. There's no reason you cannot write:

extern (C) void foo() { throw new Exception(); }
February 07, 2012
Re: Should C functions automatically be nothrow?
Le 07/02/2012 03:04, Jonathan M Davis a écrit :
> Can C functions throw? I don't know of any way that a C function could throw.
> Is it possible if you have a C function which calls a D function or something
> like that? I don't know. I wouldn't really expect the C function to be able to
> handle the D exception, in which case, it wouldn't end up throwing the
> exception to whatever code calls it.
>
> Assuming that C functions can't throw, is there any reason _not_ to have the
> compiler automatically treat C functions as nothrow?
>
> If we can't treat C funtions in general as nothrow (for whatever reason that
> may be), is there a reason why we can't explicitly mark the various C bindings
> in druntime as nothrow at the very least?
>
> - Jonathan M Davis

extern(C) goes in both directions. D can call C, but C can call D. And D 
can throw exceptions. So you can end up with extern(C) function that 
throw exceptions.
February 07, 2012
Re: Should C functions automatically be nothrow?
On 07/02/2012 02:04, Jonathan M Davis wrote:
> Can C functions throw? I don't know of any way that a C function could throw.
<snip>

On top of what the others have said, functions written in C can certainly throw such 
things as AVs.

Stewart.
February 07, 2012
Re: Should C functions automatically be nothrow?
On 02/07/2012 02:40 PM, Stewart Gordon wrote:
> On 07/02/2012 02:04, Jonathan M Davis wrote:
>> Can C functions throw? I don't know of any way that a C function could
>> throw.
> <snip>
>
> On top of what the others have said, functions written in C can
> certainly throw such things as AVs.
>
> Stewart.

AVs are not thrown. (if they were, they could be caught.)
February 07, 2012
Re: Should C functions automatically be nothrow?
On 2/7/2012 5:40 AM, Stewart Gordon wrote:
> On 07/02/2012 02:04, Jonathan M Davis wrote:
>> Can C functions throw? I don't know of any way that a C function could throw.
> <snip>
>
> On top of what the others have said, functions written in C can certainly throw
> such things as AVs.

Although seg faults are converted into D exceptions on Windows by druntime, and 
so can be caught, I suspect this was not a good idea. It's completely 
non-portable to other systems.

Nor would C code do such a conversion anyway.
February 07, 2012
Re: Should C functions automatically be nothrow?
On Tue, Feb 07, 2012 at 11:53:45AM -0800, Walter Bright wrote:
> On 2/7/2012 5:40 AM, Stewart Gordon wrote:
> >On 07/02/2012 02:04, Jonathan M Davis wrote:
> >>Can C functions throw? I don't know of any way that a C function could throw.
> ><snip>
> >
> >On top of what the others have said, functions written in C can certainly throw
> >such things as AVs.
> 
> Although seg faults are converted into D exceptions on Windows by
> druntime, and so can be caught, I suspect this was not a good idea.
> It's completely non-portable to other systems.
[...]

I was going to suggest using siglongjmp() and doing stack-unwinding in
the SEGV signal handler on Posix systems, but apparently this is very
very evil because subsequent code will still be running in the signal
handler's context and any call to signal-unsafe functions will cause
undefined behaviour.

So yeah. Bad idea. :)


T

-- 
Time flies like an arrow. Fruit flies like a banana.
February 08, 2012
Re: Should C functions automatically be nothrow?
On Tue, 07 Feb 2012 20:53:45 +0100, Walter Bright  
<newshound2@digitalmars.com> wrote:

> On 2/7/2012 5:40 AM, Stewart Gordon wrote:
>> On 07/02/2012 02:04, Jonathan M Davis wrote:
>>> Can C functions throw? I don't know of any way that a C function could  
>>> throw.
>> <snip>
>>
>> On top of what the others have said, functions written in C can  
>> certainly throw
>> such things as AVs.
>
> Although seg faults are converted into D exceptions on Windows by  
> druntime, and so can be caught, I suspect this was not a good idea. It's  
> completely non-portable to other systems.
>
> Nor would C code do such a conversion anyway.
>

I'm not a big fan of translating async exceptions which causes enough bad  
usage in C++ land.
We're translating them to Errors though.

Visual C++ allows to switch between different EH models  
http://msdn.microsoft.com/en-us/library/1deeycx5(v=vs.100).aspx.
February 08, 2012
Re: Should C functions automatically be nothrow?
Am Tue, 7 Feb 2012 12:31:06 -0800
schrieb "H. S. Teoh" <hsteoh@quickfur.ath.cx>:

> On Tue, Feb 07, 2012 at 11:53:45AM -0800, Walter Bright wrote:
> > On 2/7/2012 5:40 AM, Stewart Gordon wrote:
> > >On 07/02/2012 02:04, Jonathan M Davis wrote:
> > >>Can C functions throw? I don't know of any way that a C function
> > >>could throw.
> > ><snip>
> > >
> > >On top of what the others have said, functions written in C can
> > >certainly throw such things as AVs.
> > 
> > Although seg faults are converted into D exceptions on Windows by
> > druntime, and so can be caught, I suspect this was not a good idea.
> > It's completely non-portable to other systems.
> [...]
> 
> I was going to suggest using siglongjmp() and doing stack-unwinding in
> the SEGV signal handler on Posix systems, but apparently this is very
> very evil because subsequent code will still be running in the signal
> handler's context and any call to signal-unsafe functions will cause
> undefined behaviour.
> 
> So yeah. Bad idea. :)
> 
> 
> T
> 

Does libsigsegv have the same restrictions or how is it implemented?
Top | Discussion index | About this forum | D home