Skip to content

False positive in missing interpolator warning #10217

@scabug

Description

@scabug
scala> object T; """foo \$T bar"""
<console>:13: warning: possible missing interpolator: detected interpolated identifier `$T`
 """foo \$T bar"""
 ^
defined object T
res0: String = foo \$T bar

but then..

scala> object T; s"""foo \$T bar"""
scala.StringContext$InvalidEscapeException: invalid escape at terminal index 4 in "foo \". Use \\ for literal \.
  at scala.StringContext$.loop$1(StringContext.scala:211)
  at scala.StringContext$.replace$1(StringContext.scala:246)
  at scala.StringContext$.treatEscapes0(StringContext.scala:250)
  at scala.StringContext$.treatEscapes(StringContext.scala:195)
  at scala.StringContext.$anonfun$s$1(StringContext.scala:95)
  at scala.StringContext.standardInterpolator(StringContext.scala:123)
  at scala.StringContext.s(StringContext.scala:95)
  ... 29 elided

N.B. the difference between line 1 and line 2 is the extra 's' before the string.

The compiler warns about a "possible missing interpolator" when no interpolation would occur.

workaround

Use the f-interpolator and double the "$"'s and the ""s:

scala> object T; f"""foo \\$$T bar"""
object T
val res2: String = foo \$T bar

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions