diff --git a/compiler/src/dotty/tools/dotc/typer/Checking.scala b/compiler/src/dotty/tools/dotc/typer/Checking.scala index 67a5e682253b..50e7789b2d5a 100644 --- a/compiler/src/dotty/tools/dotc/typer/Checking.scala +++ b/compiler/src/dotty/tools/dotc/typer/Checking.scala @@ -607,15 +607,24 @@ trait Checking { /** If `sym` is an implicit conversion, check that implicit conversions are enabled. * @pre sym.is(Implicit) */ - def checkImplicitConversionDefOK(sym: Symbol)(implicit ctx: Context): Unit = sym.info.stripPoly match { - case mt @ MethodType(_ :: Nil) - if !mt.isImplicitMethod && !sym.is(Synthetic) => // it's a conversion + def checkImplicitConversionDefOK(sym: Symbol)(implicit ctx: Context): Unit = { + def check(): Unit = { checkFeature( defn.LanguageModuleClass, nme.implicitConversions, i"Definition of implicit conversion $sym", ctx.owner.topLevelClass, sym.pos) - case _ => + } + + sym.info.stripPoly match { + case mt @ MethodType(_ :: Nil) + if !mt.isImplicitMethod && !sym.is(Synthetic) => // it's a conversion + check() + case AppliedType(tycon, _) + if tycon.derivesFrom(defn.Predef_ImplicitConverter) && !sym.is(Synthetic) => + check() + case _ => + } } /** If `sym` is an implicit conversion, check that that implicit conversions are enabled, unless diff --git a/compiler/src/dotty/tools/dotc/typer/Typer.scala b/compiler/src/dotty/tools/dotc/typer/Typer.scala index 643faf24e0c6..03e902381f95 100644 --- a/compiler/src/dotty/tools/dotc/typer/Typer.scala +++ b/compiler/src/dotty/tools/dotc/typer/Typer.scala @@ -1414,6 +1414,7 @@ class Typer extends Namer def typedValDef(vdef: untpd.ValDef, sym: Symbol)(implicit ctx: Context): Tree = track("typedValDef") { val ValDef(name, tpt, _) = vdef completeAnnotations(vdef, sym) + if (sym is Implicit) checkImplicitConversionDefOK(sym) val tpt1 = checkSimpleKinded(typedType(tpt)) val rhs1 = vdef.rhs match { case rhs @ Ident(nme.WILDCARD) => rhs withType tpt1.tpe diff --git a/tests/neg-custom-args/impl-conv/A.scala b/tests/neg-custom-args/impl-conv/A.scala index 717e2378aae7..6d6e8cb30d4b 100644 --- a/tests/neg-custom-args/impl-conv/A.scala +++ b/tests/neg-custom-args/impl-conv/A.scala @@ -3,6 +3,7 @@ package implConv object A { implicit def s2i(x: String): Int = Integer.parseInt(x) // error: feature + implicit val i2s: ImplicitConverter[Int, String] = _.toString // error: feature implicit class Foo(x: String) { def foo = x.length diff --git a/tests/neg-custom-args/impl-conv/B.scala b/tests/neg-custom-args/impl-conv/B.scala index 30a6be36af8a..3ea37791dac3 100644 --- a/tests/neg-custom-args/impl-conv/B.scala +++ b/tests/neg-custom-args/impl-conv/B.scala @@ -6,5 +6,6 @@ object B { "".foo val x: Int = "" // error: feature + val y: String = 1 // error: feature }