Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[AA] double.init in associative array seems 0.0 #17227

Open
dlangBugzillaToGithub opened this issue Jul 15, 2010 · 5 comments
Open

[AA] double.init in associative array seems 0.0 #17227

dlangBugzillaToGithub opened this issue Jul 15, 2010 · 5 comments
Labels
Arch:x86 Issues specific to x86 Druntime:AA Specific to Associative Arrays Druntime Specific to druntime P3 Severity:normal

Comments

@dlangBugzillaToGithub
Copy link

bearophile_hugs reported this on 2010-07-15T04:24:09Z

Transferred from https://issues.dlang.org/show_bug.cgi?id=4463

CC List

  • MichaelZ
  • Berni (@Vild)
  • Stanislav Blinov

Description

With dmd 2.047 this code doesn't assert at runtime (aa[0] has to be nan):


void main() {
    double[int] aa;
    aa[0]++;
    assert(aa[0] == 1.0);
}
@dlangBugzillaToGithub
Copy link
Author

hsteoh commented on 2012-12-21T07:45:21Z

Seems to be related to bug 3825.

@dlangBugzillaToGithub
Copy link
Author

dlang.org commented on 2016-08-18T08:22:24Z

@hsteoh: I don't believe it is related to 3825; 3825 has been apparently fixed (for d2) in 2013 (q.v.), but the behaviour described by bearophile_hugs is still present in dmd 2.071.0  :-|

@dlangBugzillaToGithub
Copy link
Author

dlang.org commented on 2016-08-18T08:30:29Z

... having said that, the double behaviour is explicitly brought up in the pull request discussion pertaining to 3825: see quickfur's first comment on https://github.com/dlang/dmd/pull/1465.

@dlangBugzillaToGithub
Copy link
Author

stanislav.blinov commented on 2018-11-22T18:09:24Z

Now that we have an .update() for AAs, this needs to be resolved. This test should pass:

unittest {
    double[int] aa;
    aa[0]++;
    import std.math;
    assert(aa[0].isNaN);
}

The appropriate create/modify for non-zero-initialized types is:

unittest {
    double[int] aa;
    aa.update(0, { return 0.0; }, (ref double d) { d++; return d; });
    assert(aa[0] == 1.0);
}

Let's not let convenience defeat the type system, please.

@dlangBugzillaToGithub
Copy link
Author

dlang (@Vild) commented on 2019-08-27T18:34:04Z

In my oppinion aa[0]++ should behave exactly the same as aa[0] = aa[0]+1. Therefore in my oppinion a correct unittest would be:

unittest 
{
    import std.exception: assertThrown;
    import core.exception: RangeError;

    double[int] aa;
    assertThrown!RangeError(aa[0]++);
}

(At the moment with dmd v2.082.0 this leads to a segmentation fault, while
with ldc2 1.12.0 and gdc 8.3.0 I get the expected "assertThrown failed".)

@thewilsonator thewilsonator added Druntime Specific to druntime Druntime:AA Specific to Associative Arrays labels Dec 8, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Arch:x86 Issues specific to x86 Druntime:AA Specific to Associative Arrays Druntime Specific to druntime P3 Severity:normal
Projects
None yet
Development

No branches or pull requests

2 participants