Skip to content

Fix issue #21630 - non-static range foreach ignores enum and alias - #21633

Closed
Bolpat wants to merge 1 commit into
dlang:masterfrom
Bolpat:ForeachEnum
Closed

Fix issue #21630 - non-static range foreach ignores enum and alias#21633
Bolpat wants to merge 1 commit into
dlang:masterfrom
Bolpat:ForeachEnum

Conversation

@Bolpat

@Bolpat Bolpat commented Aug 1, 2025

Copy link
Copy Markdown
Contributor

Fixes #21630

@tgehr, my implementation does it in the parser, which allows pointing at the offending keyword enum or alias.

@dlang-bot

Copy link
Copy Markdown

Thanks for your pull request and interest in making D better, @Bolpat! 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#21633"

@Bolpat

Bolpat commented Aug 1, 2025

Copy link
Copy Markdown
Contributor Author

The tests fail because there’s also foreach (alias x; AliasSeq!(…)) which is fine, but the parser can’t know if the aggregate will be a sequence or not. This is annoying. A semantics-based approach suffers from the fact that it doesn’t remember if foreach or static foreach was used.

The parser can only reliably reject non-static range foreach with enum or alias, i.e. foreach (enum x; 0 .. 1), but not aggregate foreach.

@tgehr

tgehr commented Aug 1, 2025

Copy link
Copy Markdown
Contributor

Fair, tried to fix the underlying issue at #21631 .

@Bolpat

Bolpat commented Aug 1, 2025

Copy link
Copy Markdown
Contributor Author

Maybe this is salvageable. Before DIP 1010 introduced static foreach, regular sequence-based foreach didn’t allow specifying alias or enum, i.e. foreach (alias a; AliasSeq!()) { } was simply illegal. You had to write foreach (a; AliasSeq!()) { }, which means that code should generally fall into two categories:

  • Code written for compiler < 2.076.0 (excluding) couldn’t have used alias or enum on such loops.
  • Code written for 2.076.0 or later would likely use static foreach anyway.

Code written for 2.076.0 or later that uses alias/enum in foreach without static should actually be rare. Maybe it’s very little breakage.

@Bolpat Bolpat closed this Aug 5, 2025
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.

enum and alias ignored on runtime foreach loop variables

3 participants