xsharp.eu • minor compiler issue
Page 1 of 1

minor compiler issue

Posted: Fri Nov 27, 2020 2:28 pm
by SHirsch
Hello Robert,

the code to reproduce a minor compiler error.

Code: Select all

FUNCTION Start( ) AS VOID
    VAR x := ""
    x := "Test1"
    IF 1==1
        x := "Test2"  //compiler error 'Cannot use local variable 'x' before it is used
                   
        VAR x := ""  //redefinition
        x := "Test3"
    ENDIF
RETURN
The error sort order is line by line. So the error that variable x is use before declaration comes before the error of the second declaration of x.
In my current project there were some warnings between the errors (so I didn't saw the second error message). So I tried to solve the first one which is only solvable by deleting or rename the second declaration. After some time I did go on with cleaning the other errors. After deleting the redeclaration of x (which was copy/paste stuff) 'magically' the first error has gone.

Maybe the first error message can be ignored if the variable is declared in enclosing scope.

Stefan

minor compiler issue

Posted: Fri Nov 27, 2020 2:55 pm
by robert
Stefan,

Roslyn (the C# compiler) takes care of these error messages and the order in which they appear.

I know that when "resolving" variables it starts with the innermost "block" which is in this case the statement block inside the IF statement. It apparently finds the VAR x declaration inside this block and sees that you are using x before it is used, so it produces an error. If you had not defined x in this inner block then it would look for the statement list of the Start function and would then successfully resolve x.
I am not sure if we can fix this. We might break something when we start "playing" with this.

Robert

minor compiler issue

Posted: Fri Nov 27, 2020 4:06 pm
by SHirsch
Hi Robert,

understood.
Lesson learned: always have a look at all messages, clean up the obvious ones first.

Stefan