Bug 15049 – bad error message when trying to instantiate a nested class in a static method
Status
RESOLVED
Resolution
FIXED
Severity
normal
Priority
P1
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2015-09-12T17:10:19Z
Last change time
2022-09-07T14:06:20Z
Keywords
bootcamp, diagnostic
Assigned to
No Owner
Creator
ag0aep6g
Comments
Comment #0 by ag0aep6g — 2015-09-12T17:10:19Z
Found by Prudence, reduced by Ali Çehreli and myself:
http://forum.dlang.org/post/[email protected]
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.
Comment #1 by nick — 2022-09-07T14:06:20Z
With v2.100.2-beta.1-dirty, each of these gives:
staticnewnested.d(7): Error: cannot construct nested class `SingleStore` because no implicit `this` reference to outer class `MyStore` is available