Bug 22216 – Incomplete/incorrect error message for mutability overloads

Status
RESOLVED
Resolution
FIXED
Severity
minor
Priority
P3
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2021-08-16T16:10:44Z
Last change time
2023-11-14T23:05:32Z
Keywords
diagnostic, pull
Assigned to
No Owner
Creator
Steven Schveighoffer

Comments

Comment #0 by schveiguy — 2021-08-16T16:10:44Z
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).
Comment #1 by dlang-bot — 2023-11-14T17:52:22Z
@ntrel created dlang/dmd pull request #15814 "Show `TypeFunction.mod` in overload candidates" fixing this issue: - Show `TypeFunction.mod` in overload candidates Fix Issue 22216 - Incomplete/incorrect error message for mutability overloads https://github.com/dlang/dmd/pull/15814
Comment #2 by dlang-bot — 2023-11-14T23:05:32Z
dlang/dmd pull request #15814 "Show `TypeFunction.mod` in overload candidates" was merged into master: - d5842945d94c79eca1e842911a3e4837a8a49379 by Nick Treleaven: Show `TypeFunction.mod` in overload candidates Fix Issue 22216 - Incomplete/incorrect error message for mutability overloads https://github.com/dlang/dmd/pull/15814