Skip to content

Commit

Permalink
fix Issue 15624 - opApply with @safe and @System variants can't be us…
Browse files Browse the repository at this point in the history
…ed with foreach syntax
  • Loading branch information
WalterBright committed Mar 18, 2018
1 parent be288c6 commit 70fb7cb
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 3 deletions.
14 changes: 11 additions & 3 deletions src/dmd/opover.d
Expand Up @@ -2014,12 +2014,20 @@ private FuncDeclaration findBestOpApplyMatch(Expression ethis, FuncDeclaration f
fd_ambig = null;
match = m;
}
else if (m == match)
fd_ambig = f;
else if (m == match && m > MATCH.nomatch)
{
assert(fd_best);
/* Ignore covariant matches, as later on it can be redone
* after the opApply delegate has its attributes inferred.
*/
if (tf.covariant(fd_best.type) != 1 &&
fd_best.type.covariant(tf) != 1)
fd_ambig = f; // not covariant, so ambiguous
}
return 0; // continue
});

if (fd_ambig && fd_best)
if (fd_ambig)
{
.error(ethis.loc, "`%s.%s` matches more than one declaration:\n`%s`: `%s`\nand:\n`%s`: `%s`",
ethis.toChars(), fstart.ident.toChars(),
Expand Down
51 changes: 51 additions & 0 deletions test/runnable/test15624.d
@@ -0,0 +1,51 @@
/* PERMUTE_ARGS:
*/

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

struct Foo {
int x;
int opApply(int delegate(int, string, string) @safe dg) @safe {
x = 1;
return 0;
}
int opApply(int delegate(int, string, string) @system dg) @system {
x = 2;
return 0;
}
}

void testSafe() @safe {
Foo foo;
foreach (i, k, v; foo) {
}
assert(foo.x == 1);
}

void testSystem() @system {
Foo foo;
foreach (i, k, v; foo) {
}
assert(foo.x == 2);
}

void test() @system
{
Foo f;

int dgsafe (int x, string s, string t) @safe { return 1; }
int dgsystem(int x, string s, string t) @system { return 1; }

f.opApply(&dgsafe);
assert(f.x == 1);
f.opApply(&dgsystem);
assert(f.x == 2);
}

int main()
{
testSafe();
testSystem();
test();
return 0;
}

0 comments on commit 70fb7cb

Please sign in to comment.