Skip to content

Calling opAssign on an rvalue should be illegal #21507

@jmdavis

Description

@jmdavis
void main ()
{
    foo() = 42;
}

int foo()
{
    return 0;
}

gives the error

q.d(3): Error: cannot modify expression `foo()` because it is not an lvalue
    foo() = 42;
       ^

However,

void main ()
{
    foo() = S.init;
}

S foo()
{
    return S.init;
}

struct S
{
    int i;

    void opAssign(S s)
    {
        this.i = s.i;
    }
}

compiles just fine.

I don't see why overloading opAssign should behave any differently in this respect than the built-in assignment does. It just makes it so that you can accidentally assign to an rvalue and have the result disappear on you without getting any errors about doing something nonsensical.

opOpAssign has the same problem: #19061

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions