Skip to content

Commit

Permalink
Fix issue 6400 - Better interaction between with() and opDispatch (Pa…
Browse files Browse the repository at this point in the history
…rt 2)
  • Loading branch information
JinShil committed Nov 26, 2017
1 parent 46fc949 commit 4dfa27e
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 38 deletions.
4 changes: 3 additions & 1 deletion src/ddmd/expressionsem.d
Expand Up @@ -1353,6 +1353,9 @@ private extern (C++) final class ExpressionSemanticVisitor : Visitor
// 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.
// The innermost with() scope of the hierarchy to satisfy the condition
// above wins.
// https://issues.dlang.org/show_bug.cgi?id=6400
for (Scope* sc2 = sc; sc2; sc2 = sc2.enclosing)
{
if (!sc2.scopesym)
Expand All @@ -1372,7 +1375,6 @@ private extern (C++) final class ExpressionSemanticVisitor : Visitor
return;
}
}
break;
}
}

Expand Down
37 changes: 0 additions & 37 deletions test/compilable/b6400.d

This file was deleted.

69 changes: 69 additions & 0 deletions test/runnable/b6400.d
@@ -0,0 +1,69 @@
/* TEST_OUTPUT:
---
Foo
Bar
Foo
Bar
Bar
Foo
Bar
---
*/

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

enum int base(string name) = 10 * (name[$-1] - '0');
struct Foo { int opDispatch(string name)() { pragma(msg, "Foo"); return base!name + 1; } }
struct Bar { int opDispatch(string name)() { pragma(msg, "Bar"); return base!name + 2; } }
struct Baz { }

void main()
{
assert(test());
static assert(test());
}

bool test()
{
auto foo = new Foo;
auto bar = new Bar;
auto baz = new Baz;

with (foo)
{
assert(f1() == 11);
with (baz) assert(f1() == 11);
with (bar)
{
assert(f2() == 22);
with (baz) assert(f2() == 22);
with (foo)
{
assert(f3() == 31);
with (baz) assert(f3() == 31);
with (bar)
{
assert(f4() == 42);
with (baz) assert(f4() == 42);
with (baz)
{
assert(f5() == 52);
with (baz) assert(f5() == 52);
}
with (foo)
{
assert(f6() == 61);
with (baz) assert(f6() == 61);
}
with (bar)
{
assert(f7() == 72);
with (baz) assert(f7() == 72);
}
}
}
}
}

return true;
}

0 comments on commit 4dfa27e

Please sign in to comment.