[Issue 19363] New: Manifest constant delegates are mutable
d-bugmail at puremagic.com
d-bugmail at puremagic.com
Sun Nov 4 23:53:26 UTC 2018
https://issues.dlang.org/show_bug.cgi?id=19363
Issue ID: 19363
Summary: Manifest constant delegates are mutable
Product: D
Version: D2
Hardware: x86
OS: Windows
Status: NEW
Severity: enhancement
Priority: P1
Component: dmd
Assignee: nobody at puremagic.com
Reporter: contact at lngnslnvsk.net
The compiler does not check whether the function used to form a delegate
mutates its context or not when declaring a manifest constant.
This should not work:
```
class C
{
int member;
int foo()
{
member++;
return 123 + member;
}
}
enum u = &(new C().foo);
void main()
{
import std.stdio : writeln;
writeln(u()); // 124
writeln(u()); // 125
writeln(u()); // 126
writeln(u()); // 127
}
```
This was found by this user:
https://forum.dlang.org/post/cfjftxwgnlaovaolbhop@forum.dlang.org
The expected behavior would be the statement `enum u = &(new C().foo);` failing
for the expression `&(new C().foo)` is not constant, akin to assigning to
const.
`const u = &(new C().foo);` => `Error: expression &C(0).foo is not a constant`
--
More information about the Digitalmars-d-bugs
mailing list