errno is not nothrow

Jonathan M Davis newsgroup.d at jmdavisprog.com
Fri May 11 19:58:04 UTC 2018


On Friday, May 11, 2018 12:42:01 Jonathan M Davis via Digitalmars-d wrote:
> On Friday, May 11, 2018 10:05:12 Shachar Shemesh via Digitalmars-d wrote:
> > At least under Linux, you cannot get or set the value of errno from a
> > nothrow function.
> >
> > Is this on purpose, or is this a bug?
>
> In general, the C bindings in druntime haven't ended up with much in the
> way of attributes on them - in part, because many of the predate a lot of
> the attributes, and in part, because it can take some time and effort to
> figure out what the correct attributes are, so they frequently don't get
> put on like they should be.
>
> In the case of errno, it's coming from core.stdc.errno, and it looks like
> on pretty much every system, it's declared as an alias for an extern(C)
> function - e.g.
>
> ref int __error();
>
> and none of them are marked with any attributes. In this particular case,
> I would think that they could be marked with @trusted, nothrow, and
> @nogc. I don't know about pure. Probably not, but I could see it being a
> treated a bit like floating point flags where pure functions are allowed
> to muck with them even though they're arguably global state. So, I don't
> know.
>
> In any case, it's an easy fix, so I created a quick PR to fix it:
>
> https://github.com/dlang/druntime/pull/2180

Okay. I didn't read the code carefully enough. core.stdc.errno does that
annoying thing where attributes are applied with : at the top, making them
really easy to miss. So, errno should already be nothrow, @nogc, and
@trusted. I don't know why it wouldn't be working for you on Linux, and I
don't have a Linux system to test with at the moment.

void main() nothrow @safe @nogc
{
    import core.stdc.errno;
    errno = 1;
}

works just fine on FreeBSD.

- Jonathan M Davis



More information about the Digitalmars-d mailing list