Bug 13204 – recursive alias declaration

Status
RESOLVED
Resolution
FIXED
Severity
normal
Priority
P2
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2014-07-25T20:01:00Z
Last change time
2017-07-03T17:07:16Z
Keywords
diagnostic, pull, rejects-valid, spec
Assigned to
nobody
Creator
hoganmeier

Comments

Comment #0 by hoganmeier — 2014-07-25T20:01:47Z
struct ABase(uint v) { alias whatever = Foo; } alias A1 = ABase!1; // L6 alias Foo = A1; $ dmd -c test.d test.d(6): Error: alias ddraw.A1 recursive alias declaration test.d(6): Error: template instance ddraw.ABase!1u error instantiating Not sure if it's supposed to work. At least it compiles when not using a template. And the error message could be more helpful. This one was hard to figure out.
Comment #1 by issues.dlang — 2014-07-25T21:24:12Z
Okay. Think this through. When you declare alias A1 = ABase!1; // L6 it now has to instantiate ABase with 1. When it does that it has to define alias whatever = Foo; Foo is then an alias to A1, which is an alias to ABase!1. So, to determine what Foo is, it has to instantiate ABase!1, which it's already in the middle of doing. It's clearly recursive and isn't going to end. You always have to be _very_ careful when attempting to instantiate a template within itself. You need template constraints or static ifs to fix the problem. So, in this case, you'd probably need to do something like this (untested). struct ABase(uint v) { static if(v == 1) alias whatever = ABase; else alias whatever = Foo; } And as to an improved error message, I don't know what else would help you. It told you the line that where the problem was, and the definition of Foo clearly is attempting to instantiate the template that the error was in. If you can suggest an error message that would be more helpful, that would be great, but as far as I can tell, the compiler is telling you exactly what's wrong.
Comment #2 by hoganmeier — 2014-07-25T21:55:30Z
Of course the recursion is obvious in this reduced form! (thanks to DustMite) But in the real code with more aliases etc. it's not and you only get the start of the circle. So it would be nice if at least one more node was given if possible, i.e. the whatever alias.
Comment #3 by issues.dlang — 2014-07-25T22:19:46Z
Well, as I said if you can come up with what a better error message would look like, please suggest it and open it as an enhancement. I'm not at all against the idea. I just don't what would actually be better, and as someone who had to deal with the problem and found the current message to be lacking, you'll have a better idea of what would have helped you than I would.
Comment #4 by k.hara.pg — 2014-07-28T14:02:25Z
I think this is fixable issue. (In reply to Jonathan M Davis from comment #1) > Okay. Think this through. When you declare > > alias A1 = ABase!1; // L6 In this first timing, ABase!1 is not yet instantiated, so its eponymous member is yet unknown. But, > it now has to instantiate ABase with 1. When it does that it has to define > > alias whatever = Foo; > > Foo is then an alias to A1, which is an alias to ABase!1. So, to determine > what Foo is, it has to instantiate ABase!1, which it's already in the middle > of doing. In this second timing, compiler can recognize an aliasing from ABase!1 to the instantiated struct type ABase!1.ABase, because the eponymous template member can be lexically known, even though the semantic analysis of ABase!1 is not yet completed. So compiler can make the alias A1 to the struct ABase!1.ABase.
Comment #5 by k.hara.pg — 2014-07-28T15:20:27Z
Comment #6 by hoganmeier — 2014-07-28T18:39:55Z
Thanks Kenji!
Comment #7 by github-bugzilla — 2014-08-15T01:39:36Z
Commits pushed to master at https://github.com/D-Programming-Language/dmd https://github.com/D-Programming-Language/dmd/commit/44fe178acefdf45f56d42bbad582c42f412c78e1 fix Issue 13204 - recursive alias declaration https://github.com/D-Programming-Language/dmd/commit/83e0a9919cc07f6de2b76111878ef1905fbcaf3f Merge pull request #3822 from 9rnsr/fix13204 Issue 13204 - recursive alias declaration
Comment #8 by github-bugzilla — 2014-08-28T04:10:14Z
Commit pushed to 2.066 at https://github.com/D-Programming-Language/dmd https://github.com/D-Programming-Language/dmd/commit/5fed9e61acaca03822dd871372ebf6cfe19df989 Merge pull request #3822 from 9rnsr/fix13204 Issue 13204 - recursive alias declaration
Comment #9 by k.hara.pg — 2014-09-14T13:12:20Z
*** Issue 8462 has been marked as a duplicate of this issue. ***
Comment #10 by dlang-bugzilla — 2017-07-03T17:07:16Z
*** Issue 10705 has been marked as a duplicate of this issue. ***