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

assertion failed: Inconsistent state in TS[212X, 211, 2, 1, 0]: it owns TypeVar(TypeParamRef(S2)) whose owningState is TS[211, 2, 1, 0] #13407

Closed
dlwh opened this issue Aug 27, 2021 · 4 comments · Fixed by #13408
Assignees
Milestone

Comments

@dlwh
Copy link

dlwh commented Aug 27, 2021

Maybe related to #13101 but I can repro in latest RC, which I think has a fix for that.

Compiler version

3.0.1, 3.0.2-RC1, 3.0.2-RC2

Minimized code

sealed trait Assertion[Cond <: Boolean]

object Assertion {
  given Assertion[true] with {
  }
}


trait Tensor[S <: Tuple] {
  // === isn't defined
  def +[S2 <: Tuple](other: Tensor[S2])(using Assertion[S === S2]): Tensor[S] = new Tensor {}
}

object Tensor {
  def mk[S <: Tuple]: Tensor[S] = new Tensor {}
}

object Foo {
  val t1: Tensor[("batch", "len", "embed")] = Tensor.mk
  val t2: Tensor[("embed", "hid")] = Tensor.mk
  def foo(x: Any) = x
  // ok
//  foo(t1 + t2)

  def bar(x: Any) = {
    x
  }

  bar ( foo (t1 +t2))
}

Output (click arrow to expand)

