Bug 12927 – Can't get at compile-time the immutable field of a struct instance in a TypeTuple
Status
RESOLVED
Resolution
INVALID
Severity
regression
Priority
P1
Component
dmd
Product
D
Version
D2
Platform
x86
OS
Windows
Creation time
2014-06-15T22:29:00Z
Last change time
2014-06-16T14:40:23Z
Keywords
rejects-valid
Assigned to
nobody
Creator
bearophile_hugs
Comments
Comment #0 by bearophile_hugs — 2014-06-15T22:29:07Z
I am not fully sure this code is valid, but this used to work until recently:
alias TypeTuple(T...) = T;
struct Foo {
int a;
immutable int b;
}
struct Bar {
auto foos = TypeTuple!(Foo(10, 20), Foo(10, 20));
void bar() {
enum i = 1;
enum r = foos[i].b;
}
}
void main() {}
With dmd 2.066alpha it gives:
temp.d(10,18): Error: value of 'this' is not known at compile time
I use this idiom to have struct instances with some mutable fields and with some "enum" fields.
Comment #1 by k.hara.pg — 2014-06-16T14:34:12Z
In 2.063 and earlier, the code had been incorrectly accepted.
Bar.foos is an instance field, so cannot evaluate it without valid 'this' expression.
Comment #2 by bearophile_hugs — 2014-06-16T14:40:23Z
(In reply to Kenji Hara from comment #1)
> In 2.063 and earlier, the code had been incorrectly accepted.
>
> Bar.foos is an instance field, so cannot evaluate it without valid 'this'
> expression.
I see.
And is it right to reject this too:
alias TypeTuple(T...) = T;
struct Foo {
int a;
immutable int b;
}
__gshared foos = TypeTuple!(Foo(10, 20), Foo(10, 20));
struct Bar {
void bar() {
enum i = 1;
enum r = foos[i].b;
}
}
void main() {}
dmd 2.066alpha gives:
test.d(6): Error: static variable _foos_field_1 cannot be read at compile time