Bug 23192 – Can't iterate aggregate fields with static foreach inside a member function
Status
RESOLVED
Resolution
FIXED
Severity
normal
Priority
P1
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2022-06-16T14:03:19Z
Last change time
2022-06-18T09:01:34Z
Keywords
pull
Assigned to
No Owner
Creator
Paul Backus
Comments
Comment #0 by snarwin+bugzilla — 2022-06-16T14:03:19Z
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
Comment #1 by dlang-bot — 2022-06-18T08:11:25Z
@BorisCarvajal created dlang/dmd pull request #14227 "Fix Issue 23192 - Can't iterate aggregate fields with static foreach inside a member function" fixing this issue:
- Fix Issue 23192 - Can't iterate aggregate fields with static foreach inside a member function
https://github.com/dlang/dmd/pull/14227
Comment #2 by dlang-bot — 2022-06-18T09:01:34Z
dlang/dmd pull request #14227 "Fix Issue 23192 - Can't iterate aggregate fields with static foreach inside a member function" was merged into master:
- 200d58ae800d43c6cd093c8346f9033565c43bb4 by Boris Carvajal:
Fix Issue 23192 - Can't iterate aggregate fields with static foreach inside a member function
https://github.com/dlang/dmd/pull/14227