Bug 20687 – Allow member function address as const initializer
Status
RESOLVED
Resolution
FIXED
Severity
normal
Priority
P3
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2020-03-19T22:41:06Z
Last change time
2023-05-10T03:45:33Z
Keywords
pull
Assigned to
No Owner
Creator
Boris Carvajal
Comments
Comment #0 by boris2.9 — 2020-03-19T22:41:06Z
Code:
struct S
{
void foo(){}
}
void main()
{
S i;
enum fp = &S.foo;
auto dg = &i.foo;
assert(fp == dg.funcptr);
// Allow this
static immutable dgfp = &S.foo;
assert(dgfp == dg.funcptr);
}
You can already use an enum.
https://forum.dlang.org/thread/[email protected]
Comment #1 by dlang-bot — 2020-03-19T23:15:29Z
@BorisCarvajal created dlang/dmd pull request #10946 "Fix Issue 20687 - Allow member function address as const initializer" fixing this issue:
- Fix Issue 20687 - Allow member function address as const initializer
https://github.com/dlang/dmd/pull/10946
Comment #2 by dlang-bot — 2020-03-20T05:55:45Z
dlang/dmd pull request #10946 "Fix Issue 20687 - Allow member function address as const initializer" was merged into stable:
- 4ad1a199e2d5a9a0b87b1513e5690b9e29a8ed54 by Boris Carvajal:
Fix Issue 20687 - Allow member function address as const initializer
https://github.com/dlang/dmd/pull/10946
Comment #3 by dlang-bot — 2020-03-22T09:44:26Z
dlang/dmd pull request #10948 "Revert "Fix Issue 20687 - Allow member function address as const initializer"" was merged into stable:
- bb766e125c590249730f967892bda6697b4a648b by Iain Buclaw:
Revert "Fix Issue 20687 - Allow member function address as const initializer"
https://github.com/dlang/dmd/pull/10948
Comment #4 by pro.mathias.lang — 2020-03-22T09:46:09Z
This has been re-opened, as it is not clear whether we want it to be possible or not.
The fact it's possible at runtime could be considered an accept-invalid, instead of this being a reject-valid.
Comment #5 by schveiguy — 2020-03-22T13:36:42Z
It's very clear to me it should be available at compile time and runtime. Whether it is callable on its own is a matter for debate, but that would be a breaking change.
If we made it unavailable at runtime, it is still available with &instance.fun.funcptr. What does funcptr return? If we started disallowing funcptr manipulation, then maybe this becomes invalid, but that is a whole new ball of code breakage that I don't think we want to get into right now.
Even if we disallowed creating a variable that accepts such a member-function pointer, a delegate has no type information for it's context pointer, so something like this would still be possible:
struct S
{
int x;
void foo() {++x;}
}
struct T
{
int *x;
void bar() {*x = 5;}
}
auto s = S(100000);
T t;
auto dg = &s.foo;
dg.funcptr = (&t.bar).funcptr;
dg(); // uses s.x as if it were a pointer
If we wanted to talk about type safety in this area, the status quo does not cut it. While not type-safe, at least it can be made consistent.
Comment #6 by dlang-bot — 2020-03-23T04:21:11Z
@BorisCarvajal created dlang/dmd pull request #10958 "Fix Issue 20687 - Allow member function address as const initializer" fixing this issue:
- Fix Issue 20687 - Allow member function address as const initializer
https://github.com/dlang/dmd/pull/10958
Comment #7 by dlang-bot — 2020-03-28T00:04:36Z
@MoonlightSentinel created dlang/dmd pull request #10970 "Merge branch 'master' into stable" fixing this issue:
- Fix Issue 20687 - Allow member function address as const initializer
- Merge pull request #10946 from BorisCarvajal/mptr_constinit
Fix Issue 20687 - Allow member function address as const initializer
merged-on-behalf-of: Nicholas Wilson <[email protected]>
- Revert "Fix Issue 20687 - Allow member function address as const initializer"
https://github.com/dlang/dmd/pull/10970
Comment #8 by dlang-bot — 2020-04-12T21:07:07Z
@MartinNowak created dlang/dmd pull request #11027 "merge stable" fixing this issue:
- Fix Issue 20687 - Allow member function address as const initializer
- Merge pull request #10946 from BorisCarvajal/mptr_constinit
Fix Issue 20687 - Allow member function address as const initializer
merged-on-behalf-of: Nicholas Wilson <[email protected]>
- Revert "Fix Issue 20687 - Allow member function address as const initializer"
https://github.com/dlang/dmd/pull/11027
Comment #9 by dlang-bot — 2020-04-21T08:50:26Z
dlang/dmd pull request #11027 "merge stable" was merged into master:
- 4ad1a199e2d5a9a0b87b1513e5690b9e29a8ed54 by Boris Carvajal:
Fix Issue 20687 - Allow member function address as const initializer
- aba2deaefdd3bb0ce9d3293b715959a6de416679 by The Dlang Bot:
Merge pull request #10946 from BorisCarvajal/mptr_constinit
Fix Issue 20687 - Allow member function address as const initializer
merged-on-behalf-of: Nicholas Wilson <[email protected]>
- 1f3245d3241d90f4d74b9398558ddc71d57335c4 by Iain Buclaw:
Revert "Fix Issue 20687 - Allow member function address as const initializer"
https://github.com/dlang/dmd/pull/11027
Comment #10 by dlang-bot — 2023-05-10T03:45:33Z
dlang/dmd pull request #10958 "Fix Issue 20687 - Allow member function address as const initializer" was merged into master:
- 7bcf238b606efcc9808c795447adca9ea47b4e3a by Boris Carvajal:
Fix Issue 20687 - Allow member function address as const initializer of static data
https://github.com/dlang/dmd/pull/10958