Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Invalid escape sequences in s"" interpolated strings throw at runtime, inconsistent with f"" and double quoted strings #9234

Closed
scabug opened this issue Mar 18, 2015 · 8 comments

Comments

@scabug
Copy link

scabug commented Mar 18, 2015

Compiling

def s = "bad escape: \z"

produces a compile-time error like:

error: invalid escape character
       def s = "bad escape: \z"
                             ^

The standard s"" and f"" string interpolators use the same escape sequences as normal double quoted strings. Like double quoted strings the f"" interpolator reports bad sequences at compile time.

Inconsistently, the s"" string interpolator reports bad escape sequences at runtime by throwing an exception.

For example, compiling the following code succeeds:

def f = s"bad escape: \z"

At run time invoking f will produce an exception:

scala.StringContext$InvalidEscapeException: invalid escape '\z' not one of [\b, \t, \n, \f, \r, \\, \", \'] at index 12 in "bad escape: \z". Use \\ for literal \.
  at scala.StringContext$.loop$1(StringContext.scala:234)
  at scala.StringContext$.replace$1(StringContext.scala:244)
  at scala.StringContext$.treatEscapes0(StringContext.scala:248)
  at scala.StringContext$.treatEscapes(StringContext.scala:193)
  at scala.StringContext$$anonfun$s$1.apply(StringContext.scala:94)
  at scala.StringContext$$anonfun$s$1.apply(StringContext.scala:94)
  at scala.StringContext.standardInterpolator(StringContext.scala:122)
  at scala.StringContext.s(StringContext.scala:94)
  at .f(<console>:7)

I would prefer that the s"" interpolator report bad escape errors as early as possible, consistent with standard double quoted strings and f"" interpolated strings.

@scabug
Copy link
Author

scabug commented Mar 18, 2015

Imported From: https://issues.scala-lang.org/browse/SI-9234?orig=1
Reporter: @leifwickland
Affected Versions: 2.11.0, 2.11.4, 2.11.6

@scabug
Copy link
Author

scabug commented Nov 7, 2016

Gary Hewett (techano) said:
Just got hit by this "issue" -- thankful there is at least one place on the web where it is/was documented - right here :)

play.api.http.HttpErrorHandlerExceptions$$anon$1: Execution exception[[InvalidEscapeException: invalid escape '\s' not one of [\b, \t, \n, \f, \r, \, ", '] at index 2 in "),\s+(". Use \ for literal .]]
at play.api.http.HttpErrorHandlerExceptions$.throwableToUsefulException(HttpErrorHandler.scala:280)
at play.api.http.DefaultHttpErrorHandler.onServerError(HttpErrorHandler.scala:206)
at play.api.GlobalSettings$class.onError(GlobalSettings.scala:160)

I'd cast my vote (if possible) that yes a compile time error on this would definitely trump runtime errors...

@scabug
Copy link
Author

scabug commented Feb 1, 2017

Hubert Czerpak (hczerpak) said:
I'd like to have a look into this. I think I know were to check.

@scabug
Copy link
Author

scabug commented Feb 2, 2017

@som-snytt said (edited on Feb 2, 2017 12:23:34 AM UTC):
This would only be possible with a macro for s-interpolator. Probably there's no reason not to do that, and folks have asked for it. It would enable this issue and also trivial optimizations like s"hello, ${"world"}".

Just wanted to say, once more, how I don't like spending time formatting JIRA comments.

I meant s"hello, ${"world"}" where the whole. string is a literal. Wow, it also borked that. No, I refuse to click the question mark link to research the formatting language. I have a life, too.

@scabug
Copy link
Author

scabug commented Feb 2, 2017

Hubert Czerpak (hczerpak) said:
Thanks, I thought it might be what I need to do since f-interpolator seems to be implemented as a macro and it doesn't have the same problem as s-interpolator.

@latkin
Copy link

latkin commented Mar 27, 2018

Just got hit by this, would love to see a fix.

@NthPortal
Copy link

It looks like this may be fixed in 2.13 (see scala/scala-dev#475), but can't be in 2.12 for bincompat reasons

@som-snytt
Copy link

There is a trivial test test/files/neg/t6631.scala

scala 2.13.2> val x = 42
val x: Int = 42

scala 2.13.2> s"$x"
val res0: String = 42

scala 2.13.2> s"$x\t$x"
val res1: String = 42   42

scala 2.13.2> s"$x\x$x"
                ^
              error: invalid escape '\x' not one of [\b, \t, \n, \f, \r, \\, \", \', \uxxxx] at index 0 in "\x". Use \\ for literal \.

scala 2.13.2> def f = () => s"$x\x$x"
                              ^
              error: invalid escape '\x' not one of [\b, \t, \n, \f, \r, \\, \", \', \uxxxx] at index 0 in "\x". Use \\ for literal \.

scala/scala#7678

@SethTisue SethTisue modified the milestones: Backlog, 2.13.0 May 6, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants