dmd/compiler/test/compilable/aliasdecl.d
Paul Backus 946079235d 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] https://github.com/dlang/dmd/pull/1187
[2] https://dlang.org/changelog/2.061.html
[3] https://github.com/dlang/dmd/pull/1413
[4] https://dlang.org/changelog/2.062.html
[5] https://github.com/dlang/dlang.org/pull/200#issuecomment-11711854
2023-05-10 11:43:19 +02:00

47 lines
1.3 KiB
D

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));
alias FP5 = extern(C) pure nothrow @safe @nogc void function(),
DG5 = extern(D) pure nothrow @safe @nogc void delegate();
static assert(FP5.stringof == "extern (C) void function() pure nothrow " /* ~ "@safe " */ ~ "@nogc");
static assert(DG5.stringof == "void delegate() pure nothrow " /* ~ "@safe " */ ~ "@nogc");
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));
// https://issues.dlang.org/show_bug.cgi?id=18429
struct S
{
alias a this;
enum a = 1;
}
struct S2
{
int value;
alias this = value;
}
auto s = S2(10);
int n = s;
assert(n == 10);
}