Bug 22519 – [dip1000] cannot take address of `ref return`
Status
RESOLVED
Resolution
FIXED
Severity
enhancement
Priority
P1
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2021-11-18T10:00:45Z
Last change time
2021-11-26T01:19:39Z
Keywords
pull
Assigned to
No Owner
Creator
Dennis
Comments
Comment #0 by dkorpel — 2021-11-18T10:00:45Z
Compile with -preview=dip1000
```
@safe:
void main()
{
int x;
int* a = &identity(x); // error
int* b = identity(x).addressOf; // workaround
}
ref int identity(return ref int x) {return x;}
int* addressOf(return ref int x) {return &x;}
```
> onlineapp.d(5): Error: cannot take address of `ref return` of `identity()`
> in `@safe` function `main`
This is a needless limitation. As shown, it can be worked around using the `addressOf` function instead of the & operator.
Comment #1 by dlang-bot — 2021-11-23T20:03:03Z
@dkorpel created dlang/dmd pull request #13351 "Fix Issue 22519 - cannot take address of `ref return`" fixing this issue:
- Fix Issue 22519 - cannot take address of `ref return`
https://github.com/dlang/dmd/pull/13351
Comment #2 by stanislav.blinov — 2021-11-25T20:22:36Z
How does this interact with -preview=rvaluerefparam ?
I smell a bug with that `addressOf` implementation, though it probably should be addressed in the implementation of the abovementioned switch.
Comment #3 by dkorpel — 2021-11-25T20:47:16Z
(In reply to Stanislav Blinov from comment #2)
> How does this interact with -preview=rvaluerefparam ?
I thought rvaluerefparam was syntactic sugar for creating a local variable just before the function call and passing that to the function parameter, so it should work just fine.
I also agree that it should be the concern of the rvaluerefparam implementation, especially since it's not in the language yet and the DIP for it hasn't even reached draft review.
Comment #4 by dlang-bot — 2021-11-26T01:19:39Z
dlang/dmd pull request #13351 "Fix Issue 22519 - cannot take address of `ref return`" was merged into master:
- 3f1c763b9ed58f323550bd6d99e3dec031016f7b by dkorpel:
Fix Issue 22519 - cannot take address of `ref return`
https://github.com/dlang/dmd/pull/13351