6306 – Regression(2.054): [CTFE] Strange behavior of indirect recursive call in CTFE

D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 6306 - Regression(2.054): [CTFE] Strange behavior of indirect recursive call in CTFE
Summary: Regression(2.054): [CTFE] Strange behavior of indirect recursive call in CTFE
Status: RESOLVED FIXED
Alias: None
Product: D
Classification: Unclassified
Component: dmd (show other issues)
Version: D2
Hardware: x86 Windows
: P2 regression
Assignee: No Owner
URL:
Keywords: wrong-code
Depends on:
Blocks:
Reported: 2011年07月13日 02:06 UTC by Hisayuki Mima
Modified: 2011年07月31日 12:08 UTC (History)
2 users (show)

See Also:


Attachments
Add an attachment (proposed patch, testcase, etc.)

Note You need to log in before you can comment on or make changes to this issue.
Description Hisayuki Mima 2011年07月13日 02:06:02 UTC
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 Don 2011年07月27日 00:25:42 UTC
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) );


AltStyle によって変換されたページ (->オリジナル) /