Skip to content

MatchError on Term.show #14185

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

Closed
cchantep opened this issue Dec 29, 2021 · 0 comments · Fixed by #14201
Closed

MatchError on Term.show #14185

cchantep opened this issue Dec 29, 2021 · 0 comments · Fixed by #14201
Assignees

Comments

@cchantep
Copy link
Contributor

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] {}
@nicolasstucki nicolasstucki self-assigned this Jan 3, 2022
nicolasstucki added a commit to dotty-staging/dotty that referenced this issue Jan 3, 2022
nicolasstucki added a commit to dotty-staging/dotty that referenced this issue Jan 3, 2022
olsdavis pushed a commit to olsdavis/dotty that referenced this issue Apr 4, 2022
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.

2 participants