D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 16542 - makeArray not usable with const initializer
Summary: makeArray not usable with const initializer
Status: RESOLVED FIXED
Alias: None
Product: D
Classification: Unclassified
Component: phobos (show other issues)
Version: D2
Hardware: x86_64 Mac OS X
: P1 major
Assignee: Lucia Cojocaru
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2016-09-25 21:50 UTC by Ryan
Modified: 2020-03-21 03:56 UTC (History)
2 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description Ryan 2016-09-25 21:50:10 UTC
example:
---------------------------------------
void doSomething(T)(in T initialValue)
{
  T[] t = theAllocator.makeArray!T(100, initialValue); // won't compile

  T[] t2 = theAllocator.makeArray!T(100, cast()initialValue); // this one compiles
  
  // ... use t and t2
}

-----------------------------------------

If all you are doing is copying the value, you shouldn't actually be changing initialValue, so why not make it const?
Comment 1 basile-z 2017-01-10 15:32:48 UTC
Can you add a test case please ? I suspect this report to be either either bad formulated or invalid because with DMD 2.073-b1 the following test passes:

unittest
{
    const int[] v;
    void test(T)(in T t)
    {
        auto a = theAllocator.makeArray(100, t);
    }
    test(v);
}
Comment 2 Ryan 2017-01-10 16:04:41 UTC
The code below compiles and runs unless you uncomment the first case. Then it fails on DMD 2.72.2 on mac. I ran this file with 'rdmd -unittest -main test.d'.

It fails to find a template match for the first case, but when you cast away the const, it works fine.
-------------------------------------------
module test;

void doSomething(T)(in T initialValue)
{
  import std.experimental.allocator;
  import std.stdio;

  pragma(msg, typeof(initialValue).stringof ~ "  " ~ typeof(cast()initialValue).stringof);

  //T[] t = theAllocator.makeArray!T(5, initialValue); // won't compile

  T[] t2 = theAllocator.makeArray!T(5, cast()initialValue); // this one compiles
  
  // ... use t and t2
  writeln(t2);
}

unittest
{
    int i = 5;
    int[3] j = [1,2,3];

    doSomething(i);
    doSomething(j);
}
------------------------------------------------

Thanks,
Comment 3 github-bugzilla 2017-10-30 14:22:49 UTC
Commits pushed to master at https://github.com/dlang/phobos

https://github.com/dlang/phobos/commit/cd66c0cba650d63785ccb79a115964e6472692ca
fix issue 16542

https://github.com/dlang/phobos/commit/2e9271962ffbe2cdabff8fe39be3c55121de6232
Merge pull request #5028 from somzzz/issue_16542

fix issue 16542 -  makeArray not usable with const initializer