www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - "with" statement and opDispatch()

reply Jacob Shtokolov <jacob.100205 gmail.com> writes:
Hi,

It seems that I found a bug in the compiler frontend, but I'm not 
completely sure that this is a bug, so first I'd like to ask 
people about that.

Given the description, the "with" statement is an equivalent of:

{
     Object tmp;
     tmp = expression;
     ...
     tmp.ident;
}

So, opDispatch() should probably be fine as well.
However, I found a case when it doesn't work as expected.

Let's take a code from the D Tour: 
https://tour.dlang.org/tour/en/gems/opdispatch-opapply

opDispatch() in this example is defined as a property, so there 
are two opDispatch() defined: one for "read" and another one for 
"write" cases.

But when I use this structure with the "with" statement (sorry 
for tautology), the compiler calls the wrong opDispatch().

Take a look at this example:

Shortened: https://run.dlang.io/is/pwbm0d
Gist: 
https://gist.github.com/run-dlang/a8fbbc5dc49346b887b9aaee8758ffca

Here is what I see in the AST info:

"""
with (test)
{
     (VariantN!32LU __tmpfordtor701 = ((VariantN!32LU 
__tmpfordtor700 = (*__withSym).opDispatch();) , 
__tmpfordtor700).opAssign(3.14);) , __tmpfordtor701;
}
writeln(test.opDispatch());
"""

Which means that instead of calling .opDispatch(val) to write a 
value, it calls opDispatch() to read a reference, and then 
opAssign() to set the value.

So the program throws a "Range Violation" exception.

I know that support for opDispatch() inside the "with" statement 
was added pretty recently: 
https://issues.dlang.org/show_bug.cgi?id=6400

However, it seems that the compiler generates different code for 
normal flow and for "with" statement, which is, probably, wrong.

Bug or not?
Should I create a bug report?

Thanks,
Jacob
Jan 14
parent reply Petar Kirov [ZombineDev] <petar.p.kirov gmail.com> writes:
On Monday, 14 January 2019 at 17:59:53 UTC, Jacob Shtokolov wrote:
 [..]
Nice catch!
 Bug or not?
Bug.
 Should I create a bug report?
Yes, please!
Jan 14
parent Jacob Shtokolov <jacob.100205 gmail.com> writes:
On Monday, 14 January 2019 at 20:43:35 UTC, Petar Kirov 
[ZombineDev] wrote:
 Nice catch!

 Bug or not?
Bug.
Thanks for your answer! Reported: https://issues.dlang.org/show_bug.cgi?id=19588
Jan 15