This code
---
void main()
{
import std.range;
auto a = only(S.init);
auto b = a;
}
struct S
{
this(ref return scope inout(S) rhs) scope @safe inout pure nothrow
{
}
}
---
results in the error
---
q.d(6): Error: no property `__ctor` for `b` of type `std.range.OnlyResult!(S)`
/usr/local/include/dmd/std/range/package.d(10333): struct `OnlyResult` defined here
---
The error seems to stem from the fact that OnlyResult declares a copy constructor but then makes it private for some reason.
Comment #1 by dlang-bot — 2024-02-26T19:31:02Z
@jmdavis created dlang/phobos pull request #8924 "Fix bugzilla issue 24415 - only doesn't work with elements with a copy constructor." fixing this issue:
- Fix bugzilla issue 24415 - only doesn't work with elements with a copy constructor.
I have no clue why anyone would have made a copy constructor private,
but making the copy constructor private on OnlyResult makes it useless
for anything outside of std.range.package, making only useless with
elements that have a copy constructor.
So, this commit makes it so that the copy constructor is no longer
private.
https://github.com/dlang/phobos/pull/8924
Comment #2 by snarwin+bugzilla — 2024-02-26T20:01:06Z
This is a compiler bug. The presence of a private templated constructor earlier in the overload set prevents the public copy constructor from being called from another module.
Minimal reproduction:
--- lib.d
module lib;
struct S
{
int n;
private this()(int n) { this.n = m; }
this(ref inout S other) inout {}
}
--- app.d
module app;
import lib;
void main()
{
S a;
auto b = a;
}
---
Which produces this error message when compiled:
---
app.d(8): Error: no property `__ctor` for `b` of type `lib.S`
lib.d(3): struct `S` defined here
---
Note that the bug is order-dependent. If the copy constructor is moved above the private templated constructor, the example compiles successfully.
Comment #3 by snarwin+bugzilla — 2024-02-26T20:05:49Z
In fact, the public constructor doesn't have to be a copy constructor to trigger the bug. Any kind of constructor will do.
--- lib.d
module lib;
struct S
{
int n;
private this()(int n) { this.n = m; }
this(string s) inout {}
}
--- app.d
module app;
import lib;
void main()
{
S a = "hello"; // Error
}
---
Comment #4 by b2.temp — 2024-02-26T22:57:46Z
To fo further, isn't this the bug of having overload sets made of members with different visibility ?
E.g not at all related to constructors ;)
Comment #5 by snarwin+bugzilla — 2024-02-26T23:14:00Z
dlang/phobos pull request #8924 "Work around bugzilla issue 24415 - only doesn't work with elements with a copy constructor." was merged into stable:
- 66bbf4dae038879d1ff66af3b7ab25ea66c8320d by Jonathan M Davis:
Work around bugzilla issue 24415 - only doesn't work with elements with a copy constructor.
Since the compiler is treating the auto-generated copy-constructor for
OnlyResult as private (thus rendering it useless outside of
std.range.package), this commit adds an explicit one and makes it
public. Once the dmd bug has been fixed, the explicit copy constructor
should be removed.
https://github.com/dlang/phobos/pull/8924
Comment #7 by dlang-bot — 2024-04-25T19:12:17Z
@ibuclaw created dlang/phobos pull request #8988 "merge stable" mentioning this issue:
- Work around bugzilla issue 24415 - only doesn't work with elements with a copy constructor.
Since the compiler is treating the auto-generated copy-constructor for
OnlyResult as private (thus rendering it useless outside of
std.range.package), this commit adds an explicit one and makes it
public. Once the dmd bug has been fixed, the explicit copy constructor
should be removed.
https://github.com/dlang/phobos/pull/8988
Comment #8 by dlang-bot — 2024-04-26T05:07:41Z
dlang/phobos pull request #8988 "merge stable" was merged into master:
- 34ff27e58d83e7a6135af56af2eb34ba3a6d7252 by Jonathan M Davis:
Work around bugzilla issue 24415 - only doesn't work with elements with a copy constructor.
Since the compiler is treating the auto-generated copy-constructor for
OnlyResult as private (thus rendering it useless outside of
std.range.package), this commit adds an explicit one and makes it
public. Once the dmd bug has been fixed, the explicit copy constructor
should be removed.
https://github.com/dlang/phobos/pull/8988
Comment #9 by robert.schadek — 2024-12-13T19:33:31Z