Skip to content

MatchError on Term.show #14185

Closed
Closed
@cchantep

Description

@cchantep

Maybe related to #13942

Compiler version

3.1.2-RC1-bin-20211222-c94b333-NIGHTLY-git-c94b333

Minimized code

import scala.quoted.*

object Test {
  inline def foo[A, M[_]]: Unit = ${ fooImpl[A, M] }

  private def fooImpl[A, M[_]](
      using
      q: Quotes,
      tc: Type[M],
      pt: Type[A]
    ): Expr[Unit] = {
    import q.reflect.*

    val ptpe = TypeRepr.of[A]
    val neededGivenType = TypeRepr.of[M](using tc).appliedTo(ptpe)

    val neededGiven: Option[Term] = Implicits.search(neededGivenType) match {
      case suc: ImplicitSearchSuccess =>
        Some(suc.tree)

      case _ =>
        None
    }

    println(neededGiven.mkString)
    println("---")
    println(neededGiven.map(_.show).mkString)

    '{ () }
  }
}

// ---

/** Type level evidence that type `A` is not type `B`. */
final class IsNot[A, B]() {
  override val toString = "not"
}

object IsNot {
  implicit def defaultEvidence[A, B]: IsNot[A, B] = new IsNot[A, B]()

  @annotation.implicitAmbiguous("Could not prove type ${A} is not (IsNot) ${A}")
  implicit def ambiguousEvidence1[A]: IsNot[A, A] = null
  implicit def ambiguousEvidence2[A]: IsNot[A, A] = null
}

// ---

sealed trait SomeTypeclass[T]

object SomeTypeclass extends SomeTypeclassLowPrio {

  given collection[T, Repr <: Iterable[T]](
      using SomeTypeclass[T],
      Repr IsNot Option[T]
  ): SomeTypeclass[Repr] = new SomeTypeclass[Repr] {}
}

sealed trait SomeTypeclassLowPrio {
  given int: SomeTypeclass[Int] = new SomeTypeclass[Int] {}
}

Then when testing:

Test.foo[Seq[Int], SomeTypeclass]

Output

Apply(TypeApply(Ident(collection),List(TypeTree[TypeVar(TypeParamRef(T) -> TypeRef(TermRef(ThisType(TypeRef(NoPrefix,module class <root>)),object scala),class Int))], TypeTree[TypeVar(TypeParamRef(Repr) -> AppliedType(TypeRef(ThisType(TypeRef(NoPrefix,module class immutable)),trait Seq),List(TypeRef(TermRef(ThisType(TypeRef(NoPrefix,module class <root>)),object scala),class Int))))])),List(Ident(int), TypeApply(Ident(defaultEvidence),List(TypeTree[TypeVar(TypeParamRef(A) -> AppliedType(TypeRef(ThisType(TypeRef(NoPrefix,module class immutable)),trait Seq),List(TypeRef(TermRef(ThisType(TypeRef(NoPrefix,module class <root>)),object scala),class Int))))], TypeTree[TypeVar(TypeParamRef(B) -> AppliedType(TypeRef(TermRef(ThisType(TypeRef(NoPrefix,module class <root>)),object scala),Option),List(TypeVar(TypeParamRef(T) -> TypeRef(TermRef(ThisType(TypeRef(NoPrefix,module class <root>)),object scala),class Int)))))]))))
---
-- Error: ----------------------------------------------------------------------
1 |Test.foo[Seq[Int], SomeTypeclass]
  |^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  |Exception occurred while executing macro expansion.
  |scala.MatchError: TypeVar(TypeParamRef(T) -> TypeRef(TermRef(ThisType(TypeRef(NoPrefix,module class <root>)),object scala),class Int)) (of class dotty.tools.dotc.core.Types$TypeVar)
  |	at scala.quoted.runtime.impl.printers.Extractors$ExtractorsPrinter.visitType(Extractors.scala:242)
  |	at scala.quoted.runtime.impl.printers.Extractors$.showType(Extractors.scala:12)
  |	at scala.quoted.runtime.impl.QuotesImpl$$anon$18.show(QuotesImpl.scala:2987)
  |	at scala.quoted.runtime.impl.QuotesImpl$$anon$18.show(QuotesImpl.scala:2986)
  |	at scala.quoted.runtime.impl.QuotesImpl$reflect$TypeReprMethods$.show(QuotesImpl.scala:1708)
  |	at scala.quoted.runtime.impl.QuotesImpl$reflect$TypeReprMethods$.show(QuotesImpl.scala:1708)

Expectation

Successfully show the term representation.

Note that if updating given collection as below, it works fine.

  given collection[T, Repr <: Iterable[T]](
      using SomeTypeclass[T]
      // REMOVED: Repr IsNot Option[T]
  ): SomeTypeclass[Repr] = new SomeTypeclass[Repr] {}

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions