D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 19363 - Manifest constant delegates are mutable
Summary: Manifest constant delegates are mutable
Status: NEW
Alias: None
Product: D
Classification: Unclassified
Component: dmd (show other issues)
Version: D2
Hardware: All All
: P3 minor
Assignee: No Owner
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2018-11-04 23:53 UTC by lngns
Modified: 2024-12-13 19:01 UTC (History)
0 users

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description lngns 2018-11-04 23:53:26 UTC
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`
Comment 1 lngns 2018-11-05 00:01:53 UTC
Actually, `const u = &(new C().foo);` fails even if C.foo does not mutate its object and is annotated const.
So I would suggest failing only for non-const methods, though that may be a bigger change.
Comment 2 dlangBugzillaToGithub 2024-12-13 19:01:10 UTC
THIS ISSUE HAS BEEN MOVED TO GITHUB

https://github.com/dlang/dmd/issues/19503

DO NOT COMMENT HERE ANYMORE, NOBODY WILL SEE IT, THIS ISSUE HAS BEEN MOVED TO GITHUB