Bug 6306 – Regression(2.054): [CTFE] Strange behavior of indirect recursive call in CTFE
Status
RESOLVED
Resolution
FIXED
Severity
regression
Priority
P2
Component
dmd
Product
D
Version
D2
Platform
x86
OS
Windows
Creation time
2011-07-13T02:06:00Z
Last change time
2011-07-31T12:08:56Z
Keywords
wrong-code
Assigned to
nobody
Creator
youxkei
Comments
Comment #0 by youxkei — 2011-07-13T02:06:02Z
void main(){}
struct Result{bool match; string value; string rest;}
Result paren(string input){
if(input[0] == '('){
input = input[1..$];
}else{
return Result(false, "", input);
}
string[] strs;
while(true){
Result r = function(string input){
Result r = paren(input);
if(r.match){
return r;
}
if(input[0] == ')'){
return Result(false, "", input);
}else{
return Result(true, input[0..1], input[1..$]);
}
}(input);
if(r.match){
strs = strs ~ r.value;
input = r.rest;
}else{
break;
}
}
if(input[0] == ')'){
input = input[1..$];
}else{
return Result(false, "", input);
}
string value = "(";
foreach(str; strs){
value = value ~ str;
}
value = value ~ ")";
return Result(true, value, input);
}
bool test(){//unittest
auto r = paren("((a))");
assert(r.match);
assert(r.rest == "");
assert(r.value == "((a))"); //assertion1
return true;
}
unittest{
static assert(test()); //line1
test();
}
This code cannot be compiled by dmd v2.054 because the assertion1, which is the compile-time assertion, fails.
(Incidentally, r.value is "(a(a))" at assertion1.)
However, dmd v2.052 can compile this code well.
When I commented out the line 1, the code became able to be compiled dmd v2.054 and the run-time unittest succeeded.
The function paren is a function which parses string in balanced-parentheses and returns parsed string.
It has indirect recursive call or it calls a function which calls it.
When I rewrote the code as a code which has direct recursive call, the code the code became able to be compiled dmd v2.054 and the run-time unittest succeeded, too.
This is why I think indirect recursive call has strange behavior and it causes the assertion1 to fail.
Comment #1 by clugdbug — 2011-07-27T00:25:42Z
Reduced test case shows this is very nasty bug in failing to restore local variable values after an indirect recursive call.
-------------
void recurse6306() {
bug6306(false);
}
bool bug6306(bool b) {
int x = 0;
if (b)
recurse6306();
assert(x == 0); // fails!!!!!
x = 1;
return true;
}
static assert( bug6306(true) );