[Issue 19754] cast() sometimes yields lvalue, sometimes yields rvalue

d-bugmail at puremagic.com d-bugmail at puremagic.com
Thu Mar 28 13:56:17 UTC 2019


https://issues.dlang.org/show_bug.cgi?id=19754

--- Comment #10 from RazvanN <razvan.nitu1305 at gmail.com> ---
(In reply to Andrei Alexandrescu from comment #8)
> It seems the main anomaly is that cast() yields (sometimes...) an lvalue for
> shared data. Razvan, can you investigate if code would break if we forced
> cast() on shared data to always return an rvalue?

The intention in the compiler code is that casts should always return rvalues,
however the optimizer can simply bypass the check sometimes. For example:

const(int) x;
cast()x = 5;

Here the optimizer sees that x is a constant and rewrites the cast to
`cast(int)0`; later on when the lhs expression is analyzed to see if it is
modifiable you get the confusing error that Mathias pointed out. In the case of
`shared x` it cannot constfold x since the variable could be modified from
another thread so it simply drops `shared` from the lhs type. I would argue
that an optimizing pass should be done only after semantic is done.

It seems that the simplest way to deal with these cases is to simply allow cast
expressions to return lvalue in some situations; these situations are already
described in the spec [1], under the "Lvalue" section.

[1] https://dlang.org/spec/expression.html#definitions-and-terms

--


More information about the Digitalmars-d-bugs mailing list