Skip to content

Commit 9150c11

Browse files
committed
Reject ambiguities between implicit conversions and extension methods
1 parent e26281f commit 9150c11

File tree

1 file changed

+11
-5
lines changed

1 file changed

+11
-5
lines changed

compiler/src/dotty/tools/dotc/typer/Implicits.scala

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -967,16 +967,22 @@ trait Implicits { self: Typer =>
967967
adapt(generated, pt, locked)
968968
else {
969969
val untpdGenerated = untpd.TypedSplice(generated)
970-
if (cand.isConversion)
970+
def tryConversion(implicit ctx: Context) =
971971
typed(
972972
untpd.Apply(untpdGenerated, untpd.TypedSplice(argument) :: Nil),
973973
pt, locked)
974-
else {
975-
assert(cand.isExtension)
974+
if (cand.isExtension) {
976975
val SelectionProto(name: TermName, mbrType, _, _) = pt
977-
extMethodApply(untpd.Select(untpdGenerated, name), argument, mbrType)
976+
val result = extMethodApply(untpd.Select(untpdGenerated, name), argument, mbrType)
977+
if (!ctx.reporter.hasErrors && cand.isConversion) {
978+
val testCtx = ctx.fresh.setExploreTyperState()
979+
tryConversion(testCtx)
980+
if (testCtx.reporter.hasErrors)
981+
ctx.error(em"ambiguous implicit: $generated is eligible both as an implicit conversion and as an extension method container")
982+
}
983+
result
978984
}
979-
// TODO disambiguate if candidate can be an extension or a conversion
985+
else tryConversion
980986
}
981987
lazy val shadowing =
982988
typedUnadapted(untpd.Ident(cand.implicitRef.implicitName) withPos pos.toSynthetic)(

0 commit comments

Comments
 (0)