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

Support constant folding for s, f and raw interpolators. #10315

Open
nicolasstucki opened this issue Nov 13, 2020 · 3 comments
Open

Support constant folding for s, f and raw interpolators. #10315

nicolasstucki opened this issue Nov 13, 2020 · 3 comments

Comments

@nicolasstucki
Copy link
Contributor

We should be able to constant fold s, f and raw interpolator with constant inputs.

  val r: "abc" = s"ab${"c"}"

This would be useful when combined with inlining

inline val x = "abc"
inline val y = s"${y}dce"

A realistic use of this feature would be with the scala.compiletime.error method.

inline def f(x: Int) = 
  compiletime.error(s"Error code: $x")

f(3) // error: `Error code: 3`

This would need to happen when typing, probably in Inliner.

@nicolasstucki nicolasstucki changed the title Support constant folding for s, f and raw intrpolators. Support constant folding for s, f and raw interpolators. May 11, 2021
@soronpo
Copy link
Collaborator

soronpo commented May 13, 2021

@nicolasstucki Say I want to tackle this, I have some questions regarding the implementation.

  1. Where should I start? Where are these implemented in the compiler?
  2. Does changing s, f, etc. to inline affect binary compatibility?
  3. Theoretically we can special case this only within the context of compiletime.error(), since we can read the tree as a raw StringContext and constant fold it accordingly. Is this better, or should we treat interpolation just like arithmetic operations that are inlined anywhere?

@soronpo soronpo self-assigned this May 13, 2021
@som-snytt
Copy link
Collaborator

I must have looked at this on Mar 22 when I had less time. I think the answer to 3 must be that it should be normal constant folding, it's adding string constants. Getting the Scala 2 f to emit constants was a priority. There was also a Scala 2 ticket to make interpolation work normally in the annotation, where type param names are available as constant strings in a special context. I started hacking on Scala 3 f but haven't returned to it yet in my freer time. I remember wondering if it matters that stringInterpolatorOpt runs rather late.

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

3 participants