"with" statement and opDispatch()
Jacob Shtokolov
jacob.100205 at gmail.com
Mon Jan 14 17:59:53 UTC 2019
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
More information about the Digitalmars-d
mailing list