Skip to content

Commit

Permalink
Dealias type references when healing types in quotes (#17049)
Browse files Browse the repository at this point in the history
We where only doing the dealiasing if the type had no prefix but missed
it if the type reference was on some path.

Fixes #17037
  • Loading branch information
nicolasstucki authored Mar 10, 2023
2 parents fd91ce1 + bb5c098 commit 28676ae
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 5 deletions.
19 changes: 14 additions & 5 deletions compiler/src/dotty/tools/dotc/transform/PCPCheckAndHeal.scala
Original file line number Diff line number Diff line change
Expand Up @@ -192,11 +192,9 @@ class PCPCheckAndHeal(@constructorOnly ictx: Context) extends TreeMapWithStages(
case tp: TypeRef =>
tp.prefix match
case NoPrefix if level > levelOf(tp.symbol) && !tp.typeSymbol.hasAnnotation(defn.QuotedRuntime_SplicedTypeAnnot) =>
val tp1 = tp.dealias
if tp1 != tp then apply(tp1)
else tryHeal(tp.symbol, tp, pos)
tryHealTypeOfType(tp.symbol, tp, pos)
case prefix: ThisType if !tp.symbol.isStatic && level > levelOf(prefix.cls) =>
tryHeal(tp.symbol, tp, pos)
tryHealTypeOfType(tp.symbol, tp, pos)
case prefix: TermRef if tp.symbol.isTypeSplice =>
prefix.symbol.info.argInfos match
case (tb: TypeBounds) :: _ =>
Expand All @@ -205,7 +203,7 @@ class PCPCheckAndHeal(@constructorOnly ictx: Context) extends TreeMapWithStages(
// Heal explicit type splice in the code
if level > 0 then getQuoteTypeTags.getTagRef(prefix) else tp
case prefix: TermRef if !prefix.symbol.isStatic && level > levelOf(prefix.symbol) =>
tryHeal(prefix.symbol, tp, pos)
tryHealTypeOfType(prefix.symbol, tp, pos)
case _ =>
mapOver(tp)
case tp @ TermRef(NoPrefix, _) if !tp.symbol.isStatic && level > levelOf(tp.symbol) =>
Expand All @@ -217,6 +215,17 @@ class PCPCheckAndHeal(@constructorOnly ictx: Context) extends TreeMapWithStages(
derivedAnnotatedType(tp, apply(tp.parent), tp.annot.derivedAnnotation(newAnnotTree))
case _ =>
mapOver(tp)

/** Try to dealias or heal reference to type `T` used in a higher level than its definition.
* Returns a reference to a type tag generated by `QuoteTypeTags` that contains a
* reference to a type alias containing the equivalent of `${summon[quoted.Type[T]]}`.
* Emits and error if `T` cannot be healed and returns `T`.
*/
private def tryHealTypeOfType(sym: Symbol, tp: TypeRef, pos: SrcPos)(using Context): Type = {
val tp1 = tp.dealias
if tp1 != tp then apply(tp1)
else tryHeal(tp.symbol, tp, pos)
}
}

/** Check phase consistency of terms and heal inconsistent type references. */
Expand Down
8 changes: 8 additions & 0 deletions tests/pos-macros/i17037.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import scala.quoted.*

class Foo:
type Bar = Int

def macroImpl(using Quotes) =
val foo = new Foo
Type.of[foo.Bar]
10 changes: 10 additions & 0 deletions tests/pos-macros/i17037b.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import scala.quoted.*

class Foo:
type Bar = Int

def macroImpl(using Quotes) =
val foo = Foo()
Type.of[foo.Bar] match
case '[foo.Bar] => '{true}
case _ => '{false}
7 changes: 7 additions & 0 deletions tests/pos-macros/i17037c.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import scala.quoted.*

class Foo:
type Bar = Int
def macroImpl(using Quotes) =
val foo = new Foo
Type.of[this.Bar]

0 comments on commit 28676ae

Please sign in to comment.