Bug 18863 – opDispatch with WithStatement & Template Instance
Status
RESOLVED
Resolution
DUPLICATE
Severity
enhancement
Priority
P1
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2018-05-15T20:30:11Z
Last change time
2018-05-16T16:38:02Z
Assigned to
No Owner
Creator
John Hall
Comments
Comment #0 by john.michael.hall — 2018-05-15T20:30:11Z
The with statement does not check opDispatch with template instances. An older bug (#6400) was resolved fixing the case of a symbol. This enhancement is for template instances.
See below:
struct Foo(int x)
{
auto opDispatch(string s)()
if (s == "bar")
{
x++;
return x;
}
}
void main()
{
int y = 0;
with(Foo!1)
{
y = bar; //error: undefined identifier bar
}
assert(y == 2);
}
By contrast, the following compiles without error
struct Foo
{
int x;
auto opDispatch(string s)()
if (s == "bar")
{
x++;
return x;
}
}
void main()
{
int y = 0;
with(Foo(1))
{
y = bar;
}
assert(y == 2);
}
Discussion thread:
https://forum.dlang.org/thread/[email protected]
Comment #1 by simen.kjaras — 2018-05-16T08:56:52Z
You've got a bug in your code: ++x has to fail for the template case, since you're trying to increment a compile-time value. This is what the call to bar is lowered to:
Foo!1.opDispatch!"bar"()
When you try and compile that, you get these error messages:
Error: cannot modify constant 1
Error: template instance `foo.Foo!1.Foo.opDispatch!"bar"` error instantiating
Comment #2 by nick — 2018-05-16T16:38:02Z
*** This issue has been marked as a duplicate of issue 14145 ***