DIP 1016--ref T accepts r-values--Formal Assessment

Steven Schveighoffer schveiguy at gmail.com
Thu Jan 31 15:04:22 UTC 2019


On 1/31/19 2:26 AM, Andrei Alexandrescu wrote:
> The trouble is major.
> 
> Replace "if" with "while":
> 
> while (ref_fun(10)) { ... }
> ==>
> {
>    int __tmp = 10;
>    while (ref_fun(__tmp)) { ... }
> }
> 
> That means ref_fun is called with the same lvalue multiple times. In all 
> likelihood this is not what you want!

Yes, the trouble specifically is loops. Because loops execute their 
internal expressions over and over again.

Unfortunately, this means lowering isn't possible. That is, lowering to 
something expressible in the normal language isn't possible.

However, we all know that loops are essentially "lowered" in the AST to 
simple ifs and gotos. We just need to operate at that level.

So for instance the above looks something like this in AST:

loop_continue:
if(ref_fun(10))
{
   ...
}
else
    goto loop_end;
goto loop_continue;
loop_end:

(I know I'm omitting a lot of extra stuff like scope cleanup, that is 
implied here, as I don't know the exact details).

What needs to happen is the temporary (with extra scope)is inserted 
between the loop start and the if statement:

loop_continue:
{
    int __tmp = 10;
    if(ref_fun(__tmp))
    {
       ...
    }
    else
       goto loop_end;
}
goto loop_continue;
loop_end:

> A possible retort is: "Of course, while would not be lowered that way, 
> but a slightly different way!" etc. The point is, ALL OF THAT must be in 
> the DIP, not assumed obvious or clarified in informal discusson outside 
> the DIP.
> 
> Again: please be thorough, state your assumptions, cover all cases.

Agree, this needs to handle all possible cases. Really I think loops are 
the only problems, foreach, for, and while/do..while

A possible way forward is inventing a new syntax to allow declarations 
in this space, and then lowering can happen. Something similar to 
if(auto x = ...)

-Steve


More information about the Digitalmars-d-announce mailing list