Bug 20275 – Tuple created in template in with() includes with-symbol
Status
RESOLVED
Resolution
WORKSFORME
Severity
minor
Priority
P3
Component
dmd
Product
D
Version
D2
Platform
x86_64
OS
Linux
Creation time
2019-10-07T08:20:55Z
Last change time
2023-08-19T15:16:12Z
Assigned to
No Owner
Creator
FeepingCreature
Comments
Comment #0 by default_357-line — 2019-10-07T08:20:55Z
Consider this code:
enum E { A }
alias AliasSeq(T...) = T;
alias EnumMembers(T) = AliasSeq!(__traits(allMembers, T));
void main()
{
pragma(msg, EnumMembers!E.stringof);
with (S())
{
pragma(msg, EnumMembers!E.stringof);
auto members = [EnumMembers!E];
}
}
struct S
{
alias EnumMembers = .EnumMembers;
}
run.dlang.io: https://run.dlang.io/is/VwW2Zw
When run, it is seen that the `EnumMembers` aliased in the struct references the with-symbol:
tuple("A")
(*__withSym).tuple("A")
onlineapp.d(12): Error: expression has no value
This is despite the fact that none of the members of the tuple reference the struct, are looked up in the struct, or have anything to do with the struct. Also the tuple isn't formed in the lexical context of the with() statement. As a result, the attempt to form an array of enum members fails with a cryptic error.
Comment #1 by b2.temp — 2019-11-03T23:15:26Z
little more clear:
---
enum E { A }
alias GlobalEnumMembers(T) = __traits(allMembers, T);
void main()
{
pragma(msg, GlobalEnumMembers!E.stringof);
with (S())
{
pragma(msg, EnumMembers!E.stringof); // (*__withSym).tuple("A")
}
}
struct S
{
alias EnumMembers = GlobalEnumMembers;
}
---
so "(*__withSym)" is just a `S` and ".tuple("A")" the member tuple, i.e the alias.While this is distrurbing, I don't see a bug. This is like
---
enum E { A }
alias GlobalEnumMembers(T) = __traits(allMembers, T);
void main()
{
pragma(msg, GlobalEnumMembers!E.stringof);
S s;
{
pragma(msg, s.EnumMembers!E.stringof);
}
}
struct S
{
alias EnumMembers = GlobalEnumMembers;
}
---
which prints s.tuple("A").
The only thing is that `with(S())` creates a kind of anonymous/temp variable.
voting for close as invalid.