Skip to content

Commit 2ab13f5

Browse files
authored
Merge pull request #7954 from MartinNowak/fix18480
cleaner fix for Issue 18480 - dmd hangs with self-alias declaration
2 parents 22aeea7 + 414ae99 commit 2ab13f5

3 files changed

Lines changed: 18 additions & 19 deletions

File tree

src/dmd/access.d

Lines changed: 4 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -547,8 +547,8 @@ public Dsymbol mostVisibleOverload(Dsymbol s)
547547
{
548548
if (!s.isOverloadable())
549549
return s;
550-
Dsymbol next, fstart = s, mostVisible = s, previous = null;
551-
AliasDeclaration adOrig = null;
550+
551+
Dsymbol next, fstart = s, mostVisible = s;
552552
for (; s; s = next)
553553
{
554554
// void func() {}
@@ -571,7 +571,8 @@ public Dsymbol mostVisibleOverload(Dsymbol s)
571571
// private void name(int) {}
572572
else if (auto ad = s.isAliasDeclaration())
573573
{
574-
assert(ad.isOverloadable, "Non overloadable Aliasee in overload list");
574+
assert(ad.isOverloadable || ad.type && ad.type.ty == Terror,
575+
"Non overloadable Aliasee in overload list");
575576
// Yet unresolved aliases store overloads in overnext.
576577
if (ad.semanticRun < PASS.semanticdone)
577578
next = ad.overnext;
@@ -593,13 +594,7 @@ public Dsymbol mostVisibleOverload(Dsymbol s)
593594
*/
594595
auto aliasee = ad.toAlias();
595596
if (aliasee.isFuncAliasDeclaration || aliasee.isOverDeclaration)
596-
{
597-
if(ad.toChars() == aliasee.toChars())
598-
{
599-
adOrig = ad;
600-
}
601597
next = aliasee;
602-
}
603598
else
604599
{
605600
/* A simple alias can be at the end of a function or template overload chain.
@@ -619,15 +614,6 @@ public Dsymbol mostVisibleOverload(Dsymbol s)
619614

620615
if (next && mostVisible.prot().isMoreRestrictiveThan(next.prot()))
621616
mostVisible = next;
622-
623-
// fixes https://issues.dlang.org/show_bug.cgi?id=18480
624-
if(next && next == previous)
625-
{
626-
assert(adOrig);
627-
error(adOrig.loc, "`alias X = X` not allowed (with `X = %s`)", adOrig.toChars());
628-
return next;
629-
}
630-
previous = s;
631617
}
632618
return mostVisible;
633619
}

src/dmd/dsymbolsem.d

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5492,6 +5492,19 @@ void aliasSemantic(AliasDeclaration ds, Scope* sc)
54925492
global.gag = 0;
54935493
}
54945494

5495+
// https://issues.dlang.org/show_bug.cgi?id=18480
5496+
// Detect `alias sym = sym;` to prevent creating loops in overload overnext lists.
5497+
// Selective imports are allowed to alias to the same name `import mod : sym=sym`.
5498+
if (ds.type.ty == Tident && !ds._import)
5499+
{
5500+
auto tident = cast(TypeIdentifier)ds.type;
5501+
if (tident.ident is ds.ident && !tident.idents.dim)
5502+
{
5503+
error(ds.loc, "`alias %s = %s;` cannot alias itself, use a qualified name to create an overload set",
5504+
ds.ident.toChars(), tident.ident.toChars());
5505+
ds.type = Type.terror;
5506+
}
5507+
}
54955508
/* This section is needed because Type.resolve() will:
54965509
* const x = 3;
54975510
* alias y = x;

test/fail_compilation/test18480.d

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
/*
33
TEST_OUTPUT:
44
---
5-
fail_compilation/imports/test18480a.d(2): Error: `alias X = X` not allowed (with `X = TestTemplate`)
5+
fail_compilation/imports/test18480a.d(2): Error: `alias TestTemplate = TestTemplate;` cannot alias itself, use a qualified name to create an overload set
66
---
77
https://issues.dlang.org/show_bug.cgi?id=18480
88
*/

0 commit comments

Comments
 (0)