scala / scala3

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

TyperState crash: search for extension method in implicit scope #18687

Open m8nmueller opened 1 year ago

m8nmueller commented 1 year ago

Compiler version

3.3.1

Minimized code

trait SuspendSupport:
  type Suspension[-T, +R]
  extension [T, R](s: Suspension[T, R])
    def resume(arg: T): R

  def suspend[T, R](body: Suspension[T, R] => R): T

trait Async(using val support: SuspendSupport)

private class YZ(using ac: Async):
  private def test =
    Some(()).getOrElse:
      ac.support.suspend[scala.util.Try[Unit], Unit](k =>
        k.resume(scala.util.Failure(Exception()))
        1
      )

Output (click arrow to expand)

```scala java.lang.AssertionError: assertion failed: TS[1, 0] attempted to take ownership of T which is already owned by committable TS[12, 1, 0] while typechecking test.scala exception occurred while typechecking test.scala exception occurred while compiling List(test.scala) An unhandled exception was thrown in the compiler. Please file a crash report here: https://github.com/lampepfl/dotty/issues/new/choose while compiling: during phase: mode: Mode(ImplicitsEnabled) library version: version 2.13.10 compiler version: version 3.3.1 settings: tree: EmptyTree tree position: : tree type: symbol: val call site: package in module class == Source file context for tree position == Exception in thread "main" java.lang.AssertionError: assertion failed: TS[1, 0] attempted to take ownership of T which is already owned by committable TS[12, 1, 0] at scala.runtime.Scala3RunTime$.assertFailed(Scala3RunTime.scala:8) at dotty.tools.dotc.core.TyperState.includeVar(TyperState.scala:271) at dotty.tools.dotc.core.TyperState.$anonfun$2$$anonfun$1(TyperState.scala:233) 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.core.TyperState.$anonfun$2(TyperState.scala:233) at scala.collection.immutable.List.forall(List.scala:386) at dotty.tools.dotc.core.TyperState.mergeConstraintWith(TyperState.scala:235) at dotty.tools.dotc.typer.ProtoTypes$FunProto.typedArgs(ProtoTypes.scala:480) at dotty.tools.dotc.typer.ProtoTypes$FunProto.fold(ProtoTypes.scala:566) at dotty.tools.dotc.core.Types$TypeAccumulator.foldOver(Types.scala:6250) at dotty.tools.dotc.core.Types$TypeTraverser.traverseChildren(Types.scala:6288) at dotty.tools.dotc.typer.ImplicitRunInfo$collectParts$2$.traverse(Implicits.scala:640) at dotty.tools.dotc.core.Types$TypeTraverser.apply(Types.scala:6287) at dotty.tools.dotc.core.Types$TypeTraverser.apply(Types.scala:6287) at dotty.tools.dotc.typer.ProtoTypes$SelectionProto.fold(ProtoTypes.scala:244) at dotty.tools.dotc.core.Types$TypeAccumulator.foldOver(Types.scala:6250) at dotty.tools.dotc.core.Types$TypeTraverser.traverseChildren(Types.scala:6288) at dotty.tools.dotc.typer.ImplicitRunInfo$collectParts$2$.traverse(Implicits.scala:640) at dotty.tools.dotc.core.Types$TypeTraverser.apply(Types.scala:6287) at dotty.tools.dotc.core.Types$TypeTraverser.apply(Types.scala:6287) at dotty.tools.dotc.typer.ProtoTypes$ViewProto.fold(ProtoTypes.scala:623) at dotty.tools.dotc.core.Types$TypeAccumulator.foldOver(Types.scala:6250) at dotty.tools.dotc.core.Types$TypeTraverser.traverseChildren(Types.scala:6288) at dotty.tools.dotc.typer.ImplicitRunInfo$collectParts$2$.traverse(Implicits.scala:640) at dotty.tools.dotc.typer.ImplicitRunInfo$collectParts$2$.apply(Implicits.scala:645) at dotty.tools.dotc.typer.ImplicitRunInfo.recur$1(Implicits.scala:721) at dotty.tools.dotc.typer.ImplicitRunInfo.computeIScope(Implicits.scala:732) at dotty.tools.dotc.typer.ImplicitRunInfo.implicitScope(Implicits.scala:804) at dotty.tools.dotc.typer.ImplicitRunInfo.implicitScope$(Implicits.scala:584) at dotty.tools.dotc.Run.implicitScope(Run.scala:36) at dotty.tools.dotc.typer.Implicits$ImplicitSearch.implicitScope(Implicits.scala:1575) at dotty.tools.dotc.typer.Implicits$ImplicitSearch.searchImplicit(Implicits.scala:1538) at dotty.tools.dotc.typer.Implicits$ImplicitSearch.searchImplicit(Implicits.scala:1547) at dotty.tools.dotc.typer.Implicits$ImplicitSearch.bestImplicit(Implicits.scala:1572) at dotty.tools.dotc.typer.Implicits.inferImplicit(Implicits.scala:1060) at dotty.tools.dotc.typer.Implicits.inferImplicit$(Implicits.scala:818) at dotty.tools.dotc.typer.Typer.inferImplicit(Typer.scala:116) at dotty.tools.dotc.typer.Implicits.inferImplicit(Implicits.scala:1080) at dotty.tools.dotc.typer.Implicits.inferImplicit$(Implicits.scala:818) at dotty.tools.dotc.typer.Typer.inferImplicit(Typer.scala:116) at dotty.tools.dotc.typer.Implicits.inferImplicit(Implicits.scala:1080) at dotty.tools.dotc.typer.Implicits.inferImplicit$(Implicits.scala:818) at dotty.tools.dotc.typer.Typer.inferImplicit(Typer.scala:116) at dotty.tools.dotc.typer.Implicits.inferView(Implicits.scala:856) at dotty.tools.dotc.typer.Implicits.inferView$(Implicits.scala:818) at dotty.tools.dotc.typer.Typer.inferView(Typer.scala:116) at dotty.tools.dotc.typer.Typer.tryExtensionOrConversion(Typer.scala:3527) at dotty.tools.dotc.typer.Typer.typedSelect(Typer.scala:703) at dotty.tools.dotc.typer.Typer.typeSelectOnTerm$1(Typer.scala:753) at dotty.tools.dotc.typer.Typer.typedSelect(Typer.scala:790) at dotty.tools.dotc.typer.Typer.typedNamed$1(Typer.scala:3017) 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.typedExpr(Typer.scala:3300) at dotty.tools.dotc.typer.Applications.realApply$1(Applications.scala:941) 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.Typer.typedUnnamed$1(Typer.scala:3048) 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.traverse$1(Typer.scala:3237) 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.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.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.typedFunction(Typer.scala:1377) at dotty.tools.dotc.typer.Typer.typedUnnamed$1(Typer.scala:3058) at dotty.tools.dotc.typer.Typer.typedUnadapted(Typer.scala:3112) 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.(Applications.scala:779) at dotty.tools.dotc.typer.Applications$ApplyToUntyped.(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.Typer.typedUnnamed$1(Typer.scala:3048) at dotty.tools.dotc.typer.Typer.typedUnadapted(Typer.scala:3112) 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.(Applications.scala:779) at dotty.tools.dotc.typer.Applications$ApplyToUntyped.(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.Typer.typedUnnamed$1(Typer.scala:3048) 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.Namer.typedAheadExpr$$anonfun$1(Namer.scala:1653) at dotty.tools.dotc.typer.Namer.typedAhead(Namer.scala:1643) at dotty.tools.dotc.typer.Namer.typedAheadExpr(Namer.scala:1653) at dotty.tools.dotc.typer.Namer.typedAheadRhs$1$$anonfun$1(Namer.scala:1906) at dotty.tools.dotc.inlines.PrepareInlineable$.dropInlineIfError(PrepareInlineable.scala:243) at dotty.tools.dotc.typer.Namer.typedAheadRhs$1(Namer.scala:1906) at dotty.tools.dotc.typer.Namer.rhsType$1(Namer.scala:1914) at dotty.tools.dotc.typer.Namer.cookedRhsType$1(Namer.scala:1932) at dotty.tools.dotc.typer.Namer.lhsType$1(Namer.scala:1933) at dotty.tools.dotc.typer.Namer.inferredResultType(Namer.scala:1944) at dotty.tools.dotc.typer.Namer.inferredType$1(Namer.scala:1691) at dotty.tools.dotc.typer.Namer.valOrDefDefSig(Namer.scala:1698) 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.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.compileSources(Run.scala:194) at dotty.tools.dotc.Run.compile(Run.scala:179) at dotty.tools.dotc.Driver.doCompile(Driver.scala:37) at dotty.tools.dotc.Driver.process(Driver.scala:197) at dotty.tools.dotc.Driver.process(Driver.scala:165) at dotty.tools.dotc.Driver.process(Driver.scala:177) at dotty.tools.dotc.Driver.main(Driver.scala:207) at dotty.tools.MainGenericCompiler$.run$1(MainGenericCompiler.scala:162) at dotty.tools.MainGenericCompiler$.main(MainGenericCompiler.scala:186) at dotty.tools.MainGenericCompiler.main(MainGenericCompiler.scala) at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77) at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.base/java.lang.reflect.Method.invoke(Method.java:568) at coursier.bootstrap.launcher.a.a(Unknown Source) at coursier.bootstrap.launcher.Launcher.main(Unknown Source) ```
bishabosha commented 1 year ago

