Skip to content

Commit

Permalink
Avoid creation of @SplicedType quote local refrences (#17051)
Browse files Browse the repository at this point in the history
The type declarations annotated with `@SplicedType` are only meant for
types that come from outside the quote. If the reference to the
`Type[T]` is to a definition within the quote (i.e. its level is grater
than 0) we can use it directly. The `@SplicedType` for that type will be
generated in a future compilation phase when the `Type[T]` definition
reaches level 0.

Fixes #17026
  • Loading branch information
nicolasstucki authored Mar 15, 2023
2 parents 9d51be0 + 06b5684 commit b625340
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 2 deletions.
6 changes: 4 additions & 2 deletions compiler/src/dotty/tools/dotc/staging/HealType.scala
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ package staging
import dotty.tools.dotc.core.Contexts._
import dotty.tools.dotc.core.Decorators._
import dotty.tools.dotc.core.Flags._
import dotty.tools.dotc.core.StdNames._
import dotty.tools.dotc.core.Symbols._
import dotty.tools.dotc.core.Types._
import dotty.tools.dotc.staging.QuoteContext.*
Expand Down Expand Up @@ -68,13 +69,14 @@ class HealType(pos: SrcPos)(using Context) extends TypeMap {
* reference to a type alias containing the equivalent of `${summon[quoted.Type[T]]}`.
* Emits an error if `T` cannot be healed and returns `T`.
*/
protected def tryHeal(sym: Symbol, tp: TypeRef, pos: SrcPos): TypeRef = {
protected def tryHeal(sym: Symbol, tp: TypeRef, pos: SrcPos): Type = {
val reqType = defn.QuotedTypeClass.typeRef.appliedTo(tp)
val tag = ctx.typer.inferImplicitArg(reqType, pos.span)
tag.tpe match
case tp: TermRef =>
ctx.typer.checkStable(tp, pos, "type witness")
getQuoteTypeTags.getTagRef(tp)
if levelOf(tp.symbol) > 0 then tp.select(tpnme.Underlying)
else getQuoteTypeTags.getTagRef(tp)
case _: SearchFailureType =>
report.error(
ctx.typer.missingArgMsg(tag, reqType, "")
Expand Down
3 changes: 3 additions & 0 deletions tests/pos-macros/i17026.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import scala.quoted.*
def macroImpl(using Quotes) =
'{ def weird[A: Type](using Quotes) = Type.of[A] }
7 changes: 7 additions & 0 deletions tests/pos-macros/i17026b.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import scala.quoted.*

def macroImpl(using Quotes) =
'{
def weird[A: ToExpr: Type](a: A)(using quotes: Quotes) =
'{ Some(${ Expr(a) }) }
}

0 comments on commit b625340

Please sign in to comment.