Cast converts AA to rvalue?

Steven Schveighoffer schveiguy at gmail.com
Wed Aug 10 00:28:53 UTC 2022


On 8/9/22 7:02 PM, Johan wrote:
> Testcase:
> ```
> shared int[int] aa;
> void main () {
>      cast()aa[1] = 1;
> }
> ```
> 
> Up to dlang 2.097, this program runs and works fine.
> Since dlang 2.098, the program errors with:
> `core.exception.RangeError@/app/example.d(3): Range violation`
> 
> I think the 2.098+ behavior is correct, but I cannot find anything about 
> the change of this language behavior in the release notes.

So what is happening is you are casting away shared on the expression 
`aa[1]`. This expression by itself is an *access* of a value, not an 
*assignment*. This is consistent for structs defining both opIndex and 
opIndexAssign (the expression calls `opIndex`, not `opIndexAssign`), as 
far back as I can test.

If you use `cast()(aa[1]) = 1`, it has a range error even on older versions.

That it ever worked is puzzling.

-Steve


More information about the Digitalmars-d-learn mailing list