Skip to content

Commit

Permalink
Merge pull request #6439 from LemonBoy/b6400
Browse files Browse the repository at this point in the history
Fix issue 6400 - Better interaction between with() and opDispatch
  • Loading branch information
WalterBright committed Jan 15, 2017
2 parents 9b26093 + 2c80087 commit c49fb86
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 0 deletions.
24 changes: 24 additions & 0 deletions src/expression.d
Expand Up @@ -3929,6 +3929,30 @@ extern (C++) class IdentifierExp : Expression
return e;
}

// If we've reached this point and are inside a with() scope then we may
// try one last attempt by checking whether the 'wthis' object supports
// dynamic dispatching via opDispatch.
// This is done by rewriting this expression as wthis.ident.
for (Scope* sc2 = sc; sc2; sc2 = sc2.enclosing)
{
if (!sc2.scopesym)
continue;

if (auto ss = sc2.scopesym.isWithScopeSymbol())
{
if (ss.withstate.wthis)
{
Expression e;
e = new VarExp(loc, ss.withstate.wthis);
e = new DotIdExp(loc, e, ident);
e = e.trySemantic(sc);
if (e)
return e;
}
break;
}
}

const(char)* n = importHint(ident.toChars());
if (n)
error("'%s' is not defined, perhaps you need to import %s; ?", ident.toChars(), n);
Expand Down
37 changes: 37 additions & 0 deletions test/compilable/b6400.d
@@ -0,0 +1,37 @@
/* TEST_OUTPUT:
---
Foo
Bar
---
*/
class Foo
{
void opDispatch(string name)() { pragma(msg, "Foo"); }
}
class Bar
{
void opDispatch(string name)() { pragma(msg, "Bar"); }
}
class Baz
{
}

void main()
{
auto foo = new Foo;
auto bar = new Bar;
auto baz = new Baz;

with (foo)
{
f0();
with (bar)
{
f1();
}
with (baz)
{
static assert(!__traits(compiles, f2()));
}
}
}

0 comments on commit c49fb86

Please sign in to comment.