diff --git a/tests/run-macros/i8671/Macro_1.scala b/tests/run-macros/i8671/Macro_1.scala new file mode 100644 index 000000000000..354ddff20035 --- /dev/null +++ b/tests/run-macros/i8671/Macro_1.scala @@ -0,0 +1,28 @@ +import scala.quoted._ + +case class FileName private(name: String) + +object FileName { + def unsafe(s: String) = new FileName(s) + + implicit inline def ToFileName(inline s: String): FileName = + ${createFileName('{s})} + + def fileNameFromString(s: String): Either[String, FileName] = + Right(FileName.unsafe(s)) + + def createFileName(fileName: Expr[String])(using qctx: QuoteContext): Expr[FileName] = + fileName match { + case e@Const(s) => + fileNameFromString(s) match { + case Right(fn) => + '{FileName.unsafe(${Expr(fn.name)})} // Or `Expr(fn)` if there is a `Liftable[FileName]` + case Left(_) => + qctx.throwError(s"$s is not a valid file name! It must not contain a /", fileName) + } + + case _ => + qctx.throwError(s"$fileName is not a valid file name. It must be a literal string", fileName) + } +} + diff --git a/tests/run-macros/i8671/Test_2.scala b/tests/run-macros/i8671/Test_2.scala new file mode 100644 index 000000000000..e2fc4f0416b2 --- /dev/null +++ b/tests/run-macros/i8671/Test_2.scala @@ -0,0 +1,6 @@ +import FileName._ + +@main def Test = { + val fileName1: FileName = ToFileName("fileName1") + println(fileName1) +}