Skip to content

Commit 16a45c4

Browse files
Merge pull request #9232 from dotty-staging/fix-#9227
Fix #9227: Do not suspend REPL compilation unit
2 parents e496a0b + 763bb08 commit 16a45c4

File tree

6 files changed

+27
-5
lines changed

6 files changed

+27
-5
lines changed

compiler/src/dotty/tools/dotc/CompilationUnit.scala

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ class CompilationUnit protected (val source: SourceFile) {
2323

2424
var tpdTree: tpd.Tree = tpd.EmptyTree
2525

26+
/** Is this the compilation unit of a Java file */
2627
def isJava: Boolean = source.file.name.endsWith(".java")
2728

2829
/** The source version for this unit, as determined by a language import */
@@ -47,7 +48,14 @@ class CompilationUnit protected (val source: SourceFile) {
4748

4849
var suspended: Boolean = false
4950

51+
/** Can this compilation unit be suspended */
52+
def isSuspendable: Boolean = true
53+
54+
/** Suspends the compilation unit by thowing a SuspendException
55+
* and recoring the suspended compilation unit
56+
*/
5057
def suspend()(using Context): Nothing =
58+
assert(isSuspendable)
5159
if !suspended then
5260
if (ctx.settings.XprintSuspension.value)
5361
ctx.echo(i"suspended: $this")

compiler/src/dotty/tools/dotc/transform/Splicer.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -393,7 +393,7 @@ object Splicer {
393393
ex.getTargetException match {
394394
case ex: scala.quoted.Reporting.StopQuotedContext =>
395395
throw ex
396-
case MissingClassDefinedInCurrentRun(sym) =>
396+
case MissingClassDefinedInCurrentRun(sym) if ctx.compilationUnit.isSuspendable =>
397397
if (ctx.settings.XprintSuspension.value)
398398
ctx.echo(i"suspension triggered by a dependency on $sym", pos)
399399
ctx.compilationUnit.suspend() // this throws a SuspendException

compiler/src/dotty/tools/dotc/typer/Inliner.scala

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1407,14 +1407,15 @@ class Inliner(call: tpd.Tree, rhsToInline: tpd.Tree)(using Context) {
14071407
assert(level == 0)
14081408
val inlinedFrom = enclosingInlineds.last
14091409
val dependencies = macroDependencies(body)
1410-
1410+
val suspendable = ctx.compilationUnit.isSuspendable
14111411
if dependencies.nonEmpty && !ctx.reporter.errorsReported then
14121412
for sym <- dependencies do
14131413
if ctx.compilationUnit.source.file == sym.associatedFile then
14141414
ctx.error(em"Cannot call macro $sym defined in the same source file", call.sourcePos)
1415-
if (ctx.settings.XprintSuspension.value)
1415+
if (suspendable && ctx.settings.XprintSuspension.value)
14161416
ctx.echo(i"suspension triggered by macro call to ${sym.showLocated} in ${sym.associatedFile}", call.sourcePos)
1417-
ctx.compilationUnit.suspend() // this throws a SuspendException
1417+
if suspendable then
1418+
ctx.compilationUnit.suspend() // this throws a SuspendException
14181419

14191420
val evaluatedSplice = inContext(tastyreflect.MacroExpansion.context(inlinedFrom)) {
14201421
Splicer.splice(body, inlinedFrom.sourcePos, MacroClassLoader.fromContext)

compiler/src/dotty/tools/repl/ReplCompiler.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ class ReplCompiler extends Compiler {
146146
val objectTermName = ctx.source.file.toString.toTermName
147147
objectNames.update(defs.state.objectIndex, objectTermName)
148148

149-
val unit = CompilationUnit(ctx.source)
149+
val unit = new ReplCompilationUnit(ctx.source)
150150
unit.untpdTree = wrapped(defs, objectTermName, span)
151151
unit
152152
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package dotty.tools.repl
2+
3+
import dotty.tools.dotc.CompilationUnit
4+
import dotty.tools.dotc.util.SourceFile
5+
6+
7+
class ReplCompilationUnit(source: SourceFile) extends CompilationUnit(source):
8+
override def isSuspendable: Boolean = false

compiler/test-resources/repl/i9227

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
scala> import scala.quoted._; inline def myMacro[T]: Unit = ${ myMacroImpl[T] }; def myMacroImpl[T](using qctx: QuoteContext): Expr[Unit] = '{}; println(myMacro[Int])
2+
1 | import scala.quoted._; inline def myMacro[T]: Unit = ${ myMacroImpl[T] }; def myMacroImpl[T](using qctx: QuoteContext): Expr[Unit] = '{}; println(myMacro[Int])
3+
| ^^^^^^^^^^^^
4+
| Cannot call macro method myMacroImpl defined in the same source file
5+
| This location contains code that was inlined from rs$line$1:1

0 commit comments

Comments
 (0)