D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 7183 - Bad error message when trying to use this in a static member function of a struct
Summary: Bad error message when trying to use this in a static member function of a st...
Status: NEW
Alias: None
Product: D
Classification: Unclassified
Component: dmd (show other issues)
Version: D2
Hardware: Other All
: P3 normal
Assignee: No Owner
URL:
Keywords: diagnostic
Depends on:
Blocks:
 
Reported: 2011-12-29 08:46 UTC by Matt Arsenault
Modified: 2024-12-13 17:57 UTC (History)
3 users (show)

See Also:


Attachments
Example of bad + good error (569 bytes, text/x-dsrc)
2011-12-29 08:46 UTC, Matt Arsenault
Details

Note You need to log in before you can comment on or make changes to this issue.
Description Matt Arsenault 2011-12-29 08:46:55 UTC
Created attachment 1059 [details]
Example of bad + good error

When attempting to use 'this' in a static member function on a struct, the
error message is not helpful.

bad_error_static_this_struct.d(13): Error: need 'this' to access member x

However if the same declaration is made to be 'class' the error is much more
appropriate:

bad_error_static_this_struct.d(25): Error: 'this' is only defined in non-static
member functions, not comparex

Attached is an example.
Comment 1 Don 2011-12-29 10:02:03 UTC
The class error message is generated in the front-end. The struct error message is generated in the glue layer. Apart from the diagnostic issue, the error should really be generated in the front-end.

Having it in the glue layer creates problems for CTFE. The following example errors with "variable x is used before initialization" which is nonsense.

struct BadError
{
    double x = 2.0;
    static int comparex()
    {
        return (this.x <= 3.0);
    }
}

static assert({  BadError z; z.comparex(); return true; }());
Comment 2 Andrej Mitrovic 2014-04-28 12:13:20 UTC
Unfortunately both examples now emit the less informative diagnostic:

-----
struct S
{
    int x;
    static void f() { x = 1; }
}

class C
{
    int x;
    static void f() { x = 1; }
}

void main() { }
-----

test.d(4): Error: need 'this' for 'x' of type 'int'
test.d(10): Error: need 'this' for 'x' of type 'int'
Comment 3 dlangBugzillaToGithub 2024-12-13 17:57:29 UTC
THIS ISSUE HAS BEEN MOVED TO GITHUB

https://github.com/dlang/dmd/issues/18392

DO NOT COMMENT HERE ANYMORE, NOBODY WILL SEE IT, THIS ISSUE HAS BEEN MOVED TO GITHUB