diff --git a/src/parse.c b/src/parse.c index 75206cca69f9..317fd72c6c29 100644 --- a/src/parse.c +++ b/src/parse.c @@ -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) @@ -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; diff --git a/test/fail_compilation/diag9574.d b/test/fail_compilation/diag9574.d new file mode 100644 index 000000000000..52258e1a26cc --- /dev/null +++ b/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; +}