Skip to content

Commit

Permalink
Merge pull request #1685 from AndrejMitrovic/Fix9574
Browse files Browse the repository at this point in the history
Issue 9574 - Better diagnostic on old 'alias this = that' syntax.
  • Loading branch information
WalterBright committed Mar 7, 2013
2 parents 0679c4c + 9b3ecfb commit 1e9511b
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 3 deletions.
15 changes: 12 additions & 3 deletions src/parse.c
Expand Up @@ -3121,7 +3121,8 @@ Dsymbols *Parser::parseDeclarations(StorageClass storage_class, unsigned char *c
else if (t != tfirst)
error("multiple declarations must have the same type, not %s and %s",
tfirst->toChars(), t->toChars());
if (!ident)
bool isThis = (t->ty == Tident && ((TypeIdentifier *)t)->ident == Id::This);
if (!isThis && !ident)
error("no identifier for declarator %s", t->toChars());

if (tok == TOKtypedef || tok == TOKalias)
Expand All @@ -3146,8 +3147,16 @@ Dsymbols *Parser::parseDeclarations(StorageClass storage_class, unsigned char *c
deprecation("use of typedef is deprecated; use alias instead");
}
else
{ if (init)
error("alias cannot have initializer");
{
if (init)
{
if (isThis)
error("Cannot use syntax 'alias this = %s', use 'alias %s this' instead",
init->toChars(), init->toChars());
else
error("alias cannot have initializer");
}

v = new AliasDeclaration(loc, ident, t);
}
v->storage_class = storage_class;
Expand Down
19 changes: 19 additions & 0 deletions test/fail_compilation/diag9574.d
@@ -0,0 +1,19 @@
/*
TEST_OUTPUT:
---
fail_compilation/diag9574.d(12): Error: Cannot use syntax 'alias this = x', use 'alias x this' instead
fail_compilation/diag9574.d(18): Error: Cannot use syntax 'alias this = x', use 'alias x this' instead
---
*/

struct S
{
int x;
alias this = x;
}

class C
{
int x;
alias this = x;
}

0 comments on commit 1e9511b

Please sign in to comment.