Issue 21692 - Non-mutable extern(D) scope class instances cannot be created.
Summary: Non-mutable extern(D) scope class instances cannot be created.
Status: RESOLVED DUPLICATE of issue 19272
Alias: None
Product: D
Classification: Unclassified
Component: dmd (show other issues)
Version: D2
Hardware: x86_64 Linux
: P1 normal
Assignee: No Owner
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2021-03-08 22:29 UTC by thomas.bockman
Modified: 2022-05-30 13:38 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 thomas.bockman 2021-03-08 22:29:48 UTC
stackConstD and stackImutD should be allowed in the program below, just like the other combinations. Alternatively, if there is some good reason for this restriction, a better error message is needed, because construction is not modification.

extern(C++) class C { }
extern(D) class D { }

void main() {
    scope stackConstC = new const(C)();
    scope stackConstD = new const(D)(); // Error: cannot modify const expression stackConstD
    auto gcConstD = new const(D)();

    scope stackImutC = new immutable(C)();
    scope stackImutD = new immutable(D)(); // Error: cannot modify immutable expression stackImutD
    auto gcImutD = new immutable(D)();
}
Comment 1 kinke 2021-03-08 23:15:21 UTC
This seems caused by `scope` allocations for (D) classes getting an implicit `delete stackConstD`, while C++ classes don't. Which is probably a bug in its own right.

The `delete` expression is deprecated; it resets the pointer to null, that's why it cannot be const. An updated lowering could use `destroy()` instead (it's only about calling the dtor/finalizer AFAICT), for C++ classes too, which should make it work with const as well (without dtor or with a const dtor).
Comment 2 thomas.bockman 2021-03-08 23:43:29 UTC
(In reply to kinke from comment #1)
> This seems caused by `scope` allocations for (D) classes getting an implicit
> `delete stackConstD`, while C++ classes don't. Which is probably a bug in
> its own right.

Good catch! I filed a separate bug for that:
    https://issues.dlang.org/show_bug.cgi?id=21693

> The `delete` expression is deprecated; it resets the pointer to null, that's
> why it cannot be const. An updated lowering could use `destroy()` instead
> (it's only about calling the dtor/finalizer AFAICT), for C++ classes too,
> which should make it work with const as well (without dtor or with a const
> dtor).

Yes, that is how it should work.
Comment 3 Nick Treleaven 2022-05-30 13:38:43 UTC

*** This issue has been marked as a duplicate of issue 19272 ***