[error] java.lang.AssertionError: assertion failed: Inconsistent state in TS[212X, 211, 2, 1, 0]: it owns TypeVar(TypeParamRef(S2)) whose owningState is TS[211, 2, 1, 0]
[error] 	at scala.runtime.Scala3RunTime$.assertFailed(Scala3RunTime.scala:8)
[error] 	at dotty.tools.dotc.core.TyperState.gc$$anonfun$1(TyperState.scala:220)
[error] 	at scala.runtime.function.JProcedure1.apply(JProcedure1.java:15)
[error] 	at scala.runtime.function.JProcedure1.apply(JProcedure1.java:10)
[error] 	at dotty.tools.dotc.util.SimpleIdentitySet$Set1.foreach(SimpleIdentitySet.scala:69)
[error] 	at dotty.tools.dotc.core.TyperState.gc(TyperState.scala:226)
[error] 	at dotty.tools.dotc.core.TyperState.commit(TyperState.scala:163)
[error] 	at dotty.tools.dotc.typer.Inferencing$.isFullyDefined(Inferencing.scala:40)
[error] 	at dotty.tools.dotc.typer.Implicits.ignoredInstanceNormalImport$1(Implicits.scala:881)
[error] 	at dotty.tools.dotc.typer.Implicits.$anonfun$1(Implicits.scala:896)
[error] 	at dotty.tools.dotc.typer.ImplicitSearchError.hiddenImplicitsAddendum(ErrorReporting.scala:419)
[error] 	at dotty.tools.dotc.typer.ImplicitSearchError.missingArgMsg(ErrorReporting.scala:267)
[error] 	at dotty.tools.dotc.typer.Implicits.missingArgMsg(Implicits.scala:897)
[error] 	at dotty.tools.dotc.typer.Implicits.missingArgMsg$(Implicits.scala:774)
[error] 	at dotty.tools.dotc.typer.Typer.missingArgMsg(Typer.scala:106)
[error] 	at dotty.tools.dotc.typer.Typer.issueErrors$1$$anonfun$1$$anonfun$1(Typer.scala:3353)
[error] 	at dotty.tools.dotc.reporting.NoExplanation.msg(Message.scala:132)
[error] 	at dotty.tools.dotc.reporting.Message.message(Message.scala:87)
[error] 	at dotty.tools.dotc.reporting.Message.isNonSensical(Message.scala:99)
[error] 	at dotty.tools.dotc.reporting.HideNonSensicalMessages.isHidden(HideNonSensicalMessages.scala:16)
[error] 	at dotty.tools.dotc.reporting.HideNonSensicalMessages.isHidden$(HideNonSensicalMessages.scala:10)
[error] 	at dotty.tools.dotc.reporting.AbstractReporter.isHidden(AbstractReporter.scala:8)
[error] 	at dotty.tools.dotc.reporting.Reporter.report(Reporter.scala:148)
[error] 	at dotty.tools.dotc.reporting.Reporter.flush$$anonfun$1(Reporter.scala:206)
[error] 	at scala.runtime.function.JProcedure1.apply(JProcedure1.java:15)
[error] 	at scala.runtime.function.JProcedure1.apply(JProcedure1.java:10)
[error] 	at scala.collection.immutable.List.foreach(List.scala:333)
[error] 	at dotty.tools.dotc.reporting.Reporter.flush(Reporter.scala:206)
[error] 	at dotty.tools.dotc.core.TyperState.commit(TyperState.scala:143)
[error] 	at dotty.tools.dotc.typer.Applications.fail$1(Applications.scala:986)
[error] 	at dotty.tools.dotc.typer.Applications.realApply$7$$anonfun$6$$anonfun$2(Applications.scala:992)
[error] 	at scala.Option.getOrElse(Option.scala:201)
[error] 	at dotty.tools.dotc.typer.Applications.realApply$8$$anonfun$7(Applications.scala:993)
[error] 	at dotty.tools.dotc.typer.Typer.tryEither(Typer.scala:2952)
[error] 	at dotty.tools.dotc.typer.Applications.realApply$1(Applications.scala:994)
[error] 	at dotty.tools.dotc.typer.Applications.typedApply(Applications.scala:1032)
[error] 	at dotty.tools.dotc.typer.Applications.typedApply$(Applications.scala:317)
[error] 	at dotty.tools.dotc.typer.Typer.typedApply(Typer.scala:106)
[error] 	at dotty.tools.dotc.typer.Typer.typedUnnamed$1(Typer.scala:2693)
[error] 	at dotty.tools.dotc.typer.Typer.typedUnadapted(Typer.scala:2756)
[error] 	at dotty.tools.dotc.typer.Typer.typed(Typer.scala:2821)
[error] 	at dotty.tools.dotc.typer.Typer.typed(Typer.scala:2825)
[error] 	at dotty.tools.dotc.typer.Typer.traverse$1(Typer.scala:2874)
[error] 	at dotty.tools.dotc.typer.Typer.typedStats(Typer.scala:2897)
[error] 	at dotty.tools.dotc.typer.Typer.typedClassDef(Typer.scala:2352)
[error] 	at dotty.tools.dotc.typer.Typer.typedTypeOrClassDef$2(Typer.scala:2681)
[error] 	at dotty.tools.dotc.typer.Typer.typedNamed$1(Typer.scala:2685)
[error] 	at dotty.tools.dotc.typer.Typer.typedUnadapted(Typer.scala:2755)
[error] 	at dotty.tools.dotc.typer.Typer.typed(Typer.scala:2821)
[error] 	at dotty.tools.dotc.typer.Typer.typed(Typer.scala:2825)
[error] 	at dotty.tools.dotc.typer.Typer.traverse$1(Typer.scala:2847)
[error] 	at dotty.tools.dotc.typer.Typer.typedStats(Typer.scala:2897)
[error] 	at dotty.tools.dotc.typer.Typer.typedPackageDef(Typer.scala:2475)
[error] 	at dotty.tools.dotc.typer.Typer.typedUnnamed$1(Typer.scala:2726)
[error] 	at dotty.tools.dotc.typer.Typer.typedUnadapted(Typer.scala:2756)
[error] 	at dotty.tools.dotc.typer.Typer.typed(Typer.scala:2821)
[error] 	at dotty.tools.dotc.typer.Typer.typed(Typer.scala:2825)
[error] 	at dotty.tools.dotc.typer.Typer.typedExpr(Typer.scala:2941)
[error] 	at dotty.tools.dotc.typer.FrontEnd.liftedTree1$1(FrontEnd.scala:79)
[error] 	at dotty.tools.dotc.typer.FrontEnd.typeCheck$$anonfun$1(FrontEnd.scala:84)
[error] 	at dotty.tools.dotc.typer.FrontEnd.monitor(FrontEnd.scala:43)
[error] 	at dotty.tools.dotc.typer.FrontEnd.typeCheck(FrontEnd.scala:85)
[error] 	at dotty.tools.dotc.typer.FrontEnd.runOn$$anonfun$3(FrontEnd.scala:120)
[error] 	at scala.runtime.function.JProcedure1.apply(JProcedure1.java:15)
[error] 	at scala.runtime.function.JProcedure1.apply(JProcedure1.java:10)
[error] 	at scala.collection.immutable.List.foreach(List.scala:333)
[error] 	at dotty.tools.dotc.typer.FrontEnd.runOn(FrontEnd.scala:120)
[error] 	at dotty.tools.dotc.Run.runPhases$4$$anonfun$4(Run.scala:205)
[error] 	at scala.runtime.function.JProcedure1.apply(JProcedure1.java:15)
[error] 	at scala.runtime.function.JProcedure1.apply(JProcedure1.java:10)
[error] 	at scala.collection.ArrayOps$.foreach$extension(ArrayOps.scala:1323)
[error] 	at dotty.tools.dotc.Run.runPhases$5(Run.scala:216)
[error] 	at dotty.tools.dotc.Run.compileUnits$$anonfun$1(Run.scala:224)
[error] 	at scala.runtime.java8.JFunction0$mcV$sp.apply(JFunction0$mcV$sp.scala:18)
[error] 	at dotty.tools.dotc.util.Stats$.maybeMonitored(Stats.scala:67)
[error] 	at dotty.tools.dotc.Run.compileUnits(Run.scala:231)
[error] 	at dotty.tools.dotc.Run.compileSources(Run.scala:166)
[error] 	at dotty.tools.dotc.Run.compile(Run.scala:150)
[error] 	at dotty.tools.dotc.Driver.doCompile(Driver.scala:39)
[error] 	at dotty.tools.xsbt.CompilerBridgeDriver.run(CompilerBridgeDriver.java:88)
[error] 	at dotty.tools.xsbt.CompilerBridge.run(CompilerBridge.java:22)
[error] 	at sbt.internal.inc.AnalyzingCompiler.compile(AnalyzingCompiler.scala:91)
[error] 	at sbt.internal.inc.MixedAnalyzingCompiler.$anonfun$compile$7(MixedAnalyzingCompiler.scala:192)
[error] 	at scala.runtime.java8.JFunction0$mcV$sp.apply(JFunction0$mcV$sp.java:23)
[error] 	at sbt.internal.inc.MixedAnalyzingCompiler.timed(MixedAnalyzingCompiler.scala:247)
[error] 	at sbt.internal.inc.MixedAnalyzingCompiler.$anonfun$compile$4(MixedAnalyzingCompiler.scala:182)
[error] 	at sbt.internal.inc.MixedAnalyzingCompiler.$anonfun$compile$4$adapted(MixedAnalyzingCompiler.scala:163)
[error] 	at sbt.internal.inc.JarUtils$.withPreviousJar(JarUtils.scala:239)
[error] 	at sbt.internal.inc.MixedAnalyzingCompiler.compileScala$1(MixedAnalyzingCompiler.scala:163)
[error] 	at sbt.internal.inc.MixedAnalyzingCompiler.compile(MixedAnalyzingCompiler.scala:210)
[error] 	at sbt.internal.inc.IncrementalCompilerImpl.$anonfun$compileInternal$1(IncrementalCompilerImpl.scala:528)
[error] 	at sbt.internal.inc.IncrementalCompilerImpl.$anonfun$compileInternal$1$adapted(IncrementalCompilerImpl.scala:528)
[error] 	at sbt.internal.inc.Incremental$.$anonfun$apply$5(Incremental.scala:175)
[error] 	at sbt.internal.inc.Incremental$.$anonfun$apply$5$adapted(Incremental.scala:173)
[error] 	at sbt.internal.inc.Incremental$$anon$2.run(Incremental.scala:459)
[error] 	at sbt.internal.inc.IncrementalCommon$CycleState.next(IncrementalCommon.scala:116)
[error] 	at sbt.internal.inc.IncrementalCommon$$anon$1.next(IncrementalCommon.scala:56)
[error] 	at sbt.internal.inc.IncrementalCommon$$anon$1.next(IncrementalCommon.scala:52)
[error] 	at sbt.internal.inc.IncrementalCommon.cycle(IncrementalCommon.scala:263)
[error] 	at sbt.internal.inc.Incremental$.$anonfun$incrementalCompile$8(Incremental.scala:414)
[error] 	at sbt.internal.inc.Incremental$.withClassfileManager(Incremental.scala:501)
[error] 	at sbt.internal.inc.Incremental$.incrementalCompile(Incremental.scala:401)
[error] 	at sbt.internal.inc.Incremental$.apply(Incremental.scala:167)
[error] 	at sbt.internal.inc.IncrementalCompilerImpl.compileInternal(IncrementalCompilerImpl.scala:528)
[error] 	at sbt.internal.inc.IncrementalCompilerImpl.$anonfun$compileIncrementally$1(IncrementalCompilerImpl.scala:482)
[error] 	at sbt.internal.inc.IncrementalCompilerImpl.handleCompilationError(IncrementalCompilerImpl.scala:332)
[error] 	at sbt.internal.inc.IncrementalCompilerImpl.compileIncrementally(IncrementalCompilerImpl.scala:420)
[error] 	at sbt.internal.inc.IncrementalCompilerImpl.compile(IncrementalCompilerImpl.scala:137)
[error] 	at sbt.Defaults$.compileIncrementalTaskImpl(Defaults.scala:2357)
[error] 	at sbt.Defaults$.$anonfun$compileIncrementalTask$2(Defaults.scala:2314)
[error] 	at sbt.internal.io.Retry$.apply(Retry.scala:46)
[error] 	at sbt.internal.io.Retry$.apply(Retry.scala:28)
[error] 	at sbt.internal.io.Retry$.apply(Retry.scala:23)
[error] 	at sbt.internal.server.BspCompileTask$.compute(BspCompileTask.scala:31)
[error] 	at sbt.Defaults$.$anonfun$compileIncrementalTask$1(Defaults.scala:2310)
[error] 	at scala.Function1.$anonfun$compose$1(Function1.scala:49)
[error] 	at sbt.internal.util.$tilde$greater.$anonfun$$u2219$1(TypeFunctions.scala:62)
[error] 	at sbt.std.Transform$$anon$4.work(Transform.scala:68)
[error] 	at sbt.Execute.$anonfun$submit$2(Execute.scala:282)
[error] 	at sbt.internal.util.ErrorHandling$.wideConvert(ErrorHandling.scala:23)
[error] 	at sbt.Execute.work(Execute.scala:291)
[error] 	at sbt.Execute.$anonfun$submit$1(Execute.scala:282)
[error] 	at sbt.ConcurrentRestrictions$$anon$4.$anonfun$submitValid$1(ConcurrentRestrictions.scala:265)
[error] 	at sbt.CompletionService$$anon$2.call(CompletionService.scala:64)
[error] 	at java.base/java.util.concurrent.FutureTask.run(FutureTask.java:264)
[error] 	at java.base/java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:515)
[error] 	at java.base/java.util.concurrent.FutureTask.run(FutureTask.java:264)
[error] 	at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1130)
[error] 	at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:630)
[error] 	at java.base/java.lang.Thread.run(Thread.java:831)
@som-snytt
Copy link
Contributor

