Skip to content

Commit 4cb4bbb

Browse files
committed
Add quotes.isWhileTyping
1 parent 1b7dcd1 commit 4cb4bbb

File tree

4 files changed

+46
-0
lines changed

4 files changed

+46
-0
lines changed

compiler/src/scala/quoted/runtime/impl/QuotesImpl.scala

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,10 @@ class QuotesImpl private (using val ctx: Context) extends Quotes, QuoteUnpickler
7676

7777
object reflect extends reflectModule:
7878

79+
object CompilationInfo extends CompilationInfoModule:
80+
def isWhileTyping: Boolean = !ctx.isAfterTyper
81+
end CompilationInfo
82+
7983
extension (expr: Expr[Any])
8084
def asTerm: Term =
8185
val exprImpl = expr.asInstanceOf[ExprImpl]

library/src/scala/quoted/Quotes.scala

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,19 @@ trait Quotes { self: runtime.QuoteUnpickler & runtime.QuoteMatching =>
217217
*/
218218
trait reflectModule { self: reflect.type =>
219219

220+
/** Module object of `type CompilationInfo` */
221+
val CompilationInfo: CompilationInfoModule
222+
223+
/** Methods of the module object `val CompilationInfo` */
224+
trait CompilationInfoModule { this: CompilationInfo.type =>
225+
/** Are we expanding a `inline` macro while typing the program?
226+
*
227+
* This will be true when the macro is used in a transparent inline.
228+
*/
229+
def isWhileTyping: Boolean
230+
}
231+
232+
220233
/** Returns the `Term` representation this expression */
221234
extension (expr: Expr[Any])
222235
def asTerm: Term
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import scala.quoted._
2+
3+
inline def isWhileTyping: Boolean = ${ whileTypeing }
4+
5+
transparent inline def isWhileTypingTransparent: Boolean = ${ whileTypeing }
6+
7+
private def whileTypeing(using Quotes): Expr[Boolean] =
8+
import quotes.reflect._
9+
Expr(CompilationInfo.isWhileTyping)
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
2+
@main def Test =
3+
assert(!isWhileTyping)
4+
assert(isWhileTypingTransparent)
5+
assert(f1 == "afterTyper")
6+
assert(f2 == "afterTyper")
7+
assert(f3 == "inTyper")
8+
assert(f4 == "inTyper")
9+
10+
inline def f1 =
11+
inline if isWhileTyping then "inTyper" else "afterTyper"
12+
13+
inline def f2 =
14+
inline if isWhileTypingTransparent /*delayed*/ then "inTyper" else "afterTyper"
15+
16+
transparent inline def f3 =
17+
inline if isWhileTyping /*forced*/ then "inTyper" else "afterTyper"
18+
19+
transparent inline def f4 =
20+
inline if isWhileTypingTransparent then "inTyper" else "afterTyper"

0 commit comments

Comments
 (0)