D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 2571 - const/invariant/immutable static arrays: const(T)[N] and const(T[N]) are the same, but DMD treats them as different
Summary: const/invariant/immutable static arrays: const(T)[N] and const(T[N]) are the ...
Status: RESOLVED FIXED
Alias: None
Product: D
Classification: Unclassified
Component: dmd (show other issues)
Version: D2
Hardware: x86 Windows
: P2 normal
Assignee: No Owner
URL:
Keywords: rejects-valid, wrong-code
Depends on:
Blocks:
 
Reported: 2009-01-10 08:41 UTC by Stewart Gordon
Modified: 2015-06-09 01:21 UTC (History)
1 user (show)

See Also:


Attachments
Example of a squashed summary line (35.43 KB, image/png)
2010-06-24 17:49 UTC, Stewart Gordon
Details

Note You need to log in before you can comment on or make changes to this issue.
Description Stewart Gordon 2009-01-10 08:41:59 UTC
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 Stewart Gordon 2009-01-10 10:48:12 UTC
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 Stewart Gordon 2010-06-24 17:49:56 UTC
Created attachment 675 [details]
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 Stewart Gordon 2010-06-24 17:52:10 UTC
Comment on attachment 675 [details]
Example of a squashed summary line

Sorry, attached to wrong bug report.  Reposted on issue 4386.
Comment 4 Walter Bright 2010-11-07 14:29:20 UTC
You're right, this is a bit of a mess and should be fixed.
Comment 5 Walter Bright 2010-11-07 20:42:55 UTC
http://www.dsource.org/projects/dmd/changeset/744