diff options
Diffstat (limited to 'src/compiler')
-rw-r--r-- | src/compiler/scala/tools/nsc/typechecker/Typers.scala | 27 |
1 files changed, 19 insertions, 8 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 _ => } } |