Bug 1730 – Bogus error message calling a non-const struct method on a const struct reference
Status
RESOLVED
Resolution
FIXED
Severity
minor
Priority
P2
Component
dmd
Product
D
Version
D2
Platform
x86
OS
Windows
Creation time
2007-12-13T23:52:00Z
Last change time
2015-06-09T01:14:23Z
Keywords
diagnostic, pull
Assigned to
andrej.mitrovich
Creator
davidl
Comments
Comment #0 by davidl — 2007-12-13T23:52:08Z
struct mystruct{
invariant(char)[] toChars()
{
return "asdf";
}
}
void func(in mystruct s)
{
invariant(char)[] k=s.toChars;
}
testconst.d(9): function testconst.mystruct.toChars () does not match parameter types ()
testconst.d(9): Error: s.toChars can only be called on a mutable object, not const(mystruct)
Comment #1 by caron800 — 2007-12-14T01:10:32Z
On 12/14/07, [email protected] <[email protected]> wrote:
> struct mystruct{
> invariant(char)[] toChars()
> {
> return "asdf";
> }
> }
> void func(in mystruct s)
> {
> invariant(char)[] k=s.toChars;
> }
That's correct behaviour, surely?
Change the function definition to
const invariant(char)[] toChars()
{
return "asdf";
}
and the problem should go away. toChars() as written is incorrectly
declared. It does not modify this, therefore it should be declared
const.
That said, I don't understant how s got to be const in the first
place! Does "in" make things const?
Comment #2 by davidl — 2007-12-15T07:40:06Z
But at least the first error message is confusing.
Comment #3 by smjg — 2009-11-20T11:10:48Z
Probably part of the same bug:
----------
class Class {
void fn1() const {
fn2();
}
void fn2() {}
}
----------
const_call.d(3): Error: function const_call.Class.fn2 () is not callable using argument types () const
----------
This may make sense at the internal level, as trying to convert the 'this' argument implicitly from const(Class) to Class. But as a user error message, it's confusing.
Comment #4 by gim913 — 2010-03-13T07:25:52Z
I has similar problem with:
struct Foo {
bool boo() {
return false;
}
int foo () const {
assert (boo);
return 66;
}
}
this message is totally misleading
Comment #5 by andrej.mitrovich — 2012-12-23T13:45:29Z
*** Issue 3642 has been marked as a duplicate of this issue. ***
Comment #6 by andrej.mitrovich — 2012-12-23T13:45:34Z
*** Issue 4497 has been marked as a duplicate of this issue. ***
Comment #7 by andrej.mitrovich — 2012-12-23T13:47:23Z
There's a ton of duplicate reports on this diagnostic. We better come up with a better error message than what we have now because there's a lot of complaints.
Comment #8 by andrej.mitrovich — 2012-12-25T06:31:46Z