Bug 4962 – Improve error message for wrong constructor name?

Status
RESOLVED
Resolution
FIXED
Severity
enhancement
Priority
P2
Component
dmd
Product
D
Version
D2
Platform
x86
OS
Windows
Creation time
2010-09-30T13:14:00Z
Last change time
2010-11-01T13:09:41Z
Keywords
diagnostic
Assigned to
nobody
Creator
bearophile_hugs

Comments

Comment #0 by bearophile_hugs — 2010-09-30T13:14:17Z
This is born from a post in D.learn: http://www.digitalmars.com/webnews/newsgroups.php?art_group=digitalmars.D.learn&article_id=22040 In code written by D programmers coming from C++ or some other languages, or in D code translated from other languages, constructors may have the same name of the struct/union/class (it's a bug): struct Foo { Foo(int x_) {} } void main() {} DMD 2.049 prints (similar errors are produced if Foo is a class or union): test.d(2): found 'int' when expecting ')' test.d(2): no identifier for declarator Foo test.d(2): semicolon expected, not 'x_' test.d(2): no identifier for declarator x_ test.d(2): semicolon expected, not ')' test.d(2): Declaration expected, not ')' test.d(5): } expected following member declarations in aggregate In this situation it's positive to give a single error message that is more easy to understand. To do this well I think DMD has to essentially recognize that as a Java-style constructor syntax, and refuse it, with a single error similar to: test.d(3): class/struct/union constructors are always named 'this' not 'Foo'. But I am not sure, because the creation of such test may have some negative side effects. If such side effects are too much bad, then it's better to not add this test and error message and it's better to close this enhancement request.
Comment #1 by bearophile_hugs — 2010-11-01T13:09:41Z
With DMD 2.050 that program gives the following error messages, that while not being perfect, are good enough, so I close this enhancement request: test.d(2): function declaration without return type. (Note that constructors are always named 'this') test.d(2): no identifier for declarator Foo(int x_)