sarl: Expression with side effect is not allowed in guards
Describe the bug A clear and concise description of what the bug is.
Unecessary side-effect issue on behavior guards
To Reproduce
on E_MoveRandomly [occurrence.fromMe && (MT_getEntityState(occurrence.entity).routeLength > 0)] {
Expected behavior A clear and concise description of what you expected to happen.
Should work with no error
Screenshot

System configuration: – SARL version: 0.10.0 – SARL compiler: 0.10.0
-
- Eclipse compiler without Maven
-
- Eclipse compiler with Maven
-
- Maven compiler on the command line
-
- sarlc compiler – Java JDK version (with the manufacturer name): 1.8 Oracle – Operating System:
-
- Linux 64bits
-
- Windows 64bits
-
- MacOS 64bits
Additional context Add any other context about the problem here.
About this issue
- Original URL
- State: closed
- Created 4 years ago
- Comments: 15 (15 by maintainers)
Commits related to this issue
- [tests] Add test for Issue #1009. See #1009. Signed-off-by: Stéphane Galland <galland@arakhne.org> — committed to sarl/sarl by gallandarakhneorg 4 years ago
- [lang] Add functions in IPureOperationNameValidator that takes string as argument. see #1009 Signed-off-by: Stéphane Galland <galland@arakhne.org> — committed to sarl/sarl by gallandarakhneorg 4 years ago
- [lang] Add new function templates for pure functions. The new supported functions names are: `compare`, `compareTo`, `abs`, `acos`, `asin`, `atan`, `atan2`, `cbrt`, `ceil`, `cos`, `cosh`, `exp`, `flo... — committed to sarl/sarl by gallandarakhneorg 4 years ago
- [contribs][experienceindex] Fixing of the warning implied by the addition of new pure functions' names. see #1009 Signed-off-by: Stéphane Galland <galland@arakhne.org> — committed to sarl/sarl by gallandarakhneorg 4 years ago
- [maven][docs] Add utility function for testing the purity of the function prototypes. see #1009 Signed-off-by: Stéphane Galland <galland@arakhne.org> — committed to sarl/sarl by gallandarakhneorg 4 years ago
- [docs][faq] Add section related to the warning about the purity of guards. see #1009 Signed-off-by: Sebastian Sardina <sebastian.sardina@rmit.edu.au> — committed to sarl/sarl by ssardina 4 years ago
- [docs] Update the explanaition related to the compilation errors. close #1010 see #1009 Signed-off-by: Stéphane Galland <galland@arakhne.org> — committed to sarl/sarl by gallandarakhneorg 4 years ago
I will change the tables into the documentation. Because the “code” column provides the code for
@SuppressWarnings. But, it is incomplete.Dear @ssardina, regarding your comment about the list of all the errors that could be given by SARL, I have added this page for SARL, and this page for Janus. Both of them are accessible from several points into the documentation pages.
If you found that some explanaitions may be more detailed, please let us know.
According to the commit above, I confirm that the SARL compiler is providing the error “Side effect not allowed in guards” as expected.
The cause of the error is discussed above (not-standard function naming convention), and could be manually solved by attaching explicitly the
@Pureannotation to theMT_getEntityStatefunction.I have moved this issue from “Bug” to “FAQ” and “Documentation” tags in order to upgrade the documentation sources with some information on this case.
Thank you for the code example. I think a bug may still exist that avoid SARL to properly detect the purity of the expression. We will use your inputs to set up a complete test.
Dear @ssardina.
The enforcement of no-side-effect in guards was introduced in version 0.9: guard expression must not have side effect. The problem is that your function
MT_getEntityStateis not detected as a pure (i.e. without side-effect) function.A quick fix is, but far to be perfect:
A better fix is to mark the function
MT_getEntityStateas pure explicitly:Indeed, the purity of a function is detected according to a simple algorithm within the SARL compiler, so that a function pure when one of the following conditions becomes true:
@Pure"clone","contains(?:[A-Z1-9_][a-zA-Z1-9_]*)?","equals","get(?:[A-Z1-9_][a-zA-Z1-9_]*)?","has[A-Z1-9_][a-zA-Z1-9_]*","hashCode","is[A-Z1-9_][a-zA-Z1-9_]*","iterator","length","to[A-Z1-9_][a-zA-Z1-9_]*","size".@Pure).As you could see, your naming convention for the function avoid the SARL compiler to assume that your function is pure, and your function’s code seems to call an not-pure function. In this case, explicit marking with
@Pureis mandatory.