Same on head.

@dlwh
Copy link
Author

dlwh commented Aug 27, 2021

Here's another slightly different repro:

sealed trait Assertion[Cond <: Boolean]

object Assertion {
  given Assertion[true] with {
  }
}

type IsPermutation[T <: Tuple, T2 <: Tuple] <: Boolean = T match {
  case EmptyTuple => T2 match {
    case EmptyTuple => true
    case _ => false
  }
  case h *: tail => T2 match {
    case EmptyTuple => false
    case _ => IsPermutation[tail, Remove[T2, h]]
  }
}

type Remove[T <: Tuple, X] <: Tuple = T match {
  case EmptyTuple => EmptyTuple
  case head *: tail => head match {
    case X => tail
    case _ => head *: Remove[tail, X]
  }
}

trait Tensor[S <: Tuple] {
  // === isn't defined
  def +[S2 <: Tuple](other: Tensor[S2])(using Assertion[IsPermutation[S, S2]]): Tensor[S] = new Tensor {}
}

object Tensor {
  def mk[S <: Tuple]: Tensor[S] = new Tensor {}
}

object Foo {
  val t1: Tensor[("batch", "len", "embed")] = Tensor.mk
  val t2: Tensor[("embed", "hid")] = Tensor.mk
  def foo(x: Any) = x
  // ok
//  foo(t1 + t2)

