Issue 14624 - The array operator overloading fallback is not correct
Summary: The array operator overloading fallback is not correct
Status: RESOLVED FIXED
Alias: None
Product: D
Classification: Unclassified
Component: dmd (show other issues)
Version: D2
Hardware: All All
: P1 normal
Assignee: No Owner
URL:
Keywords: pull, wrong-code
: 14941 (view as issue list)
Depends on:
Blocks:
 
Reported: 2015-05-28 02:49 UTC by Kenji Hara
Modified: 2015-08-29 09:32 UTC (History)
2 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description Kenji Hara 2015-05-28 02:49:18 UTC
Test case:

struct A1
{
    int x;
    ref int opIndex() { return x; }
    ref int opSlice() { assert(0); }
}
void main()
{
    A1 a = A1(1);

    auto x = a[];       // a.opIndex(), OK
    assert(x == a.x);

    // When a.opIndexUnary!"-" is not found,
    // it should be rewritten to: -a.opIndex() rather than -a.opSlice()
    auto y = -a[];      // asserts in opSlice(), NG
    assert(y == -a.x);

    // When a.opIndexAssign(int) is not found,
    // it should be rewritten to: a.opIndex() = 1; rather than a.opSlice() = 1;
    a[] = 1;            // asserts in opSlice(), NG
    assert(a.x == 1);

    // When a.opIndexOpAssign!"+"(int) is not found,
    // it should be rewritten to: a.opIndex() += 1; rather than a.opSlice() += 1;
    a[] += 1;           // asserts in opSlice(), NG
    assert(a.x == 2);
}

I caught the issue in the d.learn forum thread that was posted a half year ago:
http://forum.dlang.org/post/luadir$t0g$1@digitalmars.com
Comment 2 Kenji Hara 2015-08-20 21:43:16 UTC
*** Issue 14941 has been marked as a duplicate of this issue. ***
Comment 3 Martin Nowak 2015-08-29 05:26:15 UTC
Why would we support an argument-less opIndex?
Comment 4 Martin Nowak 2015-08-29 05:41:16 UTC
I see it was introduced with multi-dimensional slicing support.
http://dlang.org/operatoroverloading#slice
Comment 5 github-bugzilla 2015-08-29 07:40:30 UTC
Commits pushed to stable at https://github.com/D-Programming-Language/dmd

https://github.com/D-Programming-Language/dmd/commit/bfb7f8d2a116578a57be29d87da3db010b565bb1
fix Issue 14624 - The array operator overloading fallback is not correct

https://github.com/D-Programming-Language/dmd/commit/d99699301eb0f5d46b4f9240236ca7c1694bb39b
Merge pull request #4948 from 9rnsr/fix14621

Issue 14621, 14624, 14625 - Fix bugs in array operator overloading semantics
Comment 6 github-bugzilla 2015-08-29 09:32:46 UTC
Commits pushed to master at https://github.com/D-Programming-Language/dmd

https://github.com/D-Programming-Language/dmd/commit/bfb7f8d2a116578a57be29d87da3db010b565bb1
fix Issue 14624 - The array operator overloading fallback is not correct

https://github.com/D-Programming-Language/dmd/commit/d99699301eb0f5d46b4f9240236ca7c1694bb39b
Merge pull request #4948 from 9rnsr/fix14621