FS#5366 - local variable create oustide a switch scope will be lost on exit if foreach is use in the switch

Attached to Project: OpenTTD
Opened by pagnon stephane (krinn) - Thursday, 15 November 2012, 02:31 GMT
Last edited by Remko Bijker (Rubidium) - Thursday, 14 November 2013, 19:45 GMT
Type Bug
Category Squirrel
Status Closed
Assigned To No-one
Operating System All
Severity Very Low
Priority Normal
Reported Version other
Due in Version Undecided
Due Date Undecided
Percent Complete 100%
Votes 0
Private No


I have just hit this too, but glx found a thread where morloth has been affect by it too
Looks like last local variable is lost if inside a switch that have a foreach in it

local anything = 0;
local something = null;
// local z = 0; // morloth fix
switch (anything)
case 0:
foreach (item, value in something) { } // comment out this line and it will works too
print(something.Count()); <--- this will trigger error "something does not exist"

affect squirrel 2.2.5, i have post it as a reminder for other devs to find the fix (i have seek in flyspray but morloth didn't report it here it seems, glad glx put his fingers on the thread, i was getting mad at it.
I put severity to very low as i just don't think it could be fix easy, the workaround is at least easy.
This task depends upon

Closed by  Remko Bijker (Rubidium)
Thursday, 14 November 2013, 19:45 GMT
Reason for closing:  Unreproducible
Additional comments about closing:  When a reliable way to reproduce this has been created, it may be reopened.
Comment by Thijs Marinussen (Yexo) - Friday, 16 November 2012, 21:44 GMT
I've added the code in the description to the Start() function of an otherwise empty test AI, but it didn't crash. Can you provide a complete example that triggers the error?
Comment by pagnon stephane (krinn) - Saturday, 17 November 2012, 18:01 GMT
This is the sample i've use when speaking about it with glx :
But the sample from morloth in the forum thread is another one that fail.

I'm sorry my test case fail, seems my assumption on how it happen is false then.
Comment by Remko Bijker (Rubidium) - Wednesday, 06 February 2013, 19:43 GMT
  • Field changed: Status (New → Unreproducable)
Without a way to easily reproduce this, it will be very hard to fix it. I tried several incarnations, but most "examples" were missing so much related code that I had to remove that it did not reproduce the issue. So please provide a simple AI that will reliably show this error (without building networks and such).
Comment by pagnon stephane (krinn) - Friday, 15 November 2013, 11:41 GMT
No i cannot build an AI where i'm able to reproduce it, from my AI where the bug is still in it i could, so here's my AI modified to enable and trigger the bug per default.
Start the AI, hit fastforward i have add a AIController.Break to help you.

Here's the clues i have to trigger it (but i lack to build an test AI that reproduce the conditions, so something more is need) :
- Have a variable create before a switch case
- Have this variable value (or type?) modified within the switch case
- The case that alter the variable MUST have a foreach loop (!)

Try accessing it then outside the switch will trigger the error the variable doesn't exists.

And the easy way to get mad : uncomment line 220 (the one "local nothing = 0;" and see the bug vanish by magic...