Bug 14017 – Assignment of a mutable array returned from non-`pure` function to `immutable` global variable is allowed

Status
RESOLVED
Resolution
INVALID
Severity
normal
Priority
P1
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2015-01-20T18:50:55Z
Last change time
2022-12-16T18:52:26Z
Keywords
accepts-invalid
Assigned to
No Owner
Creator
Denis Shelomovskii
See also
https://issues.dlang.org/show_bug.cgi?id=7492

Comments

Comment #0 by verylonglogin.reg — 2015-01-20T18:50:55Z
This code compiles but shouldn't: --- auto f() { return new int[1]; } immutable s = f(); // error only with character arrays --- Also such code is currently rejected for arrays of characters.
Comment #1 by schveiguy — 2015-01-20T19:02:27Z
It's CTFE executed, so immutable is fine. Although I think it would be permissible for pure to be inferred here because of the auto return type. The bug is the opposite -- it should allow the char[] array to also be assigned.
Comment #2 by verylonglogin.reg — 2015-01-20T21:06:11Z
Note: This issue is created as a result of Issue 7492 discussion. (In reply to Steven Schveighoffer from comment #1) > It's CTFE executed, so immutable is fine. Although I think it would be > permissible for pure to be inferred here because of the auto return type. > > The bug is the opposite -- it should allow the char[] array to also be > assigned. It's not a bug, it's a current language type system rules. See enhancement Issue 14018 for language rules change request. And please, don't mix another language change request about purity inference because of `auto` return type in this issue.
Comment #3 by razvan.nitu1305 — 2022-12-16T13:57:46Z
As Steven said, this bug is invalid.
Comment #4 by schveiguy — 2022-12-16T18:52:26Z
To address the original statement as well, if f returns a character array, it now compiles. And this is to be expected.