www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 22216] New: Incomplete/incorrect error message for mutability

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

          Issue ID: 22216
           Summary: Incomplete/incorrect error message for mutability
                    overloads
           Product: D
           Version: D2
          Hardware: All
                OS: All
            Status: NEW
          Keywords: diagnostic
          Severity: minor
          Priority: P1
         Component: dmd
          Assignee: nobody puremagic.com
          Reporter: schveiguy gmail.com

If you have a struct with a single constructor that is const:

```d
struct S
{
   this(int x) const {}
}

void main()
{
   auto s = S(1);
}
```

Result:

Error: constructor `foo.S.this(int x) const` is not callable using argument
types `(int)`

But if I add an overload for the constructor that also doesn't match:

```d
struct S
{
   this(int x) const {}
   this(string x) {}
}

void main()
{
   auto s = S(1);
}
```

The error message is missing the key `const` modifier for `this`:

foo.d(9): Error: none of the overloads of `this` are callable using argument
types `(int)`, candidates are:
foo.d(3):        `foo.S.this(int x)`
foo.d(4):        `foo.S.this(string x)`

The error for line 3 should be:

foo.d(3):        `foo.S.this(int x) const`

This problem exists also for normal methods that have overloads, but is not as
noticeable since you can call const functions on non-const objects. Though it
still can happen for immutable overloads:

```d
struct S
{
    void foo(int x) immutable {}
    void foo(string x) {}
}

void main()
{
    S s;
    s.foo(1);
}
```

foo.d(9): Error: none of the overloads of `foo` are callable using a mutable
object, candidates are:
foo.d(2):        `foo.S.foo(int x)`
foo.d(3):        `foo.S.foo(string x)`

The "using a mutable object" thing isn't exactly true, the string version is
callable using a mutable object. Which seems like a hack anyway. If I reverse
the situation, again we are missing the modifiers:

```d
struct S
{
    void foo(int x) immutable  {}
    void foo(string x) {}
}

void main()
{
    immutable S s;
    s.foo("hi");
}
```

foo.d(9): Error: none of the overloads of `foo` are callable using argument
types `(string) immutable`, candidates are:
foo.d(2):        `foo.S.foo(int x)`
foo.d(3):        `foo.S.foo(string x)`

Note now that it lists the types being called with, including the `immutable`
this parameter.

But if I reverse the overload order, the message is different:

```d
struct S
{
    void foo(string x) {}
    void foo(int x) immutable {}
}

void main()
{
    immutable S s;
    s.foo("hi");
}
```

foo.d(9): Error: none of the overloads of `foo` are callable using a
`immutable` object, candidates are:
foo.d(2):        `foo.S.foo(string x)`
foo.d(3):        `foo.S.foo(int x)`

Clearly something that decides how to print these messages is broken (note bad
grammar too).

--
Aug 16 2021