rascal
rascal copied to clipboard
`fail` statement is acting as if controlled by an `if` predicate and not its governing pattern
Test t1 is failing by returning false. The first assertion is not executed, but the assignment to b is. The second assertion is satisfied. The documentation for fail states the following:
The
failstatement is associated with the innermost pattern match by which it is controlled.
The innermost pattern match is the for statement. The if statement is not a pattern match but an expression. so it should not count as a controlling statement.
I discovered this while trying to work around a different defect. The workaround for this defect is illustrated in tests t2 and t3, both of which pass. The trick is to use the IfDefinedElse expression, which provide a conditional without an if statement. In t2, variable y is initialized upon declaration, so the IfDefinedElse statement does not evaluate its alternate value. The test succeeds and the loop runs three times. In t3, on the other hand, y is not initialized, and so the alternate value does execute. The alternate value is a block statement whose final statement yields the value 1, but the fail statement executes first and correctly uses the for as its controlling statement. The workaround seems to work because IfDefinedElse is an expression but if is a statement.
module fail_01
test bool t1()
{
bool b = true ;
int n = 0 ;
for (int x <- [1,2,3] )
{
if (true)
{
fail ;
assert false : "This statement does not execute." ;
}
b = false ;
n += 1 ;
}
assert n == 3 ;
return b;
}
test bool t2()
{
bool b = false ;
int n = 0 ;
for (int x <- [1,2,3] )
{
int y = 0 ;
y ? { fail; 1 ; } ;
b = true ;
n += 1 ;
}
assert n == 3 ;
return b;
}
test bool t3()
{
bool b = true ;
int n = 0 ;
for (int x <- [1,2,3] )
{
int y ;
y ? { fail; 1 ; } ;
b = false ;
n += 1 ;
}
assert n == 0 ;
return b;
}
Version is 0.10.0.201807232201 under Eclipse Photon.