Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 4 additions & 18 deletions src/dmd/access.d
Original file line number Diff line number Diff line change
Expand Up @@ -547,8 +547,8 @@ public Dsymbol mostVisibleOverload(Dsymbol s)
{
if (!s.isOverloadable())
return s;
Dsymbol next, fstart = s, mostVisible = s, previous = null;
AliasDeclaration adOrig = null;

Dsymbol next, fstart = s, mostVisible = s;
for (; s; s = next)
{
// void func() {}
Expand All @@ -571,7 +571,8 @@ public Dsymbol mostVisibleOverload(Dsymbol s)
// private void name(int) {}
else if (auto ad = s.isAliasDeclaration())
{
assert(ad.isOverloadable, "Non overloadable Aliasee in overload list");
assert(ad.isOverloadable || ad.type && ad.type.ty == Terror,
"Non overloadable Aliasee in overload list");
// Yet unresolved aliases store overloads in overnext.
if (ad.semanticRun < PASS.semanticdone)
next = ad.overnext;
Expand All @@ -593,13 +594,7 @@ public Dsymbol mostVisibleOverload(Dsymbol s)
*/
auto aliasee = ad.toAlias();
if (aliasee.isFuncAliasDeclaration || aliasee.isOverDeclaration)
{
if(ad.toChars() == aliasee.toChars())
{
adOrig = ad;
}
next = aliasee;
}
else
{
/* A simple alias can be at the end of a function or template overload chain.
Expand All @@ -619,15 +614,6 @@ public Dsymbol mostVisibleOverload(Dsymbol s)

if (next && mostVisible.prot().isMoreRestrictiveThan(next.prot()))
mostVisible = next;

// fixes https://issues.dlang.org/show_bug.cgi?id=18480
if(next && next == previous)
{
assert(adOrig);
error(adOrig.loc, "`alias X = X` not allowed (with `X = %s`)", adOrig.toChars());
return next;
}
previous = s;
}
return mostVisible;
}
13 changes: 13 additions & 0 deletions src/dmd/dsymbolsem.d
Original file line number Diff line number Diff line change
Expand Up @@ -5492,6 +5492,19 @@ void aliasSemantic(AliasDeclaration ds, Scope* sc)
global.gag = 0;
}

// https://issues.dlang.org/show_bug.cgi?id=18480
// Detect `alias sym = sym;` to prevent creating loops in overload overnext lists.
// Selective imports are allowed to alias to the same name `import mod : sym=sym`.
if (ds.type.ty == Tident && !ds._import)
{
auto tident = cast(TypeIdentifier)ds.type;
if (tident.ident is ds.ident && !tident.idents.dim)
{
error(ds.loc, "`alias %s = %s;` cannot alias itself, use a qualified name to create an overload set",
ds.ident.toChars(), tident.ident.toChars());
ds.type = Type.terror;
}
}
/* This section is needed because Type.resolve() will:
* const x = 3;
* alias y = x;
Expand Down
2 changes: 1 addition & 1 deletion test/fail_compilation/test18480.d
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
/*
TEST_OUTPUT:
---
fail_compilation/imports/test18480a.d(2): Error: `alias X = X` not allowed (with `X = TestTemplate`)
fail_compilation/imports/test18480a.d(2): Error: `alias TestTemplate = TestTemplate;` cannot alias itself, use a qualified name to create an overload set
---
https://issues.dlang.org/show_bug.cgi?id=18480
*/
Expand Down