Bug 1308 – Recursive alias declaration, Error: forward reference to foo
Status
RESOLVED
Resolution
INVALID
Severity
normal
Priority
P2
Component
dmd
Product
D
Version
D1 (retired)
Platform
Other
OS
Linux
Creation time
2007-07-02T14:14:49Z
Last change time
2019-08-15T09:54:44Z
Keywords
rejects-valid
Assigned to
No Owner
Creator
Jari-Matti Mäkelä
Comments
Comment #0 by onlystupidspamhere — 2007-07-02T14:14:49Z
Code:
Version 1:
template r(alias S, T...) {
static if (T.length)
alias r!(r, T[1..$]) r;
else
alias int r;
}
alias r!(r, Object) foo;
Version 2:
template f() {}
template r(alias S, T...) {
static if (T.length)
alias r!(r, T[1..$]) r;
else
alias f r;
}
alias r!(f, Object) foo;
Compiler output:
aliasbug.d(3): alias aliasbug.r!(r,Object).r recursive alias declaration
aliasbug.d(3): Error: forward reference to 'r!(r,T[1 .. __dollar])'
aliasbug.d(3): template instance r!(int) does not match any template declaration
aliasbug.d(8): template instance aliasbug.r!(r,Object) error instantiating
----
I'm not sure if the first one should work (it's a minimal case that triggers the error msg), but the latter one should. It's derived from a compile time map template.
Comment #1 by onlystupidspamhere — 2007-09-27T19:12:41Z
The original was a bit messy so here's a bit shorter one:
template r(alias S, T...) {
static if (T.length)
alias r!(r) r;
else
alias float r;
}
alias r!(r, r) foo;
---
I expect it to work this way:
1. "alias r!(r, r) foo" calls r!(r, r)
2. static if is true so "template r!(r, r)" calls r!(r)
3. static if is now false so "template r!(r)" returns float.
4. r!(r, r) returns r!(r) = float.
5. foo becomes an alias of r!(r, r) = r!(r) = float.
---
The "template instance r!(int) does not match any template declaration" doesn't make any sense. There's no instantion of r!(int) happening anywhere.
Comment #2 by lovesyao — 2007-09-27T20:00:53Z
Reply to [email protected],
> http://d.puremagic.com/issues/show_bug.cgi?id=1308
>
> ------- Comment #1 from [email protected] 2007-09-27 19:12
> ------- The original was a bit messy so here's a bit shorter one:
>
> template r(alias S, T...) {
> static if (T.length)
> alias r!(r) r;
> else
> alias float r;
> }
> alias r!(r, r) foo;
>
> ---
>
> I expect it to work this way:
>
> 1. "alias r!(r, r) foo" calls r!(r, r)
> 2. static if is true so "template r!(r, r)" calls r!(r)
> 3. static if is now false so "template r!(r)" returns float.
> 4. r!(r, r) returns r!(r) = float.
> 5. foo becomes an alias of r!(r, r) = r!(r) = float.
> ---
>
> The "template instance r!(int) does not match any template
> declaration" doesn't make any sense. There's no instantion of r!(int)
> happening anywhere.
>
int is the default that DMD uses when it don't known what to make of somthing.
If it were up to me I'd have an unknown_type to server for this.
Comment #3 by razvan.nitu1305 — 2019-08-15T09:54:44Z
Currently the error message is:
test.d(3): Error: template instance r!r recursive template expansion
test.d(8): Error: template instance test.r!(r, r) error instantiating
This is the correct behavior because when `alias r!r r` is encountered, the compiler says "r is an alias to something, let's see to what" and then it tries to analyze `r!r`. When it takes the first `r` symbol it tries to see what it is and it sees that r is an alias to the type that it is trying now to evaluate.
Closing as invalid.