Bug 2571 – const/invariant/immutable static arrays: const(T)[N] and const(T[N]) are the same, but DMD treats them as different

Status
RESOLVED
Resolution
FIXED
Severity
normal
Priority
P2
Component
dmd
Product
D
Version
D2
Platform
x86
OS
Windows
Creation time
2009-01-10T08:41:00Z
Last change time
2015-06-09T01:21:01Z
Keywords
rejects-valid, wrong-code
Assigned to
nobody
Creator
smjg

Attachments

IDFilenameSummaryContent-TypeSize
675D bugzilla 2571.pngExample of a squashed summary lineimage/png36280

Comments

Comment #0 by smjg — 2009-01-10T08:41:59Z
You can apply const to the whole of a static array type or to the element type thereof. Since static arrays have value semantics, the result is the same: you can neither change an element in the array nor reassign to it a whole new array. Nonetheless, DMD treats them as different types: ----- const_static_array_1.d ----- const(char)[26] abc1 = "abcdefghijklmnopqrstuvwxyz"; const(char[26]) abc2 = "abcdefghijklmnopqrstuvwxyz"; pragma(msg, typeof(abc1).stringof); pragma(msg, typeof(abc2).stringof); pragma(msg, (const(char)[26]).stringof); pragma(msg, (const(char[26])).stringof); pragma(msg, is(typeof(abc1) : typeof(abc2)).stringof); pragma(msg, is(typeof(abc2) : typeof(abc1)).stringof); pragma(msg, is(typeof(abc1) == typeof(abc2)).stringof); ---------- C:\Users\Stewart\Documents\Programming\D\d2\tests>dmd -c const_static_array_1.d const(char)[26u] const(char[26u]) const(char)[26u] const(char[26u]) true true false ---------- Moreover, while they are implicitly convertible, that they are treated as different types creates a silly template incompatibility: ----- const_static_array_2.d ----- const(char)[26] abc1 = "abcdefghijklmnopqrstuvwxyz"; const(char[26]) abc2 = "abcdefghijklmnopqrstuvwxyz"; class Temp(T) {} void main() { Temp!(const(char)[26]) t1; Temp!(const(char[26])) t2; t1 = t2; t2 = t1; } ---------- C:\Users\Stewart\Documents\Programming\D\d2\tests>dmd const_static_array_2.d const_static_array_2.d(10): Error: cannot implicitly convert expression (t2) of type const_static_array_2.Temp!(const(char[26u])).Temp to const_static_array_2.Temp!(const(char)[26u]).Temp const_static_array_2.d(11): Error: cannot implicitly convert expression (t1) of type const_static_array_2.Temp!(const(char)[26u]).Temp to const_static_array_2.Temp!(const(char[26u])).Temp ----------
Comment #1 by smjg — 2009-01-10T10:48:12Z
The type should be reported as const(char[26]) in either case. And here's another case of the same basic bug: ---------- invariant(const(char)[26]) abc1 = "abcdefghijklmnopqrstuvwxyz"; const(invariant(char)[26]) abc2 = "abcdefghijklmnopqrstuvwxyz"; pragma(msg, typeof(abc1).stringof); pragma(msg, typeof(abc2).stringof); pragma(msg, (invariant(const(char)[26])).stringof); pragma(msg, (const(invariant(char)[26])).stringof); pragma(msg, is(typeof(abc1) : typeof(abc2)).stringof); pragma(msg, is(typeof(abc2) : typeof(abc1)).stringof); pragma(msg, is(typeof(abc1) == typeof(abc2)).stringof); ---------- C:\Users\Stewart\Documents\Programming\D\d2\tests>dmd -c const_static_array_3.d immutable(char[26u]) const(immutable(char)[26u]) immutable(char[26u]) const(immutable(char)[26u]) true true false ---------- Again, the two should denote the exact same type, invariant(char[26u]). (immutable? invariant? See also issue 2572.)
Comment #2 by smjg — 2010-06-24T17:49:56Z
Created attachment 675 Example of a squashed summary line Rendered by Firefox 3.6.3 at inner width 1000. If you make it much narrower, you get a horizontal scrollbar.
Comment #3 by smjg — 2010-06-24T17:52:10Z
Comment on attachment 675 Example of a squashed summary line Sorry, attached to wrong bug report. Reposted on issue 4386.
Comment #4 by bugzilla — 2010-11-07T14:29:20Z
You're right, this is a bit of a mess and should be fixed.
Comment #5 by bugzilla — 2010-11-07T20:42:55Z