openroberta-lab
openroberta-lab copied to clipboard
For loop variables can be used outside of its scope
Describe the bug
- When using a for loop the counter variable wont stop you from generating and compiling code even if it's used outside of the loop it was created in
- This causes compiler errors
To Reproduce Steps to reproduce the behavior:
- create a for loop
- use the variable of that loop outside of it
- see Warning
- generate code or compile
- see error
Expected behavior
- There is a warning that you are using the variable wrong but the warning should a blocking error that stops you from generating wrong code
Screenshots
@heini208, I'd like to request you to assign this issue to me.
Hi @heini208, please guide me on to how to recreate this issue and what packages/folders should I refer to?
@infernus01 sure let me first explain the issue again. The Problem ist not in the code-generation this works as expected however the program shown in the picture should not be able to be generated. If you visit the Open-Roberta-Lab and build the program obove on any robot system you can see that you get a warning once you put the variables block "set i to x" outside of the for loop.
The problem is warnings dont stop the code generation on the right (this icon: <>) to be openable, but in this case it totally should.
So, to put it short a warning is displayed where actually an Error should be displayed similar to this:
This is usually handled by a Visitor class in this case it should be the CommonNepoValidatorAndCollectorVisitor.java We have a method called addErrorToPhrase(Phrase, "ERROR_MSG"); which does exactly what we want.
This method should be called in visitVarDecleration() if the variable is a loop variable which is not inside its own loop.
PS: After looking into the issue a bit the warning comes from Blockly which is a library we use for all our Program blocks. You could first check if its possible to know wether or not the variable is outside of it's own loop, if that's the case we could remove the blockly warning afterwards.
related to #935
Tested successfully on test server with the following program: EV3LoopVarsOutside.xml.txt