[Issue 17729] New: dmd says cast expression is "not an lvalue", but it can be used as one in other contexts

via Digitalmars-d-bugs digitalmars-d-bugs at puremagic.com
Mon Aug 7 15:12:28 PDT 2017


https://issues.dlang.org/show_bug.cgi?id=17729

          Issue ID: 17729
           Summary: dmd says cast expression is "not an lvalue", but it
                    can be used as one in other contexts
           Product: D
           Version: D2
          Hardware: All
                OS: All
            Status: NEW
          Severity: normal
          Priority: P1
         Component: dmd
          Assignee: nobody at puremagic.com
          Reporter: ag0aep6g at gmail.com

As requested by Andrei on GitHub
(<https://github.com/dlang/druntime/pull/1605#discussion_r131770687>).

Taking the address of a cast expression gets rejected:

----
struct S { int field = 0; }
void main()
{
    shared S s;
    auto p = & cast(S) s;
        /* Error: cast(S)s is not an lvalue */
}
----

But we can take the address of the field and it's the same as `s`'s:

----
struct S { int field = 0; }
void main()
{
    shared S s;
    auto p = &(cast(S) s).field;
    *p = 1;
    assert(s.field == 1);
}
----

We can even use the cast expression on the left-hand side of an assignment:

----
struct S { int field = 0; }
void main()
{
    shared S s;
    cast(S) s = S(1); /* no error */
    assert(s.field == 1); /* passes */
}
----

Either all snippets should be rejected, or they should all be accepted.

--


More information about the Digitalmars-d-bugs mailing list