minor compiler issue

This forum is meant for questions and discussions about the X# language and tools
Post Reply
User avatar
SHirsch
Posts: 286
Joined: Tue Jan 30, 2018 8:23 am
Location: Germany

minor compiler issue

Post 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
User avatar
robert
Posts: 4478
Joined: Fri Aug 21, 2015 10:57 am
Location: Netherlands

minor compiler issue

Post 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
XSharp Development Team
The Netherlands
robert@xsharp.eu
User avatar
SHirsch
Posts: 286
Joined: Tue Jan 30, 2018 8:23 am
Location: Germany

minor compiler issue

Post by SHirsch »

Hi Robert,

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

Stefan
Post Reply