Bug 19211 – get the type of a non-const delegate in a const function

Status
RESOLVED
Resolution
INVALID
Severity
enhancement
Priority
P1
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2018-09-02T04:41:12Z
Last change time
2019-12-23T09:39:01Z
Keywords
diagnostic, rejects-valid
Assigned to
No Owner
Creator
Basile-z

Comments

Comment #0 by b2.temp — 2018-09-02T04:41:12Z
This used to be possible: struct Foo { void func1(){} void func2() const {alias Fun = typeof(&func1);} } until FE 2.072 but now produces the following error message: > Error: mutable method `Foo.func1` is not callable using a `const` `this` What's sure: - this should work always inside `typeof()` since the intention is not to use the delegate. - the error message is wrong for the same reason. What's less sure: - should `&func1` always working as long as not called ? For now the following workaround has to be used: alias Fun = typeof(&(cast()this).func1);
Comment #1 by bugzilla — 2018-12-13T09:28:43Z
I'm not so sure this is a good idea to change this, as changing it could destabilize the type checking inside the typeof(). (It is erroneous code - the type checking is working as designed.) The error message is not exactly wrong, though it could be improved. Changed to enhancement request.
Comment #2 by b2.temp — 2019-12-23T09:39:01Z
let's close. one can do, instead of using &func1 which is contaminated by transitive const. --- struct Foo { void func1(){} void func2() const {alias Fun = typeof(&(new Foo).func1);} } --- The problem pointed by the original report is more that people don't understand how `const` works, i.e in a `const` function, `this` becomes `const` and is propagated by transitivity, which is even something I pointed in a blog post in after reporting.