D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 21878 - "ref" lost when indexing array in CTFE
Summary: "ref" lost when indexing array in CTFE
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: CTFE, pull
Depends on:
Blocks:
 
Reported: 2021-04-29 17:36 UTC by Vladimir Panteleev
Modified: 2021-04-30 04:28 UTC (History)
0 users

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description Vladimir Panteleev 2021-04-29 17:36:17 UTC
///////////////////////////////// test.d ////////////////////////////////
struct A
{
    int i;
    ref inout(int) opIndex(size_t idx) inout return { return i; }
}

struct B
{
    A[1] a;
    ref inout(int) opIndex(size_t idx) inout return { return a[0][idx]; }
}

bool ctfeFunc()
{
    A a;
    a[0] = 42;
    assert(a[0] == 42); // OK

    B b;
    b[0] = 42;
    assert(b[0] == 42); // fails

    return true;
}
enum eval = ctfeFunc();

unittest
{
    ctfeFunc(); // succeeds at runtime
}
/////////////////////////////////////////////////////////////////////////

It looks like the assignment is going into some rvalue which is then lost.
Comment 1 Vladimir Panteleev 2021-04-29 17:39:39 UTC
I should add that `inout` is also somehow to blame. Works fine without it.
Comment 2 Dlang Bot 2021-04-30 03:26:59 UTC
@BorisCarvajal created dlang/dmd pull request #12487 "Fix Issue 21878 - "ref" lost when indexing array in CTFE" fixing this issue:

- Fix Issue 21878 - "ref" lost when indexing array in CTFE

https://github.com/dlang/dmd/pull/12487
Comment 3 Dlang Bot 2021-04-30 04:28:03 UTC
dlang/dmd pull request #12487 "Fix Issue 21878 - "ref" lost when indexing array in CTFE" was merged into master:

- 8cd0a729c724eb7d85ed853b064982ae61f19adf by Boris Carvajal:
  Fix Issue 21878 - "ref" lost when indexing array in CTFE

https://github.com/dlang/dmd/pull/12487