Bug 5021 – Associative array assigned to in spite of exception

Status
RESOLVED
Resolution
DUPLICATE
Severity
major
Priority
P2
Component
dmd
Product
D
Version
D2
Platform
Other
OS
Linux
Creation time
2010-10-09T00:49:00Z
Last change time
2015-06-09T05:15:19Z
Assigned to
nobody
Creator
issues.dlang

Comments

Comment #0 by issues.dlang — 2010-10-09T00:49:21Z
This program: import std.string; int func() { throw new Exception("It's an exception."); } void main() { int[string] arr; try { arr["hello"] = func(); } catch(Exception e) { } assert(arr.length == 0, format("actual: %s", arr.length)); } results in this output: [email protected](20): actual: 1 ---------------- ./d(_d_assert_msg+0x18) [0x8081ab8] ./d(_Dmain+0x8f) [0x807eb0f] ./d(extern (C) int rt.dmain2.main(int, char**)) [0x8081ca6] ./d(extern (C) int rt.dmain2.main(int, char**)) [0x8081c00] ./d(extern (C) int rt.dmain2.main(int, char**)) [0x8081cea] ./d(extern (C) int rt.dmain2.main(int, char**)) [0x8081c00] ./d(main+0x96) [0x8081ba6] /usr/lib32/libc.so.6(__libc_start_main+0xe6) [0xf75f1c76] ./d() [0x807e9a1] The associative array should have been unchanged. The assignment never took place because an exception was thrown before the function could return the value to be assigned to the associative array. Since no assignment took place, the associative array should not have changed, but it did. It has 1 value in it where it should have 0.
Comment #1 by issues.dlang — 2011-08-12T01:26:13Z
Okay. I just spent a few hours trying to track down a failure due to this, so I'm bumping it up to major (what the exact criteria for what level a bug is, I don't know, but this one is definitely annoying). What seems to be happening here is that the associative array gets assigned a default-initialized value if an exception was thrown from the expression on the right-hand side of the assignment. My best guess is that a default-initialized element is added to the AA as part of arr["hello"], and then when the assignment doesn't happen due to the exception, the AA is left with the default-initialized value in it.
Comment #2 by yebblies — 2012-02-02T23:35:00Z
Same cause, same fix. *** This issue has been marked as a duplicate of issue 3825 ***