D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 7428 - regression (DMD 2.058head) ICE on slightly convoluted setup including closures
Summary: regression (DMD 2.058head) ICE on slightly convoluted setup including closures
Status: RESOLVED FIXED
Alias: None
Product: D
Classification: Unclassified
Component: dmd (show other issues)
Version: D2
Hardware: All All
: P2 regression
Assignee: No Owner
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2012-02-02 15:45 UTC by timon.gehr
Modified: 2012-02-04 23:44 UTC (History)
1 user (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description timon.gehr 2012-02-02 15:45:57 UTC
The following code compiles and runs fine with DMD 2.057, but fails on DMD 2.058head with

Internal error: toir.c 178

// import std.stdio, std.algorithm, std.range;
auto Y(R,T...)(R delegate(T) delegate (R delegate(T)) f){
    struct F{R delegate(F,T) f;};
    return (R delegate(T)delegate(F) a){return a(F((F b,T i){return f(a(b))(i);}));
    }((F b){return (T n){return b.f(b,n);};});
}


void main(){
    auto factorial=Y((long delegate(long) self){return (long i){return i?self(i-1)*i:1;};});
    auto fibonacci=Y((int delegate(int) self){return (int i){return i<2?i:self(i-1)+self(i-2);};});
    auto ackermann=Y((long delegate(long,long) self){return (long n,long m){return n?m?self(n-1,self(n,m-1)):self(n-1,1):m+1;};});
    // writeln(map!factorial(iota(0,20)));
    // writeln(map!fibonacci(iota(0,20)));
    // writeln(map!((a){return ackermann(a%4,a/4);})(iota(0,20)));
}
Comment 1 Walter Bright 2012-02-04 18:41:52 UTC
I simplified this a bit (still hurts my brain, though). The bug is tripped when a lambda is used rather than the equivalent nested function:

alias long delegate(long) dg_t;

void Y(dg_t delegate (dg_t) y)
{
    struct F { long delegate(F) f; };

  version (all)
  { // generates error
    (dg_t delegate(F) a){return a(F((F b){return y(a(b))(1);})); }
    ((F b){return (long n){return b.f(b);};});
  }
  else
  {
    auto abc(dg_t delegate(F) a)
    {
        return a(F((F b){return y(a(b))(1);}));
    }

    abc((F b){return (long n){return b.f(b);};});
  }
}


void main(){
    auto foo(dg_t self)
    {
        auto bar(long i) { return self(1); }
        return &bar;
    }

    Y(&foo);
}
Comment 2 github-bugzilla 2012-02-04 23:44:16 UTC
Commit pushed to master at https://github.com/D-Programming-Language/dmd

https://github.com/D-Programming-Language/dmd/commit/8b8abab85a916153211c902aa41b23786cda6062
fix Issue 7428 - regression (DMD 2.058head) ICE on slightly convoluted setup including closures