Skip to content

Intrinify scala.compiletime.code #10296

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

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 19 additions & 6 deletions library/src/scala/compiletime/package.scala
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ package object compiletime {
* ```scala
* error(code"My error of this code: ${println("foo")}")
* ```
* or
* ```scala
* inline def errorOnThisCode(inline x: Any) =
* error(code"My error of this code: $x")
* ```
*/
inline def error(inline msg: String): Nothing = ???

Expand All @@ -50,7 +55,9 @@ package object compiletime {
* @note only `inline` arguments will be displayed as "code".
* Other values may display unintutively.
*/
transparent inline def code (inline args: Any*): String = ???
transparent inline def code (inline args: Any*): String =
// implemented in dotty.tools.dotc.typer.Inliner.Intrinsics
error("`code` was not evaluated by the compiler")

end extension

Expand All @@ -68,18 +75,24 @@ package object compiletime {
* twice(m) // error: expected a constant value but found: m
* ```
*/
inline def requireConst(inline x: Boolean | Byte | Short | Int | Long | Float | Double | Char | String): Unit = ()
inline def requireConst(inline x: Boolean | Byte | Short | Int | Long | Float | Double | Char | String): Unit =
// implemented in dotty.tools.dotc.typer.Inliner
error("`requireConst` was not evaluated by the compiler")

/** Same as `constValue` but returns a `None` if a constant value
* cannot be constructed from the provided type. Otherwise returns
* that value wrapped in `Some`.
*/
inline def constValueOpt[T]: Option[T] = ???
inline def constValueOpt[T]: Option[T] =
// implemented in dotty.tools.dotc.typer.Inliner
error("`constValueOpt` was not evaluated by the compiler")

/** Given a constant, singleton type `T`, convert it to a value
* of the same singleton type. For example: `assert(constValue[1] == 1)`.
*/
inline def constValue[T]: T = ???
inline def constValue[T]: T =
// implemented in dotty.tools.dotc.typer.Inliner
error("`constValue` was not evaluated by the compiler")

/** Given a tuple type `(X1, ..., Xn)`, returns a tuple value
* `(constValue[X1], ..., constValue[Xn])`.
Expand All @@ -106,8 +119,8 @@ package object compiletime {
*
* the returned value would be `2`.
*/
transparent inline def summonFrom[T](f: Nothing => T): T = ???

transparent inline def summonFrom[T](f: Nothing => T): T =
error("`summonFrom` was not evaluated by the compiler")

/** Summon a given value of type `T`. Usually, the argument is not passed explicitly.
* The summoning is delayed until the call has been fully inlined.
Expand Down
2 changes: 2 additions & 0 deletions library/src/scala/compiletime/testing/package.scala
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ package object testing {
* The code should be a sequence of expressions or statements that may appear in a block.
*/
inline def typeChecks(inline code: String): Boolean =
// implemented in package dotty.tools.dotc.typer.Inliner.Intrinsics
error("`typeChecks` was not checked by the compiler")

/** Whether the code type checks in the current context? If not,
Expand All @@ -27,5 +28,6 @@ package object testing {
* The code should be a sequence of expressions or statements that may appear in a block.
*/
inline def typeCheckErrors(inline code: String): List[Error] =
// implemented in package dotty.tools.dotc.typer.Inliner.Intrinsics
error("`typeCheckErrors` was not checked by the compiler")
}