Page MenuHomeFeedback Tracker

Enscript single statement body evaluation bug
Assigned, UrgentPublic

Description

The following code is misinterpreted, because of a single statement body for function/if/while. This results in the rest of the line not parsed correctly or even a compile error

bool SingleStatementFunction() return false; void DoNothing(){} //Compile error: unknown class "void"

void ReproduceSingleStatementBug()
{
    if(false) return; int myFirstVariable = 42;

    while(false) return; string mySecondVariable = "Hello World!";

    Print(myFirstVariable); //SCRIPT       : int myFirstVariable = 0
    Print(mySecondVariable); //SCRIPT       : string mySecondVariable = ''
    DoNothing();
}

If the identical code is not put into a single line it works as intended.

bool SingleStatementFunction() return false;
void DoNothing(){}

void ReproduceSingleStatementBug()
{
    if(false) return;
    int myFirstVariable = 42;

    while(false) return;
    string mySecondVariable = "Hello World!";

    Print(myFirstVariable); //SCRIPT       : int myFirstVariable = 42
    Print(mySecondVariable); //SCRIPT       : string mySecondVariable = 'Hello World!'
    DoNothing();
}

Another way the original single line code can be transformed to get it working is by wrapping the single statements with a block e.g:

bool SingleStatementFunction(){ return false;} void DoNothing(){}

void ReproduceSingleStatementBug()
{
    if(false){ return;} int myFirstVariable = 42;

    while(false){ return;} string mySecondVariable = "Hello World!";

    Print(myFirstVariable); //SCRIPT       : int myFirstVariable = 42
    Print(mySecondVariable); //SCRIPT       : string mySecondVariable = 'Hello World!'
    DoNothing();
}

Conclusion
While one could argue that single statement functions (as in functions that do not have {}-brackets) are not intentionally supported, the misbehavior also happens with 100% valid code as shown in the ReproduceSingleStatementBug function.
Having a single statement body and another expression on the same line, separated by a colon is valid in other languages such as C++ and C#. (See additional information section for proof)

I have a feeling that the compiler does not really care much about what comes after a single body statement. Maybe there is some rule like "get the expression and then skip to next scope/line".
The issue is not only caused by the return keyword, as the following code shows using the break keyword, where it also fails:

void BreakAlsoBreaksIt()
{
    while(false) break; string myThirdVariable = "Hello World!";

    Print(myThirdVariable); //SCRIPT       : string myThirdVariable = ''
}

Details

Severity
Minor
Resolution
Open
Reproducibility
Always
Operating System
Windows 10 x64
Category
General
Additional Information

Please forward to the enfusion team as this needs compiler debugging