Skip to content

Commit

Permalink
Merge pull request #14425 from dwijnand/quote-matching-avoidance
Browse files Browse the repository at this point in the history
  • Loading branch information
dwijnand authored Feb 7, 2022
2 parents 0647084 + fe6574c commit 74c2954
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 2 deletions.
5 changes: 3 additions & 2 deletions compiler/src/scala/quoted/runtime/impl/QuotesImpl.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3056,8 +3056,9 @@ class QuotesImpl private (using val ctx: Context) extends Quotes, QuoteUnpickler
// that we have found, seal them in a quoted.Type and add them to the result
def typeHoleApproximation(sym: Symbol) =
val fromAboveAnnot = sym.hasAnnotation(dotc.core.Symbols.defn.QuotedRuntimePatterns_fromAboveAnnot)
val approx = ctx1.gadt.approximation(sym, !fromAboveAnnot)
reflect.TypeReprMethods.asType(approx)
val fullBounds = ctx1.gadt.fullBounds(sym)
val tp = if fromAboveAnnot then fullBounds.hi else fullBounds.lo
reflect.TypeReprMethods.asType(tp)
matchings.map { tup =>
Tuple.fromIArray(typeHoles.map(typeHoleApproximation).toArray.asInstanceOf[IArray[Object]]) ++ tup
}
Expand Down
13 changes: 13 additions & 0 deletions tests/pos-macros/i12343/Macro.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package test

import scala.quoted.*

object Macro:
def covImpl(arg: Expr[Any])(using Quotes): Expr[Any] = arg match // Covariant (List)
case '{ $h : List[h] } => '{ $h : List[h] }

def invImpl(arg: Expr[Any])(using Quotes): Expr[Any] = arg match // Invariant (Set)
case '{ $h : Set[h] } => '{ $h : Set[h] }

transparent inline def cov(inline arg: Any): Any = ${ covImpl('arg) }
transparent inline def inv(inline arg: Any): Any = ${ invImpl('arg) }
7 changes: 7 additions & 0 deletions tests/pos-macros/i12343/Test.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package test

object Test:
val cov1: List[Boolean] = Macro.cov(List(true))
val inv1: Set[Boolean] = Macro.inv(Set(true))
def cov2[X](a: List[X]): List[X] = Macro.cov(a)
def inv2[X](a: Set[X]): Set[X] = Macro.inv(a) // doesn't compile; Set[Nothing] is inferred

0 comments on commit 74c2954

Please sign in to comment.