Bug 21369 – Compiler doesn't allow lvalues via alias this when wrapper is an rvalue

Status
NEW
Severity
normal
Priority
P3
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2020-11-07T14:35:32Z
Last change time
2024-12-13T19:12:31Z
Keywords
rejects-valid
Assigned to
No Owner
Creator
Steven Schveighoffer
Moved to GitHub: dmd#19816 →

Comments

Comment #0 by schveiguy — 2020-11-07T14:35:32Z
Consider the following struct: struct S { int *val; ref int get() { return *val; } } void foo(ref int x) {} void main() { int x; foo(S(&x).get); } This builds fine. However, using alias this to forward to the get function: struct S { int *val; ref int get() { return *val; } alias get this; } void foo(ref int x) {} void main() { int x; foo(S(&x)); } I get: onlineapp.d(15): Error: function onlineapp.foo(ref int x) is not callable using argument types (S) onlineapp.d(15): cannot pass rvalue argument S(& x) of type S to parameter ref int x Clearly, the alias this should work, especially since if I call the aliased member directly it does work. This is a block for creating correct smart reference types.
Comment #1 by b2.temp — 2021-05-08T15:46:01Z
The problem is likely the use of `Type.aliasthisOf()` which returns a `Type`, so for a function this is TypeFunction.nextOf() and as `ref` is not a type qual, the information is lost and DMD thinks it's a rvalue.
Comment #2 by apz28 — 2021-05-08T19:41:27Z
Create temp var is OK struct S { int *val; ref int get() { return *val; } alias get this; } void foo(ref int x) {} void main() { int x; auto s = S(&x); foo(s); }
Comment #3 by schveiguy — 2021-05-11T13:53:26Z
(In reply to apham from comment #2) > Create temp var is OK That doesn't trigger the bug because then s is an lvalue.
Comment #4 by robert.schadek — 2024-12-13T19:12:31Z
THIS ISSUE HAS BEEN MOVED TO GITHUB https://github.com/dlang/dmd/issues/19816 DO NOT COMMENT HERE ANYMORE, NOBODY WILL SEE IT, THIS ISSUE HAS BEEN MOVED TO GITHUB