Sorry
int test()
{
import std.range : enumerate;
int[] a = [1];
foreach (i, e; a.enumerate) {}
return 0;
}
void main()
{
enum res = test(); // fails
int res = test(); // works fine
}
Comment #3 by k.hara.pg — 2015-09-16T02:25:43Z
Thanks.
Comment #4 by b2.temp — 2017-09-18T06:14:13Z
Remove the field names of the Tuple used in enumerate and it works i.e change the definition
"alias ElemType = Tuple!(Enumerator, "index", ElementType!Range, "value");"
to
"alias ElemType = Tuple!(Enumerator, ElementType!Range);"
(+ fix the unittests that use the field name by using indexers as postfix)
and it works, although No idea why. This is just a clue, not a fix. Removing the field names would break code.
Comment #5 by simen.kjaras — 2017-09-18T12:03:30Z
Reduced test case:
import std.meta : AliasSeq;
struct Super {
AliasSeq!(int, int) expand;
alias expand this;
}
struct Tuple {
Super inner;
alias inner this;
}
struct Range {
Tuple front;
void popFront() {}
bool empty = true;
}
int test1() {
import std.algorithm : map;
foreach (i, e; Range.init) {} // Fails
foreach (i, e; [Tuple.init].map!(a=>a)) {} // Fails
foreach (i, e; [Tuple.init]) {} // Works [line 23]
return 3;
}
enum s1 = test1();
And even more reduced, I think:
struct Super2 {
AliasSeq!int expand;
alias expand this;
}
struct Tuple2 {
Super2 inner;
alias inner this;
}
int test2() {
AliasSeq!int a = Tuple2.init; // Fails with same message
int b = Tuple2.init[0]; // Works [line 41]
return 3;
}
enum s2 = test2();
Since test2 gives the same error message, and the same kind of unpacking will happen in both cases, I assume that's the root problem.
So in summary: If an AliasSeq is used in alias this inside another alias this, some context goes missing in CTFE, possibly only during tuple assignment (see line 41). Strangely, it seems to work when the unpacking comes from an array instead of a range (line 23). Not sure why that is, but it seems tangential to the real problem here.
Comment #6 by robert.schadek — 2024-12-13T18:44:45Z