Skip to content

Commit 77dc93e

Browse files
authored
Merge pull request #5363 from dotty-staging/fix/5351
Fix #5351: Warn about implicit conversions using ImplicitConverter
2 parents 4100f88 + 1f2022d commit 77dc93e

File tree

4 files changed

+16
-4
lines changed

4 files changed

+16
-4
lines changed

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

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -607,15 +607,24 @@ trait Checking {
607607
/** If `sym` is an implicit conversion, check that implicit conversions are enabled.
608608
* @pre sym.is(Implicit)
609609
*/
610-
def checkImplicitConversionDefOK(sym: Symbol)(implicit ctx: Context): Unit = sym.info.stripPoly match {
611-
case mt @ MethodType(_ :: Nil)
612-
if !mt.isImplicitMethod && !sym.is(Synthetic) => // it's a conversion
610+
def checkImplicitConversionDefOK(sym: Symbol)(implicit ctx: Context): Unit = {
611+
def check(): Unit = {
613612
checkFeature(
614613
defn.LanguageModuleClass, nme.implicitConversions,
615614
i"Definition of implicit conversion $sym",
616615
ctx.owner.topLevelClass,
617616
sym.pos)
618-
case _ =>
617+
}
618+
619+
sym.info.stripPoly match {
620+
case mt @ MethodType(_ :: Nil)
621+
if !mt.isImplicitMethod && !sym.is(Synthetic) => // it's a conversion
622+
check()
623+
case AppliedType(tycon, _)
624+
if tycon.derivesFrom(defn.Predef_ImplicitConverter) && !sym.is(Synthetic) =>
625+
check()
626+
case _ =>
627+
}
619628
}
620629

621630
/** If `sym` is an implicit conversion, check that that implicit conversions are enabled, unless

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1419,6 +1419,7 @@ class Typer extends Namer
14191419
def typedValDef(vdef: untpd.ValDef, sym: Symbol)(implicit ctx: Context): Tree = track("typedValDef") {
14201420
val ValDef(name, tpt, _) = vdef
14211421
completeAnnotations(vdef, sym)
1422+
if (sym is Implicit) checkImplicitConversionDefOK(sym)
14221423
val tpt1 = checkSimpleKinded(typedType(tpt))
14231424
val rhs1 = vdef.rhs match {
14241425
case rhs @ Ident(nme.WILDCARD) => rhs withType tpt1.tpe

tests/neg-custom-args/impl-conv/A.scala

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package implConv
33
object A {
44

55
implicit def s2i(x: String): Int = Integer.parseInt(x) // error: feature
6+
implicit val i2s: ImplicitConverter[Int, String] = _.toString // error: feature
67

78
implicit class Foo(x: String) {
89
def foo = x.length

tests/neg-custom-args/impl-conv/B.scala

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,6 @@ object B {
66
"".foo
77

88
val x: Int = "" // error: feature
9+
val y: String = 1 // error: feature
910

1011
}

0 commit comments

Comments
 (0)