pure functions cannot be removed, actually: pure vs. total

Steven Schveighoffer schveiguy at yahoo.com
Thu Jun 7 23:14:52 UTC 2018


On 6/7/18 6:57 PM, Stefan Koch wrote:
> On Thursday, 7 June 2018 at 22:23:09 UTC, Steven Schveighoffer wrote:
>> {...}
> 
> That a function could return does not mean it will.
> 
> int fn (int arg) /*strongly*/ pure
> {
>    if (arg == 42)
>      return 42;
>    else (arg < 43)
>      return fn(--arg);
>    else
>      return fn(++arg);
> }
> 
> do you see the problem?
> If not you would you expect the compiler to ?
> 
> note: I would expected that the clang static analyzer
> would be able to determine that depending on the value of arg
> this function might never return.

I'm not talking about the compiler looking at the code to figure out if 
it will return.

I'm talking about this:

struct S
{
}

S fn() pure;

No matter what is inside fn, it will return S.init, if it returns. The 
question to answer is, can we assume all functions don't enter infinite 
loops, and if we can assume this, then why couldn't the compiler simply 
replace a call to fn with S.init?

And the case FeepingCreature gives is pretty compelling, because it's a 
real example, not a toy example.

-Steve


More information about the Digitalmars-d mailing list