Bug 3688 – Can't have declaration with assignment to const/immutable inside if condition

Status
RESOLVED
Resolution
FIXED
Severity
normal
Priority
P2
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2010-01-07T14:47:00Z
Last change time
2011-05-31T19:14:59Z
Keywords
rejects-valid
Assigned to
nobody
Creator
tomeksowi

Comments

Comment #0 by tomeksowi — 2010-01-07T14:47:07Z
Should pass: if (const int a = 4) // Error: variable test.main.a cannot modify const assert (a==4);
Comment #1 by k.hara.pg — 2011-04-18T20:20:18Z
More comprehensive code. ---- struct S { int v; this(int n) { v = n; } const bool opCast(T:bool)() { return true; } } void main() { if (int a = 1) assert(a == 1); else assert(0); if (const int a = 2) assert(a == 2); else assert(0); if (immutable int a = 3) assert(a == 3); else assert(0); if (auto s = S(10)) assert(s.v == 10); else assert(0); if (auto s = const(S)(20)) assert(s.v == 20); else assert(0); if (auto s = immutable(S)(30)) assert(s.v == 30); else assert(0); } ----
Comment #2 by bugzilla — 2011-05-29T10:33:45Z
Comment #3 by bugzilla — 2011-05-31T19:14:59Z