Bug 5555 – [AA] Built-in associative arrays in pure nothrow functions

Status
RESOLVED
Resolution
FIXED
Severity
normal
Priority
P3
Component
druntime
Product
D
Version
D2
Platform
All
OS
All
Creation time
2011-02-09T08:35:00Z
Last change time
2016-06-26T09:51:14Z
Assigned to
nobody
Creator
repeatedly
Blocks
6357

Comments

Comment #0 by repeatedly — 2011-02-09T08:35:27Z
I have following code: // aa is a string[string]; nothrow bool empty() const { return aa.length == 0; } I want use this code but dmd fails. Error: function hoge.Foo.empty 'empty' is nothrow yet may throw built-in AA's length() depends on _aaLen in rt/aaA.d. I think these functions should be nothrow. This issue breaks my template engine API ;(
Comment #1 by bearophile_hugs — 2011-02-09T09:48:44Z
There is a similar problem with pure too: int[int] aa; nothrow int foo() { return aa.length; } pure int bar() { int[int] aa2; return aa2.length; } void main() {}
Comment #2 by bearophile_hugs — 2011-09-18T11:39:35Z
Currently associative arrays can't be used much in pure nothrow functions: void main() pure nothrow { int[int] aa; aa.rehash; auto L = aa.length; auto x = aa.get(0, 10); auto k = aa.keys; auto v = aa.values; auto bk = aa.byKey(); auto bv = aa.byValue(); } DMD 2.055 gives: test.d(3): Error: pure function 'main' cannot call impure function 'rehash' test.d(4): Error: pure function 'main' cannot call impure function 'length' test.d(5): Error: pure function 'main' cannot call impure function 'get' test.d(6): Error: pure function 'main' cannot call impure function 'keys' test.d(7): Error: pure function 'main' cannot call impure function 'values' test.d(8): Error: pure function 'main' cannot call impure function 'byKey' test.d(9): Error: pure function 'main' cannot call impure function 'byValue' test.d(3): Error: aa.rehash is not nothrow test.d(4): Error: aa.length is not nothrow test.d(5): Error: aa.get is not nothrow test.d(6): Error: aa.keys is not nothrow test.d(7): Error: aa.values is not nothrow test.d(8): Error: aa.byKey is not nothrow test.d(9): Error: aa.byValue is not nothrow test.d(1): Error: function D main 'main' is nothrow yet may throw
Comment #3 by witold.baryluk+d — 2012-07-05T11:25:55Z
Any updates? I'm trying to implement toHash() method which needs to be nothrow, and inside it I use aa.byKey or aa.keys or foreach (...; aa). Currently I cannot without some hacks. Fixes in rt.* should be relatively easy for most of this functions. Regards, Witek
Comment #4 by lt.infiltrator — 2012-12-18T17:13:09Z
*** Issue 9168 has been marked as a duplicate of this issue. ***
Comment #5 by devw0rp — 2013-04-18T00:27:40Z
I'd like to remind everyone of this issue. I hold a strong belief that even if the internals of associative arrays are impure, unsafe, and may throw, properties for associative arrays should "lie" about these things because they are invaluable mechanisms for implementing so many pure, @safe, and nothrow algorithms. They aren't really pure because they modify global state, but so does allocating memory, which pure allows for. They aren't @safe, because they rely on certain memory techniques, but these are internals that form the language. They aren't nothrow, because checking the properties of an associative array may fail, but this is as much of an unrecoverable error as an OutOfMemoryError. If the properties of associative arrays take on all of these things, it would make it possible to create an entire host of code which is pure, @safe, and nothrow. (For starters, I can imagine undirected and directed graph types which meet these requirements.)
Comment #6 by lt.infiltrator — 2014-03-18T19:57:51Z
*** Issue 6357 has been marked as a duplicate of this issue. ***
Comment #7 by verylonglogin.reg — 2014-06-12T12:34:49Z
Some cases of this issue are already fixed. `byKey` and `byValue` will be marked as `pure nothrow` in this pull: https://github.com/D-Programming-Language/druntime/pull/838 If the pull is merged only the following AA related functions will remain unmarked: * `aa.get(...)` because of `lazy inout(V) defaultValue` * `foreach(key, value; aa)` aka `_aaApply2` because it accepts delegate `dg2_t dg` with unknown attributes * `aa.dup` because it uses `foreach(key, value; aa)`
Comment #8 by github-bugzilla — 2014-06-13T06:54:19Z
Commit pushed to master at https://github.com/D-Programming-Language/druntime https://github.com/D-Programming-Language/druntime/commit/dd0f141f313fa6709939b4d626ff73296108ca70 Mark associative array `byKey` and `byValue` `pure nothrow`. This fixes part of Issue 5555. Issue URL: https://issues.dlang.org/show_bug.cgi?id=5555
Comment #9 by bearophile_hugs — 2014-06-13T09:18:52Z
I suggest to not close this issue until they are also @nogc.
Comment #10 by verylonglogin.reg — 2014-06-13T10:46:41Z
(In reply to bearophile_hugs from comment #9) > I suggest to not close this issue until they are also @nogc. https://github.com/D-Programming-Language/druntime/pull/840
Comment #11 by bugzilla — 2016-06-26T09:51:14Z
(In reply to Denis Shelomovskij from comment #10) > (In reply to bearophile_hugs from comment #9) > > I suggest to not close this issue until they are also @nogc. > > https://github.com/D-Programming-Language/druntime/pull/840 This was merged. Closing.