D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 15064 - [CTFE] AliasSeq in multi-level alias this fails in CTFE
Summary: [CTFE] AliasSeq in multi-level alias this fails in CTFE
Status: NEW
Alias: None
Product: D
Classification: Unclassified
Component: dmd (show other issues)
Version: D2
Hardware: All All
: P3 normal
Assignee: No Owner
URL:
Keywords: CTFE
Depends on:
Blocks:
 
Reported: 2015-09-15 20:22 UTC by Jack Stouffer
Modified: 2024-12-13 18:44 UTC (History)
2 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description Jack Stouffer 2015-09-15 20:22:58 UTC

    
Comment 1 Kenji Hara 2015-09-16 00:27:43 UTC
Please add a test case, at least.
Comment 2 Jack Stouffer 2015-09-16 00:33:41 UTC
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 Kenji Hara 2015-09-16 02:25:43 UTC
Thanks.
Comment 4 basile-z 2017-09-18 06:14:13 UTC
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 Simen Kjaeraas 2017-09-18 12:03:30 UTC
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 dlangBugzillaToGithub 2024-12-13 18:44:45 UTC
THIS ISSUE HAS BEEN MOVED TO GITHUB

https://github.com/dlang/dmd/issues/19043

DO NOT COMMENT HERE ANYMORE, NOBODY WILL SEE IT, THIS ISSUE HAS BEEN MOVED TO GITHUB