diff options
-rw-r--r-- | src/compiler/scala/tools/nsc/typechecker/Typers.scala | 27 | ||||
-rw-r--r-- | test/files/neg/forgot-interpolator.check | 21 | ||||
-rw-r--r-- | test/files/neg/forgot-interpolator.scala | 32 | ||||
-rw-r--r-- | test/files/neg/t7848-interp-warn.check | 9 | ||||
-rw-r--r-- | test/files/neg/t7848-interp-warn.flags | 1 | ||||
-rw-r--r-- | test/files/neg/t7848-interp-warn.scala | 13 |
6 files changed, 92 insertions, 11 deletions
diff --git a/src/compiler/scala/tools/nsc/typechecker/Typers.scala b/src/compiler/scala/tools/nsc/typechecker/Typers.scala index e07c68de8a..157c6ba4de 100644 --- a/src/compiler/scala/tools/nsc/typechecker/Typers.scala +++ b/src/compiler/scala/tools/nsc/typechecker/Typers.scala @@ -4874,26 +4874,37 @@ trait Typers extends Adaptations with Tags with TypersTracking with PatternTyper } // Warn about likely interpolated strings which are missing their interpolators - def warnMissingInterpolator(tree: Literal) { + def warnMissingInterpolator(tree: Literal) = if (!isPastTyper) { // Unfortunately implicit not found strings looks for all the world like // missing interpolators. def isArgToImplicitNotFound = context.enclosingApply.tree match { case Apply(fn, _) => fn.symbol != null && fn.symbol.enclClass == ImplicitNotFoundClass case _ => false } + def warnAbout(s: String) = { + def names = InterpolatorIdentRegex findAllIn s map (n => TermName(n stripPrefix "$")) + def isSuspiciousExpr = (InterpolatorCodeRegex findFirstIn s).nonEmpty + //def isSuspiciousName = names exists (lookUp _ andThen (_.exists)) + def suspiciousName = names find (lookUp _ andThen (_.exists)) + def lookUp(n: TermName) = context.lookupSymbol(n, !_.alternatives.exists(symRequiresArg)).symbol + def symRequiresArg(s: Symbol) = ( + s.paramss.nonEmpty + && (s.paramss.head.headOption filterNot (_.isImplicit)).isDefined + ) + val suggest = "Did you forget the interpolator?" + if (isSuspiciousExpr) + unit.warning(tree.pos, s"That looks like an interpolated expression! $suggest") + else /* if (isSuspiciousName) */ suspiciousName foreach (n => + unit.warning(tree.pos, s"`$$$n` looks like an interpolated identifier! $suggest") + ) + } tree.value match { case Constant(s: String) => - def names = InterpolatorIdentRegex findAllIn s map (n => newTermName(n stripPrefix "$")) - def suspicious = ( - (InterpolatorCodeRegex findFirstIn s).nonEmpty - || (names exists (n => context.lookupSymbol(n, _ => true).symbol.exists)) - ) val noWarn = ( isArgToImplicitNotFound || !(s contains ' ') // another heuristic - e.g. a string with only "$asInstanceOf" ) - if (!noWarn && suspicious) - unit.warning(tree.pos, "looks like an interpolated String; did you forget the interpolator?") + if (!noWarn) warnAbout(s) case _ => } } diff --git a/test/files/neg/forgot-interpolator.check b/test/files/neg/forgot-interpolator.check index f6de4d7b3a..a96431841f 100644 --- a/test/files/neg/forgot-interpolator.check +++ b/test/files/neg/forgot-interpolator.check @@ -1,9 +1,24 @@ -forgot-interpolator.scala:4: warning: looks like an interpolated String; did you forget the interpolator? +forgot-interpolator.scala:4: warning: `$bippy` looks like an interpolated identifier! Did you forget the interpolator? def f = "Put the $bippy in the $bippy!" // warn ^ -forgot-interpolator.scala:14: warning: looks like an interpolated String; did you forget the interpolator? +forgot-interpolator.scala:14: warning: That looks like an interpolated expression! Did you forget the interpolator? def f = """Put the ${println("bippy")} in the bippy!""" // warn ^ +forgot-interpolator.scala:30: warning: `$beppo` looks like an interpolated identifier! Did you forget the interpolator? + def f = "$beppo was a marx bros who saw dollars." // warn + ^ +forgot-interpolator.scala:34: warning: `$aleppo` looks like an interpolated identifier! Did you forget the interpolator? + def f = "$aleppo is a pepper and a city." // warn + ^ +forgot-interpolator.scala:40: warning: `$bar` looks like an interpolated identifier! Did you forget the interpolator? + def f = "$bar is private, shall we warn just in case?" // warn + ^ +forgot-interpolator.scala:45: warning: `$hippo` looks like an interpolated identifier! Did you forget the interpolator? + def h = "$hippo takes an implicit" // warn + ^ +forgot-interpolator.scala:37: warning: private method in class Bar is never used + private def bar = 8 + ^ error: No warnings can be incurred under -Xfatal-warnings. -two warnings found +7 warnings found one error found diff --git a/test/files/neg/forgot-interpolator.scala b/test/files/neg/forgot-interpolator.scala index d67db82643..5067f1dce9 100644 --- a/test/files/neg/forgot-interpolator.scala +++ b/test/files/neg/forgot-interpolator.scala @@ -13,3 +13,35 @@ class B { class C { def f = """Put the ${println("bippy")} in the bippy!""" // warn } + +package object test { + def aleppo = 9 + def greppo(n: Int) = ??? + def zappos(n: Int)(implicit ord: math.Ordering[Int]) = ??? + def hippo(implicit n: Int) = ??? +} + +package test { + // not sure if overloading is kosher in pkg obj yet + class Doo { + def beppo(i: Int) = 8 * i + def beppo = 8 + class Dah extends Doo { + def f = "$beppo was a marx bros who saw dollars." // warn + } + } + class E { + def f = "$aleppo is a pepper and a city." // warn + } + class Bar { + private def bar = 8 + } + class Baz extends Bar { + def f = "$bar is private, shall we warn just in case?" // warn + } + class G { + def g = "$greppo takes an arg" // no warn + def z = "$zappos takes an arg too" // no warn + def h = "$hippo takes an implicit" // warn + } +} diff --git a/test/files/neg/t7848-interp-warn.check b/test/files/neg/t7848-interp-warn.check new file mode 100644 index 0000000000..cbdc9f4c27 --- /dev/null +++ b/test/files/neg/t7848-interp-warn.check @@ -0,0 +1,9 @@ +t7848-interp-warn.scala:7: warning: `$foo` looks like an interpolated identifier! Did you forget the interpolator? + "An important $foo message!" + ^ +t7848-interp-warn.scala:11: warning: That looks like an interpolated expression! Did you forget the interpolator? + "A doubly important ${foo * 2} message!" + ^ +error: No warnings can be incurred under -Xfatal-warnings. +two warnings found +one error found diff --git a/test/files/neg/t7848-interp-warn.flags b/test/files/neg/t7848-interp-warn.flags new file mode 100644 index 0000000000..7949c2afa2 --- /dev/null +++ b/test/files/neg/t7848-interp-warn.flags @@ -0,0 +1 @@ +-Xlint -Xfatal-warnings diff --git a/test/files/neg/t7848-interp-warn.scala b/test/files/neg/t7848-interp-warn.scala new file mode 100644 index 0000000000..bb3eeff60c --- /dev/null +++ b/test/files/neg/t7848-interp-warn.scala @@ -0,0 +1,13 @@ + +package test + +object Test { + def f = { + val foo = "bar" + "An important $foo message!" + } + def g = { + val foo = "bar" + "A doubly important ${foo * 2} message!" + } +} |