June 16, 2022
https://issues.dlang.org/show_bug.cgi?id=23192

          Issue ID: 23192
           Summary: Can't iterate aggregate fields with static foreach
                    inside a member function
           Product: D
           Version: D2
          Hardware: All
                OS: All
            Status: NEW
          Severity: normal
          Priority: P1
         Component: dmd
          Assignee: nobody@puremagic.com
          Reporter: snarwin+bugzilla@gmail.com

As of DMD 2.100.0, the following program fails to compile:

---
alias AliasSeq(Args...) = Args;

struct S
{
    int x;
    void fun()
    {
        static foreach (sym; AliasSeq!(S.x)) {}
    }
}
---

The error message is

---
bug.d(8): Error: value of `this` is not known at compile time
---

This is likely caused by the compiler treating the loop variable `sym` as an expression rather than an alias, since replacing the `static foreach` loop with an explicit alias declaration causes compilation to succeed:

---
struct S
{
    int x;
    void fun()
    {
        alias sym = S.x; // no error
    }
}
---

It is worth noting that the language spec's description of `static foreach` explicitly forbids this:

> Within the i-th copy, the name of the static foreach variable
> is bound to the i-th entry of the sequence, either as an enum
> variable declaration (for constants) or an alias declaration
> (for symbols). (In particular, static foreach variables are
> never runtime variables.)

https://dlang.org/spec/version.html#staticforeach

--