[Issue 23192] New: Can't iterate aggregate fields with static foreach inside a member function

d-bugmail at puremagic.com d-bugmail at puremagic.com
Thu Jun 16 14:03:19 UTC 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 at puremagic.com
          Reporter: snarwin+bugzilla at 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

--


More information about the Digitalmars-d-bugs mailing list