Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Manifest constant delegates are mutable #19503

Open
dlangBugzillaToGithub opened this issue Nov 4, 2018 · 1 comment
Open

Manifest constant delegates are mutable #19503

dlangBugzillaToGithub opened this issue Nov 4, 2018 · 1 comment

Comments

@dlangBugzillaToGithub
Copy link

lngns reported this on 2018-11-04T23:53:26Z

Transferred from https://issues.dlang.org/show_bug.cgi?id=19363

Description

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`
@dlangBugzillaToGithub
Copy link
Author

contact commented on 2018-11-05T00:01:53Z

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant