Skip to content

Commit 3506192

Browse files
committed
Reject ambiguities between implicit conversions and extension methods
1 parent 9d7093c commit 3506192

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
@@ -939,16 +939,22 @@ trait Implicits { self: Typer =>
939939
adapt(generated, pt, locked)
940940
else {
941941
val untpdGenerated = untpd.TypedSplice(generated)
942-
if (cand.isConversion)
942+
def tryConversion(implicit ctx: Context) =
943943
typed(
944944
untpd.Apply(untpdGenerated, untpd.TypedSplice(argument) :: Nil),
945945
pt, locked)
946-
else {
947-
assert(cand.isExtension)
946+
if (cand.isExtension) {
948947
val SelectionProto(name: TermName, mbrType, _, _) = pt
949-
extMethodApply(untpd.Select(untpdGenerated, name), argument, mbrType)
948+
val result = extMethodApply(untpd.Select(untpdGenerated, name), argument, mbrType)
949+
if (!ctx.reporter.hasErrors && cand.isConversion) {
950+
val testCtx = ctx.fresh.setExploreTyperState()
951+
tryConversion(testCtx)
952+
if (testCtx.reporter.hasErrors)
953+
ctx.error(em"ambiguous implicit: $generated is eligible both as an implicit conversion and as an extension method container")
954+
}
955+
result
950956
}
951-
// TODO disambiguate if candidate can be an extension or a conversion
957+
else tryConversion
952958
}
953959
lazy val shadowing =
954960
typedUnadapted(untpd.Ident(cand.implicitRef.implicitName) withPos pos.toSynthetic)(

0 commit comments

Comments
 (0)