Skip to content

null used as prefix of type application during implicit search #18695

Closed
@Katrix

Description

@Katrix

Encountered this when I got the priorization of implicits slightly wrong. Swapping fromTuple and tupleFromMirrorAndLength makes it work fine in this case. Not actually sure what the bug is here. This is as small as I could minimize it

Compiler version

3.3.1

If you're not sure what version you're using, run print scalaVersion from sbt
(if you're running scalac manually, use scalac -version instead).

Minimized code

import scala.annotation.{tailrec, unused}
import scala.deriving.Mirror
import scala.quoted.*

trait TypeLength[A] {
  type Length <: Int
  def length: Length
}
object TypeLength extends TypeLengthLowPriority:
  type Aux[A, Length0 <: Int] = TypeLength[A] {
    type Length = Length0
  }

  transparent inline given fromMirror[A](using m: Mirror.Of[A]): TypeLength[A] =
    ${ macroImpl[A, m.MirroredElemTypes] }

  @tailrec
  private def typesOfTuple(
      using q: Quotes
  )(tpe: q.reflect.TypeRepr, acc: List[q.reflect.TypeRepr]): List[q.reflect.TypeRepr] =
    import q.reflect.*
    val cons = Symbol.classSymbol("scala.*:")
    tpe.widenTermRefByName.dealias match
      case AppliedType(fn, tpes) if defn.isTupleClass(fn.typeSymbol) =>
        tpes.reverse_:::(acc)
      case AppliedType(tp, List(headType, tailType)) if tp.derivesFrom(cons) =>
        typesOfTuple(tailType, headType :: acc)
      case tpe =>
        if tpe.derivesFrom(Symbol.classSymbol("scala.EmptyTuple")) then acc.reverse
        else report.errorAndAbort(s"Unknown type encountered in tuple ${tpe.show}")

  def macroImpl[A: Type, T <: Tuple: scala.quoted.Type](
      using q: scala.quoted.Quotes
  ): scala.quoted.Expr[TypeLength[A]] =
    import q.reflect.*
    val l = typesOfTuple(TypeRepr.of[T], Nil).length
    ConstantType(IntConstant(l)).asType match
      case '[lt] =>
        val le = Expr[Int](l).asExprOf[lt & Int]
        '{
          val r: TypeLength.Aux[A, lt & Int] = new TypeLength[A] {
            type Length = lt & Int
            val length: Length = ${ le }
          }
          r
        }

  transparent inline given fromTuple[T <: Tuple]: TypeLength[T] =
    ${ macroImpl[T, T] }

trait TypeLengthLowPriority:
  self: TypeLength.type =>
  given tupleFromMirrorAndLength[A, T <: Tuple](
      using @unused m: Mirror.Of[A] { type MirroredElemTypes = T },
      length: TypeLength[A]
  ): TypeLength.Aux[T, length.Length] = length.asInstanceOf[TypeLength.Aux[T, length.Length]]

trait HKDSumGeneric[A]
object HKDSumGeneric:
  type NotZero[N <: Int] = N match
    case 0 => false
    case _ => true

  transparent inline given derived[A](using m: Mirror.SumOf[A], typeLength: TypeLength[A])(
      using NotZero[typeLength.Length] =:= true
  ): HKDSumGeneric[A] =
    derivedImpl[A, m.MirroredElemTypes, m.MirroredLabel]

  def derivedImpl[A, ElemTypes <: Tuple, Label <: String](
      using m: Mirror.SumOf[A] {
        type MirroredElemTypes = ElemTypes; type MirroredLabel = Label;
      },
      typeLength: TypeLength[ElemTypes],
      nz: NotZero[typeLength.Length] =:= true
  ): HKDSumGeneric[A] = ???

Output (click arrow to expand)

java.lang.AssertionError: assertion failed while typechecking D:\DevProjects\Stable\perspective\dotty\derivation\src\main\scala\perspective\derivation\test1.scala
[info] exception occurred while typechecking D:\DevProjects\Stable\perspective\dotty\derivation\src\main\scala\perspective\derivation\test1.scala
[info]
[info]   exception occurred while compiling List(D:\DevProjects\Stable\perspective\dotty\derivation\src\main\scala\perspective\derivation\test1.scala)
[info]
[info]   An unhandled exception was thrown in the compiler.
[info]   Please file a crash report here:
[info]   https://github.com/lampepfl/dotty/issues/new/choose
[info]
[info]      while compiling: <no file>
[info]         during phase: <no phase>
[info]                 mode: Mode(ImplicitsEnabled)
[info]      library version: version 2.13.10
[info]     compiler version: version 3.3.1
[info]             settings: -Ykind-projector "" -bootclasspath C:\Users\katri\AppData\Local\Coursier\Cache\v1\https\repo1.maven.org\maven2\org\scala-lang\scala3-library_3\3.3.1\scala3-library_3-3.3.1.jar;C:\Users\katri\AppData\Local\Coursier\Cache\v1\https\repo1.maven.org\maven2\org\scala-lang\scala-library\2.13.10\scala-library-2.13.10.jar -classpath D:\DevProjects\Stable\perspective\dotty\derivation\target\scala-3.3.1\classes;D:\DevProjects\Stable\perspective\dotty\perspective\target\scala-3.3.1\classes;C:\Users\katri\AppData\Local\Coursier\Cache\v1\https\repo1.maven.org\maven2\org\typelevel\cats-core_3\2.8.0\cats-core_3-2.8.0.jar;C:\Users\katri\AppData\Local\Coursier\Cache\v1\https\repo1.maven.org\maven2\org\typelevel\cats-kernel_3\2.8.0\cats-kernel_3-2.8.0.jar -d D:\DevProjects\Stable\perspective\dotty\derivation\target\scala-3.3.1\classes -feature true
[info]
[info]                 tree: EmptyTree
[info]        tree position: :<unknown>
[info]            tree type: <notype>
[info]               symbol: val <none>
[info]            call site: package <root> in module class <root>
[info]
[info]   == Source file context for tree position ==
[info]
[error] ## Exception when compiling 11 sources to D:\DevProjects\Stable\perspective\dotty\derivation\target\scala-3.3.1\classes
[error] java.lang.AssertionError: assertion failed
[error] scala.runtime.Scala3RunTime$.assertFailed(Scala3RunTime.scala:11)
[error] dotty.tools.dotc.ast.tpd$.TypeApply(tpd.scala:60)
[error] dotty.tools.dotc.ast.tpd$TreeOps$.appliedToTypeTrees$extension(tpd.scala:985)
[error] dotty.tools.dotc.typer.Typer.adapt1(Typer.scala:4255)
[error] dotty.tools.dotc.typer.Typer.adapt(Typer.scala:3587)
[error] dotty.tools.dotc.typer.ProtoTypes$FunProto.typedArg(ProtoTypes.scala:497)
[error] dotty.tools.dotc.typer.Applications$ApplyToUntyped.typedArg(Applications.scala:897)
[error] dotty.tools.dotc.typer.Applications$ApplyToUntyped.typedArg(Applications.scala:897)
[error] dotty.tools.dotc.typer.Applications$Application.addTyped$1(Applications.scala:589)
[error] dotty.tools.dotc.typer.Applications$Application.matchArgs(Applications.scala:653)
[error] dotty.tools.dotc.typer.Applications$Application.init(Applications.scala:492)
[error] dotty.tools.dotc.typer.Applications$TypedApply.<init>(Applications.scala:779)
[error] dotty.tools.dotc.typer.Applications$ApplyToUntyped.<init>(Applications.scala:896)
[error] dotty.tools.dotc.typer.Applications.ApplyTo(Applications.scala:1126)
[error] dotty.tools.dotc.typer.Applications.ApplyTo$(Applications.scala:352)
[error] dotty.tools.dotc.typer.Typer.ApplyTo(Typer.scala:116)
[error] dotty.tools.dotc.typer.Applications.simpleApply$1(Applications.scala:969)
[error] dotty.tools.dotc.typer.Applications.realApply$1$$anonfun$2(Applications.scala:1052)
[error] dotty.tools.dotc.typer.Typer.tryEither(Typer.scala:3324)
[error] dotty.tools.dotc.typer.Applications.realApply$1(Applications.scala:1063)
[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:3048)
[error] dotty.tools.dotc.typer.Typer.typedUnadapted(Typer.scala:3112)
[error] dotty.tools.dotc.typer.Typer.typed(Typer.scala:3184)
[error] dotty.tools.dotc.typer.Implicits.tryConversion$1(Implicits.scala:1136)
[error] dotty.tools.dotc.typer.Implicits.typedImplicit(Implicits.scala:1167)
[error] dotty.tools.dotc.typer.Implicits.typedImplicit$(Implicits.scala:818)
[error] dotty.tools.dotc.typer.Typer.typedImplicit(Typer.scala:116)
[error] dotty.tools.dotc.typer.Implicits$ImplicitSearch.tryImplicit(Implicits.scala:1242)
[error] dotty.tools.dotc.typer.Implicits$ImplicitSearch.rank$1(Implicits.scala:1341)
[error] dotty.tools.dotc.typer.Implicits$ImplicitSearch.searchImplicit(Implicits.scala:1511)
[error] dotty.tools.dotc.typer.Implicits$ImplicitSearch.searchImplicit(Implicits.scala:1539)
[error] dotty.tools.dotc.typer.Implicits$ImplicitSearch.searchImplicit(Implicits.scala:1547)
[error] dotty.tools.dotc.typer.Implicits$ImplicitSearch.bestImplicit(Implicits.scala:1572)
[error] dotty.tools.dotc.typer.Implicits.inferImplicit(Implicits.scala:1060)
[error] dotty.tools.dotc.typer.Implicits.inferImplicit$(Implicits.scala:818)
[error] dotty.tools.dotc.typer.Typer.inferImplicit(Typer.scala:116)
[error] dotty.tools.dotc.typer.Implicits.inferView(Implicits.scala:856)
[error] dotty.tools.dotc.typer.Implicits.inferView$(Implicits.scala:818)
[error] dotty.tools.dotc.typer.Typer.inferView(Typer.scala:116)
[error] dotty.tools.dotc.typer.Implicits.viewExists(Implicits.scala:831)
[error] dotty.tools.dotc.typer.Implicits.viewExists$(Implicits.scala:818)
[error] dotty.tools.dotc.typer.Typer.viewExists(Typer.scala:116)
[error] dotty.tools.dotc.typer.Implicits.ignoredConvertibleImplicits$1$$anonfun$3(Implicits.scala:960)
[error] scala.collection.Iterator$$anon$6.hasNext(Iterator.scala:479)
[error] scala.collection.Iterator.isEmpty(Iterator.scala:466)
[error] scala.collection.Iterator.isEmpty$(Iterator.scala:466)
[error] scala.collection.AbstractIterator.isEmpty(Iterator.scala:1300)
[error] scala.collection.View$Filter.isEmpty(View.scala:146)
[error] scala.collection.IterableOnceOps.nonEmpty(IterableOnce.scala:833)
[error] scala.collection.IterableOnceOps.nonEmpty$(IterableOnce.scala:833)
[error] scala.collection.AbstractIterable.nonEmpty(Iterable.scala:933)
[error] dotty.tools.dotc.reporting.MissingImplicitArgument.noChainConversionsNote$1(messages.scala:2838)
[error] dotty.tools.dotc.reporting.MissingImplicitArgument.msgPostscript$$anonfun$4(messages.scala:2846)
[error] scala.Option.orElse(Option.scala:477)
[error] dotty.tools.dotc.reporting.MissingImplicitArgument.msgPostscript(messages.scala:2846)
[error] dotty.tools.dotc.reporting.Message.message$$anonfun$1(Message.scala:345)
[error] dotty.tools.dotc.reporting.Message.inMessageContext(Message.scala:341)
[error] dotty.tools.dotc.reporting.Message.message(Message.scala:345)
[error] dotty.tools.dotc.reporting.Message.isNonSensical(Message.scala:322)
[error] dotty.tools.dotc.reporting.HideNonSensicalMessages.isHidden(HideNonSensicalMessages.scala:16)
[error] dotty.tools.dotc.reporting.HideNonSensicalMessages.isHidden$(HideNonSensicalMessages.scala:10)
[error] dotty.tools.dotc.reporting.AbstractReporter.isHidden(AbstractReporter.scala:8)
[error] dotty.tools.dotc.reporting.Reporter.issueUnconfigured(Reporter.scala:156)
[error] dotty.tools.dotc.reporting.Reporter.go$1(Reporter.scala:181)
[error] dotty.tools.dotc.reporting.Reporter.issueIfNotSuppressed(Reporter.scala:200)
[error] dotty.tools.dotc.reporting.Reporter.report(Reporter.scala:203)
[error] dotty.tools.dotc.report$.error(report.scala:68)
[error] dotty.tools.dotc.typer.Typer.issueErrors$1$$anonfun$1(Typer.scala:3808)
[error] scala.runtime.function.JProcedure3.apply(JProcedure3.java:15)
[error] scala.runtime.function.JProcedure3.apply(JProcedure3.java:10)
[error] scala.collection.LazyZip3.foreach(LazyZipOps.scala:248)
[error] dotty.tools.dotc.typer.Typer.issueErrors$1(Typer.scala:3810)
[error] dotty.tools.dotc.typer.Typer.addImplicitArgs$1(Typer.scala:3832)
[error] dotty.tools.dotc.typer.Typer.adaptNoArgsImplicitMethod$1(Typer.scala:3849)
[error] dotty.tools.dotc.typer.Typer.adaptNoArgs$1(Typer.scala:4038)
[error] dotty.tools.dotc.typer.Typer.adapt1(Typer.scala:4268)
[error] dotty.tools.dotc.typer.Typer.adapt(Typer.scala:3587)
[error] dotty.tools.dotc.typer.Typer.typed(Typer.scala:3184)
[error] dotty.tools.dotc.typer.Typer.typed(Typer.scala:3188)
[error] dotty.tools.dotc.typer.Typer.typedExpr(Typer.scala:3300)
[error] dotty.tools.dotc.typer.Typer.$anonfun$57(Typer.scala:2486)
[error] dotty.tools.dotc.inlines.PrepareInlineable$.dropInlineIfError(PrepareInlineable.scala:243)
[error] dotty.tools.dotc.typer.Typer.typedDefDef(Typer.scala:2486)
[error] dotty.tools.dotc.typer.Typer.typedNamed$1(Typer.scala:3024)
[error] dotty.tools.dotc.typer.Typer.typedUnadapted(Typer.scala:3111)
[error] dotty.tools.dotc.typer.Typer.typed(Typer.scala:3184)
[error] dotty.tools.dotc.typer.Typer.typed(Typer.scala:3188)
[error] dotty.tools.dotc.typer.Typer.traverse$1(Typer.scala:3210)
[error] dotty.tools.dotc.typer.Typer.typedStats(Typer.scala:3256)
[error] dotty.tools.dotc.typer.Typer.typedClassDef(Typer.scala:2669)
[error] dotty.tools.dotc.typer.Typer.typedTypeOrClassDef$1(Typer.scala:3036)
[error] dotty.tools.dotc.typer.Typer.typedNamed$1(Typer.scala:3040)
[error] dotty.tools.dotc.typer.Typer.typedUnadapted(Typer.scala:3111)
[error] dotty.tools.dotc.typer.Typer.typed(Typer.scala:3184)
[error] dotty.tools.dotc.typer.Typer.typed(Typer.scala:3188)
[error] dotty.tools.dotc.typer.Typer.traverse$1(Typer.scala:3210)
[error] dotty.tools.dotc.typer.Typer.typedStats(Typer.scala:3256)
[error] dotty.tools.dotc.typer.Typer.typedPackageDef(Typer.scala:2812)
[error] dotty.tools.dotc.typer.Typer.typedUnnamed$1(Typer.scala:3081)
[error] dotty.tools.dotc.typer.Typer.typedUnadapted(Typer.scala:3112)
[error] dotty.tools.dotc.typer.Typer.typed(Typer.scala:3184)
[error] dotty.tools.dotc.typer.Typer.typed(Typer.scala:3188)
[error] dotty.tools.dotc.typer.Typer.typedExpr(Typer.scala:3300)
[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:440)
[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.compileSources(Run.scala:194)
[error] dotty.tools.dotc.Run.compile(Run.scala:179)
[error] dotty.tools.dotc.Driver.doCompile(Driver.scala:37)
[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:2364)
[error] sbt.Defaults$.$anonfun$compileIncrementalTask$2(Defaults.scala:2314)
[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:2312)
[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:539)
[error] java.base/java.util.concurrent.FutureTask.run(FutureTask.java:264)
[error] java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1136)
[error] java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:635)
[error] java.base/java.lang.Thread.run(Thread.java:833)

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions