D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 19211 - get the type of a non-const delegate in a const function
Summary: get the type of a non-const delegate in a const function
Status: RESOLVED INVALID
Alias: None
Product: D
Classification: Unclassified
Component: dmd (show other issues)
Version: D2
Hardware: All All
: P1 enhancement
Assignee: No Owner
URL:
Keywords: diagnostic, rejects-valid
Depends on:
Blocks:
 
Reported: 2018-09-02 04:41 UTC by basile-z
Modified: 2019-12-23 09:39 UTC (History)
1 user (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description basile-z 2018-09-02 04:41:12 UTC
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 Walter Bright 2018-12-13 09:28:43 UTC
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 basile-z 2019-12-23 09:39:01 UTC
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.