From 717e872d37b710d4b487fcf2eb99f24ae68d8333 Mon Sep 17 00:00:00 2001 From: Nick Treleaven Date: Wed, 28 Aug 2024 08:02:55 +0100 Subject: [PATCH] Fix Bugzilla 21564 - Allow assignment syntax for instantiating mixin 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. --- compiler/src/dmd/parse.d | 21 +++++++++++++++------ compiler/test/runnable/mixin1.d | 5 +++++ 2 files changed, 20 insertions(+), 6 deletions(-) diff --git a/compiler/src/dmd/parse.d b/compiler/src/dmd/parse.d index 7ea9ad50de25..012f764676b6 100644 --- a/compiler/src/dmd/parse.d +++ b/compiler/src/dmd/parse.d @@ -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) @@ -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(); diff --git a/compiler/test/runnable/mixin1.d b/compiler/test/runnable/mixin1.d index d36d3f1ec0e0..2cded33a5412 100644 --- a/compiler/test/runnable/mixin1.d +++ b/compiler/test/runnable/mixin1.d @@ -100,6 +100,7 @@ template Foo2(T) mixin Foo2!(uint) B2; mixin Foo2!(long) C2; +mixin D2 = Foo2!(wchar); mixin Foo2!(int); void test2() @@ -107,6 +108,7 @@ void test2() B2.x2 = 3; assert(B2.x2 == 3); assert(C2.x2 == long.sizeof); + assert(D2.x2 == 2); // assert(x2 == int.sizeof); } @@ -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); } /*******************************************/