Bug 23033 – pure functions can allocate values with impure destructors

Status
NEW
Severity
normal
Priority
P3
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2022-04-17T22:08:38Z
Last change time
2024-12-13T19:22:20Z
Assigned to
No Owner
Creator
Dennis
Moved to GitHub: dmd#18099 →

Comments

Comment #0 by dkorpel — 2022-04-17T22:08:38Z
Came up here: https://github.com/dlang/dmd/pull/13993#issuecomment-1100944395 A pure function can allocate something with an impure destructor to violate the assumptions a pure function gives, for example that the result of a pure factory function is unique: ``` import core.memory; char[] globalVar; struct S { char[] chars; ~this() { globalVar = chars; } } // pure factory function char[] returnUnique() pure { auto s = new S(new char[4]); s.chars[] = 'a'; return s.chars; } void main() { string s = returnUnique(); // safe cast to immutable GC.collect(); // impure destructor is run assert(s == "aaaa"); // string is immutable globalVar[] = 'b'; // alias escaped through impure destructor assert(s == "aaaa"); // we mutated immutable memory } ```
Comment #1 by robert.schadek — 2024-12-13T19:22:20Z
THIS ISSUE HAS BEEN MOVED TO GITHUB https://github.com/dlang/dmd/issues/18099 DO NOT COMMENT HERE ANYMORE, NOBODY WILL SEE IT, THIS ISSUE HAS BEEN MOVED TO GITHUB