Really amazing bug (or feature) with compiling some ifTrue expressions

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Really amazing bug (or feature) with compiling some ifTrue expressions

Denis Kudriashov
Hi.

try these expressions in playground:

10 ifTrue: [ #falseResult ]. "=>DNU #ifFalse".

10 ifTrue: [ #falseResult. ]. #unexpectedResult. "=> #unexpectedResult"

10 ifTrue: [ #falseResult asString]. #unexpectedResult. "=> DNU #ifTrue:"

Same happen with ifFalse: and ifTrue:ifFalse couples.

If you look at bytecode you will not see any jumps in not failing cases.  
Probably such code is redundant because it not produce any behaviour. 
It start to work properly as soon as there is any assignment or message send around. 
But it is really strange from first look.
Also it means that special boolean objects with own ifTrue:ifFalse implementation will not receive messages in such cases.

I am not sure if it is a bug
Reply | Threaded
Open this post in threaded view
|

Re: Really amazing bug (or feature) with compiling some ifTrue expressions

Clément Béra
The problem is that in this case:

10 ifTrue: [ #falseResult. ]. #unexpectedResult. 

#falseResult => is used for effect. Opal removes it.

10 ifTrue: [ #falseResult. ] => is used for effect. Opal removes it.

The bytecode ends up being:

29 <20> pushConstant: 10
30 <87> pop
31 <78> pushConstant: #unexpectedResult

So the DNU does not happen.

Such bytecode optimisation should be postponed to runtime (Cogit, Sista) and not be performed by Opal. We did not know that when we wrote Opal. We may want to fix this.

On Fri, Mar 3, 2017 at 6:03 PM, Denis Kudriashov <[hidden email]> wrote:
Hi.

try these expressions in playground:

10 ifTrue: [ #falseResult ]. "=>DNU #ifFalse".

10 ifTrue: [ #falseResult. ]. #unexpectedResult. "=> #unexpectedResult"

10 ifTrue: [ #falseResult asString]. #unexpectedResult. "=> DNU #ifTrue:"

Same happen with ifFalse: and ifTrue:ifFalse couples.

If you look at bytecode you will not see any jumps in not failing cases.  
Probably such code is redundant because it not produce any behaviour. 
It start to work properly as soon as there is any assignment or message send around. 
But it is really strange from first look.
Also it means that special boolean objects with own ifTrue:ifFalse implementation will not receive messages in such cases.

I am not sure if it is a bug

Reply | Threaded
Open this post in threaded view
|

Re: Really amazing bug (or feature) with compiling some ifTrue expressions

Martin McClure-2
On 03/03/2017 01:28 PM, Clément Bera wrote:
> Such bytecode optimisation should be postponed to runtime (Cogit, Sista)
> and not be performed by Opal. We did not know that when we wrote Opal.
> We may want to fix this.
>

+1. Actually, plus an indeterminate quantity, but a bunch.

-Martin