  def bar(x: Any) = {
    x
  }

  bar ( foo (t1 +t2))
}

@dlwh
Copy link
Author

dlwh commented Aug 27, 2021

(I don't know why it matters, but the expression has to be doubly nested... Singly doesn't trigger it)

@smarter smarter self-assigned this Aug 27, 2021
@dlwh
Copy link
Author

dlwh commented Aug 27, 2021

here's a shorter one I accidentally stumbled on:

import scala.quoted.Type

trait Tensor[S <: Tuple] {
  def sum[Axis <: Shape: Type](axis: Axis): Tensor[S, V] = {
    Tensor.mk
  }
}

object Tensor {
  def mk[S <: Tuple]: Tensor[S] = new Tensor {}
}

object Foo {
  val t1: Tensor[("batch", "len", "embed")] = Tensor.mk
   def foo(x: Any) = {

  }
  foo(foo(t1.sum("len")))
}

smarter added a commit to dotty-staging/dotty that referenced this issue Aug 28, 2021
An already-committed TyperState might be committed again when errors are
flushed (cf scala#12827, scala#13150) and `commit()` calls `gc()`. This operation
could crash before this commit because we attempted to instantiate type
variables no longer owned by the TyperState. We fix this by clearing
`ownedVars` when committing a TyperState (because after committing it no
longer owns any type variable).

Fixes scala#13407.
olsdavis pushed a commit to olsdavis/dotty that referenced this issue Apr 4, 2022
An already-committed TyperState might be committed again when errors are
flushed (cf scala#12827, scala#13150) and `commit()` calls `gc()`. This operation
could crash before this commit because we attempted to instantiate type
variables no longer owned by the TyperState. We fix this by clearing
`ownedVars` when committing a TyperState (because after committing it no
longer owns any type variable).

Fixes scala#13407.
@Kordyjan Kordyjan added this to the 3.1.0 milestone Aug 2, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants