scala / scala3

The Scala 3 compiler, also known as Dotty.
https://dotty.epfl.ch
Apache License 2.0
5.71k stars 1.04k forks source link

Stale symbol crash #17294

Closed soronpo closed 1 year ago

soronpo commented 1 year ago

Could be related somehow to #17152, but this also occurs for older compiler versions and this minimization is far simpler. It may be that this should just yield a cyclic dependency error instead of a crash. I'm not sure what the cross-file macro dependency limitations are.

Compiler version

v3.2.2 Also tested on the latest nightly: scala-3.3.1-RC1-bin-20230416-2f4cc4c-NIGHTLY

Minimized code

DFVal.scala

package crash

def bits[T](t: T)(using w: Width[T]): w.Out = ???

Width.scala

package crash

import scala.quoted.*

trait Width[T]:
  type Out
object Width:
  transparent inline given [T]: Width[T] = ${ getWidthMacro[T] }
  def getWidthMacro[T](using Quotes, Type[T]): Expr[Width[T]] =
    '{ new Width[T] {} }
end Width

val x = bits(1)

Output (click arrow to expand)

```scala [error] dotty.tools.dotc.core.Denotations$StaleSymbol: stale symbol; type Out#4554 in trait Width, defined in Period(1..63, run = 2), is referred to in run Period(2..2, run = 3) [error] dotty.tools.dotc.core.Denotations$SingleDenotation.staleSymbolError(Denotations.scala:948) [error] dotty.tools.dotc.core.Denotations$SingleDenotation.bringForward(Denotations.scala:752) [error] dotty.tools.dotc.core.Denotations$SingleDenotation.toNewRun$1(Denotations.scala:799) [error] dotty.tools.dotc.core.Denotations$SingleDenotation.current(Denotations.scala:870) [error] dotty.tools.dotc.core.Symbols$Symbol.recomputeDenot(Symbols.scala:120) [error] dotty.tools.dotc.core.Symbols$Symbol.computeDenot(Symbols.scala:114) [error] dotty.tools.dotc.core.Symbols$Symbol.denot(Symbols.scala:107) [error] dotty.tools.dotc.core.Symbols$.toDenot(Symbols.scala:494) [error] dotty.tools.dotc.core.Types$NamedType.derivedSelect(Types.scala:2657) [error] dotty.tools.dotc.core.Substituters$.subst(Substituters.scala:18) [error] dotty.tools.dotc.core.Types$Type.subst(Types.scala:1798) [error] dotty.tools.dotc.core.Types$LambdaType.newLikeThis$$anonfun$2(Types.scala:3679) [error] dotty.tools.dotc.core.Types$MethodType.(Types.scala:3934) [error] dotty.tools.dotc.core.Types$CachedMethodType.(Types.scala:3953) [error] dotty.tools.dotc.core.Types$MethodTypeCompanion.apply(Types.scala:4045) [error] dotty.tools.dotc.core.Types$MethodTypeCompanion.apply(Types.scala:4044) [error] dotty.tools.dotc.core.Types$LambdaType.newLikeThis(Types.scala:3679) [error] dotty.tools.dotc.core.Types$LambdaType.newLikeThis$(Types.scala:3606) [error] dotty.tools.dotc.core.Types$MethodOrPoly.newLikeThis(Types.scala:3692) [error] dotty.tools.dotc.core.Types$LambdaType.derivedLambdaType(Types.scala:3669) [error] dotty.tools.dotc.core.Types$LambdaType.derivedLambdaType$(Types.scala:3606) [error] dotty.tools.dotc.core.Types$MethodOrPoly.derivedLambdaType(Types.scala:3692) [error] dotty.tools.dotc.core.Types$TypeMap.derivedLambdaType(Types.scala:5655) [error] dotty.tools.dotc.core.Types$TypeMap.mapOverLambda(Types.scala:5674) [error] dotty.tools.dotc.core.Types$TypeMap.mapOver(Types.scala:5701) [error] dotty.tools.dotc.core.Substituters$.substParams(Substituters.scala:163) [error] dotty.tools.dotc.core.Substituters$SubstParamsMap.apply(Substituters.scala:201) [error] dotty.tools.dotc.core.Types$TypeMap.mapOverLambda(Types.scala:5674) [error] dotty.tools.dotc.core.Types$TypeMap.mapOver(Types.scala:5701) [error] dotty.tools.dotc.core.Substituters$.substParams(Substituters.scala:163) [error] dotty.tools.dotc.core.Types$Type.substParams(Types.scala:1818) [error] dotty.tools.dotc.core.Types$LambdaType.instantiate(Types.scala:3645) [error] dotty.tools.dotc.core.Types$LambdaType.instantiate$(Types.scala:3606) [error] dotty.tools.dotc.core.Types$MethodOrPoly.instantiate(Types.scala:3692) [error] dotty.tools.dotc.typer.TypeAssigner.assignType(TypeAssigner.scala:360) [error] dotty.tools.dotc.typer.TypeAssigner.assignType$(TypeAssigner.scala:16) [error] dotty.tools.dotc.typer.Typer.assignType(Typer.scala:116) [error] dotty.tools.dotc.ast.tpd$.TypeApply(tpd.scala:58) [error] dotty.tools.dotc.ast.tpd$TreeOps$.appliedToTypeTrees$extension(tpd.scala:979) [error] dotty.tools.dotc.typer.Typer.adapt1(Typer.scala:4171) [error] dotty.tools.dotc.typer.Typer.adapt(Typer.scala:3503) [error] dotty.tools.dotc.typer.Typer.typed(Typer.scala:3126) [error] dotty.tools.dotc.typer.Typer.typed(Typer.scala:3130) [error] dotty.tools.dotc.typer.Typer.typedExpr(Typer.scala:3242) [error] dotty.tools.dotc.typer.Applications.realApply$1(Applications.scala:941) [error] dotty.tools.dotc.typer.Applications.typedApply(Applications.scala:1101) [error] dotty.tools.dotc.typer.Applications.typedApply$(Applications.scala:352) [error] dotty.tools.dotc.typer.Typer.typedApply(Typer.scala:116) [error] dotty.tools.dotc.typer.Typer.typedUnnamed$1(Typer.scala:2991) [error] dotty.tools.dotc.typer.Typer.typedUnadapted(Typer.scala:3054) [error] dotty.tools.dotc.typer.Typer.typed(Typer.scala:3126) [error] dotty.tools.dotc.typer.Typer.typed(Typer.scala:3130) [error] dotty.tools.dotc.typer.Typer.typedExpr(Typer.scala:3242) [error] dotty.tools.dotc.typer.Namer.typedAheadExpr$$anonfun$1(Namer.scala:1645) [error] dotty.tools.dotc.typer.Namer.typedAhead(Namer.scala:1635) [error] dotty.tools.dotc.typer.Namer.typedAheadExpr(Namer.scala:1645) [error] dotty.tools.dotc.typer.Namer.typedAheadRhs$1$$anonfun$1(Namer.scala:1898) [error] dotty.tools.dotc.inlines.PrepareInlineable$.dropInlineIfError(PrepareInlineable.scala:250) [error] dotty.tools.dotc.typer.Namer.typedAheadRhs$1(Namer.scala:1898) [error] dotty.tools.dotc.typer.Namer.rhsType$1(Namer.scala:1906) [error] dotty.tools.dotc.typer.Namer.cookedRhsType$1(Namer.scala:1924) [error] dotty.tools.dotc.typer.Namer.lhsType$1(Namer.scala:1925) [error] dotty.tools.dotc.typer.Namer.inferredResultType(Namer.scala:1936) [error] dotty.tools.dotc.typer.Namer.inferredType$1(Namer.scala:1683) [error] dotty.tools.dotc.typer.Namer.valOrDefDefSig(Namer.scala:1690) [error] dotty.tools.dotc.typer.Namer$Completer.typeSig(Namer.scala:787) [error] dotty.tools.dotc.typer.Namer$Completer.completeInCreationContext(Namer.scala:926) [error] dotty.tools.dotc.typer.Namer$Completer.complete(Namer.scala:814) [error] dotty.tools.dotc.core.SymDenotations$SymDenotation.completeFrom(SymDenotations.scala:174) [error] dotty.tools.dotc.core.Denotations$Denotation.completeInfo$1(Denotations.scala:187) [error] dotty.tools.dotc.core.Denotations$Denotation.info(Denotations.scala:189) [error] dotty.tools.dotc.core.SymDenotations$SymDenotation.ensureCompleted(SymDenotations.scala:393) [error] dotty.tools.dotc.typer.Typer.retrieveSym(Typer.scala:2932) [error] dotty.tools.dotc.typer.Typer.typedNamed$1(Typer.scala:2957) [error] dotty.tools.dotc.typer.Typer.typedUnadapted(Typer.scala:3053) [error] dotty.tools.dotc.typer.Typer.typed(Typer.scala:3126) [error] dotty.tools.dotc.typer.Typer.typed(Typer.scala:3130) [error] dotty.tools.dotc.typer.Typer.traverse$1(Typer.scala:3152) [error] dotty.tools.dotc.typer.Typer.typedStats(Typer.scala:3198) [error] dotty.tools.dotc.typer.Typer.typedClassDef(Typer.scala:2612) [error] dotty.tools.dotc.typer.Typer.typedTypeOrClassDef$1(Typer.scala:2979) [error] dotty.tools.dotc.typer.Typer.typedNamed$1(Typer.scala:2983) [error] dotty.tools.dotc.typer.Typer.typedUnadapted(Typer.scala:3053) [error] dotty.tools.dotc.typer.Typer.typed(Typer.scala:3126) [error] dotty.tools.dotc.typer.Typer.typed(Typer.scala:3130) [error] dotty.tools.dotc.typer.Typer.traverse$1(Typer.scala:3152) [error] dotty.tools.dotc.typer.Typer.typedStats(Typer.scala:3198) [error] dotty.tools.dotc.typer.Typer.typedPackageDef(Typer.scala:2755) [error] dotty.tools.dotc.typer.Typer.typedUnnamed$1(Typer.scala:3024) [error] dotty.tools.dotc.typer.Typer.typedUnadapted(Typer.scala:3054) [error] dotty.tools.dotc.typer.Typer.typed(Typer.scala:3126) [error] dotty.tools.dotc.typer.Typer.typed(Typer.scala:3130) [error] dotty.tools.dotc.typer.Typer.typedExpr(Typer.scala:3242) [error] dotty.tools.dotc.typer.TyperPhase.typeCheck$$anonfun$1(TyperPhase.scala:44) [error] dotty.tools.dotc.typer.TyperPhase.typeCheck$$anonfun$adapted$1(TyperPhase.scala:54) [error] scala.Function0.apply$mcV$sp(Function0.scala:42) [error] dotty.tools.dotc.core.Phases$Phase.monitor(Phases.scala:437) [error] dotty.tools.dotc.typer.TyperPhase.typeCheck(TyperPhase.scala:54) [error] dotty.tools.dotc.typer.TyperPhase.runOn$$anonfun$3(TyperPhase.scala:88) [error] scala.runtime.function.JProcedure1.apply(JProcedure1.java:15) [error] scala.runtime.function.JProcedure1.apply(JProcedure1.java:10) [error] scala.collection.immutable.List.foreach(List.scala:333) [error] dotty.tools.dotc.typer.TyperPhase.runOn(TyperPhase.scala:88) [error] dotty.tools.dotc.Run.runPhases$1$$anonfun$1(Run.scala:246) [error] scala.runtime.function.JProcedure1.apply(JProcedure1.java:15) [error] scala.runtime.function.JProcedure1.apply(JProcedure1.java:10) [error] scala.collection.ArrayOps$.foreach$extension(ArrayOps.scala:1321) [error] dotty.tools.dotc.Run.runPhases$1(Run.scala:262) [error] dotty.tools.dotc.Run.compileUnits$$anonfun$1(Run.scala:270) [error] dotty.tools.dotc.Run.compileUnits$$anonfun$adapted$1(Run.scala:279) [error] dotty.tools.dotc.util.Stats$.maybeMonitored(Stats.scala:67) [error] dotty.tools.dotc.Run.compileUnits(Run.scala:279) [error] dotty.tools.dotc.Run.compileUnits(Run.scala:200) [error] dotty.tools.dotc.Driver.finish(Driver.scala:58) [error] dotty.tools.dotc.Driver.doCompile(Driver.scala:38) [error] dotty.tools.xsbt.CompilerBridgeDriver.run(CompilerBridgeDriver.java:88) [error] dotty.tools.xsbt.CompilerBridge.run(CompilerBridge.java:22) [error] sbt.internal.inc.AnalyzingCompiler.compile(AnalyzingCompiler.scala:91) [error] sbt.internal.inc.MixedAnalyzingCompiler.$anonfun$compile$7(MixedAnalyzingCompiler.scala:193) [error] scala.runtime.java8.JFunction0$mcV$sp.apply(JFunction0$mcV$sp.java:23) [error] sbt.internal.inc.MixedAnalyzingCompiler.timed(MixedAnalyzingCompiler.scala:248) [error] sbt.internal.inc.MixedAnalyzingCompiler.$anonfun$compile$4(MixedAnalyzingCompiler.scala:183) [error] sbt.internal.inc.MixedAnalyzingCompiler.$anonfun$compile$4$adapted(MixedAnalyzingCompiler.scala:163) [error] sbt.internal.inc.JarUtils$.withPreviousJar(JarUtils.scala:239) [error] sbt.internal.inc.MixedAnalyzingCompiler.compileScala$1(MixedAnalyzingCompiler.scala:163) [error] sbt.internal.inc.MixedAnalyzingCompiler.compile(MixedAnalyzingCompiler.scala:211) [error] sbt.internal.inc.IncrementalCompilerImpl.$anonfun$compileInternal$1(IncrementalCompilerImpl.scala:534) [error] sbt.internal.inc.IncrementalCompilerImpl.$anonfun$compileInternal$1$adapted(IncrementalCompilerImpl.scala:534) [error] sbt.internal.inc.Incremental$.$anonfun$apply$5(Incremental.scala:179) [error] sbt.internal.inc.Incremental$.$anonfun$apply$5$adapted(Incremental.scala:177) [error] sbt.internal.inc.Incremental$$anon$2.run(Incremental.scala:463) [error] sbt.internal.inc.IncrementalCommon$CycleState.next(IncrementalCommon.scala:116) [error] sbt.internal.inc.IncrementalCommon$$anon$1.next(IncrementalCommon.scala:56) [error] sbt.internal.inc.IncrementalCommon$$anon$1.next(IncrementalCommon.scala:52) [error] sbt.internal.inc.IncrementalCommon.cycle(IncrementalCommon.scala:263) [error] sbt.internal.inc.Incremental$.$anonfun$incrementalCompile$8(Incremental.scala:418) [error] sbt.internal.inc.Incremental$.withClassfileManager(Incremental.scala:506) [error] sbt.internal.inc.Incremental$.incrementalCompile(Incremental.scala:405) [error] sbt.internal.inc.Incremental$.apply(Incremental.scala:171) [error] sbt.internal.inc.IncrementalCompilerImpl.compileInternal(IncrementalCompilerImpl.scala:534) [error] sbt.internal.inc.IncrementalCompilerImpl.$anonfun$compileIncrementally$1(IncrementalCompilerImpl.scala:488) [error] sbt.internal.inc.IncrementalCompilerImpl.handleCompilationError(IncrementalCompilerImpl.scala:332) [error] sbt.internal.inc.IncrementalCompilerImpl.compileIncrementally(IncrementalCompilerImpl.scala:425) [error] sbt.internal.inc.IncrementalCompilerImpl.compile(IncrementalCompilerImpl.scala:137) [error] sbt.Defaults$.compileIncrementalTaskImpl(Defaults.scala:2363) [error] sbt.Defaults$.$anonfun$compileIncrementalTask$2(Defaults.scala:2313) [error] sbt.internal.server.BspCompileTask$.$anonfun$compute$1(BspCompileTask.scala:30) [error] sbt.internal.io.Retry$.apply(Retry.scala:46) [error] sbt.internal.io.Retry$.apply(Retry.scala:28) [error] sbt.internal.io.Retry$.apply(Retry.scala:23) [error] sbt.internal.server.BspCompileTask$.compute(BspCompileTask.scala:30) [error] sbt.Defaults$.$anonfun$compileIncrementalTask$1(Defaults.scala:2311) [error] scala.Function1.$anonfun$compose$1(Function1.scala:49) [error] sbt.internal.util.$tilde$greater.$anonfun$$u2219$1(TypeFunctions.scala:62) [error] sbt.std.Transform$$anon$4.work(Transform.scala:68) [error] sbt.Execute.$anonfun$submit$2(Execute.scala:282) [error] sbt.internal.util.ErrorHandling$.wideConvert(ErrorHandling.scala:23) [error] sbt.Execute.work(Execute.scala:291) [error] sbt.Execute.$anonfun$submit$1(Execute.scala:282) [error] sbt.ConcurrentRestrictions$$anon$4.$anonfun$submitValid$1(ConcurrentRestrictions.scala:265) [error] sbt.CompletionService$$anon$2.call(CompletionService.scala:64) [error] java.base/java.util.concurrent.FutureTask.run(FutureTask.java:264) [error] java.base/java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:515) [error] java.base/java.util.concurrent.FutureTask.run(FutureTask.java:264) [error] java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128) [error] java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628) [error] java.base/java.lang.Thread.run(Thread.java:829) [error] [error] stack trace is suppressed; run last core / Compile / compileIncremental for the full output [error] (core / Compile / compileIncremental) dotty.tools.dotc.core.Denotations$StaleSymbol: stale symbol; type Out#4554 in trait Width, defined in Period(1..63, run = 2), is referred to in run Period(2..2, run = 3) ```
ftucky commented 1 year ago

