Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Compile the file at runtime #12854

Unanswered
mbore asked this question in Compiler internals Q&A
Jun 17, 2021 · 1 comments · 4 replies
Discussion options

I'd like to test, whether a source code passed as a string at runtime is a valid scala program.

My first attempt was to parse the input String to an Expr[] and then execute it with staging.run, but I haven't found any exposed parser API to transform a String into an Expr[], did I missed something?

My second attempt was to use scala.compiletime.testing.typeChecks inside a block passed to scala.quoted.staging.withQuotes, but typeChecks requires a statically known String so it also doesn't work for me. Is it possible to workaround it with the multi-stage programming?

The current approach is based on the Vulpix Framework and uses dotty.tools.dotc.Driver to compile source files (e.g. https://github.com/softwaremill/macwire/blob/300e109f2df670a8ec3178a83af73a50ea983f01/test-util/src/main/scala-3/com/softwaremill/macwire/CompileTestsSupport.scala#L92), but it seems that the Vulpix would fulfil my use case, have you considered publishing Vulpix as a separate library?

Or maybe I missed something and there is another way to compile a file at runtime?

You must be logged in to vote

Replies: 1 comment 4 replies

Comment options

Compiling a new source file at runtime is not in the scope of the metaprogramming API as it cannot be type-checked att compile-time by the nature of the problem.

typeChecks will not work at runtime as it evaluates while typing the code which is too early for the run.

The way to compile a source file is by using the compiler directly. This could compile code that contains typeChecks to generate code in the event of compilation failure.

You must be logged in to vote
4 replies
Comment options

Ok, thank you for the quick answer.
So as far as I understand, I should stick with dotty.tools.dotc.Driver#process.

Nevertheless, I am also interested in the Vulpix Framework, is it also meant to be used outside of the dotty tests?

Comment options

Vulpix was designed for internal use only. It might not be simple to use from outside the dotty project. We would also not be able to guarantee the stability of the API.

Comment options

Hopefully there would be a name change before exposing it as API.

This is a common ask. I mean "test for whether it compiles", not the framework name.

Comment options

scala.compiletime.testing.typeChecks("class Foo") and typeCheckErrors exists to that end, in scala3-library.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

AltStyle によって変換されたページ (->オリジナル) /