D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 22421 - static foreach introduces semantic difference between indexing and iteration variable
Summary: static foreach introduces semantic difference between indexing and iteration ...
Status: RESOLVED FIXED
Alias: None
Product: D
Classification: Unclassified
Component: dmd (show other issues)
Version: D2
Hardware: All All
: P1 normal
Assignee: No Owner
URL:
Keywords: pull
Depends on:
Blocks:
 
Reported: 2021-10-18 18:57 UTC by Andrei Alexandrescu
Modified: 2021-11-04 15:25 UTC (History)
1 user (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description Andrei Alexandrescu 2021-10-18 18:57:48 UTC
Consider the code below. Both versions should work the same, but the second causes an assertion error.

alias AliasSeq(T...) = T;

version(works)
    template staticMap(alias fun, args...)
    {
        alias staticMap = AliasSeq!();
        static foreach(i; 0 .. args.length)
            staticMap = AliasSeq!(staticMap, fun!(args[i]));
    }
else // BUG below
    template staticMap(alias fun, args...)
    {
        alias staticMap = AliasSeq!();
        static foreach(arg; args)
            staticMap = AliasSeq!(staticMap, fun!arg);
    }


template id(alias what)
{
    enum id = __traits(identifier, what);
}
enum A { a }
static assert(staticMap!(id, A.a) == AliasSeq!("a"));
Comment 1 Adam D. Ruppe 2021-10-18 19:33:33 UTC
i wanna follow the thread

i suspect this has to do with an assignment to a temporary in the loop forcing eval of the ctfe thing, turning it from an alias symbol into a value.

but using the `alias` keyword in the static foreach still does it which makes me not sure.
Comment 2 Dlang Bot 2021-11-01 03:58:09 UTC
@BorisCarvajal created dlang/dmd pull request #13252 "Fix Issue 22421 - static foreach introduces semantic difference between indexing and iteration variable" fixing this issue:

- Fix Issue 22421 - static foreach introduces semantic difference between indexing and iteration variable

https://github.com/dlang/dmd/pull/13252
Comment 3 Dlang Bot 2021-11-04 15:25:50 UTC
dlang/dmd pull request #13252 "Fix Issue 22421 - static foreach introduces semantic difference between indexing and iteration variable" was merged into master:

- 888beec5512e676a59c7c8ef3bee532e9a519bc1 by Boris Carvajal:
  Fix Issue 22421 - static foreach introduces semantic difference between indexing and iteration variable

https://github.com/dlang/dmd/pull/13252