fix #20610 aliasing for fields does not work (#20611)

This commit is contained in:
Walter Bright 2024-12-27 21:38:25 -08:00 committed by GitHub
parent b2c83e9921
commit 1b9bc74edc
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
6 changed files with 85 additions and 16 deletions

View file

@ -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;
@ -442,8 +443,7 @@ 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);
}
@ -451,7 +451,7 @@ extern (C++) final class AliasDeclaration : Declaration
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);
@ -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;

View file

@ -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

View file

@ -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)
@ -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;
@ -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; }
@ -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);

View file

@ -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);

View file

@ -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;
@ -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())
@ -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;
@ -6086,7 +6123,6 @@ Dsymbol toDsymbol(Type type, Scope* sc)
s = t.toDsymbol(sc);
if (e)
s = getDsymbol(e);
return s;
}

View file

@ -15,7 +15,7 @@ template Qual(alias T)
alias Qual = T;
}
void test()
void test1()
{
int x = 3;
int y = 4;
@ -25,7 +25,33 @@ void test()
assert(XY[1] == 4);
}
void main()
/**********************************************/
struct T
{
test();
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()
{
test1();
test2();
return 0;
}