Bug 11258 – Static field access problem

Status
RESOLVED
Resolution
INVALID
Severity
regression
Priority
P2
Component
dmd
Product
D
Version
D2
Platform
x86
OS
Windows
Creation time
2013-10-14T05:15:00Z
Last change time
2013-10-14T15:05:50Z
Keywords
rejects-valid
Assigned to
nobody
Creator
bearophile_hugs

Comments

Comment #0 by bearophile_hugs — 2013-10-14T05:15:19Z
struct Foo { static int spam = 1; } struct Bar { static assert(__traits(compiles, { auto x = Foo.spam; })); // line5 static void foo() { static assert(__traits(compiles, { auto x = Foo.spam; })); } } void main() {} With dmd 2.064beta this program gives: test.d(5): Error: static assert (__traits(compiles, () { auto x = Foo.spam; } )) is false In dmd 2.063 this used to work. (Additionally, I'd like those error messages to show less newlines).
Comment #1 by k.hara.pg — 2013-10-14T06:45:10Z
(In reply to comment #0) > In dmd 2.063 this used to work. This is a regression from 2.062, and same error occurs with 2.063.
Comment #2 by k.hara.pg — 2013-10-14T07:26:07Z
(In reply to comment #1) > (In reply to comment #0) > > In dmd 2.063 this used to work. > > This is a regression from 2.062, and same error occurs with 2.063. Sorry, this is NOT a regression. Current behavior is intended. In class/struct DeclDefs scope, directly using function literal is not allowed. struct Bar { int var; auto dg = { return var; }; // Error: function literals cannot be class members } Against that, __traits(compiles) has *no* special handling. Therefore, struct Bar { int var; static assert(__traits(compiles, { return var; })); // catch "cannot be class members" error, then returns false } The static assert correctly fails. Therefor current behavior is intended. On the other hand, this error check is stopped inside typeof operator. It's also an intended behavior. So, in this case, please use is(typeof({ ... })) instead. ==== Note: The old bug, existed in 2.062 and earlier, was fixed in here: https://github.com/D-Programming-Language/dmd/commit/0679c4c31ba78853873933324c69cd90e7714d15#diff-ffa5582af7d723c487d4a11ac6743b85R84
Comment #3 by bearophile_hugs — 2013-10-14T09:42:37Z
(In reply to comment #2) > Sorry, this is NOT a regression. Current behavior is intended. Thank you Kenji, this was tricky for me. Regarding the extra newlines in the error message: test.d(5): Error: static assert (__traits(compiles, () { auto x = Foo.spam; } )) is false Do you want me to open another "minor importance" bug report on it?
Comment #4 by k.hara.pg — 2013-10-14T10:11:39Z
(In reply to comment #3) > Regarding the extra newlines in the error message: > > > test.d(5): Error: static assert (__traits(compiles, () > > { > > auto x = Foo.spam; > > } > > )) is false > > > Do you want me to open another "minor importance" bug report on it? I hope you that. And I'd ask you one question: If the lambda has quite big body, how it will be output?
Comment #5 by bearophile_hugs — 2013-10-14T15:05:50Z
(In reply to comment #4) > I hope you that. > And I'd ask you one question: If the lambda has quite big body, how it will be > output? OK, I have opened Issue 11263 .