Skip to content

Only compile quoted code in bootstrapped library #8948

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 all commits
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
11 changes: 11 additions & 0 deletions library/src-bootstrapped/dotty/internal/CompileTimeMacros.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package dotty.internal

import scala.quoted._

object CompileTimeMacros:
def codeExpr(using qctx: QuoteContext)(sc: Expr[StringContext], args: Expr[Seq[Any]]): Expr[String] =
(sc, args) match
case (Expr.StringContext(Consts(parts)), Varargs(args2)) =>
Expr(StringContext(parts: _*).s(args2.map(_.show): _*))
case _ =>
qctx.throwError("compiletime.code must be used as a string interpolator `code\"...\"`")
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package dotty.internal

import scala.quoted._

object CompileTimeMacros:
def codeExpr(using qctx: QuoteContext)(sc: Expr[StringContext], args: Expr[Seq[Any]]): Expr[String] =
throw new Exception("Non bootstrapped library")
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package scala.internal.quoted

import scala.annotation.{Annotation, compileTimeOnly}
import scala.quoted._

@compileTimeOnly("Illegal reference to `scala.internal.quoted.CompileTime`")
object CompileTime {

@compileTimeOnly("Illegal reference to `scala.internal.quoted.CompileTime.exprQuote`")
def exprQuote[T](x: T): QuoteContext ?=> Expr[T] = ???

@compileTimeOnly("Illegal reference to `scala.internal.quoted.CompileTime.exprSplice`")
def exprSplice[T](x: QuoteContext ?=> Expr[T]): T = ???

@compileTimeOnly("Illegal reference to `scala.internal.quoted.CompileTime.exprNestedSplice`")
def exprNestedSplice[T](ctx: QuoteContext)(x: ctx.Nested ?=> Expr[T]): T = ???

@compileTimeOnly("Illegal reference to `scala.internal.quoted.CompileTime.typeQuote`")
def typeQuote[T <: AnyKind]: Type[T] = ???

@compileTimeOnly("Illegal reference to `scala.internal.quoted.CompileTime.patternHole`")
def patternHole[T]: T = ???

@compileTimeOnly("Illegal reference to `scala.internal.quoted.CompileTime.patternBindHole`")
class patternBindHole extends Annotation

@compileTimeOnly("Illegal reference to `scala.internal.quoted.CompileTime.patternType`")
class patternType extends Annotation

@compileTimeOnly("Illegal reference to `scala.internal.quoted.CompileTime.fromAbove`")
class fromAbove extends Annotation

@compileTimeOnly("Illegal reference to `scala.internal.quoted.CompileTime.quoteTypeTag`")
class quoteTypeTag extends Annotation

}
5 changes: 5 additions & 0 deletions library/src-non-bootstrapped/scala/quoted/Expr.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package scala.quoted

class Expr[+T] private[scala]:
def unseal(using qctx: QuoteContext): qctx.tasty.Term =
throw new Exception("Non bootstrapped library")
11 changes: 11 additions & 0 deletions library/src-non-bootstrapped/scala/quoted/QuoteContext.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package scala.quoted

trait QuoteContext { self =>

val tasty: scala.tasty.Reflection

type Nested = QuoteContext {
val tasty: self.tasty.type
}

}
6 changes: 6 additions & 0 deletions library/src-non-bootstrapped/scala/quoted/Type.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package scala.quoted

class Type[T <: AnyKind] private[scala]:
type `$splice` = T
def unseal(using qctx: QuoteContext): qctx.tasty.TypeTree =
throw new Exception("Non bootstrapped library")
9 changes: 2 additions & 7 deletions library/src/scala/compiletime/package.scala
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,8 @@ package object compiletime {
* @note only by-name arguments will be displayed as "code".
* Other values may display unintutively.
*/
transparent inline def (inline self: StringContext) code (inline args: Any*): String = ${ codeExpr('self, 'args) }
private def codeExpr(using qctx: QuoteContext)(sc: Expr[StringContext], args: Expr[Seq[Any]]): Expr[String] =
(sc, args) match
case (Expr.StringContext(Consts(parts)), Varargs(args2)) =>
Expr(StringContext(parts: _*).s(args2.map(_.show): _*))
case _ =>
qctx.throwError("compiletime.code must be used as a string interpolator `code\"...\"`")
transparent inline def (inline self: StringContext) code (inline args: Any*): String =
${ dotty.internal.CompileTimeMacros.codeExpr('self, 'args) }

inline def constValueOpt[T]: Option[T] = ???

Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.