[Issue 22979] New: "Pointers-to-member" with attributes

d-bugmail at puremagic.com d-bugmail at puremagic.com
Sun Apr 3 16:43:48 UTC 2022


https://issues.dlang.org/show_bug.cgi?id=22979

          Issue ID: 22979
           Summary: "Pointers-to-member" with attributes
           Product: D
           Version: D2
          Hardware: x86_64
                OS: Mac OS X
            Status: NEW
          Severity: normal
          Priority: P1
         Component: dmd
          Assignee: nobody at puremagic.com
          Reporter: josipp at live.de

Hi all, I am aware of the nonsensical delegate -> function conversion
sequence for non-static member functions, as demonstrated by
```d

class Class { int doubling(int x) { return x * 2; } }
void main()
{
    int function(int) fptr = &Class.doubling;  // typechecks!
}

```

However, there is some deeper unsound compiler reasoning going on.

Consider
```d

//                                vvvvv notice the member fn attribute
class Class { int doubling(int x) inout { return x * 2; } }

void main()
{
    int function(int) fptr = &Class.doubling;
}

```

dmd fails with
> Error: cannot implicitly convert expression `& doubling`
         of type `int function(int x) inout` to `int function(int)`

Adjust the type of fptr it shall be then. Alas... no.

```d
[...]

void main()
{
    int function(int) inout fptr = &Class.doubling;
}
```

fails with
> Error: `const`/`immutable`/`shared`/`inout`/`return` attributes
         are only valid for non-static member functions

But when done with `auto`, it says

```d
[...]

void main()
{
    auto fptr = &Class.doubling;
    pragma(msg, typeof(fptr));  // int function(int) inout
}
```

What gives?

--


More information about the Digitalmars-d-bugs mailing list