Skip to content

Commit

Permalink
fix #20610 aliasing for fields does not work (#20611)
Browse files Browse the repository at this point in the history
  • Loading branch information
WalterBright authored Dec 28, 2024
1 parent b2c83e9 commit 1b9bc74
Show file tree
Hide file tree
Showing 6 changed files with 85 additions and 16 deletions.
11 changes: 6 additions & 5 deletions compiler/src/dmd/declaration.d
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import dmd.func;
import dmd.funcsem : overloadApply, getLevelAndCheck;
import dmd.globals;
import dmd.gluelayer;
import dmd.hdrgen;
import dmd.id;
import dmd.identifier;
import dmd.init;
Expand Down Expand Up @@ -442,16 +443,15 @@ extern (C++) final class AliasDeclaration : Declaration
extern (D) this(const ref Loc loc, Identifier ident, Type type) @safe
{
super(loc, ident);
//printf("AliasDeclaration(id = '%s', type = %p)\n", ident.toChars(), type);
//printf("type = '%s'\n", type.toChars());
//debug printf("AliasDeclaration(id = '%s', type = `%s`, %p)\n", ident.toChars(), dmd.hdrgen.toChars(type), type.isTypeIdentifier());
this.type = type;
assert(type);
}

extern (D) this(const ref Loc loc, Identifier ident, Dsymbol s) @safe
{
super(loc, ident);
//printf("AliasDeclaration(id = '%s', s = %p)\n", ident.toChars(), s);
//debug printf("AliasDeclaration(id = '%s', s = `%s`)\n", ident.toChars(), s.toChars());
assert(s != this);
this.aliassym = s;
assert(s);
Expand Down 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 @@ -14036,7 +14036,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 @@ -14164,7 +14164,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 @@ -14502,7 +14502,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
42 changes: 39 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,41 @@ 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())
{
if (tid.idents.length)
{
static if (0)
{
printf("TypeStruct::dotExp(e = '%s', ident = '%s')\n", e.toChars(), ident.toChars());
printf("AliasDeclaration: %s\n", ad.toChars());
if (ad.aliassym)
printf("aliassym: %s\n", ad.aliassym.toChars());
printf("tid type: %s\n", toChars(tid));
}
/* Rewrite e.s as e.(tid.ident).(tid.idents)
*/
Expression die = new DotIdExp(e.loc, e, tid.ident);
foreach (id; tid.idents) // maybe use typeToExpressionHelper()
die = new DotIdExp(e.loc, die, cast(Identifier)id);
/* Ambiguous syntax, only way to disambiguate it to try it
*/
die = dmd.expressionsem.trySemantic(die, sc);
if (die && die.isDotVarExp()) // shrink wrap around DotVarExp()
{
return die;
}
}
}
}
}
s = s.toAlias();

if (auto em = s.isEnumMember())
Expand Down Expand Up @@ -6074,7 +6111,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 +6123,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 1b9bc74

Please sign in to comment.