//----
struct S{int i;}
S foo();
void main()
{
foo().i = 5;
int* p = &foo().i;
}
//----
The above compiles just fine. However, (AFAIK) it is an error: The members of an rvalue should themselves be rvalues. (or would that be an ER?)
I think both the examples above should be rejected.
It has sparked bugs such as:
http://d.puremagic.com/issues/show_bug.cgi?id=11889
Comment #1 by andrei — 2014-01-13T11:49:22Z
Yah, I think we need to fix this (even if we disallow the last line).
Comment #2 by monarchdodra — 2014-05-20T21:22:58Z
I re-encountered this problem with this new usecase. That code don't make no sense to me:
//----
struct S
{
int i;
}
enum a = [1, 2, 3];
enum b = S(1);
void foo(ref int b)
{
b = 5;
}
void main()
{
foo(a[1]);
foo(b.i);
}
//----
Comment #3 by razvan.nitu1305 — 2018-11-29T13:36:46Z
Have to be careful, because we want things like this to work:
void bar(ref T t);
T foo();
foo().bar()
Comment #5 by andrei — 2018-12-07T13:33:53Z
@Walter are you sure? You code does not work today.
Comment #6 by bugzilla — 2018-12-09T03:43:01Z
(In reply to Andrei Alexandrescu from comment #5)
> @Walter are you sure? You code does not work today.
I seem to recall that this has been discussed at length in the past, and the consensus was that it should work.
I'm surprised that it apparently was never implemented.
Clearly, this issue needs some investigation.
Comment #7 by razvan.nitu1305 — 2019-10-28T09:03:33Z
*** Issue 19507 has been marked as a duplicate of this issue. ***
Comment #8 by b2.temp — 2023-10-15T14:34:06Z
It's actually specified to be a lvalue:
> The following expressions, and no others, are called lvalue expressions or lvalues:
>
> [...]
>
> the result of the . PostfixExpression or Module Scope Operator when the rightmost side of the dot is a variable, field (direct or static), function name, or invocation of a function that returns by reference;
https://dlang.org/spec/expression.html#.define-lvalue
Comment #9 by b2.temp — 2023-12-16T21:45:33Z
*** Issue 22290 has been marked as a duplicate of this issue. ***
Comment #10 by nick — 2023-12-17T21:08:28Z
With dmd v2.106.0-rc.1, inside @safe and with -dip1000:
foo().i = 5; // still allowed
int* p = &foo().i; // error, address assigned to longer lived variable
Comment #3:
foo(a[1]); // still allowed, should fail
foo(b.i); // error, cannot pass rvalue, same for comment #4
Comment #11 by robert.schadek — 2024-12-13T18:16:01Z