Hello, I encounter the same symptom: dotty.tools.dotc.core.Denotations$StaleSymbol: stale symbol; method definition$lzyINIT2#52624 in class Generator, defined in Period(76..93, run = 2), is referred to in run Period(92..92, run = 3)

dotty.tools.dotc.core.Denotations$StaleSymbol: stale symbol; method definition$lzyINIT2#52624 in class Generator, defined in Period(76..93, run = 2), is referred to in run Period(92..92, run = 3) | => root / Compile / compileIncremental 1s 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$Symbol.name(Symbols.scala:260) at dotty.tools.dotc.core.SymDenotations$ClassDenotation.computeMemberNames$$anonfun$2(SymDenotations.scala:2328)

Observations

No metaprogramming

Code is difficult to minimize, however metaprogramming or quotes do not seem to be involved.

Apparent workaround

The faulty symbol definition$lzyINIT2#52624 is associated to a declaration

object definition extends Definition: 
  ...

Changing the line for the declaration below acts as a work-around.

val definition = new Definition:
  ...

Caching Issue

Running the sbt build a second time succeeds after the crash.

sbt:> clean 
sbt:> compile /// Crashes
sbt:> compile /// with no source change. NO CRASH. (and correct behavior!)

Hope this helps.

Pyromuffin commented 2 weeks ago

Still seeing this on 3.4.2

My repro is very complicated involving macros unfortunately, but I am seeing the same symptoms as https://github.com/scala/scala3/issues/17294#issuecomment-1523257480: it happens with a top level object and goes away when either making it a top level val or on the second compile.