replacing the argument to resume results in another strange error:

trait SuspendSupport:
  type Suspension[-T, +R]
  extension [T, R](s: Suspension[T, R])
    def resume(arg: T): R

  def suspend[T, R](body: Suspension[T, R] => R): T

trait Async(using val support: SuspendSupport)

private class YZ(using ac: Async):

  private def test =
    Some(()).getOrElse:
      ac.support.suspend[scala.util.Try[Unit], Unit](k =>
        k.resume(??? : scala.util.Failure[Nothing])
      )
-- [E007] Type Mismatch Error: test.scala:17:8 ---------------------------------
17 |        k.resume(??? : scala.util.Failure[Nothing])
   |        ^
   |Found:    (k : YZ.this.ac.support.Suspension[util.Try[Unit], Unit])
   |Required: ?{ resume: ? }
   |Note that implicit extension methods cannot be applied because they are ambiguous;
   |both YZ.this.ac.support and YZ.this.ac.support provide an extension method `resume` on (k : YZ.this.ac.support.Suspension[util.Try[Unit], Unit])
   |
   | longer explanation available when compiling with `-explain`
1 error found
bishabosha commented 1 year ago

importing the extension explicitly avoids the crash, as there is no longer "implicit extension method" search:

trait SuspendSupport:
  type Suspension[-T, +R]

  extension [T, R](s: Suspension[T, R])
    def resume(arg: T): R

  def suspend[T, R](body: Suspension[T, R] => R): T

trait Async(using val support: SuspendSupport)

private class YZ(using ac: Async):
  import ac.support.resume

  private def test =
    Some(()).getOrElse:
      ac.support.suspend[scala.util.Try[Unit], Unit](k =>
        k.resume(scala.util.Failure(Exception()))
      )
bishabosha commented 1 year ago

This is also a regression from error to crash between 3.0.2 and 3.1.0 (fixing the usage of fewer braces) - however as it was always an error I think perhaps this isn't as serious as standard regression

m8nmueller commented 1 year ago

replacing the argument to resume results in another strange error:

trait SuspendSupport:
  type Suspension[-T, +R]
  extension [T, R](s: Suspension[T, R])
    def resume(arg: T): R

  def suspend[T, R](body: Suspension[T, R] => R): T

trait Async(using val support: SuspendSupport)

private class YZ(using ac: Async):

  private def test =
    Some(()).getOrElse:
      ac.support.suspend[scala.util.Try[Unit], Unit](k =>
        k.resume(??? : scala.util.Failure[Nothing])
      )
-- [E007] Type Mismatch Error: test.scala:17:8 ---------------------------------
17 |        k.resume(??? : scala.util.Failure[Nothing])
   |        ^
   |Found:    (k : YZ.this.ac.support.Suspension[util.Try[Unit], Unit])
   |Required: ?{ resume: ? }
   |Note that implicit extension methods cannot be applied because they are ambiguous;
   |both YZ.this.ac.support and YZ.this.ac.support provide an extension method `resume` on (k : YZ.this.ac.support.Suspension[util.Try[Unit], Unit])
   |
   | longer explanation available when compiling with `-explain`
1 error found

Note that this error (instead of the crash) can also be produced by unwrapping this code block outside of getOrElse.

smarter commented 1 year ago

Likely duplicate of https://github.com/lampepfl/dotty/issues/16326 but will keep both open while I dig deeper.