Skip to content

Comments

Forbid incompatible foreach parameter types for AAs#21463

Closed
ntrel wants to merge 3 commits intodlang:masterfrom
ntrel:aa-foreach
Closed

Forbid incompatible foreach parameter types for AAs#21463
ntrel wants to merge 3 commits intodlang:masterfrom
ntrel:aa-foreach

Conversation

@ntrel
Copy link
Contributor

@ntrel ntrel commented Jun 21, 2025

Fixes #21456.

This errors unless both:

  • the AA component types implicitly convert to the respective foreach parameter types
  • each of those type checks have types the same size

Although it would be better to support larger foreach parameter types, this prevents corruption for now.

Also improve error messages (see commits).

@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#21463"

@ntrel ntrel marked this pull request as ready for review June 21, 2025 13:25
Type taav = taa.nextOf();
if (isRef ? !taav.constConv(ta) : !taav.implicitConvTo(ta))
// TODO don't require sizes to match
if (isRef ? !taav.constConv(ta) : !taav.implicitConvTo(ta) || taav.size != ta.size)
Copy link
Member

Choose a reason for hiding this comment

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

I don't think the comparison of the size is correct, the generated lambda should accept the correct parameter types instead. I have patched it here in foreachBodyToFunction: d2b3b00#diff-11fb0212c55c05ed6e743e6776abb3c56996381760c8725255c622f96f5d9d4bR4032, but that is part of the template implementation of AAs. Sorry, linking into the diff doesn't seem to work well.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

OK, so it looks like #21456 is fixed? I can split the error message part out of this PR.

Copy link
Member

Choose a reason for hiding this comment

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

It's not merged yet, but I can extract the fix to another PR. This will also cut down on #21066 a tiny bit.

Copy link
Member

Choose a reason for hiding this comment

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

see #21469

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.

foreach over associative array allows incompatible types for key and value

3 participants