D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 3510 - Cannot forward reference a templated type from within a template mixin
Summary: Cannot forward reference a templated type from within a template mixin
Status: RESOLVED FIXED
Alias: None
Product: D
Classification: Unclassified
Component: dmd (show other issues)
Version: D1 (retired)
Hardware: All All
: P2 blocker
Assignee: No Owner
URL:
Keywords: rejects-valid
Depends on:
Blocks: 340
  Show dependency treegraph
 
Reported: 2009-11-14 12:30 UTC by Matti Niemenmaa
Modified: 2014-04-18 09:12 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 Matti Niemenmaa 2009-11-14 12:30:29 UTC
Simple case:


struct S(int x) {}
template Mix() { Sa s; }

class C {
	mixin Mix!();
	alias S!(0) Sa;
}

foo.d(2): Error: forward reference to 'S!(0)'
foo.d(2): Error: Sa is used as a type
foo.d(2): Error: variable foo.C.Mix!().s voids have no value
foo.d(5): Error: mixin foo.C.Mix!() error instantiating


Move the mixin of Mix!() below the alias of S!() and it compiles.

For the following code, that move alone isn't enough:


template Tuple(T...) { alias T Tuple; }

struct S(size_t x) {}
template Mix() { Sa s; }

class C {
	mixin Mix!();
	alias Tuple!("foo") T;
	alias S!(T.length) Sa;
}

[same errors as above]


Moving the mixin below the alias of S!() gives:

foo.d(8): Error: identifier 'length' of 'T.length' is not defined
foo.d(8): Error: template instance S!(int) does not match template declaration S(uint x)
foo.d(4): Error: template instance S!(int) is used as a type
foo.d(4): Error: variable foo.C.Mix!().s voids have no value
foo.d(9): Error: mixin foo.C.Mix!() error instantiating

After moving the mixin, the code can still be made to compile by introducing a constant to hold the length. The following class C works:

alias Tuple!("foo") T;
const size_t Tlength = T.length;
alias S!(Tlength) Sa;
mixin Mix!();

Unfortunately, in my case T depends on Mix in ways the compiler can't figure out without having Mix mixed in before T (Bug 3509), so this is a blocker for me.
Comment 1 github-bugzilla 2012-03-12 20:02:19 UTC
Commit pushed to master at https://github.com/D-Programming-Language/dmd

https://github.com/D-Programming-Language/dmd/commit/2c3e9a8b007d62493c52ca601173ff2cd9cd9c0a
fix Issue 3510 - Cannot forward reference a templated type from within a template mixin
Comment 2 github-bugzilla 2012-03-12 20:02:40 UTC
Commit pushed to dmd-1.x at https://github.com/D-Programming-Language/dmd

https://github.com/D-Programming-Language/dmd/commit/1ac0b1021b77190f512399a2bde9bfac7e05f940
fix Issue 3510 - Cannot forward reference a templated type from within a template mixin