Skip to content

Conversation

@ntrel
Copy link
Contributor

@ntrel ntrel commented Aug 13, 2025

When assignment would call an operator overload.
Fixes #21507.

Note: This does not disallow calling e.g. opAssign on an rvalue, only actual assignment.

@dlang-bot
Copy link
Contributor

Thanks for your pull request and interest in making D better, @ntrel! We are looking forward to reviewing it, and you should be hearing from a maintainer soon.
Please verify that your PR follows this checklist:

  • My PR is fully covered with tests (you can see the coverage diff by visiting the details link of the codecov check)
  • My PR is as minimal as possible (smaller, focused PRs are easier to review than big ones)
  • I have provided a detailed rationale explaining my changes
  • New or modified functions have Ddoc comments (with Params: and Returns:)

Please see CONTRIBUTING.md for more information.


If you have addressed all reviews or aren't sure how to proceed, don't hesitate to ping us with a simple comment.

Bugzilla references

Your PR doesn't reference any Bugzilla issue.

If your PR contains non-trivial changes, please reference a Bugzilla issue or create a manual changelog.

⚠️⚠️⚠️ Warnings ⚠️⚠️⚠️

  • In preparation for migrating from Bugzilla to GitHub Issues, the issue reference syntax has changed. Please add the word "Bugzilla" to issue references. For example, Fix Bugzilla Issue 12345 or Fix Bugzilla 12345.(Reminder: the edit needs to be done in the Git commit message, not the GitHub pull request.)

Testing this PR locally

If you don't have a local development environment setup, you can use Digger to test this PR:

dub run digger -- build "master + dmd#21717"

@ntrel ntrel force-pushed the struct-assign-rvalue branch from e36b3b6 to 61ce191 Compare August 13, 2025 15:00
@ntrel

This comment was marked as outdated.

@ntrel ntrel force-pushed the struct-assign-rvalue branch from aa52efa to 05f17a1 Compare August 13, 2025 16:23
Comment on lines 10750 to 10752
if (!sc.intypeof && exp.e1.type.ty == Tstruct && !exp.e1.isLvalue())
{
error(exp.e1.loc, "cannot modify struct rvalue `%s`",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What other rvalues could appear here? Does it make sense to only restrict structs?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The check in opOverloadBinaryAssign would get hit by:

    int[] a;
    a.length += 1;

It would also get hit for a class C (new C) += 4;. That should be prevented too, but I don't want to do that in this pull.

@kinke
Copy link
Contributor

kinke commented Aug 15, 2025

rvalues cannot be assigned to by definition, correct?

@ntrel
Copy link
Contributor Author

ntrel commented Aug 15, 2025

I snipped the buildkite failures below. The libdparse one is fixed upstream. The other projects are failing due to one of these dependency failures.

    Building vibe-d:redis ~master: building configuration [vibe-d-redis-test-library]
redis/vibe/db/redis/idioms.d(74,8): Error: cannot assign to struct rvalue `this.opIndex(id)`
            this[id] = args;
                ^

phobos:
../dmd/generated/linux/release/64/dmd -conf= -I../dmd/druntime/import  -w -preview=dip1000 -preview=dtorfields -preview=fieldwise -m64 -fPIC -O -release -unittest -version=StdUnittest -c -ofgenerated/linux/release/64/unittest/std/container/array.o std/container/array.d
make[2]: *** [Makefile:407: generated/linux/release/64/unittest/std/container/array.o] Segmentation fault (core dumped)

ae:
net/ietf/message.d(347,7): Error: cannot assign to struct rvalue `list(this.author, this.authorEmail)`
        list(author, authorEmail) = decodeAuthor("From" in headers ? decodeRfc1522(headers["From"]) : null);
            ^
net/ietf/message.d(350,8): Error: cannot assign to struct rvalue `list(null, this.authorEmail)`
            list(null, authorEmail) = decodeAuthor(decodeRfc1522(headers["Reply-To"].findSplit(", ")[0]));
                ^
net/smtp/client.d(159,7): Error: cannot assign to struct rvalue `list(codeStr, null, line)`
        list(codeStr, null, line) = line.findSplit(" ");
            ^

Compiling parse tester... ../src/dparse/parser.d(8981): Error: cannot modify struct rvalue `checked(this.suppressMessages.back())`
            ++suppressMessages.back.checked;
                                   ^

../.dub/packages/mir-algorithm/3.22.4/mir-algorithm/source/mir/bignum/fixed.d(966,23): Error: cannot assign to struct rvalue `a.view()`
    size_t overflow = a.view *= b;
                      ^
../.dub/packages/mir-algorithm/3.22.4/mir-algorithm/source/mir/bignum/fixed.d(1063,61): Error: cannot assign to struct rvalue `view.topLeastSignificantPart(1LU)`
    view.coefficients[$ - 1] += view.topLeastSignificantPart(a.data.length) += c.view;
                                                            ^

@ntrel
Copy link
Contributor Author

ntrel commented Aug 15, 2025

rvalues cannot be assigned to by definition, correct?

Currently dmd allows it for a struct overloading assignment.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Calling opAssign on an rvalue should be illegal

5 participants