Bug 4338 – Structs with non-const destructors cannot be used as const parameters

Status
RESOLVED
Resolution
LATER
Severity
major
Priority
P2
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2010-06-17T00:03:49Z
Last change time
2022-08-15T11:32:27Z
Keywords
diagnostic
Assigned to
No Owner
Creator
Don

Comments

Comment #0 by clugdbug — 2010-06-17T00:03:49Z
TEST CASE: struct A { ~this() {} } void foo(const A a) {} --------- bug.d(4): Error: destructor bug.A.~this () is not callable using argument types () Workaround is to change ~this() into const ~this(). I find this whole situation pretty weird. How can a destructor be const? (Is a const struct effectively 'tail const', so that a const destructor only destroys the non-const bit?) And anyway, should a destructor be called on non-mutable function parameters? In any case, the error message is rather poor.
Comment #1 by webby — 2010-06-17T02:08:52Z
Comment #2 by ah08010-d — 2010-10-23T23:58:39Z
I stumbled into this problem with a struct having an immutable member. Apparently, any kind of const-ness taints the struct, which prevents calling the destructor. In my case, declaring the destructor const/immutable didn't help. :( Apparently, you can't have struct with immutable members as an "in" parameter, since that does the "const scope" thing, which triggers the destructor, which causes the failure. Also: the "const ~this()" syntax works, but "~this() const" does not, which seems odd since it works for other method names.
Comment #3 by k.hara.pg — 2011-06-21T06:19:56Z
Now this issue is temporary fixed by calling mutable dtor from const object. See change of declaration.c and Walter's comment. https://github.com/D-Programming-Language/dmd/commit/aef37eb0c8986a508ccf185286465b4cbef8a066#L1R1721
Comment #4 by k.hara.pg — 2011-06-21T06:34:18Z
*** Issue 3606 has been marked as a duplicate of this issue. ***
Comment #5 by k.hara.pg — 2011-10-09T04:18:14Z
Postblit has similar problem, see bug 4867.
Comment #6 by andrei — 2012-01-17T21:13:29Z
With the current 2.058 from head the code compiles. But it shouldn't because it's unsound. Mutable destructors shouldn't apply to objects that were immutable, otherwise they can mutate immutable objects. Consider: struct A { int* p; ~this() { *p = 5; } } void main() { auto p = new immutable(int); { auto a = immutable(A)(p); } assert(*p == 0); }
Comment #7 by verylonglogin.reg — 2012-11-03T06:51:43Z
Consolidated into Issue 8956. Do not close this issue as a duplicate because it is another issue. E.g. it can be closed as WONTFIX if destructors will become qualifier-overloadable.
Comment #8 by schuetzm — 2015-06-14T10:35:52Z
Comment #9 by ag0aep6g — 2017-07-25T10:09:40Z
(In reply to Andrei Alexandrescu from comment #6) > Mutable destructors shouldn't apply to objects that were immutable, > otherwise they can mutate immutable objects. Consider: > > struct A { > int* p; > ~this() { *p = 5; } > } > > void main() { > auto p = new immutable(int); > { auto a = immutable(A)(p); } > assert(*p == 0); > } Dedicated issue for that: issue 17682.
Comment #10 by razvan.nitu1305 — 2022-08-15T11:32:27Z
Closing this issue as the code compiles. As Andrei pointed out, it is unsound for this code to compile, however, there is a dedicated bug report for that case.