diff --git a/src/dmd/expressionsem.d b/src/dmd/expressionsem.d index 1a1d79e00e78..f47d400549d9 100644 --- a/src/dmd/expressionsem.d +++ b/src/dmd/expressionsem.d @@ -5060,6 +5060,20 @@ private extern (C++) final class ExpressionSemanticVisitor : Visitor } else if (isNeedThisScope(sc, exp.f)) { + // At this point it is possible that `f` has an ambiguity error that was + // silenced because the previous call to `resolveFuncCall` was done using + // `FuncResolveFlag.overloadOnly`. To make sure that a proper error message + // is printed, redo the call with `FuncResolveFlag.standard`. + // + // https://issues.dlang.org/show_bug.cgi?id=22157 + if (exp.f.overnext) + exp.f = resolveFuncCall(exp.loc, sc, exp.f, tiargs, null, exp.arguments, FuncResolveFlag.standard); + + if (!exp.f || exp.f.errors) + return setError(); + + // If no error is printed, it means that `f` is the single matching overload + // and it needs `this`. exp.error("need `this` for `%s` of type `%s`", exp.f.toChars(), exp.f.type.toChars()); return setError(); } diff --git a/test/fail_compilation/fail22157.d b/test/fail_compilation/fail22157.d new file mode 100644 index 000000000000..d25aaada2c58 --- /dev/null +++ b/test/fail_compilation/fail22157.d @@ -0,0 +1,34 @@ +// https://issues.dlang.org/show_bug.cgi?id=22157 + +/* +TEST_OUTPUT: +--- +fail_compilation/fail22157.d(32): Error: `fail22157.S!true.S.foo` called with argument types `()` matches both: +fail_compilation/fail22157.d(21): `fail22157.S!true.S.foo()` +and: +fail_compilation/fail22157.d(22): `fail22157.S!true.S.foo()` +fail_compilation/fail22157.d(33): Error: `fail22157.S!false.S.foo` called with argument types `()` matches both: +fail_compilation/fail22157.d(26): `fail22157.S!false.S.foo()` +and: +fail_compilation/fail22157.d(27): `fail22157.S!false.S.foo()` +--- +*/ + +struct S(bool b) +{ + static if(b) + { + void foo() {} + static void foo() {} + } + else + { + static void foo() {} + void foo() {} + } +} + +void main() { + S!true.foo; + S!false.foo; +}