Skip to content
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

Dealias type references when healing types in quotes #17049

Merged
merged 1 commit into from
Mar 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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]