Skip to content

Commit

Permalink
make TypeTraits semantic more clear and prevent invalid type swaps.
Browse files Browse the repository at this point in the history
The switch over the traits expression gives a better control over what's accepted and how the matching type or symbol is extracted.
Previously using getType() as a default fallback could lead to an invalid type swap from the expression type to the type represented TypeTrait.

This commit also restores the getDsymbolWithoutExpCtx() function as it was before the implementation of TypeTraits.
  • Loading branch information
Lars-Kristiansen committed Apr 12, 2019
1 parent 3e229cd commit 2af3013
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 13 deletions.
4 changes: 1 addition & 3 deletions src/dmd/traits.d
Original file line number Diff line number Diff line change
Expand Up @@ -69,16 +69,14 @@ enum LOGSEMANTIC = false;
* Returns:
* Dsymbol the corresponding symbol for oarg
*/
Dsymbol getDsymbolWithoutExpCtx(RootObject oarg)
private Dsymbol getDsymbolWithoutExpCtx(RootObject oarg)
{
if (auto e = isExpression(oarg))
{
if (e.op == TOK.dotVariable)
return (cast(DotVarExp)e).var;
if (e.op == TOK.dotTemplateDeclaration)
return (cast(DotTemplateExp)e).td;
if (e.op == TOK.scope_)
return (cast(ScopeExp)e).sds;
}
return getDsymbol(oarg);
}
Expand Down
42 changes: 32 additions & 10 deletions src/dmd/typesem.d
Original file line number Diff line number Diff line change
Expand Up @@ -1624,22 +1624,44 @@ extern(C++) Type typeSemantic(Type t, Loc loc, Scope* sc)
return mtype;
}

import dmd.traits : semanticTraits, getDsymbolWithoutExpCtx;
import dmd.traits : semanticTraits;
Type result;

if (Expression e = semanticTraits(mtype.exp, sc))
{
if (TupleExp te = e.toTupleExp)
mtype.sym = new TupleDeclaration(mtype.loc,
Identifier.generateId("__aliastup"), cast(Objects*) te.exps);
else if (Dsymbol ds = getDsymbol(e))
mtype.sym = ds;
else if (Dsymbol ds = getDsymbolWithoutExpCtx(e))
mtype.sym = ds;
else if (Type t = getType(e))
result = t.addMod(mtype.mod);
switch (e.op)
{
case TOK.dotVariable:
mtype.sym = (cast(DotVarExp)e).var;
break;
case TOK.variable:
mtype.sym = (cast(VarExp)e).var;
break;
case TOK.dotTemplateDeclaration:
mtype.sym = (cast(DotTemplateExp)e).td;
break;
case TOK.dSymbol:
mtype.sym = (cast(DsymbolExp)e).s;
break;
case TOK.scope_:
mtype.sym = (cast(ScopeExp)e).sds;
break;
case TOK.tuple:
mtype.sym = new TupleDeclaration(e.loc,
Identifier.generateId("__aliastup"), cast(Objects*) e.toTupleExp.exps);
break;
case TOK.dotType:
result = (cast(DotTypeExp)e).sym.isType();
break;
case TOK.type:
result = (cast(TypeExp)e).type;
break;
default:
}
}

if (result)
result = result.addMod(mtype.mod);
if (!mtype.inAliasDeclaration && !result)
{
if (!global.errors)
Expand Down

0 comments on commit 2af3013

Please sign in to comment.