Skip to content

Commit

Permalink
Merge pull request #1187 from 9rnsr/fix3011
Browse files Browse the repository at this point in the history
[enh] Issue 3011 - alias should have assignment syntax
  • Loading branch information
andralex committed Oct 22, 2012
2 parents 1fd0815 + 7859f7d commit 6c01188
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 3 deletions.
63 changes: 60 additions & 3 deletions src/parse.c
Expand Up @@ -2739,6 +2739,7 @@ Dsymbols *Parser::parseDeclarations(StorageClass storage_class, unsigned char *c
enum TOK tok = TOKreserved;
enum LINK link = linkage;
unsigned structalign = 0;
Loc loc = this->loc;

//printf("parseDeclarations() %s\n", token.toChars());
if (!comment)
Expand All @@ -2757,9 +2758,9 @@ Dsymbols *Parser::parseDeclarations(StorageClass storage_class, unsigned char *c
*/
tok = token.value;
nextToken();
if (token.value == TOKidentifier && peek(&token)->value == TOKthis)
if (token.value == TOKidentifier && peekNext() == TOKthis)
{
AliasThis *s = new AliasThis(this->loc, token.ident);
AliasThis *s = new AliasThis(loc, token.ident);
nextToken();
check(TOKthis);
check(TOKsemicolon);
Expand All @@ -2768,6 +2769,62 @@ Dsymbols *Parser::parseDeclarations(StorageClass storage_class, unsigned char *c
addComment(s, comment);
return a;
}
/* Look for:
* alias this = identifier;
*/
if (token.value == TOKthis && peekNext() == TOKassign && peekNext2() == TOKidentifier)
{
check(TOKthis);
check(TOKassign);
AliasThis *s = new AliasThis(loc, token.ident);
nextToken();
check(TOKsemicolon);
a = new Dsymbols();
a->push(s);
addComment(s, comment);
return a;
}
/* Look for:
* alias identifier = type;
*/
if (token.value == TOKidentifier && peekNext() == TOKassign)
{
a = new Dsymbols();
while (1)
{
ident = token.ident;
nextToken();
check(TOKassign);
t = parseType();
Declaration *v = new AliasDeclaration(loc, ident, t);
a->push(v);
switch (token.value)
{ case TOKsemicolon:
nextToken();
addComment(v, comment);
break;
case TOKcomma:
nextToken();
addComment(v, comment);
if (token.value != TOKidentifier)
{ error("Identifier expected following comma, not %s", token.toChars());
break;
}
else if (peek(&token)->value != TOKassign)
{ error("= expected following identifier");
nextToken();
break;
}
continue;
default:
error("semicolon expected to close %s declaration", Token::toChars(tok));
break;
}
break;
}
return a;
}

break;
case TOKtypedef:
deprecation("use of typedef is deprecated; use alias instead");
Expand Down Expand Up @@ -2935,7 +2992,7 @@ Dsymbols *Parser::parseDeclarations(StorageClass storage_class, unsigned char *c

while (1)
{
Loc loc = this->loc;
loc = this->loc;
TemplateParameters *tpl = NULL;

ident = NULL;
Expand Down
35 changes: 35 additions & 0 deletions test/compilable/aliasdecl.d
@@ -0,0 +1,35 @@
template Test(T){ alias Type = T; }

alias X1 = int;
static assert(is(X1 == int));

alias X2 = immutable(long)[], X3 = shared const double[int];
static assert(is(X2 == immutable(long)[]));
static assert(is(X3 == shared const double[int]));

alias X4 = void delegate() const, X5 = Test!int;
static assert(is(X4 == void delegate() const));
static assert(is(X5.Type == int));

void main()
{
alias Y1 = int;
static assert(is(Y1 == int));

alias Y2 = immutable(long)[], Y3 = shared const double[int];
static assert(is(Y2 == immutable(long)[]));
static assert(is(Y3 == shared const double[int]));

alias Y4 = void delegate() const, Y5 = Test!int;
static assert(is(Y4 == void delegate() const));
static assert(is(Y5.Type == int));

struct S
{
int value;
alias this = value;
}
auto s = S(10);
int n = s;
assert(n == 10);
}

0 comments on commit 6c01188

Please sign in to comment.