Skip to content
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

Stale symbol for top-level alias type in suspended compilation unit #19351

Closed
warjort opened this issue Dec 31, 2023 · 7 comments · Fixed by #19549
Closed

Stale symbol for top-level alias type in suspended compilation unit #19351

warjort opened this issue Dec 31, 2023 · 7 comments · Fixed by #19549
Assignees
Labels
Milestone

Comments

@warjort
Copy link

warjort commented Dec 31, 2023

Compiler version

3.3.1 (same with 3.4.0-RC1)

Minimized code

//Test class
package test

type Bool = [R] => (R, R) => R
val True: Bool = [R] => (t: R, _: R) => t
val False: Bool = [R] => (_: R, f: R) => f

inline def not(b: Bool): Bool = ${notMacro('b)}
inline def show(b: Bool): String = ${showMacro('b)}
//inline def not(b: Bool): Bool = ${foldMacro('b, 'False, 'True)}
//inline def show(b: Bool): String = ${foldMacro('b, '{"TRUE"}, '{"FALSE"})}

@main def testing =
  println(show(not(True)))
// Macro class
package test

import scala.quoted.*

def notMacro(bool: Expr[Bool])(using Quotes): Expr[Bool] =
  '{$bool(False, True)}

def showMacro(bool: Expr[Bool])(using Quotes): Expr[String] =
  '{$bool("TRUE", "FALSE")}

def foldMacro[T: Type](bool: Expr[Bool], t: Expr[T], f: Expr[T])(using Quotes): Expr[T] =
  '{$bool($t, $f)}

If you swap the not/show wilth the fold versions then it doesn't crash.
I guess its some issue with inferring the [R] ?

@warjort warjort added itype:bug itype:crash stat:needs triage Every issue needs to have an "area" and "itype" label labels Dec 31, 2023
@warjort
Copy link
Author

warjort commented Dec 31, 2023

error id: RfZ2t02+Apd604sQO3wkNg==
### Bloop error:

Unexpected error when compiling root: dotty.tools.dotc.core.Denotations$StaleSymbol: stale symbol; object Test$package#3983 in module class test, defined in Period(1..31, run = 2), is referred to in run Period(2..2, run = 3)
	at dotty.tools.dotc.core.Denotations$SingleDenotation.staleSymbolError(Denotations.scala:948)
	at dotty.tools.dotc.core.Denotations$SingleDenotation.bringForward(Denotations.scala:752)
	at dotty.tools.dotc.core.Denotations$SingleDenotation.toNewRun$1(Denotations.scala:799)
	at dotty.tools.dotc.core.Denotations$SingleDenotation.current(Denotations.scala:870)
	at dotty.tools.dotc.core.Symbols$Symbol.recomputeDenot(Symbols.scala:120)
	at dotty.tools.dotc.core.Symbols$Symbol.computeDenot(Symbols.scala:114)
	at dotty.tools.dotc.core.Symbols$Symbol.denot(Symbols.scala:107)
	at dotty.tools.dotc.core.Symbols$.toDenot(Symbols.scala:494)
	at dotty.tools.dotc.core.TypeOps$AvoidMap.isStaticPrefix(TypeOps.scala:456)
	at dotty.tools.dotc.core.Types$VariantTraversal.stopBecauseStaticOrLocal(Types.scala:5593)
	at dotty.tools.dotc.core.Types$TypeMap.mapOver(Types.scala:5709)
	at dotty.tools.dotc.core.Types$AvoidWildcardsMap.apply(Types.scala:6158)
	at dotty.tools.dotc.core.TypeOps$AvoidMap.apply(TypeOps.scala:491)
	at dotty.tools.dotc.core.TypeOps$$anon$2.apply(TypeOps.scala:564)
	at dotty.tools.dotc.core.Types$TypeMap.op$proxy17$1(Types.scala:5678)
	at dotty.tools.dotc.core.Types$TypeMap.mapArgs(Types.scala:5678)
	at dotty.tools.dotc.core.Types$TypeMap.mapOver(Types.scala:5715)
	at dotty.tools.dotc.core.Types$AvoidWildcardsMap.apply(Types.scala:6158)
	at dotty.tools.dotc.core.TypeOps$AvoidMap.apply(TypeOps.scala:491)
	at dotty.tools.dotc.core.TypeOps$$anon$2.apply(TypeOps.scala:564)
	at dotty.tools.dotc.core.TypeOps$.avoid(TypeOps.scala:568)
	at dotty.tools.dotc.typer.Namer.valOrDefDefSig(Namer.scala:1724)
	at dotty.tools.dotc.typer.Namer.defDefSig(Namer.scala:1789)
	at dotty.tools.dotc.typer.Namer$Completer.typeSig(Namer.scala:791)
	at dotty.tools.dotc.typer.Namer$Completer.completeInCreationContext(Namer.scala:934)
	at dotty.tools.dotc.typer.Namer$Completer.complete(Namer.scala:814)
	at dotty.tools.dotc.core.SymDenotations$SymDenotation.completeFrom(SymDenotations.scala:174)
	at dotty.tools.dotc.core.Denotations$Denotation.completeInfo$1(Denotations.scala:187)
	at dotty.tools.dotc.core.Denotations$Denotation.info(Denotations.scala:189)
	at dotty.tools.dotc.core.SymDenotations$SymDenotation.ensureCompleted(SymDenotations.scala:393)
	at dotty.tools.dotc.typer.Typer.retrieveSym(Typer.scala:2989)
	at dotty.tools.dotc.typer.Typer.typedNamed$1(Typer.scala:3014)
	at dotty.tools.dotc.typer.Typer.typedUnadapted(Typer.scala:3111)
	at dotty.tools.dotc.typer.Typer.typed(Typer.scala:3184)
	at dotty.tools.dotc.typer.Typer.typed(Typer.scala:3188)
	at dotty.tools.dotc.typer.Typer.traverse$1(Typer.scala:3210)
	at dotty.tools.dotc.typer.Typer.typedStats(Typer.scala:3256)
	at dotty.tools.dotc.typer.Typer.typedBlockStats(Typer.scala:1159)
	at dotty.tools.dotc.typer.Typer.typedBlock(Typer.scala:1163)
	at dotty.tools.dotc.typer.Typer.typedUnnamed$1(Typer.scala:3056)
	at dotty.tools.dotc.typer.Typer.typedUnadapted(Typer.scala:3112)
	at dotty.tools.dotc.typer.Typer.typed(Typer.scala:3184)
	at dotty.tools.dotc.typer.Typer.typed(Typer.scala:3188)
	at dotty.tools.dotc.typer.Typer.typedFunctionValue(Typer.scala:1626)
	at dotty.tools.dotc.typer.Typer.makeContextualFunction(Typer.scala:3170)
	at dotty.tools.dotc.typer.Typer.typedUnadapted(Typer.scala:3109)
	at dotty.tools.dotc.typer.ProtoTypes$FunProto.$anonfun$7(ProtoTypes.scala:495)
	at dotty.tools.dotc.typer.ProtoTypes$FunProto.cacheTypedArg(ProtoTypes.scala:418)
	at dotty.tools.dotc.typer.ProtoTypes$FunProto.typedArg(ProtoTypes.scala:496)
	at dotty.tools.dotc.typer.Applications$ApplyToUntyped.typedArg(Applications.scala:897)
	at dotty.tools.dotc.typer.Applications$ApplyToUntyped.typedArg(Applications.scala:897)
	at dotty.tools.dotc.typer.Applications$Application.addTyped$1(Applications.scala:589)
	at dotty.tools.dotc.typer.Applications$Application.matchArgs(Applications.scala:653)
	at dotty.tools.dotc.typer.Applications$Application.init(Applications.scala:492)
	at dotty.tools.dotc.typer.Applications$TypedApply.<init>(Applications.scala:779)
	at dotty.tools.dotc.typer.Applications$ApplyToUntyped.<init>(Applications.scala:896)
	at dotty.tools.dotc.typer.Applications.ApplyTo(Applications.scala:1126)
	at dotty.tools.dotc.typer.Applications.ApplyTo$(Applications.scala:352)
	at dotty.tools.dotc.typer.Typer.ApplyTo(Typer.scala:116)
	at dotty.tools.dotc.typer.Applications.simpleApply$1(Applications.scala:969)
	at dotty.tools.dotc.typer.Applications.realApply$1$$anonfun$2(Applications.scala:1052)
	at dotty.tools.dotc.typer.Typer.tryEither(Typer.scala:3324)
	at dotty.tools.dotc.typer.Applications.realApply$1(Applications.scala:1063)
	at dotty.tools.dotc.typer.Applications.typedApply(Applications.scala:1101)
	at dotty.tools.dotc.typer.Applications.typedApply$(Applications.scala:352)
	at dotty.tools.dotc.typer.Typer.typedApply(Typer.scala:116)
	at dotty.tools.dotc.typer.QuotesAndSplices.typedSplice(QuotesAndSplices.scala:98)
	at dotty.tools.dotc.typer.QuotesAndSplices.typedSplice$(QuotesAndSplices.scala:30)
	at dotty.tools.dotc.typer.Typer.typedSplice(Typer.scala:116)
	at dotty.tools.dotc.typer.Typer.typedUnnamed$1(Typer.scala:3092)
	at dotty.tools.dotc.typer.Typer.typedUnadapted(Typer.scala:3112)
	at dotty.tools.dotc.typer.Typer.typed(Typer.scala:3184)
	at dotty.tools.dotc.typer.Typer.typed(Typer.scala:3188)
	at dotty.tools.dotc.typer.Typer.typedExpr(Typer.scala:3300)
	at dotty.tools.dotc.typer.Typer.$anonfun$57(Typer.scala:2486)
	at dotty.tools.dotc.inlines.PrepareInlineable$.dropInlineIfError(PrepareInlineable.scala:243)
	at dotty.tools.dotc.typer.Typer.typedDefDef(Typer.scala:2486)
	at dotty.tools.dotc.typer.Typer.typedNamed$1(Typer.scala:3024)
	at dotty.tools.dotc.typer.Typer.typedUnadapted(Typer.scala:3111)
	at dotty.tools.dotc.typer.Typer.typed(Typer.scala:3184)
	at dotty.tools.dotc.typer.Typer.typed(Typer.scala:3188)
	at dotty.tools.dotc.typer.Typer.traverse$1(Typer.scala:3210)
	at dotty.tools.dotc.typer.Typer.typedStats(Typer.scala:3256)
	at dotty.tools.dotc.typer.Typer.typedClassDef(Typer.scala:2669)
	at dotty.tools.dotc.typer.Typer.typedTypeOrClassDef$1(Typer.scala:3036)
	at dotty.tools.dotc.typer.Typer.typedNamed$1(Typer.scala:3040)
	at dotty.tools.dotc.typer.Typer.typedUnadapted(Typer.scala:3111)
	at dotty.tools.dotc.typer.Typer.typed(Typer.scala:3184)
	at dotty.tools.dotc.typer.Typer.typed(Typer.scala:3188)
	at dotty.tools.dotc.typer.Typer.traverse$1(Typer.scala:3210)
	at dotty.tools.dotc.typer.Typer.typedStats(Typer.scala:3256)
	at dotty.tools.dotc.typer.Typer.typedPackageDef(Typer.scala:2812)
	at dotty.tools.dotc.typer.Typer.typedUnnamed$1(Typer.scala:3081)
	at dotty.tools.dotc.typer.Typer.typedUnadapted(Typer.scala:3112)
	at dotty.tools.dotc.typer.Typer.typed(Typer.scala:3184)
	at dotty.tools.dotc.typer.Typer.typed(Typer.scala:3188)
	at dotty.tools.dotc.typer.Typer.typedExpr(Typer.scala:3300)
	at dotty.tools.dotc.typer.TyperPhase.typeCheck$$anonfun$1(TyperPhase.scala:44)
	at dotty.tools.dotc.typer.TyperPhase.typeCheck$$anonfun$adapted$1(TyperPhase.scala:54)
	at scala.Function0.apply$mcV$sp(Function0.scala:42)
	at dotty.tools.dotc.core.Phases$Phase.monitor(Phases.scala:440)
	at dotty.tools.dotc.typer.TyperPhase.typeCheck(TyperPhase.scala:54)
	at dotty.tools.dotc.typer.TyperPhase.runOn$$anonfun$3(TyperPhase.scala:88)
	at scala.runtime.function.JProcedure1.apply(JProcedure1.java:15)
	at scala.runtime.function.JProcedure1.apply(JProcedure1.java:10)
	at scala.collection.immutable.List.foreach(List.scala:333)
	at dotty.tools.dotc.typer.TyperPhase.runOn(TyperPhase.scala:88)
	at dotty.tools.dotc.Run.runPhases$1$$anonfun$1(Run.scala:246)
	at scala.runtime.function.JProcedure1.apply(JProcedure1.java:15)
	at scala.runtime.function.JProcedure1.apply(JProcedure1.java:10)
	at scala.collection.ArrayOps$.foreach$extension(ArrayOps.scala:1321)
	at dotty.tools.dotc.Run.runPhases$1(Run.scala:262)
	at dotty.tools.dotc.Run.compileUnits$$anonfun$1(Run.scala:270)
	at dotty.tools.dotc.Run.compileUnits$$anonfun$adapted$1(Run.scala:279)
	at dotty.tools.dotc.util.Stats$.maybeMonitored(Stats.scala:67)
	at dotty.tools.dotc.Run.compileUnits(Run.scala:279)
	at dotty.tools.dotc.Run.compileUnits(Run.scala:200)
	at dotty.tools.dotc.Driver.finish(Driver.scala:58)
	at dotty.tools.dotc.Driver.doCompile(Driver.scala:38)
	at dotty.tools.xsbt.CompilerBridgeDriver.run(CompilerBridgeDriver.java:88)
	at dotty.tools.xsbt.CompilerBridge.run(CompilerBridge.java:22)
	at sbt.internal.inc.AnalyzingCompiler.compile(AnalyzingCompiler.scala:91)
	at sbt.internal.inc.bloop.internal.BloopHighLevelCompiler.compileSources$1(BloopHighLevelCompiler.scala:133)
	at sbt.internal.inc.bloop.internal.BloopHighLevelCompiler.$anonfun$compile$7(BloopHighLevelCompiler.scala:159)
	at scala.runtime.java8.JFunction0$mcV$sp.apply(JFunction0$mcV$sp.java:23)
	at sbt.internal.inc.bloop.internal.BloopHighLevelCompiler.$anonfun$compile$1(BloopHighLevelCompiler.scala:71)
	at bloop.tracing.NoopTracer$.trace(BraveTracer.scala:53)
	at sbt.internal.inc.bloop.internal.BloopHighLevelCompiler.timed$1(BloopHighLevelCompiler.scala:70)
	at sbt.internal.inc.bloop.internal.BloopHighLevelCompiler.$anonfun$compile$6(BloopHighLevelCompiler.scala:159)
	at scala.runtime.java8.JFunction0$mcV$sp.apply(JFunction0$mcV$sp.java:23)
	at monix.eval.internal.TaskRunLoop$.startFull(TaskRunLoop.scala:81)
	at monix.eval.internal.TaskRestartCallback.syncOnSuccess(TaskRestartCallback.scala:101)
	at monix.eval.internal.TaskRestartCallback.onSuccess(TaskRestartCallback.scala:74)
	at monix.eval.internal.TaskExecuteOn$AsyncRegister$$anon$1.run(TaskExecuteOn.scala:71)
	at java.base/java.util.concurrent.ForkJoinTask$RunnableExecuteAction.exec(ForkJoinTask.java:1395)
	at java.base/java.util.concurrent.ForkJoinTask.doExec(ForkJoinTask.java:373)
	at java.base/java.util.concurrent.ForkJoinPool$WorkQueue.topLevelExec(ForkJoinPool.java:1182)
	at java.base/java.util.concurrent.ForkJoinPool.scan(ForkJoinPool.java:1655)
	at java.base/java.util.concurrent.ForkJoinPool.runWorker(ForkJoinPool.java:1622)
	at java.base/java.util.concurrent.ForkJoinWorkerThread.run(ForkJoinWorkerThread.java:165)

#### Short summary: 

Unexpected error when compiling root: dotty.tools.dotc.core.Denotations$StaleSymbol: stale symbol; o...

@hamzaremmal hamzaremmal added area:metaprogramming:quotes Issues related to quotes and splices and removed stat:needs triage Every issue needs to have an "area" and "itype" label labels Jan 1, 2024
@nicolasstucki nicolasstucki self-assigned this Jan 3, 2024
@nicolasstucki
Copy link
Contributor

Moving type Bool = [R] => (R, R) => R to the other file fixes the issue.

This is an issue with suspension of the compilation unit.

@nicolasstucki
Copy link
Contributor

Minimization

Compiling the following two files at the same time

type Alias = Int
inline def myMacro(): Alias = ${myMacroExpr}
def test = myMacro()
import scala.quoted.*
def myMacroExpr(using Quotes): Expr[Alias] = '{???}

@nicolasstucki nicolasstucki changed the title Poly Functions and Macros compiler crash - Denotations$StaleSymbol: stale symbol; o... Stale symbol for top-level alias type in suspended compilation unit Jan 3, 2024
@nicolasstucki
Copy link
Contributor

Same issue for other definitions that move into the package object.

val x: Int = 1
inline def myMacro(): x.type = ${myMacroExpr}
def test = myMacro()
import scala.quoted.*
def myMacroExpr(using Quotes): Expr[x.type] = '{???}

@warjort
Copy link
Author

warjort commented Jan 3, 2024

I can confirm that just moving the alias to a different scope even in the same file fixes the problem.
e.g.

object BoolType:
  type Bool = [R] => (R, R) => R
  val True: Bool = [R] => (t: R, _: R) => t
  val False: Bool = [R] => (_: R, f: R) => f
import BoolType.*

inline def not(b: Bool): Bool = ${notMacro('b)}
inline def show(b: Bool): String = ${showMacro('b)}

I would be ok if the compiler produced an error that explained what was wrong instead of crashing.
Something like?

Definition 'not' used macro 'notMacro' which attempted to access 'Bool' in a suspended compilation unit.
Put 'Bool' and 'not' in different scopes or files.

But I guess this might be difficult to diagnose when the error involves a lot of transitive references?

@nicolasstucki
Copy link
Contributor

This is probably crashing due to a stale symbol, maybe similar to #18077. This should be fixed.

@odersky
Copy link
Contributor

odersky commented Jan 27, 2024

In all the examples there is an illegal cyclic dependency between the two units. In the minimization it is the x.type in myMacroExpr. We should report that cycle instead of crashing.

odersky added a commit that referenced this issue Jan 30, 2024
…ies (#19549)

If a suspended macro refers back to a symbol in a previously compiled
package object, report this as an error instead of crashing with a
StaleSymbol exception.

There's unfortunately not a lot of info available at the point where the
error is raised, so the error message is a bit vague. But it's better
than crashing.

Fixes #19351
@Kordyjan Kordyjan added this to the 3.4.1 milestone Feb 14, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants