This patch also gives a reasonable fix for bug 4962, "Improve error message for wrong constructor name?"
It also gives nice error messages for missing return types in function declarations.
Most importantly, it makes x(y) an invalid type, instead of being a declaration of a 'y', where &y is type 'x function()'. It's critical that syntax be removed.
---
PATCH: parse.c, Parser::parseDeclarator() line 2497
----
case TOKlparen:
if (peekNext() == TOKmul || peekNext() == TOKlparen ||
peekNext() == TOKlbracket)
{
/* Parse things with parentheses around the identifier, like:
* int (*ident[3])[]
* although the D style would be:
* int[]*[3] ident
*/
if (!global.params.useDeprecated) {
error("C-style function pointer syntax is deprecated. Use 'function' to declare function pointers");
}
nextToken();
ts = parseDeclarator(t, pident);
check(TOKrparen);
break;
}
ts = t;
Token *peekt = &token;
// Improve error messages for the common bug of a missing return type
if (isParameters(&peekt)) {
error("function declaration without return type. "
"(Note that constructors are always named 'this')");
}
else
error("unexpected ( in declarator");
break;
(In reply to comment #1)
> I changed it for D2 only, in order to avoid breaking existing D1 code.
>
> http://www.dsource.org/projects/dmd/changeset/703
The language grammar (declaration.html) also needs to be updated.
Comment #3 by clugdbug — 2011-12-30T16:44:23Z
(In reply to comment #2)
> (In reply to comment #1)
> > I changed it for D2 only, in order to avoid breaking existing D1 code.
> >
> > http://www.dsource.org/projects/dmd/changeset/703
>
> The language grammar (declaration.html) also needs to be updated.
It's marked as deprecated in declaration.html. As long as it's deprecated but not removed, I think it should remain as part of the grammer.