Issue 5010 - Error messages for properties could be improved
Summary: Error messages for properties could be improved
Status: NEW
Alias: None
Product: D
Classification: Unclassified
Component: dmd (show other issues)
Version: D2
Hardware: Other Windows
: P3 minor
Assignee: No Owner
URL:
Keywords: diagnostic
: 15158 (view as issue list)
Depends on:
Blocks:
 
Reported: 2010-10-07 07:22 UTC by Don
Modified: 2024-11-13 16:52 UTC (History)
4 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description Don 2010-10-07 07:22:52 UTC
The problem with the error messages is that they refer to the property in the syntax it has AFTER conversion to a function call.
----

CASE 1: Assignments cannot be chained through properties

@property
void prop(int x) {}

void main()
{
   int a = prop = 6;
}
----
bug.d(7): Error: expression prop(6) is void and has no value

----
The error message is quite difficult to understand. I do think it is quite reasonable that it fails to compile, though, since the getter could be defined as:

@property
int prop() { return 2; }

Secondly, the error messages when you use a getter (eg, int a = prop; ), when only a setter is defined, are:

bug.d(7): Error: function test0.prop (int x) is not callable using argument
types ()
bug.d(8): Error: expected 1 function arguments, not 0
bug.d(9): Error: expression prop() is void and has no value

This error message is confusing because no parens were used.

The same situation applies when trying to use a setter, when only a getter is defined.
Comment 1 Adrian Matoga 2012-12-12 09:33:35 UTC
I'd add also the following:

struct A
{
	@property void foo(uint x);
}

void main()
{
	A a;
	a.foo = "bar"; // (1)
}


(1) causes "Error: not a property a.foo", which may look obvious here, but gets confusing with more complex types.
Comment 2 Era Scarecrow 2013-02-09 20:03:58 UTC
> struct A
> {
>     @property void foo(uint x);
> }
> 
> void main()
> {
>     A a;
>     a.foo = "bar"; // (1)
> }
> 
> 
> (1) causes "Error: not a property a.foo", which may look obvious here, but gets
> confusing with more complex types.

 Also related (similar types) it should mention those that need down-casting, or something more useful.

  struct S {
    //setters
    void test(uint v) @property {}
    void test2(ulong v) @property {}
  }

  ulong x;
  S s;

  s.test2 = x;
  s.test = x;  //Error: not a property s.test
Comment 3 Mike Franklin 2017-08-14 23:08:13 UTC
*** Issue 15158 has been marked as a duplicate of this issue. ***
Comment 4 Nick Treleaven 2024-11-13 16:52:33 UTC
(In reply to Era Scarecrow from comment #2)
> > struct A
> > {
> >     @property void foo(uint x);
> > }
> > 
> > void main()
> > {
> >     A a;
> >     a.foo = "bar"; // (1)
> > }
> > 
> > 
> > (1) causes "Error: not a property a.foo", which may look obvious here, but gets
> > confusing with more complex types.

This is now:

propassign.d(14): Error: function `propassign.A.foo(uint x)` is not callable using argument types `(string)`
propassign.d(14):        cannot pass argument `"bar"` of type `string` to parameter `uint x`


>  Also related (similar types) it should mention those that need
> down-casting, or something more useful.
> 
>   struct S {
>     //setters
>     void test(uint v) @property {}
>     void test2(ulong v) @property {}
>   }
> 
>   ulong x;
>   S s;
> 
>   s.test2 = x;
>   s.test = x;  //Error: not a property s.test

This is now:

propassign.d(28): Error: function `propassign.S.test(uint v)` is not callable using argument types `(ulong)`
propassign.d(28):        cannot pass argument `x` of type `ulong` to parameter `uint v`