Skip to content

Commit

Permalink
fix Issue 20610 aliasing for fields does not work
Browse files Browse the repository at this point in the history
  • Loading branch information
WalterBright committed Dec 28, 2024
1 parent 2c5638e commit 0db781b
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 13 deletions.
5 changes: 3 additions & 2 deletions compiler/src/dmd/declaration.d
Original file line number Diff line number Diff line change
Expand Up @@ -612,8 +612,9 @@ extern (C++) final class AliasDeclaration : Declaration

override Dsymbol toAlias()
{
//printf("[%s] AliasDeclaration::toAlias('%s', this = %p, aliassym = %p, kind = '%s', inuse = %d)\n",
// loc.toChars(), toChars(), this, aliassym, aliassym ? aliassym.kind() : "", inuse);
static if (0)
printf("[%s] AliasDeclaration::toAlias('%s', this = %p, aliassym: %s, kind: '%s', inuse = %d)\n",
loc.toChars(), toChars(), this, aliassym ? aliassym.toChars() : "", aliassym ? aliassym.kind() : "", inuse);
assert(this != aliassym);
//static int count; if (++count == 10) *(char*)0=0;

Expand Down
2 changes: 1 addition & 1 deletion compiler/src/dmd/dsymbolsem.d
Original file line number Diff line number Diff line change
Expand Up @@ -5243,7 +5243,7 @@ void aliasInstanceSemantic(TemplateInstance tempinst, Scope* sc, TemplateDeclara
// function used to perform semantic on AliasDeclaration
void aliasSemantic(AliasDeclaration ds, Scope* sc)
{
//printf("AliasDeclaration::semantic() %s\n", ds.toChars());
//printf("AliasDeclaration::semantic() %s %p\n", ds.toChars(), ds.aliassym);

// as DsymbolSemanticVisitor::visit(AliasDeclaration), in case we're called first.
// see https://issues.dlang.org/show_bug.cgi?id=21001
Expand Down
8 changes: 4 additions & 4 deletions compiler/src/dmd/expressionsem.d
Original file line number Diff line number Diff line change
Expand Up @@ -8138,7 +8138,7 @@ private extern (C++) final class ExpressionSemanticVisitor : Visitor
static if (LOGSEMANTIC)
{
printf("DotIdExp::semantic(this = %p, '%s')\n", exp, exp.toChars());
//printf("e1.op = %d, '%s'\n", e1.op, Token.toChars(e1.op));
printAST(exp);
}

if (sc.inCfile)
Expand Down Expand Up @@ -14037,7 +14037,7 @@ Expression expressionSemantic(Expression e, Scope* sc)

private Expression dotIdSemanticPropX(DotIdExp exp, Scope* sc)
{
//printf("DotIdExp::semanticX(this = %p, '%s')\n", this, toChars());
//printf("dotIdSemanticPropX() %s\n", toChars(exp));
if (Expression ex = unaSemantic(exp, sc))
return ex;

Expand Down Expand Up @@ -14165,7 +14165,7 @@ private Expression dotIdSemanticPropX(DotIdExp exp, Scope* sc)
*/
Expression dotIdSemanticProp(DotIdExp exp, Scope* sc, bool gag)
{
//printf("DotIdExp::semanticY(this = %p, '%s')\n", exp, exp.toChars());
//printf("dotIdSemanticProp('%s')\n", exp.toChars());

//{ static int z; fflush(stdout); if (++z == 10) *(char*)0=0; }

Expand Down Expand Up @@ -14503,7 +14503,7 @@ Expression dotIdSemanticProp(DotIdExp exp, Scope* sc, bool gag)

const flag = cast(DotExpFlag) (exp.noderef * DotExpFlag.noDeref | gag * DotExpFlag.gag);

Expression e = exp.e1.type.dotExp(sc, exp.e1, exp.ident, flag);
Expression e = dotExp(exp.e1.type, sc, exp.e1, exp.ident, flag);
if (e)
{
e = e.expressionSemantic(sc);
Expand Down
6 changes: 6 additions & 0 deletions compiler/src/dmd/printast.d
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,12 @@ extern (C++) final class PrintASTVisitor : Visitor
printf("%.*s %s\n", cast(int)s.length, s.ptr, e.type ? e.type.toChars() : "");
}

override void visit(IdentifierExp e)
{
printIndent(indent);
printf("Identifier `%s` %s\n", e.ident.toChars(), e.type ? e.type.toChars() : "");
}

override void visit(IntegerExp e)
{
printIndent(indent);
Expand Down
29 changes: 26 additions & 3 deletions compiler/src/dmd/typesem.d
Original file line number Diff line number Diff line change
Expand Up @@ -4462,6 +4462,10 @@ void resolve(Type mt, const ref Loc loc, Scope* sc, out Expression pe, out Type
*/
Expression dotExp(Type mt, Scope* sc, Expression e, Identifier ident, DotExpFlag flag)
{
enum LOGDOTEXP = false;
if (LOGDOTEXP)
printf("dotExp()\n");

Expression visitType(Type mt)
{
VarDeclaration v = null;
Expand Down Expand Up @@ -5082,8 +5086,28 @@ Expression dotExp(Type mt, Scope* sc, Expression e, Identifier ident, DotExpFlag
return noMember(mt, sc, e, ident, flag);
}
// check before alias resolution; the alias itself might be deprecated!
if (s.isAliasDeclaration)
if (auto ad = s.isAliasDeclaration)
{
s.checkDeprecated(e.loc, sc);

// Fix for https://github.com/dlang/dmd/issues/20610
if (ad.originalType)
{
if (auto tid = ad.originalType.isTypeIdentifier())
{
//printf("tid type: %s\n", toChars(tid));
if (tid.idents.length)
{
/* Rewrite e.s as e.(tid.ident).(tid.idents)
*/
e = new DotIdExp(e.loc, e, tid.ident);
foreach (id; tid.idents) // maybe use typeToExpressionHelper()
e = new DotIdExp(e.loc, e, cast(Identifier)id);
return e.expressionSemantic(sc);
}
}
}
}
s = s.toAlias();

if (auto em = s.isEnumMember())
Expand Down Expand Up @@ -6074,7 +6098,7 @@ Dsymbol toDsymbol(Type type, Scope* sc)

Dsymbol visitIdentifier(TypeIdentifier type)
{
//printf("TypeIdentifier::toDsymbol('%s')\n", toChars());
//printf("TypeIdentifier::toDsymbol('%s')\n", toChars(type));
if (!sc)
return null;

Expand All @@ -6086,7 +6110,6 @@ Dsymbol toDsymbol(Type type, Scope* sc)
s = t.toDsymbol(sc);
if (e)
s = getDsymbol(e);

return s;
}

Expand Down
32 changes: 29 additions & 3 deletions compiler/test/runnable/aliasassign.d
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ template Qual(alias T)
alias Qual = T;
}

void test()
void test1()
{
int x = 3;
int y = 4;
Expand All @@ -25,7 +25,33 @@ void test()
assert(XY[1] == 4);
}

void main()
/**********************************************/

struct T
{
int k,i = 2;
}

struct S
{
int x;
T t;
alias ti = t.i;
}

void test2()
{
T t = T(1, 2);
S s;
assert(s.ti == 2);
}

/**********************************************/

int main()
{
test();
test1();
test2();

return 0;
}

0 comments on commit 0db781b

Please sign in to comment.