dlang.org icon indicating copy to clipboard operation
dlang.org copied to clipboard

Allow `ref` and `auto ref` in `IfCondition` declarations

Open Bolpat opened this issue 1 year ago • 1 comments

As DIP 1046 (ref For Variable Declarations) has been accepted, probably, ref (and auto ref) should be allowed for the condition in if, while, and switch statements:

int** pptr = …;
if (ref int* ptr = *pptr) …;

Of course, the condition tests whether the variable (ptr in the example) is true-like, not whether the reference is a non-null reference. In the example, it tests ptr !is null, and not &ptr !is null (or, equivalently, pptr !is null).

if (auto ref x = mystery()) …;

Especially in template contexts, auto ref could be used to make x be a ref if mystery() returns by reference, but a value otherwise.

Bolpat avatar Jun 28 '24 14:06 Bolpat

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.

dlang-bot avatar Jun 28 '24 14:06 dlang-bot

This doesn't account for combinations, these also parse currently:

if (scope ref x = b)
if (const ref x = b)
if (scope const ref x = b)
if (auto scope ref const x = b)

dkorpel avatar Jul 05 '24 10:07 dkorpel

I didn’t know auto and ref allowed other tokens between them. But apparently, they do.

I just found out, while auto and ref commute almost everywhere, they are required in this exact order for lambda expressions; there, they don’t commute. Probably, they shouldn’t commute anywhere as that is hard to understand.

Bolpat avatar Jul 05 '24 13:07 Bolpat

@dkorpel, it it fine? Merge?

Bolpat avatar Jul 05 '24 15:07 Bolpat

The current formulation doesn't allow if (const ref x = y)

dkorpel avatar Jul 05 '24 16:07 dkorpel

The current formulation

What do you mean? The grammar before the PR or the grammar after the latest commit in this PR?

The latter, as I see it, does allow it. The current grammar, of course, does not allow it, but the whole point of the PR is to allow it.

Bolpat avatar Jul 08 '24 13:07 Bolpat

I misread it

No issue. I misread a lot, actually.

Bolpat avatar Jul 08 '24 16:07 Bolpat

I don't think if (ref x = y) actually compiles yet. https://github.com/dlang/dmd/pull/16428 is not merged, and I don't know if it covers that case.

ntrel avatar Jul 19 '24 10:07 ntrel

I don't think if (ref x = y) actually compiles yet. dlang/dmd#16428 is not merged, and I don't know if it covers that case.

The spec technically isn’t supposed to document DMD’s behavior, but what DMD’s behavior ought to be. And it ought to be because the respective DIP was accepted.

Bolpat avatar Jul 23 '24 13:07 Bolpat

@Bolpat I think it's confusing. There should at least be a note that the reference compiler doesn't implement it.

it ought to be because the respective DIP was accepted.

The DIP didn't mention if (ref?

ntrel avatar Jul 23 '24 17:07 ntrel

The DIP didn't mention if (ref?

The DIP is written rather incomplete. It’s of course intended.

I just tried out and if (ref works. if (auto ref does not work, but probably ought to.

Bolpat avatar Jul 24 '24 17:07 Bolpat

Ok, thanks. The dmd pull is merged now anyway.

ntrel avatar Jul 24 '24 18:07 ntrel