Skip to content

Commit

Permalink
Promote implicit recursion to lint
Browse files Browse the repository at this point in the history
It was a standalone warning. Augment test example.
  • Loading branch information
som-snytt committed May 30, 2020
1 parent 8d2a47b commit c58f925
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 5 deletions.
5 changes: 4 additions & 1 deletion src/compiler/scala/tools/nsc/settings/Warnings.scala
Expand Up @@ -148,7 +148,8 @@ trait Warnings {

val warnExtraImplicit = BooleanSetting("-Wextra-implicit", "Warn when more than one implicit parameter section is defined.") withAbbreviation "-Ywarn-extra-implicit"

val warnSelfImplicit = BooleanSetting("-Wself-implicit", "Warn when an implicit resolves to an enclosing self-definition.") withAbbreviation "-Ywarn-self-implicit"
@deprecated("Use lintImplicitRecursion", since="2.13.3")
val warnSelfImplicit = BooleanSetting("-Wself-implicit", "An implicit resolves to an enclosing definition.") withAbbreviation "-Ywarn-self-implicit" withDeprecationMessage "Use -Xlint:implicit-recurses"

// Experimental lint warnings that are turned off, but which could be turned on programmatically.
// They are not activated by -Xlint and can't be enabled on the command line because they are not
Expand Down Expand Up @@ -193,6 +194,7 @@ trait Warnings {
val RecurseWithDefault = LintWarning("recurse-with-default", "Recursive call used default argument.")
val UnitSpecialization = LintWarning("unit-special", "Warn for specialization of Unit in parameter position.")
val MultiargInfix = LintWarning("multiarg-infix", "Infix operator was defined or used with multiarg operand.")
val ImplicitRecurses = LintWarning("implicit-recurses", "Implicit resolves to an enclosing definition.")

def allLintWarnings = values.toSeq.asInstanceOf[Seq[LintWarning]]
}
Expand Down Expand Up @@ -225,6 +227,7 @@ trait Warnings {
def warnRecurseWithDefault = lint contains RecurseWithDefault
def unitSpecialization = lint contains UnitSpecialization
def multiargInfix = lint contains MultiargInfix
def lintImplicitRecursion = lint.contains(ImplicitRecurses) || warnSelfImplicit

// The Xlint warning group.
val lint = MultiChoiceSetting(
Expand Down
2 changes: 1 addition & 1 deletion src/compiler/scala/tools/nsc/typechecker/Implicits.scala
Expand Up @@ -127,7 +127,7 @@ trait Implicits {
if (StatisticsStatics.areSomeColdStatsEnabled) statistics.stopCounter(findMemberImpl, findMemberStart)
if (StatisticsStatics.areSomeColdStatsEnabled) statistics.stopCounter(subtypeImpl, subtypeStart)

if (result.isSuccess && settings.warnSelfImplicit && result.tree.symbol != null) {
if (result.isSuccess && settings.lintImplicitRecursion && result.tree.symbol != null) {
val s =
if (result.tree.symbol.isAccessor) result.tree.symbol.accessed
else if (result.tree.symbol.isModule) result.tree.symbol.moduleClass
Expand Down
5 changes: 4 additions & 1 deletion test/files/neg/implicitly-self.check
Expand Up @@ -10,6 +10,9 @@ implicitly-self.scala:10: warning: Implicit resolves to enclosing value t
implicitly-self.scala:13: warning: Implicit resolves to enclosing object tcString
implicit object tcString extends TC[String] { def ix = implicitly[TC[String]].ix + 1 }
^
implicitly-self.scala:22: warning: Implicit resolves to enclosing method bad
implicit def bad[A](a: A)(implicit ev: A => T): Sizeable = ev(a)
^
error: No warnings can be incurred under -Werror.
4 warnings
5 warnings
1 error
14 changes: 12 additions & 2 deletions test/files/neg/implicitly-self.scala
@@ -1,7 +1,7 @@
// scalac: -Xfatal-warnings -Ywarn-self-implicit
// scalac: -Werror -Xlint:implicit-recurses
//

trait TC[T] { def ix: Int }
trait TC[A] { def ix: Int }

object Test {
implicit def c: Char = implicitly[Char]
Expand All @@ -12,3 +12,13 @@ object Test {
}
implicit object tcString extends TC[String] { def ix = implicitly[TC[String]].ix + 1 }
}

import language.implicitConversions

trait T
trait Sizeable { def size: Int }

class `t8357 warn on self-involved implicit` {
implicit def bad[A](a: A)(implicit ev: A => T): Sizeable = ev(a)
bad(new T{})
}

0 comments on commit c58f925

Please sign in to comment.