errno is not nothrow

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


On Friday, May 11, 2018 12:01:05 H. S. Teoh via Digitalmars-d wrote:
> On Fri, May 11, 2018 at 12:42:01PM -0600, Jonathan M Davis via
> Digitalmars-d wrote: [...]
>
> > 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.
>
> This sounds scary.  So my (strongly!) pure function that returns
> floating-point can return different results when passed the same
> parameters, if somebody in between changes floating-point flags?  That
> doesn't sound good at all.
>
>   float evil() pure /* ! */ {
>       /* change FP flags, like rounding mode or something */
>       return 0.0;
>   }
>
>   float myfunc(float f) pure { return ...; }
>
>   float result = myfunc(1.23) + evil() + myfunc(1.23);
>
> Since myfunc() is marked pure, the compiler could cache the result of
> the first call and reuse it for the second. But since evil() changes FP
> flags in between, the result will be different from what would have been
> computed had the compiler not cached the return value of the first call.
>
> :-(

Yeah, well. Don't mess with the FP flags. The documentation on pure is quite
clear that it ignores the fact that floating point flags are essentially
global state. So, if you muck with them, it's on you. But unless you're
dealing with strongly pure funtions, function elision isn't a thing, and not
many functions can be strongly pure. So, in practice, I wouldn't expect it
to be an issue, and if you're writing an "evil" function, well you can 
already
be evil by casting away pure.

- Jonathan M Davis



More information about the Digitalmars-d mailing list