Skip to content

Commit

Permalink
fix Issue 17545 - [REG2.072] __traits(getAttributes, name) evaluates …
Browse files Browse the repository at this point in the history
…name to value prematurely
  • Loading branch information
WalterBright committed Jul 4, 2017
1 parent ea2033f commit 2e6c7ac
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 1 deletion.
7 changes: 6 additions & 1 deletion src/ddmd/expression.d
Expand Up @@ -8520,6 +8520,7 @@ extern (C++) final class DotIdExp : UnaExp
{
Identifier ident;
bool noderef; // true if the result of the expression will never be dereferenced
bool wantsym; // do not replace Symbol with its initializer during semantic()

extern (D) this(Loc loc, Expression e, Identifier ident)
{
Expand Down Expand Up @@ -8772,8 +8773,12 @@ extern (C++) final class DotIdExp : UnaExp
if (v.type.ty == Terror)
return new ErrorExp();

if ((v.storage_class & STCmanifest) && v._init)
if ((v.storage_class & STCmanifest) && v._init && !wantsym)
{
/* Normally, the replacement of a symbol with its initializer is supposed to be in semantic2().
* Introduced by https://github.com/dlang/dmd/pull/5588 which should probably
* be reverted. `wantsym` is the hack to work around the problem.
*/
if (v.inuse)
{
.error(loc, "circular initialization of %s '%s'", v.kind(), v.toPrettyChars());
Expand Down
2 changes: 2 additions & 0 deletions src/ddmd/expression.h
Expand Up @@ -773,6 +773,8 @@ class DotIdExp : public UnaExp
{
public:
Identifier *ident;
bool noderef; // true if the result of the expression will never be dereferenced
bool wantsym; // do not replace Symbol with its initializer during semantic()

static DotIdExp *create(Loc loc, Expression *e, Identifier *ident);
Expression *semantic(Scope *sc);
Expand Down
3 changes: 3 additions & 0 deletions src/ddmd/traits.d
Expand Up @@ -734,6 +734,9 @@ extern (C++) Expression semanticTraits(TraitsExp e, Scope* sc)
}
else if (e.ident == Id.getMember)
{
if (ex.op == TOKdotid)
// Prevent semantic() from replacing Symbol with its initializer
(cast(DotIdExp)ex).wantsym = true;
ex = ex.semantic(scx);
return ex;
}
Expand Down
16 changes: 16 additions & 0 deletions test/compilable/test17545.d
@@ -0,0 +1,16 @@
/* TEST_OUTPUT:
---
tuple((Attrib))
---
*/

// https://issues.dlang.org/show_bug.cgi?id=17545

module example;

struct Attrib {}

@Attrib enum TEST = 123;

pragma(msg, __traits(getAttributes,
__traits(getMember, example, "TEST")));

0 comments on commit 2e6c7ac

Please sign in to comment.