HomeForumSourceResearchGuide
Sign in to reply to forum posts.
Wrong bool eval

I was trying to figure out why my code wasn't producing the expected output, and I came across this strange behavior:component provides App requires io.Output out {

int App:main(AppParam params[])
    {
    bool a = true
    bool b = true
    bool c = false
    if (a && (b || c)){
      out.println("should print, a and b are true")
    }
    else{
      out.println("for some reason this is false")
    }
    return 0
    }
}
For some reason, (true && (true || false)) evaluates to false.

Am I missing something, or could this be a bug in the language/runtime?

Hi Matheus,

This seems to be a bug in the compiler's machine code optimiser for x86-64 platforms; your example works in the expected way on other build targets.

Our initial analysis suggests that this incorrect outcome only occurs when all parts of the boolean expression are primitive local variables (i.e., it doesn't occur when any elements of the expression are function calls or global variables or arrays, etc.). It's also sensitive to the ordering of the sub-expressions, so that (b || c) && a works as expected.

As a temporary reliable workaround for the specific case when everything is a primitive local variable, you would need to perform the two elements of the operation separately, like:

bool a = true
bool b = true
bool c = false

bool t = b || c

if (a && t)

This is going to take some careful investigation to pin down the exact cause; we'll post another update here when there's a fix available. Thanks for pointing it out, and apologies for the inconvenience!

Barry

Hi Matheus,

This issue is now fixed. You can get it via a source update or by downloading the latest distribution release package which includes all recent updates.

Thanks again for sharing!

Barry