Skip to content

Commit

Permalink
Fix Bugzilla 21564 - Allow assignment syntax for instantiating mixin …
Browse files Browse the repository at this point in the history
…templates (#16387)

Following the [alias this change](https://dlang.org/changelog/2.105.0.html#dmd.alias-this-syntax),
I think mixin instantiation was the last place not using assignment
syntax.
  • Loading branch information
ntrel authored Aug 28, 2024
1 parent 4a5c56d commit 717e872
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 6 deletions.
21 changes: 15 additions & 6 deletions compiler/src/dmd/parse.d
Original file line number Diff line number Diff line change
Expand Up @@ -1707,19 +1707,28 @@ class Parser(AST, Lexer = dmd.lexer.Lexer) : Lexer
* mixin Foo;
* mixin Foo!(args);
* mixin a.b.c!(args).Foo!(args);
* mixin Foo!(args) identifier;
* mixin typeof(expr).identifier!(args);
* mixin Foo!(args) identifier;
* mixin identifier = Foo!(args);
*/
private AST.Dsymbol parseMixin()
{
AST.TemplateMixin tm;
Identifier id;
Identifier id, name;
AST.Objects* tiargs;

//printf("parseMixin()\n");
const locMixin = token.loc;
nextToken(); // skip 'mixin'

// mixin Identifier = MixinTemplateName TemplateArguments;
if (token.value == TOK.identifier && peekNext() == TOK.assign)
{
name = token.ident;
nextToken();
nextToken();
}

auto loc = token.loc;
AST.TypeQualified tqual = null;
if (token.value == TOK.dot)
Expand Down Expand Up @@ -1782,14 +1791,14 @@ class Parser(AST, Lexer = dmd.lexer.Lexer) : Lexer
nextToken();
}

id = null;
if (token.value == TOK.identifier)
// mixin MixinTemplateName TemplateArguments Identifier;
if (!name && token.value == TOK.identifier)
{
id = token.ident;
name = token.ident;
nextToken();
}

tm = new AST.TemplateMixin(locMixin, id, tqual, tiargs);
tm = new AST.TemplateMixin(locMixin, name, tqual, tiargs);
if (token.value != TOK.semicolon)
error("`;` expected after `mixin`");
nextToken();
Expand Down
5 changes: 5 additions & 0 deletions compiler/test/runnable/mixin1.d
Original file line number Diff line number Diff line change
Expand Up @@ -100,13 +100,15 @@ template Foo2(T)

mixin Foo2!(uint) B2;
mixin Foo2!(long) C2;
mixin D2 = Foo2!(wchar);
mixin Foo2!(int);

void test2()
{
B2.x2 = 3;
assert(B2.x2 == 3);
assert(C2.x2 == long.sizeof);
assert(D2.x2 == 2);
// assert(x2 == int.sizeof);
}

Expand Down Expand Up @@ -284,6 +286,9 @@ void test11()
int y = 8;
mixin Foo11!(y) B;
assert(B.abc() == 8);

mixin C = Foo11!2;
assert(C.abc() == 2);
}

/*******************************************/
Expand Down

0 comments on commit 717e872

Please sign in to comment.