[Issue 15049] New: bad error message when trying to instantiate a nested class in a static method

via Digitalmars-d-bugs digitalmars-d-bugs at puremagic.com
Sat Sep 12 10:10:19 PDT 2015


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

          Issue ID: 15049
           Summary: bad error message when trying to instantiate a nested
                    class in a static method
           Product: D
           Version: D2
          Hardware: All
                OS: All
            Status: NEW
          Keywords: diagnostic
          Severity: normal
          Priority: P1
         Component: dmd
          Assignee: nobody at puremagic.com
          Reporter: ag0aep6g at gmail.com

Found by Prudence, reduced by Ali Çehreli and myself:
http://forum.dlang.org/post/sggmxddjpqpaookmwrbr@forum.dlang.org

The following code is invalid, but the error message should be improved:

----
class MyStore
{
    class SingleStore
    {
        static void New()
        {
            new SingleStore(); /* line 7 */
        }
    }
}
----
test.d(7): Error: type SingleStore is not an expression
----

SingleStore is a nested class. That means, instances of it are bound to MyStore
instances. But New is static, so it doesn't have a MyStore to which it could
attach the `new SingleStore`.

When moving New to MyStore the error gets better:

----
class MyStore
{
    class SingleStore
    {
    }
    static void New()
    {
        new SingleStore(); /* line 8 */
    }
}
----
test.d(8): Error: 'this' is only defined in non-static member functions, not
New
test.d(8): Error: 'this' for nested class must be a class type, not _error_
----

That's still a bit stilted, but a lot better than the other one.

--


More information about the Digitalmars-d-bugs mailing list