Bug 13112 – Ignore constness when copying dynamic array argument to static array parameter
Status
RESOLVED
Resolution
INVALID
Severity
enhancement
Priority
P4
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2014-07-12T17:34:42Z
Last change time
2020-08-04T04:16:13Z
Keywords
rejects-valid
Assigned to
No Owner
Creator
Vladimir Panteleev
Comments
Comment #0 by dlang-bugzilla — 2014-07-12T17:34:42Z
void f(char[1] arr) {}
void main()
{
string s;
f(s);
}
This program currently doesn't compile:
test.d(6): Error: function test.f (char[1] arr) is not callable using argument types (string)
However, the conversion is safe, because a copy is implicitly created during dynamic->static conversion, and the array type (char) has no indirections.
Note that this limitation only seems to apply to function parameters, as the following works fine:
string s; char[1] arr = s;
Comment #1 by pro.mathias.lang — 2020-08-04T04:16:13Z
The problem is not constness, but that dynamic arrays do not convert implicitly to static array (which IMO is not an issue).
The difference you're seeing with variables is because initializer are treated differently. I would say that this behavior is more likely to be the issue than not having implicit conversion from static to dynamic array.
The following compiles (but obviously triggers a range error):
```
void f(char[1] arr) {}
void main()
{
string s;
f(s[0 .. 1]);
}
```
So closing as invalid.