D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 15604 - [REG2.070] std.array.array of structs with template opAssign and default initialised 'new'ed class member
Summary: [REG2.070] std.array.array of structs with template opAssign and default init...
Status: RESOLVED INVALID
Alias: None
Product: D
Classification: Unclassified
Component: phobos (show other issues)
Version: D2
Hardware: All All
: P1 regression
Assignee: No Owner
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2016-01-25 12:34 UTC by John Colvin
Modified: 2017-07-05 15:13 UTC (History)
3 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description John Colvin 2016-01-25 12:34:59 UTC
% cat test.d
void foo()
{
    import std.array;
    [A()].array;
}

struct A
{
    void opAssign(T)(T v) {}
    Object o = new Object;
}

% dmd test.d
/usr/local/Cellar/dmd/HEAD/include/d2/std/conv.d(3970): Error: cannot implicitly convert expression (S(A(Object()))) of type S to immutable(S)
/usr/local/Cellar/dmd/HEAD/include/d2/std/conv.d(3934): Error: template instance std.conv.emplaceInitializer!(S) error instantiating
/usr/local/Cellar/dmd/HEAD/include/d2/std/array.d(125):        instantiated from here: emplaceRef!(A, A, A)
arrayBug.d(4):        instantiated from here: array!(A[])
/usr/local/Cellar/dmd/HEAD/include/d2/std/array.d(128): Error: forward reference to inferred return type of function call () => cast(E[])result()

This regression was introduced by https://github.com/D-Programming-Language/phobos/pull/3805
Comment 1 John Colvin 2016-01-26 09:19:56 UTC
The problem comes from a deeper issue with static initialisers with indirections, which are just a nightmare.

Nonetheless, here's the fix: https://github.com/D-Programming-Language/phobos/pull/3952
Comment 2 David Nadlinger 2016-01-29 20:34:53 UTC
As I pointed out in the GitHub issue, I'm not convinced that this is a regression. Isn't it just an accepts-invalid bug in the compiler that the struct declaration is considered valid in the first place?

From looking at the codegen, I find it hard to imagine that the current behavior (a single implicitly-shared object instance) would be what the user would expect.
Comment 3 John Colvin 2016-01-30 09:46:00 UTC
(In reply to David Nadlinger from comment #2)
> As I pointed out in the GitHub issue, I'm not convinced that this is a
> regression. Isn't it just an accepts-invalid bug in the compiler that the
> struct declaration is considered valid in the first place?
> 
> From looking at the codegen, I find it hard to imagine that the current
> behavior (a single implicitly-shared object instance) would be what the user
> would expect.


I agree and have since deleted all such things from the codebase I was working on. I also hit some weird linker errors with them that I was unable to minimise examples for, so good riddance.

On the topic of static initialisers with mutable indirections, what about these:

struct A
{
    int[] arr = [1, 2, 3];
}

or this:

Object globalSingleton;
struct B
{
    Object o = globalSingleton;
}
Comment 4 Vladimir Panteleev 2017-07-05 15:13:22 UTC
(In reply to John Colvin from comment #3)
> On the topic of static initialisers with mutable indirections, what about
> these:

I believe all of those are invalid and should not compile.

I filed issue 17604 for this (even though it's probably a duplicate of another issue filed somewhere), so I think we can now close this one.