D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 5021 - Associative array assigned to in spite of exception
Summary: Associative array assigned to in spite of exception
Status: RESOLVED DUPLICATE of issue 3825
Alias: None
Product: D
Classification: Unclassified
Component: dmd (show other issues)
Version: D2
Hardware: Other Linux
: P2 major
Assignee: No Owner
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2010-10-09 00:49 UTC by Jonathan M Davis
Modified: 2015-06-09 05:15 UTC (History)
1 user (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description Jonathan M Davis 2010-10-09 00:49:21 UTC
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:

core.exception.AssertError@d.d(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 Jonathan M Davis 2011-08-12 01:26:13 UTC
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 yebblies 2012-02-02 23:35:00 UTC
Same cause, same fix.

*** This issue has been marked as a duplicate of issue 3825 ***