Skip to content

Commit 9460792

Browse files
pbackusdkorpel
authored andcommitted
Allow alias this to use assignment-style syntax
When assignment-style syntax for alias declarations was first implemented in DMD 2.061 [1][2], `alias this = identifier;` was accepted as equivalent to the existing `alias identifier this;`. One release later, in DMD 2.062, it was removed. [3] The rationale for this change, given in both the changelog [4] and a related spec PR thread [5], was to allow for the possibility that, in the future, the syntax `alias this = super.this;` might be used to merge a derived class's constructor overload set with that of its base class. However, this proposal was never implemented, and seems to have been abandoned in the intervening years. For the sake of consistency, and since the rationale for its removal no longer applies, this commit reinstates `alias this = identifier;` as valid syntax for an `alias this` declaration. [1] #1187 [2] https://dlang.org/changelog/2.061.html [3] #1413 [4] https://dlang.org/changelog/2.062.html [5] dlang/dlang.org#200 (comment)
1 parent 6d36ef8 commit 9460792

File tree

3 files changed

+17
-42
lines changed

3 files changed

+17
-42
lines changed

compiler/src/dmd/parse.d

Lines changed: 14 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -4515,10 +4515,7 @@ class Parser(AST, Lexer = dmd.lexer.Lexer) : Lexer
45154515
}
45164516
if (_init)
45174517
{
4518-
if (isThis)
4519-
error(token.loc, "cannot use syntax `alias this = %s`, use `alias %s this` instead", _init.toChars(), _init.toChars());
4520-
else
4521-
error("alias cannot have initializer");
4518+
error("alias cannot have initializer");
45224519
}
45234520
v = new AST.AliasDeclaration(aliasLoc, ident, t);
45244521

@@ -4780,23 +4777,20 @@ class Parser(AST, Lexer = dmd.lexer.Lexer) : Lexer
47804777
addComment(s, comment);
47814778
return a;
47824779
}
4783-
version (none)
4780+
/* Look for:
4781+
* alias this = identifier;
4782+
*/
4783+
if (token.value == TOK.this_ && peekNext() == TOK.assign && peekNext2() == TOK.identifier)
47844784
{
4785-
/* Look for:
4786-
* alias this = identifier;
4787-
*/
4788-
if (token.value == TOK.this_ && peekNext() == TOK.assign && peekNext2() == TOK.identifier)
4789-
{
4790-
check(TOK.this_);
4791-
check(TOK.assign);
4792-
auto s = new AliasThis(loc, token.ident);
4793-
nextToken();
4794-
check(TOK.semicolon, "`alias this = Identifier`");
4795-
auto a = new Dsymbols();
4796-
a.push(s);
4797-
addComment(s, comment);
4798-
return a;
4799-
}
4785+
check(TOK.this_);
4786+
check(TOK.assign);
4787+
auto s = new AST.AliasThis(loc, token.ident);
4788+
nextToken();
4789+
check(TOK.semicolon, "`alias this = Identifier`");
4790+
auto a = new AST.Dsymbols();
4791+
a.push(s);
4792+
addComment(s, comment);
4793+
return a;
48004794
}
48014795
/* Look for:
48024796
* alias identifier = type;

compiler/test/compilable/aliasdecl.d

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,12 @@ void main()
3636
enum a = 1;
3737
}
3838

39-
/+ struct S
39+
struct S2
4040
{
4141
int value;
4242
alias this = value;
4343
}
44-
auto s = S(10);
44+
auto s = S2(10);
4545
int n = s;
46-
assert(n == 10); +/
46+
assert(n == 10);
4747
}

compiler/test/fail_compilation/diag9574.d

Lines changed: 0 additions & 19 deletions
This file was deleted.

0 commit comments

Comments
 (0)