Bug 13830 – Circular dependency between interface and implementation segfault the compiler
Status
RESOLVED
Resolution
DUPLICATE
Severity
normal
Priority
P1
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2014-12-07T19:38:00Z
Last change time
2014-12-08T01:37:25Z
Assigned to
nobody
Creator
pro.mathias.lang
Comments
Comment #0 by pro.mathias.lang — 2014-12-07T19:38:48Z
I tried to use the CRTP in D to generate an interface at compile time based on another interface and the implementation. Unfortunately, it segfaulted the compiler.
````
import std.stdio, std.traits;
template RESTServer(I, Impl) {
private abstract class IRESTServer/* : I*/ {
//final override void test() { assert(0); }
void test(Context ctx);
}
int abuseTheCompiler() {
foreach (method; __traits(allMembers, Impl)) {
foreach (overload; MemberFunctionsTuple!(Impl, method)) {
pragma(msg, __traits(identifier, overload));
}
}
return 42;
}
static assert(abuseTheCompiler());
alias RESTServer = IRESTServer;
}
struct Context { string ip; }
interface MyAPI { void test(); }
class MyAPIImpl : RESTServer!(MyAPI, MyAPIImpl) {
override:
void test(Context ctx) {
writeln(ctx.ip);
}
}
void main() {
auto c = new MyAPIImpl();
c.test(Context("10.24.50.42"));
}
````
Question: should such code be valid ? It allows an interesting pattern for code generators.
Comment #1 by public — 2014-12-08T00:03:46Z
Does changing `Impl` to alias parameter instead of type parameter change anything?
Anyway, compiler shouldn't crash. Please paste here exact ICE error line too.
Comment #2 by k.hara.pg — 2014-12-08T01:37:25Z
The segfault is a dup of issue 13668, and fortunately it's already fixed in git-head.
*** This issue has been marked as a duplicate of issue 13668 ***